View Javadoc
1   package org.codehaus.plexus.personality.plexus.lifecycle.phase;
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 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  }