Coverage Report - org.apache.commons.classscan.builtin.DefaultMetaClassPathElement
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultMetaClassPathElement
0%
0/46
0%
0/18
2.75
 
 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.InputStream;
 17  
 import java.util.Collection;
 18  
 import java.util.HashMap;
 19  
 import java.util.Iterator;
 20  
 import java.util.Map;
 21  
 
 22  
 import org.apache.commons.classscan.ClassPathElement;
 23  
 import org.apache.commons.classscan.ClassFile;
 24  
 import org.apache.commons.classscan.model.MetaClass;
 25  
 import org.apache.commons.classscan.spi.model.SpiMetaClass;
 26  
 import org.apache.commons.classscan.spi.model.SpiMetaClassLoader;
 27  
 import org.apache.commons.classscan.spi.model.SpiMetaClassPathElement;
 28  
 import org.apache.commons.classscan.spi.model.SpiMetaRegistry;
 29  
 import org.apache.commons.classscan.util.NameSet;
 30  
 import org.slf4j.Logger;
 31  
 import org.slf4j.LoggerFactory;
 32  
 
 33  
 public class DefaultMetaClassPathElement implements SpiMetaClassPathElement {
 34  
 
 35  0
     private static final Logger logger = LoggerFactory.getLogger(DefaultMetaClassPathElement.class);
 36  
 
 37  
         private final String baseUrl;        
 38  0
     private Map<String, SpiMetaClass> workClasses = new HashMap<String, SpiMetaClass>();
 39  
     private NameSet<SpiMetaClass> sealedClasses;
 40  
 
 41  0
         public DefaultMetaClassPathElement(SpiMetaRegistry metaRegistry, ClassPathElement classPathElement) {
 42  0
                 this.baseUrl = classPathElement.getLocation();
 43  
 
 44  0
                 for(ClassFile classPathFile : classPathElement) {
 45  0
                         addFile(metaRegistry, classPathFile);
 46  
                 }
 47  0
     }
 48  
 
 49  
         private void addFile(SpiMetaRegistry metaRegistry, ClassFile classPathFile) {
 50  0
                 String className = classPathFile.getClassName();
 51  
                 try {
 52  0
                         InputStream byteStream = classPathFile.getBytes();
 53  
                         try {
 54  0
                                 SpiMetaClass mc= metaRegistry.createMetaClass(this, className, byteStream);
 55  0
                                 addMetaClass(mc);
 56  
                         }
 57  
                         finally {
 58  0
                                 byteStream.close();
 59  0
                         }
 60  0
                 } catch (Exception e) {
 61  0
                         logger.info("Exception while loading "+className, e);
 62  0
                 }
 63  0
         }
 64  
 
 65  
     void addMetaClass(SpiMetaClass mc) {
 66  0
             String name= mc.getName();
 67  0
         if (workClasses.containsKey(name)) {
 68  0
             return;
 69  
         }
 70  0
         workClasses.put(name, mc);
 71  0
     }
 72  
 
 73  
         @Override
 74  
         public boolean resolve(SpiMetaClassLoader classLoader) {
 75  0
         if( workClasses.isEmpty() ) {
 76  0
                 sealedClasses = NameSet.emptyNameSet();
 77  
         }
 78  
         else {
 79  0
                 Iterator<Map.Entry<String, SpiMetaClass>> metaClasses = workClasses.entrySet().iterator();
 80  0
                 while(metaClasses.hasNext()) {
 81  0
                         Map.Entry<String, SpiMetaClass> entry = metaClasses.next();
 82  0
                             if(!entry.getValue().resolve(classLoader)) {
 83  0
                                     logger.info("Could not resolve dependencies of "+entry.getKey());
 84  0
                                     metaClasses.remove();
 85  
                             }
 86  0
                     }
 87  0
                 sealedClasses = new NameSet<SpiMetaClass>(SpiMetaClass.class, workClasses.values());
 88  
         }
 89  
 
 90  0
         workClasses = null;
 91  0
         if( sealedClasses.isEmpty() ) {
 92  0
                 logger.info("No classes in "+baseUrl);
 93  0
                 return false;
 94  
         }       
 95  0
         return true;
 96  
         }
 97  
 
 98  
         @Override
 99  
         public MetaClass resolveMetaClass(SpiMetaClassLoader classLoader, String className) {
 100  0
                 if(workClasses==null) {
 101  0
                         return sealedClasses.getValue(className);
 102  
                 }
 103  0
                 SpiMetaClass workClass = workClasses.get(className);
 104  0
                 if( workClass != null && workClass.resolve(classLoader) ) {
 105  0
                         return workClass;
 106  
                 }
 107  0
                 return null;
 108  
         }
 109  
 
 110  
         @Override
 111  
         public Collection<? extends MetaClass> getMetaClasses() {
 112  0
                 return sealedClasses;
 113  
         }
 114  
 
 115  
         @Override
 116  
         public MetaClass getMetaClass(String className) {
 117  0
                 return sealedClasses.getValue(className);
 118  
         }
 119  
 
 120  
         @Override
 121  
         public String getName() {
 122  0
                 return baseUrl;
 123  
         }
 124  
 }