1 package org.codehaus.plexus.component.configurator.converters;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 import org.codehaus.plexus.component.configurator.ComponentConfigurationException;
28 import org.codehaus.plexus.component.configurator.converters.lookup.ConverterLookup;
29 import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
30 import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
31 import org.codehaus.plexus.configuration.PlexusConfiguration;
32 import org.codehaus.plexus.util.StringUtils;
33
34
35
36
37 public abstract class AbstractConfigurationConverter implements ConfigurationConverter {
38 private static final String IMPLEMENTATION = "implementation";
39
40
41
42
43
44
45
46
47
48
49
50
51 protected Class getClassForImplementationHint(
52 Class type, PlexusConfiguration configuration, ClassLoader classLoader)
53 throws ComponentConfigurationException {
54 Class retValue = type;
55
56 String implementation = configuration.getAttribute(IMPLEMENTATION, null);
57
58 if (implementation != null) {
59 try {
60 retValue = classLoader.loadClass(implementation);
61
62 } catch (ClassNotFoundException e) {
63 String msg = "ClassNotFoundException: Class name which was explicitly given in configuration using"
64 + " 'implementation' attribute: '" + implementation + "' cannot be loaded";
65
66 throw new ComponentConfigurationException(msg, e);
67 } catch (UnsupportedClassVersionError e) {
68 String msg = "UnsupportedClassVersionError: Class name which was explicitly given in configuration"
69 + " using 'implementation' attribute: '" + implementation + "' cannot be loaded";
70
71 throw new ComponentConfigurationException(msg, e);
72 } catch (LinkageError e) {
73 String msg = "LinkageError: Class name which was explicitly given in configuration using"
74 + " 'implementation' attribute: '" + implementation + "' cannot be loaded";
75
76 throw new ComponentConfigurationException(msg, e);
77 }
78 }
79
80 return retValue;
81 }
82
83 protected Class loadClass(String classname, ClassLoader classLoader) throws ComponentConfigurationException {
84 Class retValue;
85
86 try {
87 retValue = classLoader.loadClass(classname);
88 } catch (ClassNotFoundException e) {
89 throw new ComponentConfigurationException("Error loading class '" + classname + "'", e);
90 }
91
92 return retValue;
93 }
94
95 protected Object instantiateObject(String classname, ClassLoader classLoader)
96 throws ComponentConfigurationException {
97 Class clazz = loadClass(classname, classLoader);
98
99 return instantiateObject(clazz);
100 }
101
102 protected Object instantiateObject(Class clazz) throws ComponentConfigurationException {
103 Object retValue;
104
105 try {
106 retValue = clazz.newInstance();
107
108 return retValue;
109 } catch (IllegalAccessException e) {
110 throw new ComponentConfigurationException("Class '" + clazz.getName() + "' cannot be instantiated", e);
111 } catch (InstantiationException e) {
112 throw new ComponentConfigurationException("Class '" + clazz.getName() + "' cannot be instantiated", e);
113 }
114 }
115
116
117 protected String fromXML(String elementName) {
118 return StringUtils.lowercaseFirstLetter(StringUtils.removeAndHump(elementName, "-"));
119 }
120
121
122 protected String toXML(String fieldName) {
123 return StringUtils.addAndDeHump(fieldName);
124 }
125
126 protected Object fromExpression(
127 PlexusConfiguration configuration, ExpressionEvaluator expressionEvaluator, Class type)
128 throws ComponentConfigurationException {
129 Object v = fromExpression(configuration, expressionEvaluator);
130
131 if (v != null) {
132 if (!type.isAssignableFrom(v.getClass())) {
133 String msg = "Cannot assign configuration entry '" + configuration.getName() + "' to '" + type
134 + "' from '" + configuration.getValue(null) + "', which is of type " + v.getClass();
135 throw new ComponentConfigurationException(configuration, msg);
136 }
137 }
138 return v;
139 }
140
141 protected Object fromExpression(PlexusConfiguration configuration, ExpressionEvaluator expressionEvaluator)
142 throws ComponentConfigurationException {
143 Object v = null;
144 String value = configuration.getValue(null);
145 if (value != null && value.length() > 0) {
146
147
148
149 try {
150 v = expressionEvaluator.evaluate(value);
151 } catch (ExpressionEvaluationException e) {
152 String msg = "Error evaluating the expression '" + value + "' for configuration value '"
153 + configuration.getName() + "'";
154 throw new ComponentConfigurationException(configuration, msg, e);
155 }
156 }
157 if (v == null) {
158 value = configuration.getAttribute("default-value", null);
159 if (value != null && value.length() > 0) {
160 try {
161 v = expressionEvaluator.evaluate(value);
162 } catch (ExpressionEvaluationException e) {
163 String msg = "Error evaluating the expression '" + value + "' for configuration value '"
164 + configuration.getName() + "'";
165 throw new ComponentConfigurationException(configuration, msg, e);
166 }
167 }
168 }
169 return v;
170 }
171
172 public Object fromConfiguration(
173 ConverterLookup converterLookup,
174 PlexusConfiguration configuration,
175 Class type,
176 Class baseType,
177 ClassLoader classLoader,
178 ExpressionEvaluator expressionEvaluator)
179 throws ComponentConfigurationException {
180 return fromConfiguration(
181 converterLookup, configuration, type, baseType, classLoader, expressionEvaluator, null);
182 }
183 }