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"); you may not use this file except
7    * in compliance with the License. You may obtain a copy of the License at
8    *
9    * http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software distributed under the License
12   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13   * or implied. See the License for the specific language governing permissions and limitations under
14   * the License.
15   */
16  
17  import java.io.Reader;
18  import java.util.Arrays;
19  
20  import org.codehaus.plexus.classworlds.realm.ClassRealm;
21  import org.codehaus.plexus.component.repository.ComponentDescriptor;
22  import org.codehaus.plexus.component.repository.ComponentSetDescriptor;
23  import org.codehaus.plexus.component.repository.io.PlexusTools;
24  import org.codehaus.plexus.configuration.PlexusConfiguration;
25  import org.codehaus.plexus.configuration.PlexusConfigurationException;
26  
27  public class PlexusXmlComponentDiscoverer extends AbstractResourceBasedComponentDiscoverer {
28      public String getComponentDescriptorLocation() {
29          return "META-INF/plexus/plexus.xml";
30      }
31  
32      @Override
33      protected ComponentSetDescriptor createComponentDescriptors(Reader reader, String source, ClassRealm realm)
34              throws PlexusConfigurationException {
35          ComponentSetDescriptor componentSetDescriptor = new ComponentSetDescriptor();
36  
37          PlexusConfiguration configuration = PlexusTools.buildConfiguration(source, reader);
38  
39          if (configuration != null) {
40              PlexusConfiguration[] componentConfigurations =
41                      configuration.getChild("components").getChildren("component");
42  
43              for (PlexusConfiguration componentConfiguration : componentConfigurations) {
44                  ComponentDescriptor<?> componentDescriptor;
45  
46                  try {
47                      componentDescriptor = PlexusTools.buildComponentDescriptor(componentConfiguration, realm);
48  
49                      if (componentDescriptor == null) {
50                          continue;
51                      }
52                  } catch (PlexusConfigurationException e) {
53                      throw new PlexusConfigurationException(
54                              "Cannot build component descriptor from resource found in:\n"
55                                      + Arrays.asList(realm.getURLs()),
56                              e);
57                  }
58  
59                  componentDescriptor.setComponentType("plexus");
60  
61                  componentDescriptor.setComponentSetDescriptor(componentSetDescriptor);
62  
63                  componentSetDescriptor.addComponentDescriptor(componentDescriptor);
64              }
65          }
66  
67          return componentSetDescriptor;
68      }
69  }