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 java.lang.reflect.InvocationTargetException;
28  import java.lang.reflect.Method;
29  
30  import org.codehaus.classworlds.ClassRealmAdapter;
31  import org.codehaus.plexus.classworlds.realm.ClassRealm;
32  import org.codehaus.plexus.component.configurator.converters.lookup.ConverterLookup;
33  import org.codehaus.plexus.component.configurator.converters.lookup.DefaultConverterLookup;
34  import org.codehaus.plexus.component.configurator.expression.DefaultExpressionEvaluator;
35  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
36  import org.codehaus.plexus.configuration.PlexusConfiguration;
37  
38  /**
39   * @author <a href="mailto:brett@codehaus.org">Brett Porter</a>
40   */
41  public abstract class AbstractComponentConfigurator implements ComponentConfigurator {
42      /**
43       * This is being instantiated here because there are old component factories (beanshell) that directly access
44       * the converterLookup but do not yet state the ConverterLookup as a requirement in the component metadata.
45       * Once these are wired up as standard components properly then we won't have to instantiate the
46       * converter lookup here and we can let the container do it.
47       *
48       */
49      protected ConverterLookup converterLookup = new DefaultConverterLookup();
50  
51      public void configureComponent(Object component, PlexusConfiguration configuration, ClassRealm containerRealm)
52              throws ComponentConfigurationException {
53          configureComponent(component, configuration, new DefaultExpressionEvaluator(), containerRealm);
54      }
55  
56      public void configureComponent(
57              Object component,
58              PlexusConfiguration configuration,
59              ExpressionEvaluator expressionEvaluator,
60              ClassRealm containerRealm)
61              throws ComponentConfigurationException {
62          configureComponent(component, configuration, expressionEvaluator, containerRealm, null);
63      }
64  
65      public void configureComponent(
66              final Object component,
67              final PlexusConfiguration configuration,
68              final ExpressionEvaluator expressionEvaluator,
69              final ClassRealm containerRealm,
70              final ConfigurationListener listener)
71              throws ComponentConfigurationException {
72          // ----------------------------------------------------------------------------
73          // For compatibility with old ComponentFactories that use old ClassWorlds
74          // ----------------------------------------------------------------------------
75  
76          final org.codehaus.classworlds.ClassRealm cr = ClassRealmAdapter.getInstance(containerRealm);
77  
78          Method method;
79  
80          try {
81              try {
82                  method = getClass()
83                          .getMethod(
84                                  "configureComponent",
85                                  Object.class,
86                                  PlexusConfiguration.class,
87                                  ExpressionEvaluator.class,
88                                  org.codehaus.classworlds.ClassRealm.class,
89                                  ConfigurationListener.class);
90                  method.invoke(this, component, configuration, expressionEvaluator, cr, listener);
91              } catch (final NoSuchMethodException e) {
92                  method = getClass()
93                          .getMethod(
94                                  "configureComponent",
95                                  Object.class,
96                                  PlexusConfiguration.class,
97                                  ExpressionEvaluator.class,
98                                  org.codehaus.classworlds.ClassRealm.class);
99                  method.invoke(this, component, configuration, expressionEvaluator, cr);
100             }
101         } catch (final InvocationTargetException e) {
102             if (e.getCause() instanceof ComponentConfigurationException) {
103                 throw (ComponentConfigurationException) e.getCause();
104             } else if (e.getCause() instanceof RuntimeException) {
105                 throw (RuntimeException) e.getCause();
106             } else if (e.getCause() instanceof Error) {
107                 throw (Error) e.getCause();
108             }
109             throw new ComponentConfigurationException(
110                     "Incompatible configurator " + getClass().getName(), e);
111         } catch (final Exception e) {
112             throw new ComponentConfigurationException(
113                     "Incompatible configurator " + getClass().getName(), e);
114         }
115     }
116 }