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