View Javadoc
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 org.codehaus.plexus.component.repository.ComponentDescriptor;
20  import org.codehaus.plexus.component.repository.ComponentSetDescriptor;
21  import org.codehaus.plexus.component.repository.io.PlexusTools;
22  import org.codehaus.plexus.configuration.PlexusConfiguration;
23  import org.codehaus.plexus.configuration.PlexusConfigurationException;
24  import org.codehaus.plexus.classworlds.realm.ClassRealm;
25  
26  import java.io.Reader;
27  import java.util.ArrayList;
28  import java.util.List;
29  
30  /**
31   * @author Jason van Zyl
32   */
33  public class DefaultComponentDiscoverer
34      extends AbstractResourceBasedComponentDiscoverer
35  {
36      public String getComponentDescriptorLocation()
37      {
38          return "META-INF/plexus/components.xml";
39      }
40  
41      public ComponentSetDescriptor createComponentDescriptors( Reader componentDescriptorReader, String source, ClassRealm realm )
42          throws PlexusConfigurationException
43      {
44          PlexusConfiguration componentDescriptorConfiguration = PlexusTools.buildConfiguration( source, componentDescriptorReader );
45  
46          ComponentSetDescriptor componentSetDescriptor = new ComponentSetDescriptor();
47  
48          List<ComponentDescriptor<?>> componentDescriptors = new ArrayList<ComponentDescriptor<?>>();
49  
50          PlexusConfiguration[] componentConfigurations = componentDescriptorConfiguration.getChild( "components" ).getChildren( "component" );
51  
52          for ( PlexusConfiguration componentConfiguration : componentConfigurations )
53          {
54              ComponentDescriptor<?> componentDescriptor;
55              try
56              {
57                  componentDescriptor = PlexusTools.buildComponentDescriptor( componentConfiguration, realm );
58              }
59              catch ( PlexusConfigurationException e )
60              {
61                 	// This is not the most accurate of exceptions as the only real case where this exception
62              	// will be thrown is when the implementation class of the component sited cannot be loaded.
63              	// In the case where role and implementation classes do not exist then we just shouldn't
64              	// create the component descriptor. All information should be taken from annotations which
65              	// will be correct, so in the case we can't load the class it must be coming from and older
66              	// hand written descriptor which is incorrect.
67              	
68              	continue;            	
69              }
70  
71              componentDescriptor.setSource( source );
72  
73              componentDescriptor.setComponentType( "plexus" );
74  
75              componentDescriptor.setComponentSetDescriptor( componentSetDescriptor );
76  
77              componentDescriptors.add( componentDescriptor );
78          }
79  
80          componentSetDescriptor.setComponents( componentDescriptors );
81  
82          componentSetDescriptor.setSource( source );
83  
84          return componentSetDescriptor;
85      }
86  }