View Javadoc
1   package org.codehaus.plexus.component.configurator.converters.composite;
2   
3   /*
4    * The MIT License
5    *
6    * Copyright (c) 2004, The Codehaus
7    *
8    * Permission is hereby granted, free of charge, to any person obtaining a copy of
9    * this software and associated documentation files (the "Software"), to deal in
10   * the Software without restriction, including without limitation the rights to
11   * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12   * of the Software, and to permit persons to whom the Software is furnished to do
13   * so, subject to the following conditions:
14   *
15   * The above copyright notice and this permission notice shall be included in all
16   * copies or substantial portions of the Software.
17   *
18   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24   * SOFTWARE.
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   * @author <a href="mailto:michal@codehaus.org">Michal Maczka</a>
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                  // it is a "composite" - we compose it from its children. It does not have a value of its own
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 }