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.lang.reflect.Array;
28  import java.util.ArrayList;
29  import java.util.List;
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.ConfigurationConverter;
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  import org.codehaus.plexus.util.StringUtils;
39  
40  /**
41   * @author <a href="mailto:kenney@codehaus.org">Kenney Westerhof</a>
42   */
43  public class ArrayConverter extends AbstractConfigurationConverter {
44      public boolean canConvert(Class type) {
45          return type.isArray();
46      }
47  
48      public Object fromConfiguration(
49              ConverterLookup converterLookup,
50              PlexusConfiguration configuration,
51              Class type,
52              Class baseType,
53              ClassLoader classLoader,
54              ExpressionEvaluator expressionEvaluator,
55              ConfigurationListener listener)
56              throws ComponentConfigurationException {
57          Object retValue = fromExpression(configuration, expressionEvaluator, type);
58          if (retValue != null) {
59              return retValue;
60          }
61  
62          List values = new ArrayList();
63  
64          for (int i = 0; i < configuration.getChildCount(); i++) {
65              PlexusConfiguration childConfiguration = configuration.getChild(i);
66  
67              String configEntry = childConfiguration.getName();
68  
69              String name = fromXML(configEntry);
70  
71              Class childType = getClassForImplementationHint(null, childConfiguration, classLoader);
72  
73              // check if the name is a fully qualified classname
74  
75              if (childType == null && name.indexOf('.') > 0) {
76                  try {
77                      childType = classLoader.loadClass(name);
78                  } catch (ClassNotFoundException e) {
79                      // doesn't exist - continue processing
80                  }
81              }
82  
83              if (childType == null) {
84                  // try to find the class in the package of the baseType
85                  // (which is the component being configured)
86  
87                  String baseTypeName = baseType.getName();
88  
89                  int lastDot = baseTypeName.lastIndexOf('.');
90  
91                  String className;
92  
93                  if (lastDot == -1) {
94                      className = name;
95                  } else {
96                      String basePackage = baseTypeName.substring(0, lastDot);
97                      className = basePackage + "." + StringUtils.capitalizeFirstLetter(name);
98                  }
99  
100                 try {
101                     childType = classLoader.loadClass(className);
102                 } catch (ClassNotFoundException e) {
103                     // doesn't exist, continue processing
104                 }
105             }
106 
107             // finally just try the component type of the array
108 
109             if (childType == null) {
110                 childType = type.getComponentType();
111             }
112 
113             ConfigurationConverter converter = converterLookup.lookupConverterForType(childType);
114 
115             Object object = converter.fromConfiguration(
116                     converterLookup,
117                     childConfiguration,
118                     childType,
119                     baseType,
120                     classLoader,
121                     expressionEvaluator,
122                     listener);
123 
124             values.add(object);
125         }
126 
127         try {
128             return values.toArray((Object[]) Array.newInstance(type.getComponentType(), 0));
129         } catch (ArrayStoreException e) {
130             throw new ComponentConfigurationException("Cannot assign configuration values to array of type "
131                     + type.getComponentType().getName() + ": " + values);
132         }
133     }
134 }