1 package org.codehaus.plexus.personality.plexus.lifecycle.phase;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import org.codehaus.plexus.classworlds.realm.ClassRealm;
20 import org.codehaus.plexus.component.configurator.ComponentConfigurationException;
21 import org.codehaus.plexus.component.configurator.ComponentConfigurator;
22 import org.codehaus.plexus.component.manager.ComponentManager;
23 import org.codehaus.plexus.component.repository.ComponentDescriptor;
24 import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
25 import org.codehaus.plexus.configuration.PlexusConfiguration;
26 import org.codehaus.plexus.lifecycle.phase.AbstractPhase;
27 import org.codehaus.plexus.util.StringUtils;
28
29 public class AutoConfigurePhase extends AbstractPhase {
30 public static final String DEFAULT_CONFIGURATOR_ID = "default";
31
32 public void execute(Object object, ComponentManager manager, ClassRealm lookupRealm)
33 throws PhaseExecutionException {
34 try {
35 ComponentDescriptor<?> descriptor = manager.getComponentDescriptor();
36
37 String configuratorId = descriptor.getComponentConfigurator();
38
39 if (StringUtils.isEmpty(configuratorId)) {
40 configuratorId = DEFAULT_CONFIGURATOR_ID;
41 }
42
43 ComponentConfigurator componentConfigurator =
44 manager.getContainer().lookup(ComponentConfigurator.class, configuratorId);
45
46 PlexusConfiguration configuration =
47 manager.getContainer().getConfigurationSource().getConfiguration(descriptor);
48
49 if (configuration != null) {
50 ClassRealm realm = manager.getRealm();
51
52 componentConfigurator.configureComponent(object, configuration, realm);
53 }
54 } catch (ComponentLookupException e) {
55 throw new PhaseExecutionException(
56 "Unable to auto-configure component as its configurator could not be found", e);
57 } catch (ComponentConfigurationException e) {
58 throw new PhaseExecutionException("Unable to auto-configure component", e);
59 }
60 }
61 }