001/*
002 * Copyright 2012-2015 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.boot.devtools.classpath;
018
019import java.io.File;
020import java.net.URL;
021import java.util.ArrayList;
022import java.util.Collections;
023import java.util.Iterator;
024import java.util.List;
025
026import org.apache.commons.logging.Log;
027import org.apache.commons.logging.LogFactory;
028
029import org.springframework.util.ResourceUtils;
030
031/**
032 * Provides access to entries on the classpath that refer to folders.
033 *
034 * @author Phillip Webb
035 * @since 1.3.0
036 */
037public class ClassPathFolders implements Iterable<File> {
038
039        private static final Log logger = LogFactory.getLog(ClassPathFolders.class);
040
041        private final List<File> folders = new ArrayList<File>();
042
043        public ClassPathFolders(URL[] urls) {
044                if (urls != null) {
045                        addUrls(urls);
046                }
047        }
048
049        private void addUrls(URL[] urls) {
050                for (URL url : urls) {
051                        addUrl(url);
052                }
053        }
054
055        private void addUrl(URL url) {
056                if (url.getProtocol().equals("file") && url.getPath().endsWith("/")) {
057                        try {
058                                this.folders.add(ResourceUtils.getFile(url));
059                        }
060                        catch (Exception ex) {
061                                logger.warn("Unable to get classpath URL " + url);
062                                logger.trace("Unable to get classpath URL " + url, ex);
063                        }
064                }
065        }
066
067        @Override
068        public Iterator<File> iterator() {
069                return Collections.unmodifiableList(this.folders).iterator();
070        }
071
072}