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
20      extends AbstractConfigurationConverter
21  {
22      public static final String ROLE = ConfigurationConverter.class.getName();
23  
24      private ClassRealm classRealm;
25  
26      /**
27       * Constructs this ClassRealmConverter with the given ClassRealm.
28       * If there's a way to automatically configure this component
29       * using the current classrealm, this method can go away.
30       *
31       * @param classRealm
32       */
33      public ClassRealmConverter( ClassRealm classRealm )
34      {
35          setClassRealm( classRealm );
36      }
37  
38      @Deprecated
39      public ClassRealmConverter( final org.codehaus.classworlds.ClassRealm classRealm )
40      {
41          setClassRealm( classRealm );
42      }
43  
44      public void setClassRealm( final ClassRealm classRealm )
45      {
46          this.classRealm = classRealm;
47      }
48  
49      @Deprecated
50      public void setClassRealm( final org.codehaus.classworlds.ClassRealm classRealm )
51      {
52          if ( classRealm.getClassLoader() instanceof ClassRealm )
53          {
54              setClassRealm( (ClassRealm) classRealm.getClassLoader() );
55          }
56          else
57          {
58              setClassRealm( ClassRealmReverseAdapter.getInstance( classRealm ) );
59          }
60      }
61  
62      public boolean canConvert( Class type )
63      {
64          // backwards compatibility for old ClassWorld fields
65          return org.codehaus.classworlds.ClassRealm.class.isAssignableFrom( type )
66              || ClassRealm.class.isAssignableFrom( type );
67      }
68  
69      public Object fromConfiguration( ConverterLookup converterLookup, PlexusConfiguration configuration, Class type,
70                                       Class baseType, ClassLoader classLoader, ExpressionEvaluator expressionEvaluator,
71                                       ConfigurationListener listener )
72          throws ComponentConfigurationException
73      {
74          Object retValue = fromExpression( configuration, expressionEvaluator, type );
75  
76          if ( retValue == null )
77          {
78              retValue = classRealm;
79          }
80  
81          // backwards compatibility for old ClassWorld fields
82          if ( retValue instanceof ClassRealm && org.codehaus.classworlds.ClassRealm.class.isAssignableFrom( type ) )
83          {
84              retValue = ClassRealmAdapter.getInstance( (ClassRealm) retValue );
85          }
86  
87          return retValue;
88      }
89  
90  }