1 package org.codehaus.plexus.component.configurator.converters.composite;
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 java.util.Collection;
28 import java.util.Dictionary;
29 import java.util.Map;
30
31 import org.codehaus.plexus.component.configurator.ComponentConfigurationException;
32 import org.codehaus.plexus.component.configurator.ConfigurationListener;
33 import org.codehaus.plexus.component.configurator.converters.AbstractConfigurationConverter;
34 import org.codehaus.plexus.component.configurator.converters.ComponentValueSetter;
35 import org.codehaus.plexus.component.configurator.converters.lookup.ConverterLookup;
36 import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
37 import org.codehaus.plexus.configuration.PlexusConfiguration;
38
39
40
41
42 public class ObjectWithFieldsConverter extends AbstractConfigurationConverter {
43 public boolean canConvert(Class type) {
44 boolean retValue = true;
45
46 if (Dictionary.class.isAssignableFrom(type)) {
47 retValue = false;
48 } else if (Map.class.isAssignableFrom(type)) {
49 retValue = false;
50 } else if (Collection.class.isAssignableFrom(type)) {
51 retValue = false;
52 }
53
54 return retValue;
55 }
56
57 public Object fromConfiguration(
58 ConverterLookup converterLookup,
59 PlexusConfiguration configuration,
60 Class type,
61 Class baseType,
62 ClassLoader classLoader,
63 ExpressionEvaluator expressionEvaluator,
64 ConfigurationListener listener)
65 throws ComponentConfigurationException {
66 Object retValue = fromExpression(configuration, expressionEvaluator, type);
67
68 if (retValue == null) {
69 try {
70
71 Class implementation = getClassForImplementationHint(type, configuration, classLoader);
72
73 if (type == implementation && type.isInterface() && configuration.getChildCount() <= 0) {
74 return null;
75 }
76
77 retValue = instantiateObject(implementation);
78
79 processConfiguration(
80 converterLookup, retValue, classLoader, configuration, expressionEvaluator, listener);
81 } catch (ComponentConfigurationException e) {
82 if (e.getFailedConfiguration() == null) {
83 e.setFailedConfiguration(configuration);
84 }
85
86 throw e;
87 }
88 }
89 return retValue;
90 }
91
92 public void processConfiguration(
93 ConverterLookup converterLookup, Object object, ClassLoader classLoader, PlexusConfiguration configuration)
94 throws ComponentConfigurationException {
95 processConfiguration(converterLookup, object, classLoader, configuration, null);
96 }
97
98 public void processConfiguration(
99 ConverterLookup converterLookup,
100 Object object,
101 ClassLoader classLoader,
102 PlexusConfiguration configuration,
103 ExpressionEvaluator expressionEvaluator)
104 throws ComponentConfigurationException {
105 processConfiguration(converterLookup, object, classLoader, configuration, expressionEvaluator, null);
106 }
107
108 public void processConfiguration(
109 ConverterLookup converterLookup,
110 Object object,
111 ClassLoader classLoader,
112 PlexusConfiguration configuration,
113 ExpressionEvaluator expressionEvaluator,
114 ConfigurationListener listener)
115 throws ComponentConfigurationException {
116 int items = configuration.getChildCount();
117
118 for (int i = 0; i < items; i++) {
119 PlexusConfiguration childConfiguration = configuration.getChild(i);
120
121 String elementName = childConfiguration.getName();
122
123 ComponentValueSetter valueSetter =
124 new ComponentValueSetter(fromXML(elementName), object, converterLookup, listener);
125
126 valueSetter.configure(childConfiguration, classLoader, expressionEvaluator);
127 }
128 }
129 }