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
30      extends AbstractPhase
31  {
32      public static final String DEFAULT_CONFIGURATOR_ID = "default";
33  
34      public void execute( Object object,
35                           ComponentManager manager,
36                           ClassRealm lookupRealm )
37          throws PhaseExecutionException
38      {
39          try
40          {
41              ComponentDescriptor<?> descriptor = manager.getComponentDescriptor();
42  
43              String configuratorId = descriptor.getComponentConfigurator();
44  
45              if ( StringUtils.isEmpty( configuratorId ) )
46              {
47                  configuratorId = DEFAULT_CONFIGURATOR_ID;
48              }
49  
50              ComponentConfigurator componentConfigurator = manager.getContainer().lookup( ComponentConfigurator.class, configuratorId );
51  
52              PlexusConfiguration configuration = manager.getContainer().getConfigurationSource().getConfiguration( descriptor );
53  
54              if ( configuration != null )
55              {
56                  ClassRealm realm = manager.getRealm();
57  
58                  componentConfigurator.configureComponent( object, configuration, realm );
59              }
60          }
61          catch ( ComponentLookupException e )
62          {
63              throw new PhaseExecutionException( "Unable to auto-configure component as its configurator could not be found", e );
64          }
65          catch ( ComponentConfigurationException e )
66          {
67              throw new PhaseExecutionException( "Unable to auto-configure component", e );
68          }
69      }
70  }