1 package org.codehaus.plexus.container.initialization;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
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
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 }