View Javadoc
1   package org.codehaus.plexus.component.configurator;
2   
3   /*
4    * The MIT License
5    *
6    * Copyright (c) 2004, The Codehaus
7    *
8    * Permission is hereby granted, free of charge, to any person obtaining a copy of
9    * this software and associated documentation files (the "Software"), to deal in
10   * the Software without restriction, including without limitation the rights to
11   * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12   * of the Software, and to permit persons to whom the Software is furnished to do
13   * so, subject to the following conditions:
14   *
15   * The above copyright notice and this permission notice shall be included in all
16   * copies or substantial portions of the Software.
17   *
18   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24   * SOFTWARE.
25   */
26  
27  import org.codehaus.classworlds.ClassRealmAdapter;
28  import org.codehaus.plexus.classworlds.realm.ClassRealm;
29  import org.codehaus.plexus.component.configurator.converters.lookup.ConverterLookup;
30  import org.codehaus.plexus.component.configurator.converters.lookup.DefaultConverterLookup;
31  import org.codehaus.plexus.component.configurator.expression.DefaultExpressionEvaluator;
32  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
33  import org.codehaus.plexus.configuration.PlexusConfiguration;
34  
35  import java.lang.reflect.InvocationTargetException;
36  import java.lang.reflect.Method;
37  
38  /**
39   * @author <a href="mailto:brett@codehaus.org">Brett Porter</a>
40   * @version $Id$
41   */
42  public abstract class AbstractComponentConfigurator
43      implements ComponentConfigurator
44  {
45      /**
46       * This is being instantiated here because there are old component factories (beanshell) that directly access
47       * the converterLookup but do not yet state the ConverterLookup as a requirement in the component metadata.
48       * Once these are wired up as standard components properly then we won't have to instantiate the
49       * converter lookup here and we can let the container do it.
50       *
51       * @plexus.requirement
52       */
53      protected ConverterLookup converterLookup = new DefaultConverterLookup();
54  
55      public void configureComponent( Object component,
56                                      PlexusConfiguration configuration,
57                                      ClassRealm containerRealm )
58          throws ComponentConfigurationException
59      {
60          configureComponent( component, configuration, new DefaultExpressionEvaluator(), containerRealm );
61      }
62  
63      public void configureComponent( Object component,
64                                      PlexusConfiguration configuration,
65                                      ExpressionEvaluator expressionEvaluator,
66                                      ClassRealm containerRealm )
67          throws ComponentConfigurationException
68      {
69          configureComponent( component, configuration, expressionEvaluator, containerRealm, null );
70      }
71  
72      public void configureComponent( final Object component, final PlexusConfiguration configuration,
73                                      final ExpressionEvaluator expressionEvaluator, final ClassRealm containerRealm,
74                                      final ConfigurationListener listener )
75          throws ComponentConfigurationException
76      {
77          // ----------------------------------------------------------------------------
78          // For compatibility with old ComponentFactories that use old ClassWorlds
79          // ----------------------------------------------------------------------------
80  
81          final org.codehaus.classworlds.ClassRealm cr = ClassRealmAdapter.getInstance( containerRealm );
82  
83          Method method;
84  
85          try
86          {
87              try
88              {
89                  method =
90                      getClass().getMethod( "configureComponent", Object.class, PlexusConfiguration.class,
91                                            ExpressionEvaluator.class, org.codehaus.classworlds.ClassRealm.class,
92                                            ConfigurationListener.class );
93                  method.invoke( this, component, configuration, expressionEvaluator, cr, listener );
94              }
95              catch ( final NoSuchMethodException e )
96              {
97                  method =
98                      getClass().getMethod( "configureComponent", Object.class, PlexusConfiguration.class,
99                                            ExpressionEvaluator.class, org.codehaus.classworlds.ClassRealm.class );
100                 method.invoke( this, component, configuration, expressionEvaluator, cr );
101             }
102         }
103         catch ( final InvocationTargetException e )
104         {
105             if ( e.getCause() instanceof ComponentConfigurationException )
106             {
107                 throw (ComponentConfigurationException) e.getCause();
108             }
109             else if ( e.getCause() instanceof RuntimeException )
110             {
111                 throw (RuntimeException) e.getCause();
112             }
113             else if ( e.getCause() instanceof Error )
114             {
115                 throw (Error) e.getCause();
116             }
117             throw new ComponentConfigurationException( "Incompatible configurator " + getClass().getName(), e );
118         }
119         catch ( final Exception e )
120         {
121             throw new ComponentConfigurationException( "Incompatible configurator " + getClass().getName(), e );
122         }
123     }
124 
125 }