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