Coverage Report - org.codehaus.plexus.configuration.PlexusConfigurationMerger
 
Classes in this File Line Coverage Branch Coverage Complexity
PlexusConfigurationMerger
49 %
49/99
47 %
19/40
3,75
 
 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  0
 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  46
         PlexusConfiguration mergedConfiguration = new XmlPlexusConfiguration( "plexus" );
 58  
 
 59  
         // ----------------------------------------------------------------------
 60  
         // Load on start
 61  
         // ----------------------------------------------------------------------
 62  
 
 63  46
         PlexusConfiguration loadOnStart = user.getChild( "load-on-start" );
 64  
 
 65  46
         if ( loadOnStart.getChildCount() != 0 )
 66  
         {
 67  29
             mergedConfiguration.addChild( loadOnStart );
 68  
         }
 69  
 
 70  
         // ----------------------------------------------------------------------
 71  
         // System properties
 72  
         // ----------------------------------------------------------------------
 73  
 
 74  46
         PlexusConfiguration systemProperties = user.getChild( "system-properties" );
 75  
 
 76  46
         if ( systemProperties.getChildCount() != 0 )
 77  
         {
 78  28
             mergedConfiguration.addChild( systemProperties );
 79  
         }
 80  
 
 81  
         // ----------------------------------------------------------------------
 82  
         // Configurations directory
 83  
         // ----------------------------------------------------------------------
 84  
 
 85  46
         PlexusConfiguration[] configurationsDirectories = user.getChildren( "configurations-directory" );
 86  
 
 87  46
         if ( configurationsDirectories.length != 0 )
 88  
         {
 89  56
             for (PlexusConfiguration configurationsDirectory : configurationsDirectories) {
 90  28
                 mergedConfiguration.addChild(configurationsDirectory);
 91  
             }
 92  
         }
 93  
 
 94  
         // ----------------------------------------------------------------------
 95  
         // Logging
 96  
         // ----------------------------------------------------------------------
 97  
 
 98  46
         PlexusConfiguration logging = user.getChild( "logging" );
 99  
 
 100  46
         if ( logging.getChildCount() != 0 )
 101  
         {
 102  1
             mergedConfiguration.addChild( logging );
 103  
         }
 104  
         else
 105  
         {
 106  45
             mergedConfiguration.addChild( system.getChild( "logging" ) );
 107  
         }
 108  
 
 109  
         // ----------------------------------------------------------------------
 110  
         // Container initialization phases
 111  
         // ----------------------------------------------------------------------
 112  
 
 113  46
         mergedConfiguration.addChild( system.getChild( "container-initialization") );
 114  
 
 115  46
         mergedConfiguration.addChild( system.getChild( "component-lookup-manager") );
 116  
 
 117  
         // ----------------------------------------------------------------------
 118  
         // Component repository
 119  
         // ----------------------------------------------------------------------
 120  
 
 121  46
         PlexusConfiguration componentRepository = user.getChild( "component-repository" );
 122  
 
 123  46
         if ( componentRepository.getChildCount() != 0 )
 124  
         {
 125  0
             mergedConfiguration.addChild( componentRepository );
 126  
         }
 127  
         else
 128  
         {
 129  46
             mergedConfiguration.addChild( system.getChild( "component-repository" ) );
 130  
         }
 131  
 
 132  
         // ----------------------------------------------------------------------
 133  
         // Resources
 134  
         // ----------------------------------------------------------------------
 135  
 
 136  46
         copyResources( system, mergedConfiguration );
 137  
 
 138  46
         copyResources( user, mergedConfiguration );
 139  
 
 140  
         // ----------------------------------------------------------------------
 141  
         // Component manager manager
 142  
         // ----------------------------------------------------------------------
 143  
 
 144  46
         PlexusConfiguration componentManagerManager =  user.getChild( "component-manager-manager" );
 145  
 
 146  46
         if ( componentManagerManager.getChildCount() != 0 )
 147  
         {
 148  0
             mergedConfiguration.addChild( componentManagerManager );
 149  
 
 150  0
             copyComponentManagers( system.getChild( "component-manager-manager" ), componentManagerManager );
 151  
         }
 152  
         else
 153  
         {
 154  46
             mergedConfiguration.addChild( system.getChild( "component-manager-manager" ) );
 155  
         }
 156  
 
 157  
         // ----------------------------------------------------------------------
 158  
         // Component discoverer manager
 159  
         // ----------------------------------------------------------------------
 160  
 
 161  46
         PlexusConfiguration componentDiscovererManager =  user.getChild( "component-discoverer-manager" );
 162  
 
 163  46
         if ( componentDiscovererManager.getChildCount() != 0 )
 164  
         {
 165  0
             mergedConfiguration.addChild( componentDiscovererManager );
 166  
 
 167  0
             copyComponentDiscoverers( system.getChild( "component-discoverer-manager" ), componentDiscovererManager );
 168  
         }
 169  
         else
 170  
         {
 171  46
             mergedConfiguration.addChild( system.getChild( "component-discoverer-manager" ) );
 172  
         }
 173  
 
 174  
         // ----------------------------------------------------------------------
 175  
         // Component factory manager
 176  
         // ----------------------------------------------------------------------
 177  
 
 178  46
         PlexusConfiguration componentFactoryManager =  user.getChild( "component-factory-manager" );
 179  
 
 180  46
         if ( componentFactoryManager.getChildCount() != 0 )
 181  
         {
 182  0
             mergedConfiguration.addChild( componentFactoryManager );
 183  
 
 184  0
             copyComponentFactories( system.getChild( "component-factory-manager" ), componentFactoryManager );
 185  
         }
 186  
         else
 187  
         {
 188  46
             mergedConfiguration.addChild( system.getChild( "component-factory-manager" ) );
 189  
         }
 190  
 
 191  
         // ----------------------------------------------------------------------
 192  
         // Lifecycle handler managers
 193  
         // ----------------------------------------------------------------------
 194  
 
 195  46
         PlexusConfiguration lifecycleHandlerManager = user.getChild( "lifecycle-handler-manager" );
 196  
 
 197  46
         if ( lifecycleHandlerManager.getChildCount() != 0 )
 198  
         {
 199  0
             mergedConfiguration.addChild( lifecycleHandlerManager );
 200  
 
 201  0
             copyLifecycles( system.getChild( "lifecycle-handler-manager" ), lifecycleHandlerManager );
 202  
         }
 203  
         else
 204  
         {
 205  46
             mergedConfiguration.addChild( system.getChild( "lifecycle-handler-manager" ) );
 206  
         }
 207  
 
 208  
         // ----------------------------------------------------------------------
 209  
         // Component factory manager
 210  
         // ----------------------------------------------------------------------
 211  
 
 212  46
         PlexusConfiguration componentComposerManager =  user.getChild( "component-composer-manager" );
 213  
 
 214  46
         if ( componentComposerManager.getChildCount() != 0 )
 215  
         {
 216  0
             mergedConfiguration.addChild( componentComposerManager );
 217  
 
 218  0
             copyComponentComposers( system.getChild( "component-composer-manager" ), componentComposerManager );
 219  
         }
 220  
         else
 221  
         {
 222  46
             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  46
         PlexusConfiguration components = system.getChild( "components" );
 235  
 
 236  46
         mergedConfiguration.addChild( components );
 237  
 
 238  46
         copyComponents( user.getChild( "components" ), components );
 239  
 
 240  46
         return mergedConfiguration;
 241  
     }
 242  
 
 243  
     private static void copyResources( PlexusConfiguration source, PlexusConfiguration destination )
 244  
     {
 245  92
         PlexusConfiguration handlers[] = source.getChild( "resources" ).getChildren();
 246  
 
 247  92
         PlexusConfiguration dest = destination.getChild( "resources" );
 248  
 
 249  92
         for (PlexusConfiguration handler : handlers) {
 250  0
             dest.addChild(handler);
 251  
         }
 252  92
     }
 253  
 
 254  
     private static void copyComponentManagers( PlexusConfiguration source, PlexusConfiguration destination )
 255  
     {
 256  
         try
 257  
         {
 258  0
             PlexusConfiguration id = destination.getChild( "default-component-manager-id" );
 259  
 
 260  0
             String sid = source.getChild( "default-component-manager-id" ).getValue();
 261  
 
 262  0
             if ( id.getValue() == null )
 263  
             {
 264  0
                 id.setValue( sid );   
 265  
             }
 266  
         }
 267  0
         catch ( PlexusConfigurationException e )
 268  
         {
 269  
             // do nothing
 270  0
         }
 271  
 
 272  
 
 273  0
         PlexusConfiguration handlers[] = source.getChild( "component-managers" ).getChildren( "component-manager" );
 274  
 
 275  0
         PlexusConfiguration dest = destination.getChild( "component-managers" );
 276  
 
 277  0
         for (PlexusConfiguration handler : handlers) {
 278  0
             dest.addChild(handler);
 279  
         }
 280  0
     }
 281  
 
 282  
     private static void copyComponentDiscoverers( PlexusConfiguration source, PlexusConfiguration destination )
 283  
     {
 284  0
         PlexusConfiguration handlers[] = source.getChild( "component-discoverers" ).getChildren( "component-discoverer" );
 285  
 
 286  0
         PlexusConfiguration dest = destination.getChild( "component-discoverers" );
 287  
 
 288  0
         for (PlexusConfiguration handler : handlers) {
 289  0
             dest.addChild(handler);
 290  
         }
 291  0
     }
 292  
 
 293  
     private static void copyComponentFactories( PlexusConfiguration source, PlexusConfiguration destination )
 294  
     {
 295  0
         PlexusConfiguration handlers[] = source.getChild( "component-factories" ).getChildren( "component-factory" );
 296  
 
 297  0
         PlexusConfiguration dest = destination.getChild( "component-factories" );
 298  
 
 299  0
         for (PlexusConfiguration handler : handlers) {
 300  0
             dest.addChild(handler);
 301  
         }
 302  0
     }
 303  
 
 304  
     private static void copyComponentComposers( PlexusConfiguration source, PlexusConfiguration destination )
 305  
     {
 306  
         try
 307  
         {
 308  0
             PlexusConfiguration id = destination.getChild( "default-component-composer-id" );
 309  
 
 310  0
             String sid = source.getChild( "default-component-composer-id" ).getValue();
 311  
 
 312  0
             if ( id.getValue() == null )
 313  
             {
 314  0
                 id.setValue( sid );
 315  
             }
 316  
         }
 317  0
         catch ( PlexusConfigurationException e )
 318  
         {
 319  
             // do nothing
 320  0
         }
 321  
 
 322  0
         PlexusConfiguration composers[] = source.getChild( "component-composers" ).getChildren( "component-composer" );
 323  
 
 324  0
         PlexusConfiguration dest = destination.getChild( "component-composers" );
 325  
 
 326  0
         for (PlexusConfiguration composer : composers) {
 327  0
             dest.addChild(composer);
 328  
         }
 329  0
     }
 330  
 
 331  
     private static void copyLifecycles( PlexusConfiguration source, PlexusConfiguration destination )
 332  
     {
 333  0
         PlexusConfiguration handlers[] = source.getChild( "lifecycle-handlers" ).getChildren( "lifecycle-handler" );
 334  
 
 335  0
         PlexusConfiguration dest = destination.getChild( "lifecycle-handlers" );
 336  
 
 337  0
         for (PlexusConfiguration handler : handlers) {
 338  0
             dest.addChild(handler);
 339  
         }
 340  0
     }
 341  
 
 342  
     private static void copyComponents( PlexusConfiguration source, PlexusConfiguration destination )
 343  
     {
 344  46
         PlexusConfiguration components[] = source.getChildren( "component" );
 345  
 
 346  1299
         for (PlexusConfiguration component : components) {
 347  1253
             destination.addChild(component);
 348  
         }
 349  46
     }
 350  
 }