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