View Javadoc
1   package org.codehaus.plexus.container.initialization;
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.ComponentRepository;
20  import org.codehaus.plexus.component.repository.ComponentDescriptor;
21  import org.codehaus.plexus.component.repository.io.PlexusTools;
22  import org.codehaus.plexus.component.repository.exception.ComponentRepositoryException;
23  import org.codehaus.plexus.component.composition.CycleDetectedInComponentGraphException;
24  import org.codehaus.plexus.component.manager.PerLookupComponentManagerFactory;
25  import org.codehaus.plexus.component.manager.SingletonComponentManagerFactory;
26  import org.codehaus.plexus.configuration.PlexusConfiguration;
27  import org.codehaus.plexus.configuration.PlexusConfigurationException;
28  import org.codehaus.plexus.DefaultComponentRegistry;
29  import org.codehaus.plexus.ComponentRegistry;
30  import org.codehaus.plexus.lifecycle.LifecycleHandlerManager;
31  
32  /**
33   * @author Jason van Zyl
34   */
35  public class InitializeComponentRegistryPhase 
36      implements ContainerInitializationPhase
37  {
38      public void execute( ContainerInitializationContext context )
39          throws ContainerInitializationException
40      {
41          ComponentRepository repository = getComponentRepository( context );
42  
43          LifecycleHandlerManager lifecycleHandlerManager = getLifecycleHandlerManager( context );
44  
45          ComponentRegistry componentRegistry = new DefaultComponentRegistry( context.getContainer(),
46              repository,
47              lifecycleHandlerManager );
48  
49          componentRegistry.registerComponentManagerFactory( new PerLookupComponentManagerFactory() );
50  
51          componentRegistry.registerComponentManagerFactory( new SingletonComponentManagerFactory() );
52  
53          context.getContainer().setComponentRegistry( componentRegistry );
54      }
55  
56      private ComponentRepository getComponentRepository( ContainerInitializationContext context )
57          throws ContainerInitializationException
58      {
59          ComponentRepository repository = context.getContainerConfiguration().getComponentRepository();
60  
61          // Add the components defined in the container xml configuration
62          try
63          {
64              PlexusConfiguration configuration = context.getContainerXmlConfiguration();
65  
66              PlexusConfiguration[] componentConfigurations = configuration.getChild( "components" ).getChildren( "component" );
67              for ( PlexusConfiguration componentConfiguration : componentConfigurations )
68              {
69                  ComponentDescriptor<?> componentDescriptor = PlexusTools.buildComponentDescriptor( componentConfiguration, context.getContainer().getContainerRealm() );
70                  componentDescriptor.setRealm( context.getContainer().getContainerRealm() );
71                  repository.addComponentDescriptor( componentDescriptor );
72              }
73          }
74          catch ( PlexusConfigurationException e )
75          {
76              throw new ContainerInitializationException( "Error initializing component repository: " + "Cannot unmarshall component descriptor: ", e );
77          }
78          catch ( CycleDetectedInComponentGraphException e )
79          {
80              throw new ContainerInitializationException( "A cycle has been detected in the components of the system: ", e );
81          }
82          
83          return repository;
84      }
85  
86      private LifecycleHandlerManager getLifecycleHandlerManager( ContainerInitializationContext context )
87      {
88          LifecycleHandlerManager lifecycleHandlerManager = context.getContainerConfiguration().getLifecycleHandlerManager();
89          lifecycleHandlerManager.initialize();
90          return lifecycleHandlerManager;
91      }
92  }