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