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