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
28      extends AbstractResourceBasedComponentDiscoverer
29  {
30      public String getComponentDescriptorLocation()
31      {
32          return "META-INF/plexus/plexus.xml";
33      }
34  
35      @Override
36      protected ComponentSetDescriptor createComponentDescriptors( Reader reader, String source, ClassRealm realm )
37          throws PlexusConfigurationException
38      {
39          ComponentSetDescriptor componentSetDescriptor = new ComponentSetDescriptor();
40  
41          PlexusConfiguration configuration = PlexusTools.buildConfiguration( source, reader );
42  
43          if ( configuration != null )
44          {
45              PlexusConfiguration[] componentConfigurations = configuration.getChild( "components" ).getChildren( "component" );
46  
47              for ( PlexusConfiguration componentConfiguration : componentConfigurations )
48              {
49                  ComponentDescriptor<?> componentDescriptor;
50                  
51                  try
52                  {
53                      componentDescriptor = PlexusTools.buildComponentDescriptor( componentConfiguration, realm );
54  
55                      if ( componentDescriptor == null )
56                      {
57                          continue;
58                      }
59                  }
60                  catch ( PlexusConfigurationException e )
61                  {
62                      throw new PlexusConfigurationException( "Cannot build component descriptor from resource found in:\n" + Arrays.asList( realm.getURLs() ), e );
63                  }
64  
65                  componentDescriptor.setComponentType( "plexus" );
66  
67                  componentDescriptor.setComponentSetDescriptor( componentSetDescriptor );
68  
69                  componentSetDescriptor.addComponentDescriptor( componentDescriptor );
70              }
71          }
72  
73          return componentSetDescriptor;
74      }
75  }