View Javadoc

1   package org.apache.maven.plugin.antrun.components;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.tools.ant.ComponentHelper;
23  import org.apache.tools.ant.Project;
24  import org.apache.tools.ant.ProjectHelper;
25  import org.apache.tools.ant.RuntimeConfigurable;
26  import org.apache.tools.ant.Target;
27  import org.apache.tools.ant.UnknownElement;
28  import org.codehaus.plexus.component.configurator.ComponentConfigurationException;
29  import org.codehaus.plexus.component.configurator.ConfigurationListener;
30  import org.codehaus.plexus.component.configurator.converters.AbstractConfigurationConverter;
31  import org.codehaus.plexus.component.configurator.converters.ConfigurationConverter;
32  import org.codehaus.plexus.component.configurator.converters.lookup.ConverterLookup;
33  import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
34  import org.codehaus.plexus.configuration.PlexusConfiguration;
35  import org.codehaus.plexus.configuration.PlexusConfigurationException;
36  import org.codehaus.plexus.util.StringUtils;
37  
38  /**
39   * Plexus ConfigurationConverter to set up Ant Target component fields.
40   *
41   * @author <a href="mailto:kenney@apache.org">Kenney Westerhof</a>
42   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
43   * @version $Id: AntTargetConverter.html 816154 2012-05-06 21:49:58Z hboutemy $
44   */
45  public class AntTargetConverter
46      extends AbstractConfigurationConverter
47  {
48      public static final String MAVEN_EXPRESSION_EVALUATOR_ID = "maven.expressionEvaluator";
49  
50      public static final String ROLE = ConfigurationConverter.class.getName();
51  
52      /**
53       * @see org.codehaus.plexus.component.configurator.converters.ConfigurationConverter#canConvert(java.lang.Class)
54       */
55      public boolean canConvert( Class type )
56      {
57          return Target.class.isAssignableFrom( type );
58      }
59  
60      /**
61       * @see org.codehaus.plexus.component.configurator.converters.ConfigurationConverter#fromConfiguration(org.codehaus.plexus.component.configurator.converters.lookup.ConverterLookup, org.codehaus.plexus.configuration.PlexusConfiguration, java.lang.Class, java.lang.Class, java.lang.ClassLoader, org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator, org.codehaus.plexus.component.configurator.ConfigurationListener)
62       */
63      public Object fromConfiguration( ConverterLookup converterLookup, PlexusConfiguration configuration, Class type,
64                                      Class baseType, ClassLoader classLoader, ExpressionEvaluator expressionEvaluator,
65                                      ConfigurationListener listener )
66          throws ComponentConfigurationException
67      {
68          Object retValue = fromExpression( configuration, expressionEvaluator, type );
69          if ( retValue != null )
70          {
71              return retValue;
72          }
73  
74          Class implementation = getClassForImplementationHint( type, configuration, classLoader );
75  
76          retValue = instantiateObject( implementation );
77  
78          if ( !( retValue instanceof Target ) )
79          {
80              retValue = new Target();
81          }
82  
83          processConfiguration( (Target) retValue, configuration, expressionEvaluator );
84  
85          return retValue;
86      }
87  
88      private void processConfiguration( Target target, PlexusConfiguration configuration,
89                                        ExpressionEvaluator expressionEvaluator )
90          throws ComponentConfigurationException
91      {
92          Project project = new Project();
93          project.setName( "DummyProject" );
94  
95          target.setName( "" );
96          target.setProject( project );
97          String[] attributeNames = configuration.getAttributeNames();
98          for ( int i = 0; i < attributeNames.length; i++ )
99          {
100             String attributeName = attributeNames[i];
101             String attributeValue = configuration.getAttribute( attributeNames[i], null );
102 
103             addAttributes( target, attributeName, attributeValue );
104         }
105         project.addTarget( target );
106 
107         project.addReference( MAVEN_EXPRESSION_EVALUATOR_ID, expressionEvaluator );
108 
109         initDefinitions( project, target );
110 
111         processConfiguration( null, project, target, configuration );
112 
113         project.init();
114     }
115 
116     private void processConfiguration( RuntimeConfigurable parentWrapper, Project project, Target target,
117                                       PlexusConfiguration configuration )
118         throws ComponentConfigurationException
119     {
120         int items = configuration.getChildCount();
121 
122         Object parent = parentWrapper == null ? null : parentWrapper.getProxy();
123 
124         for ( int i = 0; i < items; i++ )
125         {
126             PlexusConfiguration childConfiguration = configuration.getChild( i );
127             UnknownElement task = new UnknownElement( childConfiguration.getName() );
128             task.setProject( project );
129             task.setNamespace( "" );
130             task.setQName( childConfiguration.getName() );
131             task.setTaskType( ProjectHelper.genComponentName( task.getNamespace(), childConfiguration.getName() ) );
132             task.setTaskName( childConfiguration.getName() );
133             task.setOwningTarget( target );
134             task.init();
135 
136             if ( parent != null )
137             {
138                 ( (UnknownElement) parent ).addChild( task );
139             }
140             else
141             {
142                 target.addTask( task );
143             }
144 
145             RuntimeConfigurable wrapper = new RuntimeConfigurable( task, task.getTaskName() );
146 
147             try
148             {
149                 if ( childConfiguration.getValue() != null )
150                 {
151                     wrapper.addText( childConfiguration.getValue() );
152                 }
153             }
154             catch ( PlexusConfigurationException e )
155             {
156                 throw new ComponentConfigurationException( "Error reading text value from element '"
157                     + childConfiguration.getName() + "'", e );
158             }
159 
160             String[] attrNames = childConfiguration.getAttributeNames();
161 
162             for ( int a = 0; a < attrNames.length; a++ )
163             {
164                 try
165                 {
166                     String v = childConfiguration.getAttribute( attrNames[a] );
167                     wrapper.setAttribute( attrNames[a], v );
168                 }
169                 catch ( PlexusConfigurationException e )
170                 {
171                     throw new ComponentConfigurationException( "Error getting attribute '" + attrNames[a]
172                         + "' of tag '" + childConfiguration.getName() + "'", e );
173                 }
174             }
175 
176             if ( parentWrapper != null )
177             {
178                 parentWrapper.addChild( wrapper );
179             }
180 
181             processConfiguration( wrapper, project, target, childConfiguration );
182         }
183     }
184 
185     protected void initDefinitions( Project project, Target unused )
186     {
187         ComponentHelper componentHelper = ComponentHelper.getComponentHelper( project );
188 
189         componentHelper.initDefaultDefinitions();
190     }
191 
192     /**
193      * Add specific <code>attributeValue</code> to the tasks for given <code>attributeName</code> like
194      * <code>"if"</code>, <code>"unless"</code>, <code>"name"</code> and <code>"description"</code>.
195      * <br/>
196      * <b>Note:</b> <code>"depends"</code> from Ant tasks is not be used.
197      *
198      * @see <a href="http://ant.apache.org/manual/using.html#targets">Ant targets</a>
199      *
200      * @param tasks should be not null
201      * @param attributeName if empty, skipped
202      * @param attributeValue if empty, skipped
203      */
204     private static void addAttributes( Target tasks, String attributeName, String attributeValue )
205     {
206         if ( StringUtils.isEmpty( attributeName ) )
207         {
208             return;
209         }
210         if ( StringUtils.isEmpty( attributeValue ) )
211         {
212             return;
213         }
214 
215         if ( attributeName.toLowerCase().equals( "name" ) )
216         {
217             tasks.setName( attributeValue );
218         }
219         if ( attributeName.toLowerCase().equals( "unless" ) )
220         {
221             tasks.setUnless( attributeValue );
222         }
223         if ( attributeName.toLowerCase().equals( "description" ) )
224         {
225             tasks.setDescription( attributeValue );
226         }
227         if ( attributeName.toLowerCase().equals( "if" ) )
228         {
229             tasks.setIf( attributeValue );
230         }
231     }
232 }