View Javadoc
1   package org.codehaus.plexus.component.configurator.converters.special;
2   
3   import org.codehaus.classworlds.ClassRealmAdapter;
4   import org.codehaus.classworlds.ClassRealmReverseAdapter;
5   import org.codehaus.plexus.classworlds.realm.ClassRealm;
6   import org.codehaus.plexus.component.configurator.ComponentConfigurationException;
7   import org.codehaus.plexus.component.configurator.ConfigurationListener;
8   import org.codehaus.plexus.component.configurator.converters.AbstractConfigurationConverter;
9   import org.codehaus.plexus.component.configurator.converters.ConfigurationConverter;
10  import org.codehaus.plexus.component.configurator.converters.lookup.ConverterLookup;
11  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
12  import org.codehaus.plexus.configuration.PlexusConfiguration;
13  
14  /**
15   * ConfigurationConverter to set up ClassRealm component fields.
16   *
17   * @author <a href="mailto:kenney@neonics.com">Kenney Westerhof</a>
18   */
19  public class ClassRealmConverter extends AbstractConfigurationConverter {
20      public static final String ROLE = ConfigurationConverter.class.getName();
21  
22      private ClassRealm classRealm;
23  
24      /**
25       * Constructs this ClassRealmConverter with the given ClassRealm.
26       * If there's a way to automatically configure this component
27       * using the current classrealm, this method can go away.
28       *
29       * @param classRealm {@link ClassRealm}.
30       */
31      public ClassRealmConverter(ClassRealm classRealm) {
32          setClassRealm(classRealm);
33      }
34  
35      @Deprecated
36      public ClassRealmConverter(final org.codehaus.classworlds.ClassRealm classRealm) {
37          setClassRealm(classRealm);
38      }
39  
40      public void setClassRealm(final ClassRealm classRealm) {
41          this.classRealm = classRealm;
42      }
43  
44      @Deprecated
45      public void setClassRealm(final org.codehaus.classworlds.ClassRealm classRealm) {
46          if (classRealm.getClassLoader() instanceof ClassRealm) {
47              setClassRealm((ClassRealm) classRealm.getClassLoader());
48          } else {
49              setClassRealm(ClassRealmReverseAdapter.getInstance(classRealm));
50          }
51      }
52  
53      public boolean canConvert(Class type) {
54          // backwards compatibility for old ClassWorld fields
55          return org.codehaus.classworlds.ClassRealm.class.isAssignableFrom(type)
56                  || ClassRealm.class.isAssignableFrom(type);
57      }
58  
59      public Object fromConfiguration(
60              ConverterLookup converterLookup,
61              PlexusConfiguration configuration,
62              Class type,
63              Class baseType,
64              ClassLoader classLoader,
65              ExpressionEvaluator expressionEvaluator,
66              ConfigurationListener listener)
67              throws ComponentConfigurationException {
68          Object retValue = fromExpression(configuration, expressionEvaluator, type);
69  
70          if (retValue == null) {
71              retValue = classRealm;
72          }
73  
74          // backwards compatibility for old ClassWorld fields
75          if (retValue instanceof ClassRealm && org.codehaus.classworlds.ClassRealm.class.isAssignableFrom(type)) {
76              retValue = ClassRealmAdapter.getInstance((ClassRealm) retValue);
77          }
78  
79          return retValue;
80      }
81  }