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
16
17
18
19 public class ClassRealmConverter extends AbstractConfigurationConverter {
20 public static final String ROLE = ConfigurationConverter.class.getName();
21
22 private ClassRealm classRealm;
23
24
25
26
27
28
29
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
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
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 }