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