View Javadoc
1   package org.codehaus.plexus.configuration;
2   
3   import org.codehaus.plexus.configuration.xml.XmlPlexusConfiguration;
4   
5   /*
6    * Copyright 2001-2006 Codehaus Foundation.
7    *
8    * Licensed under the Apache License, Version 2.0 (the "License");
9    * you may not use this file except in compliance with the License.
10   * You may obtain a copy of the License at
11   *
12   *      http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  /**
22   * @todo This merger explicity uses the XML implementation of the plexus configuration but
23   * it must work for configurations coming from any source.
24   */
25  public class PlexusConfigurationMerger
26  {
27      // -----------------------------------+-----------------------------------------------------------------
28      //  E L E M E N T                     |
29      // -----------------------------------+-----------------------------------------------------------------
30      // load-on-start                      | user
31      // -----------------------------------+-----------------------------------------------------------------
32      // system-properties                  | user
33      // -----------------------------------+-----------------------------------------------------------------
34      // configurations-directory           | user
35      // -----------------------------------+-----------------------------------------------------------------
36      // logging                            | user wins
37      // -----------------------------------+-----------------------------------------------------------------
38      // component-repository               | user wins
39      // -----------------------------------+-----------------------------------------------------------------
40      // resources                          | user wins, but system resources show through
41      // -----------------------------------+-----------------------------------------------------------------
42      // component-manager-manager          | user wins, but system resources show through
43  	// -----------------------------------+-----------------------------------------------------------------
44  	// component-discoverer-manager       | user wins, but system resources show through
45  	// -----------------------------------+-----------------------------------------------------------------
46  	// component-factory-manager          | user wins, but system resources show through
47      // -----------------------------------+-----------------------------------------------------------------
48      // lifecycle-handler-manager          | user wins, but system lifecycles show through
49  	// -----------------------------------+-----------------------------------------------------------------
50  	// component-composer-manager         | user wins, but system lifecycles show through
51      // -----------------------------------+-----------------------------------------------------------------
52      // components                         | user
53      // -----------------------------------+-----------------------------------------------------------------
54  
55      public static PlexusConfiguration merge( PlexusConfiguration user, PlexusConfiguration system )
56      {
57          PlexusConfiguration mergedConfiguration = new XmlPlexusConfiguration( "plexus" );
58  
59          // ----------------------------------------------------------------------
60          // Load on start
61          // ----------------------------------------------------------------------
62  
63          PlexusConfiguration loadOnStart = user.getChild( "load-on-start" );
64  
65          if ( loadOnStart.getChildCount() != 0 )
66          {
67              mergedConfiguration.addChild( loadOnStart );
68          }
69  
70          // ----------------------------------------------------------------------
71          // System properties
72          // ----------------------------------------------------------------------
73  
74          PlexusConfiguration systemProperties = user.getChild( "system-properties" );
75  
76          if ( systemProperties.getChildCount() != 0 )
77          {
78              mergedConfiguration.addChild( systemProperties );
79          }
80  
81          // ----------------------------------------------------------------------
82          // Configurations directory
83          // ----------------------------------------------------------------------
84  
85          PlexusConfiguration[] configurationsDirectories = user.getChildren( "configurations-directory" );
86  
87          if ( configurationsDirectories.length != 0 )
88          {
89              for (PlexusConfiguration configurationsDirectory : configurationsDirectories) {
90                  mergedConfiguration.addChild(configurationsDirectory);
91              }
92          }
93  
94          // ----------------------------------------------------------------------
95          // Logging
96          // ----------------------------------------------------------------------
97  
98          PlexusConfiguration logging = user.getChild( "logging" );
99  
100         if ( logging.getChildCount() != 0 )
101         {
102             mergedConfiguration.addChild( logging );
103         }
104         else
105         {
106             mergedConfiguration.addChild( system.getChild( "logging" ) );
107         }
108 
109         // ----------------------------------------------------------------------
110         // Container initialization phases
111         // ----------------------------------------------------------------------
112 
113         mergedConfiguration.addChild( system.getChild( "container-initialization") );
114 
115         mergedConfiguration.addChild( system.getChild( "component-lookup-manager") );
116 
117         // ----------------------------------------------------------------------
118         // Component repository
119         // ----------------------------------------------------------------------
120 
121         PlexusConfiguration componentRepository = user.getChild( "component-repository" );
122 
123         if ( componentRepository.getChildCount() != 0 )
124         {
125             mergedConfiguration.addChild( componentRepository );
126         }
127         else
128         {
129             mergedConfiguration.addChild( system.getChild( "component-repository" ) );
130         }
131 
132         // ----------------------------------------------------------------------
133         // Resources
134         // ----------------------------------------------------------------------
135 
136         copyResources( system, mergedConfiguration );
137 
138         copyResources( user, mergedConfiguration );
139 
140         // ----------------------------------------------------------------------
141         // Component manager manager
142         // ----------------------------------------------------------------------
143 
144         PlexusConfiguration componentManagerManager =  user.getChild( "component-manager-manager" );
145 
146         if ( componentManagerManager.getChildCount() != 0 )
147         {
148             mergedConfiguration.addChild( componentManagerManager );
149 
150             copyComponentManagers( system.getChild( "component-manager-manager" ), componentManagerManager );
151         }
152         else
153         {
154             mergedConfiguration.addChild( system.getChild( "component-manager-manager" ) );
155         }
156 
157         // ----------------------------------------------------------------------
158         // Component discoverer manager
159         // ----------------------------------------------------------------------
160 
161         PlexusConfiguration componentDiscovererManager =  user.getChild( "component-discoverer-manager" );
162 
163         if ( componentDiscovererManager.getChildCount() != 0 )
164         {
165             mergedConfiguration.addChild( componentDiscovererManager );
166 
167             copyComponentDiscoverers( system.getChild( "component-discoverer-manager" ), componentDiscovererManager );
168         }
169         else
170         {
171             mergedConfiguration.addChild( system.getChild( "component-discoverer-manager" ) );
172         }
173 
174         // ----------------------------------------------------------------------
175         // Component factory manager
176         // ----------------------------------------------------------------------
177 
178         PlexusConfiguration componentFactoryManager =  user.getChild( "component-factory-manager" );
179 
180         if ( componentFactoryManager.getChildCount() != 0 )
181         {
182             mergedConfiguration.addChild( componentFactoryManager );
183 
184             copyComponentFactories( system.getChild( "component-factory-manager" ), componentFactoryManager );
185         }
186         else
187         {
188             mergedConfiguration.addChild( system.getChild( "component-factory-manager" ) );
189         }
190 
191         // ----------------------------------------------------------------------
192         // Lifecycle handler managers
193         // ----------------------------------------------------------------------
194 
195         PlexusConfiguration lifecycleHandlerManager = user.getChild( "lifecycle-handler-manager" );
196 
197         if ( lifecycleHandlerManager.getChildCount() != 0 )
198         {
199             mergedConfiguration.addChild( lifecycleHandlerManager );
200 
201             copyLifecycles( system.getChild( "lifecycle-handler-manager" ), lifecycleHandlerManager );
202         }
203         else
204         {
205             mergedConfiguration.addChild( system.getChild( "lifecycle-handler-manager" ) );
206         }
207 
208         // ----------------------------------------------------------------------
209         // Component factory manager
210         // ----------------------------------------------------------------------
211 
212         PlexusConfiguration componentComposerManager =  user.getChild( "component-composer-manager" );
213 
214         if ( componentComposerManager.getChildCount() != 0 )
215         {
216             mergedConfiguration.addChild( componentComposerManager );
217 
218             copyComponentComposers( system.getChild( "component-composer-manager" ), componentComposerManager );
219         }
220         else
221         {
222             mergedConfiguration.addChild( system.getChild( "component-composer-manager" ) );
223         }
224 
225         // ----------------------------------------------------------------------
226         // Components
227         // ----------------------------------------------------------------------
228         // We grab the system components first and then add the user defined
229         // components so that user defined components will win. When component
230         // descriptors are processed the last definition wins because the component
231         // descriptors are stored in a Map in the component repository.
232         // ----------------------------------------------------------------------
233 
234         PlexusConfiguration components = system.getChild( "components" );
235 
236         mergedConfiguration.addChild( components );
237 
238         copyComponents( user.getChild( "components" ), components );
239 
240         return mergedConfiguration;
241     }
242 
243     private static void copyResources( PlexusConfiguration source, PlexusConfiguration destination )
244     {
245         PlexusConfiguration handlers[] = source.getChild( "resources" ).getChildren();
246 
247         PlexusConfiguration dest = destination.getChild( "resources" );
248 
249         for (PlexusConfiguration handler : handlers) {
250             dest.addChild(handler);
251         }
252     }
253 
254     private static void copyComponentManagers( PlexusConfiguration source, PlexusConfiguration destination )
255     {
256         try
257         {
258             PlexusConfiguration id = destination.getChild( "default-component-manager-id" );
259 
260             String sid = source.getChild( "default-component-manager-id" ).getValue();
261 
262             if ( id.getValue() == null )
263             {
264                 id.setValue( sid );   
265             }
266         }
267         catch ( PlexusConfigurationException e )
268         {
269             // do nothing
270         }
271 
272 
273         PlexusConfiguration handlers[] = source.getChild( "component-managers" ).getChildren( "component-manager" );
274 
275         PlexusConfiguration dest = destination.getChild( "component-managers" );
276 
277         for (PlexusConfiguration handler : handlers) {
278             dest.addChild(handler);
279         }
280     }
281 
282     private static void copyComponentDiscoverers( PlexusConfiguration source, PlexusConfiguration destination )
283     {
284         PlexusConfiguration handlers[] = source.getChild( "component-discoverers" ).getChildren( "component-discoverer" );
285 
286         PlexusConfiguration dest = destination.getChild( "component-discoverers" );
287 
288         for (PlexusConfiguration handler : handlers) {
289             dest.addChild(handler);
290         }
291     }
292 
293     private static void copyComponentFactories( PlexusConfiguration source, PlexusConfiguration destination )
294     {
295         PlexusConfiguration handlers[] = source.getChild( "component-factories" ).getChildren( "component-factory" );
296 
297         PlexusConfiguration dest = destination.getChild( "component-factories" );
298 
299         for (PlexusConfiguration handler : handlers) {
300             dest.addChild(handler);
301         }
302     }
303 
304     private static void copyComponentComposers( PlexusConfiguration source, PlexusConfiguration destination )
305     {
306         try
307         {
308             PlexusConfiguration id = destination.getChild( "default-component-composer-id" );
309 
310             String sid = source.getChild( "default-component-composer-id" ).getValue();
311 
312             if ( id.getValue() == null )
313             {
314                 id.setValue( sid );
315             }
316         }
317         catch ( PlexusConfigurationException e )
318         {
319             // do nothing
320         }
321 
322         PlexusConfiguration composers[] = source.getChild( "component-composers" ).getChildren( "component-composer" );
323 
324         PlexusConfiguration dest = destination.getChild( "component-composers" );
325 
326         for (PlexusConfiguration composer : composers) {
327             dest.addChild(composer);
328         }
329     }
330 
331     private static void copyLifecycles( PlexusConfiguration source, PlexusConfiguration destination )
332     {
333         PlexusConfiguration handlers[] = source.getChild( "lifecycle-handlers" ).getChildren( "lifecycle-handler" );
334 
335         PlexusConfiguration dest = destination.getChild( "lifecycle-handlers" );
336 
337         for (PlexusConfiguration handler : handlers) {
338             dest.addChild(handler);
339         }
340     }
341 
342     private static void copyComponents( PlexusConfiguration source, PlexusConfiguration destination )
343     {
344         PlexusConfiguration components[] = source.getChildren( "component" );
345 
346         for (PlexusConfiguration component : components) {
347             destination.addChild(component);
348         }
349     }
350 }