View Javadoc
1   package org.codehaus.plexus.component.configurator.converters.basic;
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 org.codehaus.plexus.component.configurator.ComponentConfigurationException;
28  import org.codehaus.plexus.component.configurator.ConfigurationListener;
29  import org.codehaus.plexus.component.configurator.converters.AbstractConfigurationConverter;
30  import org.codehaus.plexus.component.configurator.converters.lookup.ConverterLookup;
31  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
32  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
33  import org.codehaus.plexus.component.configurator.expression.TypeAwareExpressionEvaluator;
34  import org.codehaus.plexus.configuration.PlexusConfiguration;
35  
36  /**
37   */
38  public abstract class AbstractBasicConverter extends AbstractConfigurationConverter {
39      protected abstract Object fromString(String str) throws ComponentConfigurationException;
40  
41      protected Object fromExpression(
42              PlexusConfiguration configuration, ExpressionEvaluator expressionEvaluator, Class type)
43              throws ComponentConfigurationException {
44          Object v = null;
45  
46          String value = configuration.getValue(null);
47  
48          if (value != null && value.length() > 0) {
49              // Object is provided by an expression
50              // This seems a bit ugly... canConvert really should return false in this instance, but it doesn't have the
51              //   configuration to know better
52              try {
53                  if (expressionEvaluator instanceof TypeAwareExpressionEvaluator) {
54                      v = ((TypeAwareExpressionEvaluator) expressionEvaluator).evaluate(value, type);
55                  } else {
56                      v = expressionEvaluator.evaluate(value);
57                  }
58              } catch (ExpressionEvaluationException e) {
59                  String msg = "Error evaluating the expression '" + value + "' for configuration value '"
60                          + configuration.getName() + "'";
61                  throw new ComponentConfigurationException(configuration, msg, e);
62              }
63          }
64  
65          if (v == null) {
66              value = configuration.getAttribute("default-value", null);
67  
68              if (value != null && value.length() > 0) {
69                  try {
70                      if (expressionEvaluator instanceof TypeAwareExpressionEvaluator) {
71                          v = ((TypeAwareExpressionEvaluator) expressionEvaluator).evaluate(value, type);
72                      } else {
73                          v = expressionEvaluator.evaluate(value);
74                      }
75                  } catch (ExpressionEvaluationException e) {
76                      String msg = "Error evaluating the expression '" + value + "' for configuration value '"
77                              + configuration.getName() + "'";
78                      throw new ComponentConfigurationException(configuration, msg, e);
79                  }
80              }
81          }
82  
83          /*
84           * NOTE: We don't check the type here which would be ugly to do correctly (e.g. value=Short -> type=int), the
85           * reflective setter/field injection will fail by itself when the type didn't match.
86           */
87  
88          return v;
89      }
90  
91      public Object fromConfiguration(
92              ConverterLookup converterLookup,
93              PlexusConfiguration configuration,
94              Class type,
95              Class baseType,
96              ClassLoader classLoader,
97              ExpressionEvaluator expressionEvaluator,
98              ConfigurationListener listener)
99              throws ComponentConfigurationException {
100         if (configuration.getChildCount() > 0) {
101             throw new ComponentConfigurationException("When configuring a basic element the configuration cannot "
102                     + "contain any child elements. " + "Configuration element '" + configuration.getName() + "'.");
103         }
104 
105         Object retValue = fromExpression(configuration, expressionEvaluator, type);
106 
107         if (retValue instanceof String) {
108             try {
109                 retValue = fromString((String) retValue);
110             } catch (ComponentConfigurationException e) {
111                 if (e.getFailedConfiguration() == null) {
112                     e.setFailedConfiguration(configuration);
113                 }
114 
115                 throw e;
116             }
117         }
118 
119         return retValue;
120     }
121 }