Coverage Report - org.apache.commons.classscan.builtin.UrlClassPath
 
Classes in this File Line Coverage Branch Coverage Complexity
UrlClassPath
0%
0/36
0%
0/12
2.167
 
 1  
 /*
 2  
  * Licensed under the Apache License, Version 2.0 (the "License");
 3  
  * you may not use this file except in compliance with the License.
 4  
  * You may obtain a copy of the License at
 5  
  *
 6  
  *      http://www.apache.org/licenses/LICENSE-2.0
 7  
  *
 8  
  * Unless required by applicable law or agreed to in writing, software
 9  
  * distributed under the License is distributed on an "AS IS" BASIS,
 10  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 11  
  * See the License for the specific language governing permissions and
 12  
  * limitations under the License.
 13  
  */
 14  
 package org.apache.commons.classscan.builtin;
 15  
 
 16  
 import java.io.IOException;
 17  
 import java.net.URL;
 18  
 import java.net.URLClassLoader;
 19  
 import java.util.ArrayList;
 20  
 import java.util.Collection;
 21  
 import java.util.Collections;
 22  
 import java.util.HashMap;
 23  
 import java.util.List;
 24  
 import java.util.Map;
 25  
 
 26  
 import org.apache.commons.classscan.ClassPathElement;
 27  
 import org.apache.commons.classscan.ResourceFile;
 28  
 import org.apache.commons.classscan.spi.model.SpiClassPath;
 29  
 import org.apache.commons.classscan.spi.model.SpiClassPathElement;
 30  
 import org.apache.commons.classscan.spi.model.SpiMetaClassLoader;
 31  
 import org.apache.commons.classscan.spi.model.SpiMetaRegistry;
 32  
 import org.slf4j.Logger;
 33  
 import org.slf4j.LoggerFactory;
 34  
 
 35  
 public class UrlClassPath implements SpiClassPath {
 36  
 
 37  0
     private static final Logger logger = LoggerFactory.getLogger(UrlClassPath.class);
 38  
 
 39  
     private final SpiMetaRegistry registry;
 40  
         private final List<SpiClassPathElement> classPathElements;
 41  
         
 42  0
         public UrlClassPath(SpiMetaRegistry registry, URLClassLoader urlClassLoader) {
 43  0
                 this.registry = registry;
 44  0
                 classPathElements = Collections.unmodifiableList(getClassPathElements(registry, urlClassLoader.getURLs()));
 45  0
         }
 46  
 
 47  
         static List<SpiClassPathElement> getClassPathElements(SpiMetaRegistry registry, URL[] urls) {
 48  0
                 List<SpiClassPathElement> elements = new ArrayList<SpiClassPathElement>();
 49  0
                 Map<String, URL> locations = new HashMap<String, URL>();
 50  0
                 for(URL url : urls) {
 51  0
                         String baseLocation= url.toExternalForm();
 52  0
                         if( locations.containsKey(baseLocation) ) {
 53  0
                                 continue;
 54  
                         }
 55  0
                         locations.put(baseLocation, url);
 56  
                         
 57  
                         try {
 58  0
                                 SpiClassPathElement element= registry.createClassPathElement(url);
 59  0
                                 elements.add(element);
 60  0
                         } catch (IOException e) {
 61  0
                             logger.info("Problem with URL "+url, e);
 62  0
                         }
 63  
                 }
 64  0
                 return elements;
 65  
         }
 66  
 
 67  
         @Override
 68  
         public List<? extends ClassPathElement> getClassPathElements() {
 69  0
                 return classPathElements;
 70  
         }
 71  
 
 72  
         @Override
 73  
         public SpiMetaClassLoader createMetaClassLoader(SpiMetaRegistry registry, ClassLoader classLoader) {
 74  0
                 return new UrlMetaClassLoader(registry, classLoader);
 75  
         }
 76  
 
 77  
         @Override
 78  
         public Map<ClassPathElement, ResourceFile> getResources(String fileName) {
 79  0
                 Map<ClassPathElement, ResourceFile> resources = new HashMap<ClassPathElement, ResourceFile>();
 80  0
                 Map<String,ClassPathElement> visited = new HashMap<String,ClassPathElement>();
 81  0
                 searchElements(visited, resources, classPathElements, fileName);
 82  0
                 return resources;
 83  
         }
 84  
         
 85  
         public void searchElements(Map<String,ClassPathElement> visited, Map<ClassPathElement, ResourceFile> resources, Collection<SpiClassPathElement> elements, String fileName) {
 86  0
                 for(SpiClassPathElement element : elements) {
 87  0
                         String location = element.getLocation();
 88  0
                         if(visited.put(location, element)!=null) {
 89  0
                                 continue;
 90  
                         }
 91  
 
 92  0
                         ResourceFile resource= element.getResource(fileName);
 93  0
                         if(resource!=null) {
 94  0
                                 resources.put(element, resource);
 95  
                         }
 96  
                         
 97  0
                         Collection<SpiClassPathElement> additionalLocations = element.getAdditionalLocations(registry);
 98  0
                         if(additionalLocations!=null) {
 99  0
                                 searchElements(visited, resources, additionalLocations, fileName);
 100  
                         }
 101  0
                 }
 102  0
         }
 103  
 }