Coverage Report - org.codehaus.plexus.component.discovery.AbstractResourceBasedComponentDiscoverer
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractResourceBasedComponentDiscoverer
80 %
17/21
100 %
2/2
2,667
 
 1  
 package org.codehaus.plexus.component.discovery;
 2  
 
 3  
 /*
 4  
  * Copyright 2001-2006 Codehaus Foundation.
 5  
  *
 6  
  * Licensed under the Apache License, Version 2.0 (the "License");
 7  
  * you may not use this file except in compliance with the License.
 8  
  * You may obtain a copy of the License at
 9  
  *
 10  
  *      http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  
  * See the License for the specific language governing permissions and
 16  
  * limitations under the License.
 17  
  */
 18  
 
 19  
 import java.io.IOException;
 20  
 import java.io.Reader;
 21  
 import java.net.URL;
 22  
 import java.net.URLConnection;
 23  
 import java.util.ArrayList;
 24  
 import java.util.Collections;
 25  
 import java.util.Enumeration;
 26  
 import java.util.List;
 27  
 
 28  
 import org.codehaus.plexus.classworlds.realm.ClassRealm;
 29  
 import org.codehaus.plexus.component.repository.ComponentSetDescriptor;
 30  
 import org.codehaus.plexus.configuration.PlexusConfigurationException;
 31  
 import org.codehaus.plexus.context.Context;
 32  
 import org.codehaus.plexus.context.ContextMapAdapter;
 33  
 import org.codehaus.plexus.util.IOUtil;
 34  
 import org.codehaus.plexus.util.InterpolationFilterReader;
 35  
 import org.codehaus.plexus.util.ReaderFactory;
 36  
 
 37  
 //TODO: this should be a default strategy of searching through classloaders. a discoverer should really not have to be
 38  
 // concerned finding a particular resource and how to turn it into a set of component descriptors.
 39  
 
 40  
 /**
 41  
  * @author Jason van Zyl
 42  
  */
 43  123
 public abstract class AbstractResourceBasedComponentDiscoverer
 44  
     implements ComponentDiscoverer
 45  
 {
 46  
     protected abstract String getComponentDescriptorLocation();
 47  
 
 48  
     protected abstract ComponentSetDescriptor createComponentDescriptors( Reader reader, String source, ClassRealm realm )
 49  
         throws PlexusConfigurationException;
 50  
 
 51  
     public List<ComponentSetDescriptor> findComponents( Context context, ClassRealm realm )
 52  
         throws PlexusConfigurationException
 53  
     {
 54  280
         List<ComponentSetDescriptor> componentSetDescriptors = new ArrayList<ComponentSetDescriptor>();
 55  
 
 56  
         Enumeration<URL> resources;
 57  
         
 58  
         try
 59  
         {
 60  280
             resources = realm.getResources( getComponentDescriptorLocation() );
 61  
         }
 62  0
         catch ( IOException e )
 63  
         {
 64  0
             throw new PlexusConfigurationException( "Unable to retrieve resources for: " + getComponentDescriptorLocation() + " in class realm: " + realm.getId() );
 65  281
         }
 66  
 
 67  281
         for ( URL url : Collections.list( resources ))
 68  
         {
 69  520
             Reader reader = null;
 70  
             
 71  
             try
 72  
             {
 73  520
                 URLConnection conn = url.openConnection();
 74  
 
 75  520
                 conn.setUseCaches( false );
 76  
 
 77  520
                 conn.connect();
 78  
 
 79  518
                 reader = ReaderFactory.newXmlReader( conn.getInputStream() );
 80  
 
 81  516
                 InterpolationFilterReader interpolationFilterReader = new InterpolationFilterReader( reader, new ContextMapAdapter( context ) );
 82  
 
 83  516
                 ComponentSetDescriptor componentSetDescriptor = createComponentDescriptors( interpolationFilterReader, url.toString(), realm );
 84  
 
 85  520
                 componentSetDescriptors.add( componentSetDescriptor );
 86  
             }
 87  0
             catch ( IOException ex )
 88  
             {
 89  0
                 throw new PlexusConfigurationException( "Error reading configuration " + url, ex );
 90  
             }
 91  
             finally
 92  
             {
 93  520
                 IOUtil.close( reader );
 94  520
             }
 95  520
         }
 96  
 
 97  281
         return componentSetDescriptors;
 98  
     }
 99  
 }