Coverage Report - org.codehaus.plexus.container.initialization.InitializeUserConfigurationSourcePhase
 
Classes in this File Line Coverage Branch Coverage Complexity
InitializeUserConfigurationSourcePhase
84 %
21/25
100 %
4/4
7
 
 1  
 package org.codehaus.plexus.container.initialization;
 2  
 
 3  
 /*
 4  
  * Copyright 2001-2006 Codehaus Foundation.
 5  
  *
 6  
  * Licensed under the Apache License, Version 2.0 (the "License");
 7  
  * you may not use this file except in compliance with the License.
 8  
  * You may obtain a copy of the License at
 9  
  *
 10  
  *      http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  
  * See the License for the specific language governing permissions and
 16  
  * limitations under the License.
 17  
  */
 18  
 
 19  
 import java.util.ArrayList;
 20  
 import java.util.List;
 21  
 
 22  
 import org.codehaus.plexus.PlexusConstants;
 23  
 import org.codehaus.plexus.component.composition.CycleDetectedInComponentGraphException;
 24  
 import org.codehaus.plexus.component.repository.ComponentDescriptor;
 25  
 import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
 26  
 import org.codehaus.plexus.component.repository.exception.ComponentRepositoryException;
 27  
 import org.codehaus.plexus.configuration.source.ChainedConfigurationSource;
 28  
 import org.codehaus.plexus.configuration.source.ConfigurationSource;
 29  
 
 30  
 /**
 31  
  * @author Jason van Zyl
 32  
  * @author cstamas
 33  
  */
 34  61
 public class InitializeUserConfigurationSourcePhase
 35  
     extends AbstractCoreComponentInitializationPhase
 36  
 {
 37  
     public void initializeCoreComponent( ContainerInitializationContext context )
 38  
         throws ContainerInitializationException
 39  
     {
 40  61
         ConfigurationSource existingConfigurationSource = context.getContainer().getConfigurationSource();
 41  
 
 42  
         try
 43  
         {
 44  
             // is the user overriding the ConfigurationSource (role-hint: default) or only extending it?
 45  61
             if ( context.getContainer().hasComponent( ConfigurationSource.class, PlexusConstants.PLEXUS_DEFAULT_HINT ) )
 46  
             {
 47  
                 // overriding
 48  
 
 49  1
                 ConfigurationSource cs = context.getContainer().lookup( ConfigurationSource.class );
 50  
 
 51  
                 // swap the user provided configuration source with current one
 52  1
                 context.getContainer().setConfigurationSource( cs );
 53  1
             }
 54  
             else
 55  
             {
 56  
                 // extending
 57  60
                 List<ConfigurationSource> userConfigurationSources = context.getContainer().lookupList( ConfigurationSource.class );
 58  
 
 59  60
                 if ( userConfigurationSources.size() > 0 )
 60  
                 {
 61  1
                     List<ConfigurationSource> configurationSources =
 62  
                         new ArrayList<ConfigurationSource>( userConfigurationSources.size() + 1 );
 63  
 
 64  
                     // adding user provied ones to be able to interfere
 65  1
                     configurationSources.addAll( userConfigurationSources );
 66  
 
 67  
                     // at the end adding the container source, to make sure config will be returned
 68  
                     // from plexus.xml if no user interference is given
 69  1
                     configurationSources.add( existingConfigurationSource );
 70  
 
 71  1
                     ConfigurationSource configurationSource = new ChainedConfigurationSource( configurationSources );
 72  
 
 73  1
                     context.getContainer().setConfigurationSource( configurationSource );
 74  
 
 75  
                 }
 76  
 
 77  
                 // register the default source, either the chained or the existing one as default
 78  60
                 ComponentDescriptor<ConfigurationSource> cd = new ComponentDescriptor<ConfigurationSource>();
 79  
 
 80  60
                 cd.setRole( ConfigurationSource.ROLE );
 81  
 
 82  60
                 cd.setRoleHint( PlexusConstants.PLEXUS_DEFAULT_HINT );
 83  
 
 84  60
                 cd.setImplementationClass( context.getContainer().getConfigurationSource().getClass() );
 85  
 
 86  
                 try
 87  
                 {
 88  60
                     context.getContainer().addComponentDescriptor( cd );
 89  
                 }
 90  0
                 catch ( CycleDetectedInComponentGraphException cre )
 91  
                 {
 92  0
                     throw new ContainerInitializationException( "Error setting up configuration source.", cre );
 93  60
                 }
 94  
             }
 95  
 
 96  
         }
 97  0
         catch ( ComponentLookupException e )
 98  
         {
 99  0
             throw new ContainerInitializationException( "Error setting up user configuration source.", e );
 100  61
         }
 101  61
     }
 102  
 }