View Javadoc
1   package org.codehaus.plexus;
2   
3   import java.net.URL;
4   import java.util.ArrayList;
5   import java.util.List;
6   import java.util.Map;
7   
8   import org.codehaus.plexus.classworlds.ClassWorld;
9   import org.codehaus.plexus.classworlds.realm.ClassRealm;
10  import org.codehaus.plexus.component.discovery.ComponentDiscoverer;
11  import org.codehaus.plexus.component.discovery.ComponentDiscovererManager;
12  import org.codehaus.plexus.component.discovery.ComponentDiscoveryListener;
13  import org.codehaus.plexus.component.discovery.DefaultComponentDiscoverer;
14  import org.codehaus.plexus.component.discovery.DefaultComponentDiscovererManager;
15  import org.codehaus.plexus.component.discovery.PlexusXmlComponentDiscoverer;
16  import org.codehaus.plexus.component.factory.ComponentFactoryManager;
17  import org.codehaus.plexus.component.factory.DefaultComponentFactoryManager;
18  import org.codehaus.plexus.component.repository.ComponentRepository;
19  import org.codehaus.plexus.component.repository.DefaultComponentRepository;
20  import org.codehaus.plexus.configuration.source.ConfigurationSource;
21  import org.codehaus.plexus.container.initialization.ContainerInitializationPhase;
22  import org.codehaus.plexus.container.initialization.InitializeComponentDiscovererManagerPhase;
23  import org.codehaus.plexus.container.initialization.InitializeComponentFactoryManagerPhase;
24  import org.codehaus.plexus.container.initialization.InitializeComponentRegistryPhase;
25  import org.codehaus.plexus.container.initialization.InitializeContainerConfigurationSourcePhase;
26  import org.codehaus.plexus.container.initialization.InitializeLoggerManagerPhase;
27  import org.codehaus.plexus.container.initialization.InitializeSystemPropertiesPhase;
28  import org.codehaus.plexus.container.initialization.InitializeUserConfigurationSourcePhase;
29  import org.codehaus.plexus.lifecycle.BasicLifecycleHandler;
30  import org.codehaus.plexus.lifecycle.DefaultLifecycleHandlerManager;
31  import org.codehaus.plexus.lifecycle.LifecycleHandler;
32  import org.codehaus.plexus.lifecycle.LifecycleHandlerManager;
33  import org.codehaus.plexus.personality.plexus.lifecycle.phase.ConfigurablePhase;
34  import org.codehaus.plexus.personality.plexus.lifecycle.phase.ContextualizePhase;
35  import org.codehaus.plexus.personality.plexus.lifecycle.phase.DisposePhase;
36  import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializePhase;
37  import org.codehaus.plexus.personality.plexus.lifecycle.phase.LogDisablePhase;
38  import org.codehaus.plexus.personality.plexus.lifecycle.phase.LogEnablePhase;
39  import org.codehaus.plexus.personality.plexus.lifecycle.phase.StartPhase;
40  import org.codehaus.plexus.personality.plexus.lifecycle.phase.StopPhase;
41  
42  /** @author Jason van Zyl */
43  public class DefaultContainerConfiguration implements ContainerConfiguration {
44      private String name;
45  
46      private Map<Object, Object> context;
47  
48      private ClassWorld classWorld;
49  
50      private ClassRealm realm;
51  
52      private String containerConfiguration;
53  
54      private URL containerConfigurationURL;
55  
56      private ConfigurationSource configurationSource;
57  
58      private List<Class> componentDiscoverers = new ArrayList<Class>();
59  
60      private List<Class> componentDiscoveryListeners = new ArrayList<Class>();
61  
62      public ContainerConfiguration setName(String name) {
63          this.name = name;
64  
65          return this;
66      }
67  
68      public ContainerConfiguration setContext(Map<Object, Object> context) {
69          this.context = context;
70  
71          return this;
72      }
73  
74      public ContainerConfiguration setClassWorld(ClassWorld classWorld) {
75          this.classWorld = classWorld;
76  
77          return this;
78      }
79  
80      public ContainerConfiguration setRealm(ClassRealm realm) {
81          this.realm = realm;
82  
83          return this;
84      }
85  
86      public ContainerConfiguration setContainerConfiguration(String containerConfiguration) {
87          this.containerConfiguration = containerConfiguration;
88  
89          return this;
90      }
91  
92      public String getContainerConfiguration() {
93          return containerConfiguration;
94      }
95  
96      public ContainerConfiguration setContainerConfigurationURL(URL containerConfiguration) {
97          this.containerConfigurationURL = containerConfiguration;
98  
99          return this;
100     }
101 
102     public URL getContainerConfigurationURL() {
103         return containerConfigurationURL;
104     }
105 
106     public String getName() {
107         return name;
108     }
109 
110     public Map<Object, Object> getContext() {
111         return context;
112     }
113 
114     public ClassWorld getClassWorld() {
115         return classWorld;
116     }
117 
118     public ClassRealm getRealm() {
119         return realm;
120     }
121 
122     // Programmatic Container Initialization and Setup
123 
124     public ContainerConfiguration setInitializationPhases(ContainerInitializationPhase[] initializationPhases) {
125         this.initializationPhases = initializationPhases;
126 
127         return this;
128     }
129 
130     public ContainerInitializationPhase[] getInitializationPhases() {
131         return initializationPhases;
132     }
133 
134     private ContainerInitializationPhase[] initializationPhases = {
135         new InitializeComponentRegistryPhase(),
136         new InitializeComponentFactoryManagerPhase(),
137         new InitializeContainerConfigurationSourcePhase(),
138         new InitializeLoggerManagerPhase(),
139         new InitializeSystemPropertiesPhase(),
140         new InitializeComponentDiscovererManagerPhase(),
141         new InitializeUserConfigurationSourcePhase()
142     };
143 
144     // Component discoverer
145 
146     private ComponentDiscovererManager componentDiscovererManager;
147 
148     public ContainerConfiguration addComponentDiscoveryListener(ComponentDiscoveryListener componentDiscoveryListener) {
149         getComponentDiscovererManager().registerComponentDiscoveryListener(componentDiscoveryListener);
150 
151         return this;
152     }
153 
154     public ContainerConfiguration addComponentDiscoverer(ComponentDiscoverer componentDiscoverer) {
155         ((DefaultComponentDiscovererManager) getComponentDiscovererManager())
156                 .addComponentDiscoverer(componentDiscoverer);
157 
158         return this;
159     }
160 
161     public ContainerConfiguration addComponentDiscoverer(Class<?> clazz) {
162         componentDiscoverers.add(clazz);
163         return this;
164     }
165 
166     public ContainerConfiguration addComponentDiscoveryListener(Class<?> clazz) {
167         componentDiscoveryListeners.add(clazz);
168         return this;
169     }
170 
171     public List<Class> getComponentDiscoverers() {
172         return componentDiscoverers;
173     }
174 
175     public List<Class> getComponentDiscoveryListeners() {
176         return componentDiscoveryListeners;
177     }
178 
179     public ContainerConfiguration setComponentDiscovererManager(ComponentDiscovererManager componentDiscovererManager) {
180         this.componentDiscovererManager = componentDiscovererManager;
181 
182         return this;
183     }
184 
185     public ComponentDiscovererManager getComponentDiscovererManager() {
186         if (componentDiscovererManager == null) {
187             componentDiscovererManager = new DefaultComponentDiscovererManager();
188 
189             ((DefaultComponentDiscovererManager) componentDiscovererManager)
190                     .addComponentDiscoverer(new DefaultComponentDiscoverer());
191 
192             ((DefaultComponentDiscovererManager) componentDiscovererManager)
193                     .addComponentDiscoverer(new PlexusXmlComponentDiscoverer());
194         }
195 
196         return componentDiscovererManager;
197     }
198 
199     private ComponentFactoryManager componentFactoryManager;
200 
201     public ComponentFactoryManager getComponentFactoryManager() {
202         if (componentFactoryManager == null) {
203             componentFactoryManager = new DefaultComponentFactoryManager();
204         }
205 
206         return componentFactoryManager;
207     }
208 
209     public ContainerConfiguration setComponentFactoryManager(ComponentFactoryManager componentFactoryManager) {
210         this.componentFactoryManager = componentFactoryManager;
211 
212         return this;
213     }
214 
215     private ComponentRepository componentRepository;
216 
217     public ContainerConfiguration setComponentRepository(ComponentRepository componentRepository) {
218         this.componentRepository = componentRepository;
219 
220         return this;
221     }
222 
223     public ComponentRepository getComponentRepository() {
224         if (componentRepository == null) {
225             componentRepository = new DefaultComponentRepository();
226         }
227 
228         return componentRepository;
229     }
230 
231     // Lifecycle handler manager
232 
233     private LifecycleHandlerManager lifecycleHandlerManager;
234 
235     public ContainerConfiguration addLifecycleHandler(LifecycleHandler lifecycleHandler) {
236         getLifecycleHandlerManager().addLifecycleHandler(lifecycleHandler);
237 
238         return this;
239     }
240 
241     public ContainerConfiguration setLifecycleHandlerManager(LifecycleHandlerManager lifecycleHandlerManager) {
242         this.lifecycleHandlerManager = lifecycleHandlerManager;
243 
244         return this;
245     }
246 
247     public LifecycleHandlerManager getLifecycleHandlerManager() {
248         if (lifecycleHandlerManager == null) {
249             lifecycleHandlerManager = new DefaultLifecycleHandlerManager();
250 
251             // Plexus
252             LifecycleHandler plexus = new BasicLifecycleHandler("plexus");
253             // Begin
254             plexus.addBeginSegment(new LogEnablePhase());
255             plexus.addBeginSegment(new ContextualizePhase());
256             //            plexus.addBeginSegment( new AutoConfigurePhase() );
257             plexus.addBeginSegment(new InitializePhase());
258             plexus.addBeginSegment(new StartPhase());
259             // End
260             plexus.addEndSegment(new StopPhase());
261             plexus.addEndSegment(new DisposePhase());
262             plexus.addEndSegment(new LogDisablePhase());
263             lifecycleHandlerManager.addLifecycleHandler(plexus);
264 
265             // Basic
266             LifecycleHandler basic = new BasicLifecycleHandler("basic");
267             // Begin
268             basic.addBeginSegment(new LogEnablePhase());
269             basic.addBeginSegment(new ContextualizePhase());
270             //            basic.addBeginSegment( new AutoConfigurePhase() );
271             basic.addBeginSegment(new InitializePhase());
272             basic.addBeginSegment(new StartPhase());
273             // End
274             basic.addEndSegment(new StopPhase());
275             basic.addEndSegment(new DisposePhase());
276             basic.addEndSegment(new LogDisablePhase());
277             lifecycleHandlerManager.addLifecycleHandler(basic);
278 
279             // Plexus configurable
280             LifecycleHandler plexusConfigurable = new BasicLifecycleHandler("plexus-configurable");
281             // Begin
282             plexusConfigurable.addBeginSegment(new LogEnablePhase());
283             plexusConfigurable.addBeginSegment(new ContextualizePhase());
284             plexusConfigurable.addBeginSegment(new ConfigurablePhase());
285             plexusConfigurable.addBeginSegment(new InitializePhase());
286             plexusConfigurable.addBeginSegment(new StartPhase());
287             // End
288             plexusConfigurable.addEndSegment(new StopPhase());
289             plexusConfigurable.addEndSegment(new DisposePhase());
290             plexusConfigurable.addEndSegment(new LogDisablePhase());
291             lifecycleHandlerManager.addLifecycleHandler(plexusConfigurable);
292 
293             // Passive
294             LifecycleHandler passive = new BasicLifecycleHandler("passive");
295             lifecycleHandlerManager.addLifecycleHandler(passive);
296 
297             // Bootstrap
298             LifecycleHandler bootstrap = new BasicLifecycleHandler("bootstrap");
299             bootstrap.addBeginSegment(new ContextualizePhase());
300             lifecycleHandlerManager.addLifecycleHandler(bootstrap);
301         }
302 
303         return lifecycleHandlerManager;
304     }
305 
306     // Configuration Sources
307 
308     public ContainerConfiguration setConfigurationSource(ConfigurationSource configurationSource) {
309         this.configurationSource = configurationSource;
310 
311         return this;
312     }
313 
314     public ConfigurationSource getConfigurationSource() {
315         return configurationSource;
316     }
317 }