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 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.ComponentValueSetter;
31  import org.codehaus.plexus.component.configurator.converters.lookup.ConverterLookup;
32  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
33  import org.codehaus.plexus.configuration.PlexusConfiguration;
34  
35  import java.util.Collection;
36  import java.util.Dictionary;
37  import java.util.Map;
38  
39  /**
40   * @author <a href="mailto:michal@codehaus.org">Michal Maczka</a>
41   * @version $Id$
42   */
43  public class ObjectWithFieldsConverter
44      extends AbstractConfigurationConverter
45  {
46      public boolean canConvert( Class type )
47      {
48          boolean retValue = true;
49  
50          if ( Dictionary.class.isAssignableFrom( type ) )
51          {
52              retValue = false;
53          }
54  
55          else if ( Map.class.isAssignableFrom( type ) )
56          {
57              retValue = false;
58          }
59          else if ( Collection.class.isAssignableFrom( type ) )
60          {
61              retValue = false;
62          }
63  
64          return retValue;
65      }
66  
67      public Object fromConfiguration( ConverterLookup converterLookup,
68                                       PlexusConfiguration configuration,
69                                       Class type,
70                                       Class baseType,
71                                       ClassLoader classLoader,
72                                       ExpressionEvaluator expressionEvaluator,
73                                       ConfigurationListener listener )
74          throws ComponentConfigurationException
75      {
76          Object retValue = fromExpression( configuration, expressionEvaluator, type );
77  
78          if ( retValue == null )
79          {
80              try
81              {
82                  // it is a "composite" - we compose it from its children. It does not have a value of its own
83                  Class implementation = getClassForImplementationHint( type, configuration, classLoader );
84  
85                  if ( type == implementation && type.isInterface() && configuration.getChildCount() <= 0 )
86                  {
87                      return null;
88                  }
89  
90                  retValue = instantiateObject( implementation );
91  
92                  processConfiguration( converterLookup, retValue, classLoader, configuration, expressionEvaluator, listener );
93              }
94              catch ( ComponentConfigurationException e )
95              {
96                  if ( e.getFailedConfiguration() == null )
97                  {
98                      e.setFailedConfiguration( configuration );
99                  }
100 
101                 throw e;
102             }
103         }
104         return retValue;
105     }
106 
107 
108     public void processConfiguration( ConverterLookup converterLookup,
109                                       Object object,
110                                       ClassLoader classLoader,
111                                       PlexusConfiguration configuration )
112         throws ComponentConfigurationException
113     {
114         processConfiguration( converterLookup, object, classLoader, configuration, null );
115     }
116 
117     public void processConfiguration( ConverterLookup converterLookup,
118                                       Object object,
119                                       ClassLoader classLoader,
120                                       PlexusConfiguration configuration,
121                                       ExpressionEvaluator expressionEvaluator )
122         throws ComponentConfigurationException
123     {
124         processConfiguration( converterLookup, object, classLoader, configuration, expressionEvaluator, null );
125     }
126 
127     public void processConfiguration( ConverterLookup converterLookup,
128                                       Object object,
129                                       ClassLoader classLoader,
130                                       PlexusConfiguration configuration,
131                                       ExpressionEvaluator expressionEvaluator,
132                                       ConfigurationListener listener )
133         throws ComponentConfigurationException
134     {
135         int items = configuration.getChildCount();
136 
137         for ( int i = 0; i < items; i++ )
138         {
139             PlexusConfiguration childConfiguration = configuration.getChild( i );
140 
141             String elementName = childConfiguration.getName();
142 
143             ComponentValueSetter valueSetter = new ComponentValueSetter( fromXML( elementName ), object, converterLookup, listener );
144 
145             valueSetter.configure( childConfiguration, classLoader, expressionEvaluator );
146         }
147     }
148 }