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
44      implements ContainerConfiguration
45  {
46      private String name;
47  
48      private Map<Object, Object> context;
49  
50      private ClassWorld classWorld;
51  
52      private ClassRealm realm;
53  
54      private String containerConfiguration;
55  
56      private URL containerConfigurationURL;
57  
58      private ConfigurationSource configurationSource;
59      
60      private List<Class> componentDiscoverers = new ArrayList<Class>();
61      
62      private List<Class> componentDiscoveryListeners = new ArrayList<Class>();
63      
64      public ContainerConfiguration setName( String name )
65      {
66          this.name = name;
67  
68          return this;
69      }
70  
71      public ContainerConfiguration setContext( Map<Object, Object> context )
72      {
73          this.context = context;
74  
75          return this;
76      }
77  
78      public ContainerConfiguration setClassWorld( ClassWorld classWorld )
79      {
80          this.classWorld = classWorld;
81  
82          return this;
83      }
84  
85      public ContainerConfiguration setRealm( ClassRealm realm )
86      {
87          this.realm = realm;
88  
89          return this;
90      }
91  
92      public ContainerConfiguration setContainerConfiguration( String containerConfiguration )
93      {
94          this.containerConfiguration = containerConfiguration;
95  
96          return this;
97      }
98  
99      public String getContainerConfiguration()
100     {
101         return containerConfiguration;
102     }
103 
104     public ContainerConfiguration setContainerConfigurationURL( URL containerConfiguration )
105     {
106         this.containerConfigurationURL = containerConfiguration;
107 
108         return this;
109     }
110 
111     public URL getContainerConfigurationURL()
112     {
113         return containerConfigurationURL;
114     }
115 
116     public String getName()
117     {
118         return name;
119     }
120 
121     public Map<Object, Object> getContext()
122     {
123         return context;
124     }
125 
126     public ClassWorld getClassWorld()
127     {
128         return classWorld;
129     }
130 
131     public ClassRealm getRealm()
132     {
133         return realm;
134     }
135 
136     // Programmatic Container Initialization and Setup
137 
138     public ContainerConfiguration setInitializationPhases( ContainerInitializationPhase[] initializationPhases )
139     {
140         this.initializationPhases = initializationPhases;
141 
142         return this;
143     }
144 
145     public ContainerInitializationPhase[] getInitializationPhases()
146     {
147         return initializationPhases;
148     }
149 
150     private ContainerInitializationPhase[] initializationPhases =
151         {
152             new InitializeComponentRegistryPhase(),
153             new InitializeComponentFactoryManagerPhase(),
154             new InitializeContainerConfigurationSourcePhase(),
155             new InitializeLoggerManagerPhase(),
156             new InitializeSystemPropertiesPhase(),
157             new InitializeComponentDiscovererManagerPhase(),
158             new InitializeUserConfigurationSourcePhase()
159 
160         };
161 
162     // Component discoverer
163 
164     private ComponentDiscovererManager componentDiscovererManager;
165 
166     public ContainerConfiguration addComponentDiscoveryListener( ComponentDiscoveryListener componentDiscoveryListener )
167     {
168         getComponentDiscovererManager().registerComponentDiscoveryListener( componentDiscoveryListener );
169 
170         return this;
171     }
172 
173     public ContainerConfiguration addComponentDiscoverer( ComponentDiscoverer componentDiscoverer )
174     {
175         ((DefaultComponentDiscovererManager)getComponentDiscovererManager()).addComponentDiscoverer( componentDiscoverer );
176 
177         return this;
178     }
179 
180     public ContainerConfiguration addComponentDiscoverer( Class<?> clazz )
181     {
182         componentDiscoverers.add( clazz );
183         return this;
184     }
185 
186     public ContainerConfiguration addComponentDiscoveryListener( Class<?> clazz )
187     {
188         componentDiscoveryListeners.add( clazz );
189         return this;
190     }
191 
192     public List<Class> getComponentDiscoverers()
193     {
194         return componentDiscoverers;
195     }
196 
197     public List<Class> getComponentDiscoveryListeners()
198     {
199         return componentDiscoveryListeners;
200     }    
201     
202     public ContainerConfiguration setComponentDiscovererManager( ComponentDiscovererManager componentDiscovererManager )
203     {
204         this.componentDiscovererManager = componentDiscovererManager;
205 
206         return this;
207     }
208 
209     public ComponentDiscovererManager getComponentDiscovererManager()
210     {
211         if ( componentDiscovererManager == null )
212         {
213             componentDiscovererManager = new DefaultComponentDiscovererManager();
214 
215             ((DefaultComponentDiscovererManager)componentDiscovererManager).addComponentDiscoverer( new DefaultComponentDiscoverer() );
216 
217             ((DefaultComponentDiscovererManager)componentDiscovererManager).addComponentDiscoverer( new PlexusXmlComponentDiscoverer() );
218         }
219 
220         return componentDiscovererManager;
221     }
222 
223 
224     private ComponentFactoryManager componentFactoryManager;
225 
226     public ComponentFactoryManager getComponentFactoryManager()
227     {
228         if ( componentFactoryManager == null )
229         {
230             componentFactoryManager = new DefaultComponentFactoryManager();                        
231         }
232 
233         return componentFactoryManager;
234     }
235 
236     public ContainerConfiguration setComponentFactoryManager( ComponentFactoryManager componentFactoryManager )
237     {
238         this.componentFactoryManager = componentFactoryManager;
239 
240         return this;
241     }
242 
243     private ComponentRepository componentRepository;
244 
245     public ContainerConfiguration setComponentRepository( ComponentRepository componentRepository )
246     {
247         this.componentRepository = componentRepository;
248 
249         return this;
250     }
251 
252     public ComponentRepository getComponentRepository()
253     {
254         if ( componentRepository == null )
255         {
256             componentRepository = new DefaultComponentRepository();
257         }
258 
259         return componentRepository;
260     }
261 
262     // Lifecycle handler manager
263 
264     private LifecycleHandlerManager lifecycleHandlerManager;
265 
266     public ContainerConfiguration addLifecycleHandler( LifecycleHandler lifecycleHandler )
267     {
268         getLifecycleHandlerManager().addLifecycleHandler( lifecycleHandler );
269 
270         return this;
271     }
272 
273     public ContainerConfiguration setLifecycleHandlerManager( LifecycleHandlerManager lifecycleHandlerManager )
274     {
275         this.lifecycleHandlerManager = lifecycleHandlerManager;
276 
277         return this;
278     }
279 
280     public LifecycleHandlerManager getLifecycleHandlerManager()
281     {
282         if ( lifecycleHandlerManager == null )
283         {
284             lifecycleHandlerManager = new DefaultLifecycleHandlerManager();
285 
286             // Plexus
287             LifecycleHandler plexus = new BasicLifecycleHandler( "plexus" );
288             // Begin
289             plexus.addBeginSegment( new LogEnablePhase() );
290             plexus.addBeginSegment( new ContextualizePhase() );
291 //            plexus.addBeginSegment( new AutoConfigurePhase() );
292             plexus.addBeginSegment( new InitializePhase() );
293             plexus.addBeginSegment( new StartPhase() );
294             // End
295             plexus.addEndSegment( new StopPhase() );
296             plexus.addEndSegment( new DisposePhase() );
297             plexus.addEndSegment( new LogDisablePhase() );
298             lifecycleHandlerManager.addLifecycleHandler( plexus );
299 
300             // Basic
301             LifecycleHandler basic = new BasicLifecycleHandler( "basic" );
302             // Begin
303             basic.addBeginSegment( new LogEnablePhase() );
304             basic.addBeginSegment( new ContextualizePhase() );
305 //            basic.addBeginSegment( new AutoConfigurePhase() );
306             basic.addBeginSegment( new InitializePhase() );
307             basic.addBeginSegment( new StartPhase() );
308             // End
309             basic.addEndSegment( new StopPhase() );
310             basic.addEndSegment( new DisposePhase() );
311             basic.addEndSegment( new LogDisablePhase() );
312             lifecycleHandlerManager.addLifecycleHandler( basic );
313 
314             // Plexus configurable
315             LifecycleHandler plexusConfigurable = new BasicLifecycleHandler( "plexus-configurable" );
316             // Begin
317             plexusConfigurable.addBeginSegment( new LogEnablePhase() );
318             plexusConfigurable.addBeginSegment( new ContextualizePhase() );
319             plexusConfigurable.addBeginSegment( new ConfigurablePhase() );
320             plexusConfigurable.addBeginSegment( new InitializePhase() );
321             plexusConfigurable.addBeginSegment( new StartPhase() );
322             // End
323             plexusConfigurable.addEndSegment( new StopPhase() );
324             plexusConfigurable.addEndSegment( new DisposePhase() );
325             plexusConfigurable.addEndSegment( new LogDisablePhase() );
326             lifecycleHandlerManager.addLifecycleHandler( plexusConfigurable );
327 
328             // Passive
329             LifecycleHandler passive = new BasicLifecycleHandler( "passive" );
330             lifecycleHandlerManager.addLifecycleHandler( passive );
331 
332             // Bootstrap
333             LifecycleHandler bootstrap = new BasicLifecycleHandler( "bootstrap" );
334             bootstrap.addBeginSegment( new ContextualizePhase() );
335             lifecycleHandlerManager.addLifecycleHandler( bootstrap );
336         }
337 
338         return lifecycleHandlerManager;
339     }
340 
341     // Configuration Sources
342 
343     public ContainerConfiguration setConfigurationSource( ConfigurationSource configurationSource )
344     {
345         this.configurationSource = configurationSource;
346 
347         return this;
348     }
349 
350     public ConfigurationSource getConfigurationSource()
351     {
352         return configurationSource;
353     }
354 }