1 package org.codehaus.plexus.container.initialization;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
31
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
40 if (context.getContainer().hasComponent(ConfigurationSource.class, PlexusConstants.PLEXUS_DEFAULT_HINT)) {
41
42
43 ConfigurationSource cs = context.getContainer().lookup(ConfigurationSource.class);
44
45
46 context.getContainer().setConfigurationSource(cs);
47 } else {
48
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
57 configurationSources.addAll(userConfigurationSources);
58
59
60
61 configurationSources.add(existingConfigurationSource);
62
63 ConfigurationSource configurationSource = new ChainedConfigurationSource(configurationSources);
64
65 context.getContainer().setConfigurationSource(configurationSource);
66 }
67
68
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 }