Coverage Report - org.apache.commons.classscan.builtin.JarClassPathElement
 
Classes in this File Line Coverage Branch Coverage Complexity
JarClassPathElement
0%
0/51
0%
0/10
3.375
JarClassPathElement$1
0%
0/11
0%
0/4
3.375
 
 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.JarURLConnection;
 18  
 import java.net.MalformedURLException;
 19  
 import java.net.URI;
 20  
 import java.net.URISyntaxException;
 21  
 import java.net.URL;
 22  
 import java.util.ArrayList;
 23  
 import java.util.Collection;
 24  
 import java.util.Enumeration;
 25  
 import java.util.Iterator;
 26  
 import java.util.List;
 27  
 import java.util.jar.Attributes;
 28  
 import java.util.jar.JarEntry;
 29  
 import java.util.jar.JarFile;
 30  
 import java.util.jar.Manifest;
 31  
 import java.util.regex.Pattern;
 32  
 
 33  
 import org.apache.commons.classscan.ClassFile;
 34  
 import org.apache.commons.classscan.ResourceFile;
 35  
 import org.apache.commons.classscan.spi.model.SpiClassPathElement;
 36  
 import org.apache.commons.classscan.spi.model.SpiMetaRegistry;
 37  
 import org.slf4j.Logger;
 38  
 import org.slf4j.LoggerFactory;
 39  
 
 40  0
 public class JarClassPathElement implements SpiClassPathElement {
 41  
 
 42  0
     private static final Logger logger = LoggerFactory.getLogger(JarClassPathElement.class);
 43  
 
 44  
     final private URL location;
 45  
     private URL[] classPathLocations;
 46  
         
 47  0
         public JarClassPathElement(URL location) {
 48  0
                 this.location = location;
 49  0
         }
 50  
 
 51  
         @Override
 52  
         public Iterator<ClassFile> iterator() {                
 53  0
                 final JarFile jarFile= getJarFile();                
 54  0
         final Enumeration<JarEntry> jarEntries = jarFile.entries();
 55  
         
 56  0
                 return new ClassFileIterator() {                        
 57  
                         ClassFile getNext() {
 58  0
                                 while(jarEntries.hasMoreElements()) {
 59  0
                                         JarClassFile jcf= new JarClassFile(jarFile, jarEntries.nextElement());
 60  0
                                         if(jcf.getClassName()!=null) {
 61  0
                                                 return jcf;
 62  
                                         }
 63  0
                                 }
 64  
                                 try {
 65  0
                                         jarFile.close();
 66  0
                                 } catch (IOException ex) {
 67  0
                             logger.warn("Failed to close " + location, ex);
 68  0
                                 }
 69  0
                                 return null;
 70  
                         }
 71  
                 };
 72  
         }
 73  
 
 74  
         private JarFile getJarFile() {                
 75  
         try {
 76  0
             JarURLConnection conn = (JarURLConnection) location.openConnection();
 77  0
             JarFile jf= conn.getJarFile();
 78  0
             extractClasspath(jf);
 79  0
             return jf;
 80  
         }
 81  0
         catch (IOException ex) {
 82  0
             logger.warn("Failed to open " + location, ex);
 83  0
             return null;
 84  
         }
 85  
         }
 86  
 
 87  
         private void extractClasspath(JarFile jf) throws IOException {
 88  0
                 Manifest manifest = jf.getManifest();
 89  0
                 if(manifest == null) {
 90  0
                         return;
 91  
                 }
 92  
                 
 93  
                 URI resolveLocation;
 94  
                 try {
 95  0
                         resolveLocation = location.toURI();
 96  0
                 } catch (URISyntaxException e) {
 97  0
                 logger.warn("Could not create URI out of {}", location);
 98  0
                 return;
 99  0
                 }
 100  
                 
 101  0
         Attributes attributes = manifest.getMainAttributes();
 102  0
         String classPath = attributes.getValue(Attributes.Name.CLASS_PATH);
 103  0
         if (classPath == null) {
 104  0
             return;
 105  
         }
 106  
         
 107  0
         List<URL> newLocations = new ArrayList<URL>();
 108  0
                 for (String pathElement : PATH_SPLITTER.split(classPath)) {
 109  0
             URI resolvedLocation = resolveLocation.resolve(pathElement);
 110  
             try {
 111  0
                                 URL newLocation = resolvedLocation.toURL();
 112  0
                     newLocations.add(newLocation);
 113  0
             } catch (MalformedURLException e) {
 114  0
                     logger.warn("Could not resolve Class-Path element {} relative to {}", pathElement, resolveLocation);
 115  0
                         }
 116  
         }
 117  0
         classPathLocations = newLocations.toArray(new URL[newLocations.size()]);
 118  0
         }
 119  
 
 120  0
     private static Pattern PATH_SPLITTER = Pattern.compile("\\s+");
 121  
     
 122  
         @Override
 123  
         public String getLocation() {
 124  0
                 return location.toExternalForm();
 125  
         }
 126  
 
 127  
         @Override
 128  
         public Collection<SpiClassPathElement> getAdditionalLocations(SpiMetaRegistry registry) {
 129  0
                 if(classPathLocations==null) {
 130  0
                         return null;
 131  
                 }
 132  0
                 return UrlClassPath.getClassPathElements(registry, classPathLocations);
 133  
         }
 134  
 
 135  
         @Override
 136  
         public ResourceFile getResource(String fileName) {
 137  0
                 JarFile jarFile= getJarFile();
 138  0
                 JarEntry jarEntry = jarFile.getJarEntry(fileName);
 139  0
                 if(jarEntry==null) {
 140  
                         try {
 141  0
                                 jarFile.close();
 142  0
                         } catch (IOException e) {
 143  0
                     logger.info("ignoring exception on close", e);
 144  0
                         }
 145  0
                         return null;
 146  
                 }
 147  0
                 return new JarResourceFile(jarFile, jarEntry);
 148  
         }
 149  
 }