View Javadoc
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.configuration.source.ChainedConfigurationSource;
27  import org.codehaus.plexus.configuration.source.ConfigurationSource;
28  
29  /**
30   * @author Jason van Zyl
31   * @author cstamas
32   */
33  public class InitializeUserConfigurationSourcePhase
34      extends AbstractCoreComponentInitializationPhase
35  {
36      public void initializeCoreComponent( ContainerInitializationContext context )
37          throws ContainerInitializationException
38      {
39          ConfigurationSource existingConfigurationSource = context.getContainer().getConfigurationSource();
40  
41          try
42          {
43              // is the user overriding the ConfigurationSource (role-hint: default) or only extending it?
44              if ( context.getContainer().hasComponent( ConfigurationSource.class, PlexusConstants.PLEXUS_DEFAULT_HINT ) )
45              {
46                  // overriding
47  
48                  ConfigurationSource cs = context.getContainer().lookup( ConfigurationSource.class );
49  
50                  // swap the user provided configuration source with current one
51                  context.getContainer().setConfigurationSource( cs );
52              }
53              else
54              {
55                  // extending
56                  List<ConfigurationSource> userConfigurationSources = context.getContainer().lookupList( ConfigurationSource.class );
57  
58                  if ( userConfigurationSources.size() > 0 )
59                  {
60                      List<ConfigurationSource> configurationSources =
61                          new ArrayList<ConfigurationSource>( userConfigurationSources.size() + 1 );
62  
63                      // adding user provied ones to be able to interfere
64                      configurationSources.addAll( userConfigurationSources );
65  
66                      // at the end adding the container source, to make sure config will be returned
67                      // from plexus.xml if no user interference is given
68                      configurationSources.add( existingConfigurationSource );
69  
70                      ConfigurationSource configurationSource = new ChainedConfigurationSource( configurationSources );
71  
72                      context.getContainer().setConfigurationSource( configurationSource );
73  
74                  }
75  
76                  // register the default source, either the chained or the existing one as default
77                  ComponentDescriptor<ConfigurationSource> cd = new ComponentDescriptor<ConfigurationSource>();
78  
79                  cd.setRole( ConfigurationSource.ROLE );
80  
81                  cd.setRoleHint( PlexusConstants.PLEXUS_DEFAULT_HINT );
82  
83                  cd.setImplementationClass( context.getContainer().getConfigurationSource().getClass() );
84  
85                  try
86                  {
87                      context.getContainer().addComponentDescriptor( cd );
88                  }
89                  catch ( CycleDetectedInComponentGraphException cre )
90                  {
91                      throw new ContainerInitializationException( "Error setting up configuration source.", cre );
92                  }
93              }
94  
95          }
96          catch ( ComponentLookupException e )
97          {
98              throw new ContainerInitializationException( "Error setting up user configuration source.", e );
99          }
100     }
101 }