From fded11d618a387193c8894a6994673593a4de868 Mon Sep 17 00:00:00 2001 From: Peter Donald Date: Sat, 23 Mar 2002 09:07:33 +0000 Subject: [PATCH] Introduce a basic task git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@271985 13f79535-47bb-0310-9956-ffa450edef68 --- .../java/org/apache/antlib/core/AntParam.java | 89 ++++++++ .../java/org/apache/antlib/core/AntTask.java | 203 ++++++++++++++++++ .../apache/antlib/core/Resources.properties | 3 + 3 files changed, 295 insertions(+) create mode 100644 proposal/myrmidon/src/java/org/apache/antlib/core/AntParam.java create mode 100644 proposal/myrmidon/src/java/org/apache/antlib/core/AntTask.java diff --git a/proposal/myrmidon/src/java/org/apache/antlib/core/AntParam.java b/proposal/myrmidon/src/java/org/apache/antlib/core/AntParam.java new file mode 100644 index 000000000..30a9a2206 --- /dev/null +++ b/proposal/myrmidon/src/java/org/apache/antlib/core/AntParam.java @@ -0,0 +1,89 @@ +/* + * Copyright (C) The Apache Software Foundation. All rights reserved. + * + * This software is published under the terms of the Apache Software License + * version 1.1, a copy of which has been included with this distribution in + * the LICENSE.txt file. + */ +package org.apache.antlib.core; + +import org.apache.avalon.excalibur.i18n.ResourceManager; +import org.apache.avalon.excalibur.i18n.Resources; +import org.apache.myrmidon.api.TaskException; + +/** + * Simple holder for parameters. + * + * @author Peter Donald + * @version $Revision$ $Date$ + * @todo Refactor this and all the other parameter, sysproperty, + * property etc into a single class in framework + */ +public class AntParam +{ + private final static Resources REZ = + ResourceManager.getPackageResources( AntParam.class ); + + private String m_name; + private Object m_value; + + /** + * Set the name of the parameter. + * + * @param name the name of parameter + */ + public void setName( final String name ) + { + m_name = name; + } + + /** + * Set the value of the parameter. + * + * @param value the parameter value + */ + public void setValue( final Object value ) + { + m_value = value; + } + + /** + * Retrieve name of parameter. + * + * @return the name of parameter. + */ + public String getName() + { + return m_name; + } + + /** + * Retrieve the value of parameter. + * + * @return the value of parameter. + */ + public Object getValue() + { + return m_value; + } + + /** + * Make sure that neither the name or the value + * is null. + */ + public void validate() + throws TaskException + { + if( null == m_name ) + { + final String message = REZ.getString( "param.noname.error" ); + throw new TaskException( message ); + } + else if( null == m_value ) + { + final String message = + REZ.getString( "param.novalue.error", m_name ); + throw new TaskException( message ); + } + } +} diff --git a/proposal/myrmidon/src/java/org/apache/antlib/core/AntTask.java b/proposal/myrmidon/src/java/org/apache/antlib/core/AntTask.java new file mode 100644 index 000000000..c1c58fdcb --- /dev/null +++ b/proposal/myrmidon/src/java/org/apache/antlib/core/AntTask.java @@ -0,0 +1,203 @@ +/* + * Copyright (C) The Apache Software Foundation. All rights reserved. + * + * This software is published under the terms of the Apache Software License + * version 1.1, a copy of which has been included with this distribution in + * the LICENSE.txt file. + */ +package org.apache.antlib.core; + +import java.io.File; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.Map; +import org.apache.avalon.framework.parameters.Parameters; +import org.apache.myrmidon.api.AbstractTask; +import org.apache.myrmidon.api.TaskException; +import org.apache.myrmidon.interfaces.embeddor.Embeddor; +import org.apache.myrmidon.interfaces.model.Project; +import org.apache.myrmidon.interfaces.workspace.Workspace; + +/** + * Create a new Workspace and process a build in + * that new workspace. + * + * @author Peter Donald + * @ant.task name="ant" + */ +public class AntTask + extends AbstractTask +{ + /** + * Default build file. + */ + private static final String DEFAULT_BUILD_FILE = "build.ant"; + + /** + * If true, inherit all properties from parent Project + * If false, inherit only userProperties and those defined + * inside the ant call itself + */ + private boolean m_inheritAll; + + /** + * The build file which to execute. If not set defaults to + * using "build.ant" in the basedir of current project. + */ + private File m_file; + + /** + * The target to process in build file. If not specified + * will use default in specified build file. + */ + private String m_target; + + /** + * The "type" of the build file. By default this is null which + * means the type will be determined by the build file extension. + */ + private String m_type; + private final ArrayList m_parameters = new ArrayList(); + + /** + * Specify whether should inherit properties in sub-build. + * + * @param inheritAll true to inherit else false + */ + public void setInheritAll( final boolean inheritAll ) + { + m_inheritAll = inheritAll; + } + + /** + * set the build file to process. + * + * @param file the build file + */ + public void setFile( final File file ) + { + m_file = file; + } + + /** + * set the type of build file. + * + * @param type the type of build file + */ + public void setType( final String type ) + { + m_type = type; + } + + /** + * set the target to process. If none is defined it will + * execute the default target of the build file + */ + public void setTarget( final String target ) + { + m_target = target; + } + + /** + * Add a parameter to processing of build file. + * + * @param param the parameter + */ + public void addParam( final AntParam param ) + { + m_parameters.add( param ); + } + + /** + * Execute the specified build, with specified parameters. + * + * @throws TaskException if an error occurs. + */ + public void execute() + throws TaskException + { + if( null == m_file ) + { + m_file = getContext().resolveFile( DEFAULT_BUILD_FILE ); + } + + final Embeddor embeddor = + (Embeddor)getContext().getService( Embeddor.class ); + + try + { + final Project project = + embeddor.createProject( m_file.toString(), + m_type, + new Parameters() ); + final Workspace workspace = + embeddor.createWorkspace( buildParameters() ); + + if( null == m_target ) + { + m_target = project.getDefaultTargetName(); + } + + workspace.executeProject( project, m_target ); + } + catch( final Exception e ) + { + throw new TaskException( e.toString(), e ); + } + } + + /** + * Build the parameters to pass to sub-project. + * These include the current tasks properties + * (if inheritall=true) and any supplied by the user. + * + * @return the created parameters + */ + private Parameters buildParameters() + throws TaskException + { + final Parameters parameters = new Parameters(); + + if( m_inheritAll ) + { + final Map properties = getContext().getProperties(); + final Iterator keys = properties.keySet().iterator(); + while( keys.hasNext() ) + { + final String key = (String)keys.next(); + final Object value = properties.get( key ); + setProperty( parameters, key, value ); + } + } + + final int size = m_parameters.size(); + for( int i = 0; i < size; i++ ) + { + final AntParam param = (AntParam)m_parameters.get( i ); + param.validate(); + final String name = param.getName(); + final String value = param.getValue().toString(); + setProperty( parameters, name, value ); + } + + return parameters; + } + + /** + * Utility method to add the property into parameters object. + * + * @param parameters where to put property + * @param name the property + * @param value the value of property + * @todo allow non-string params to be passed down + */ + private void setProperty( final Parameters parameters, + final String name, + final Object value ) + { + if( !name.startsWith( "myrmidon." ) ) + { + parameters.setParameter( name, value.toString() ); + } + } +} diff --git a/proposal/myrmidon/src/java/org/apache/antlib/core/Resources.properties b/proposal/myrmidon/src/java/org/apache/antlib/core/Resources.properties index aa815df20..bbf5ed201 100644 --- a/proposal/myrmidon/src/java/org/apache/antlib/core/Resources.properties +++ b/proposal/myrmidon/src/java/org/apache/antlib/core/Resources.properties @@ -37,3 +37,6 @@ trycatch.missing-second.error=Missing or elements from