git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@272456 13f79535-47bb-0310-9956-ffa450edef68master
| @@ -1,23 +0,0 @@ | |||
| /* | |||
| * 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.myrmidon; | |||
| /** | |||
| * Abstract interface to hold constants. | |||
| * | |||
| * @author <a href="mailto:peter@apache.org">Peter Donald</a> | |||
| * @version $Revision$ $Date$ | |||
| */ | |||
| public interface Constants | |||
| { | |||
| //Constants to indicate the build of Myrmidon | |||
| String BUILD_DATE = "@@DATE@@"; | |||
| String BUILD_VERSION = "@@VERSION@@"; | |||
| String BUILD_DESCRIPTION = "Myrmidon " + BUILD_VERSION + " compiled on " + BUILD_DATE; | |||
| } | |||
| @@ -1,242 +0,0 @@ | |||
| /* | |||
| * 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.myrmidon.framework; | |||
| import org.apache.aut.converter.Converter; | |||
| import org.apache.aut.converter.ConverterException; | |||
| import org.apache.avalon.excalibur.i18n.ResourceManager; | |||
| import org.apache.avalon.excalibur.i18n.Resources; | |||
| import org.apache.avalon.framework.configuration.Configuration; | |||
| import org.apache.avalon.framework.configuration.ConfigurationException; | |||
| import org.apache.myrmidon.api.AbstractTask; | |||
| import org.apache.myrmidon.api.TaskContext; | |||
| import org.apache.myrmidon.api.TaskException; | |||
| import org.apache.myrmidon.interfaces.configurer.Configurer; | |||
| import org.apache.myrmidon.interfaces.executor.ExecutionFrame; | |||
| import org.apache.myrmidon.interfaces.executor.Executor; | |||
| import org.apache.myrmidon.interfaces.role.RoleInfo; | |||
| import org.apache.myrmidon.interfaces.role.RoleManager; | |||
| import org.apache.myrmidon.interfaces.type.TypeException; | |||
| import org.apache.myrmidon.interfaces.type.TypeFactory; | |||
| import org.apache.myrmidon.interfaces.type.TypeManager; | |||
| /** | |||
| * This is the class that Task writers should extend to provide custom tasks. | |||
| * | |||
| * @author <a href="mailto:peter@apache.org">Peter Donald</a> | |||
| * @version $Revision$ $Date$ | |||
| */ | |||
| public abstract class AbstractContainerTask | |||
| extends AbstractTask | |||
| { | |||
| private static final Resources REZ = | |||
| ResourceManager.getPackageResources( AbstractContainerTask.class ); | |||
| ///For converting own attributes | |||
| private Converter m_converter; | |||
| ///For configuring own sub-elements | |||
| private Configurer m_configurer; | |||
| ///For executing sub-elements as tasks | |||
| private Executor m_executor; | |||
| private ExecutionFrame m_frame; | |||
| /** | |||
| * Retrieve context from container. | |||
| * | |||
| * @param context the context | |||
| */ | |||
| public void contextualize( TaskContext context ) | |||
| throws TaskException | |||
| { | |||
| super.contextualize( context ); | |||
| m_configurer = (Configurer)getService( Configurer.class ); | |||
| m_converter = (Converter)getService( Converter.class ); | |||
| m_executor = (Executor)getService( Executor.class ); | |||
| m_frame = (ExecutionFrame)getService( ExecutionFrame.class ); | |||
| } | |||
| /** | |||
| * Helper method to convert an object to a specific type. | |||
| * | |||
| * @param to type to convert object to | |||
| * @param object the object to convert | |||
| * @return the converted object | |||
| * @exception ConfigurationException if an error occurs | |||
| */ | |||
| protected final Object convert( final Class to, final Object object ) | |||
| throws ConfigurationException | |||
| { | |||
| try | |||
| { | |||
| return m_converter.convert( to, object, getContext() ); | |||
| } | |||
| catch( final ConverterException ce ) | |||
| { | |||
| final String message = REZ.getString( "container.bad-config.error" ); | |||
| throw new ConfigurationException( message, ce ); | |||
| } | |||
| } | |||
| /** | |||
| * Configure an object using specific configuration element. | |||
| * | |||
| * @param object the object | |||
| * @param element the configuration element | |||
| * @exception ConfigurationException if an error occurs | |||
| */ | |||
| protected final void configureElement( final Object object, | |||
| final Configuration element ) | |||
| throws ConfigurationException | |||
| { | |||
| m_configurer.configureElement( object, element, getContext() ); | |||
| } | |||
| /** | |||
| * Configure an object using specific configuration element. | |||
| * | |||
| * @param object the object | |||
| * @param clazz the class to use when configuring element | |||
| * @param element the configuration element | |||
| * @exception ConfigurationException if an error occurs | |||
| */ | |||
| protected final void configureElement( final Object object, | |||
| final Class clazz, | |||
| final Configuration element ) | |||
| throws ConfigurationException | |||
| { | |||
| m_configurer.configureElement( object, clazz, element, getContext() ); | |||
| } | |||
| /** | |||
| * Configure an objects attribute using parameters. | |||
| * | |||
| * @param object the object | |||
| * @param name the attibute name | |||
| * @param value the attibute value | |||
| * @exception ConfigurationException if an error occurs | |||
| */ | |||
| protected final void configureAttribute( final Object object, final String name, final String value ) | |||
| throws ConfigurationException | |||
| { | |||
| m_configurer.configureAttribute( object, name, value, getContext() ); | |||
| } | |||
| /** | |||
| * Configure an objects attribute using parameters. | |||
| * | |||
| * @param object the object | |||
| * @param clazz the class to use when configuring element | |||
| * @param name the attibute name | |||
| * @param value the attibute value | |||
| * @exception ConfigurationException if an error occurs | |||
| */ | |||
| protected final void configureAttribute( final Object object, | |||
| final Class clazz, | |||
| final String name, final String value ) | |||
| throws ConfigurationException | |||
| { | |||
| m_configurer.configureAttribute( object, clazz, name, value, getContext() ); | |||
| } | |||
| /** | |||
| * Utility method to execute specified tasks in current ExecutionFrame. | |||
| */ | |||
| protected final void executeTasks( final Configuration[] tasks ) | |||
| throws TaskException | |||
| { | |||
| for( int i = 0; i < tasks.length; i++ ) | |||
| { | |||
| final Configuration task = tasks[ i ]; | |||
| executeTask( task ); | |||
| } | |||
| } | |||
| /** | |||
| * Utility method to execute specified task in current ExecutionFrame. | |||
| */ | |||
| protected final void executeTask( final Configuration task ) | |||
| throws TaskException | |||
| { | |||
| m_executor.execute( task, m_frame ); | |||
| } | |||
| /** | |||
| * Create an instance of type with specified type and in specified role. | |||
| */ | |||
| protected final Object newInstance( final Class roleType, final String typeName ) | |||
| throws TaskException | |||
| { | |||
| try | |||
| { | |||
| final RoleInfo role = getRoleByType( roleType ); | |||
| final TypeFactory typeFactory = getTypeFactory( role.getName() ); | |||
| return typeFactory.create( typeName ); | |||
| } | |||
| catch( Exception e ) | |||
| { | |||
| final String message = | |||
| REZ.getString( "container.no-create-type-for-type.error", roleType.getName(), typeName ); | |||
| throw new TaskException( message, e ); | |||
| } | |||
| } | |||
| /** | |||
| * Create an instance of type with specified type and in specified role. | |||
| */ | |||
| protected final Object newInstance( final String roleName, final String typeName ) | |||
| throws TaskException | |||
| { | |||
| try | |||
| { | |||
| final TypeFactory typeFactory = getTypeFactory( roleName ); | |||
| return typeFactory.create( typeName ); | |||
| } | |||
| catch( final Exception e ) | |||
| { | |||
| final String message = | |||
| REZ.getString( "container.no-create-type.error", roleName, typeName ); | |||
| throw new TaskException( message, e ); | |||
| } | |||
| } | |||
| /** | |||
| * Looks up a role using the role type. | |||
| */ | |||
| protected final RoleInfo getRoleByType( final Class roleType ) | |||
| throws TaskException | |||
| { | |||
| final RoleManager roleManager = (RoleManager)getService( RoleManager.class ); | |||
| final RoleInfo role = roleManager.getRoleByType( roleType ); | |||
| if( role == null ) | |||
| { | |||
| final String message = REZ.getString( "container.unknown-role-type.error", roleType.getName() ); | |||
| throw new TaskException( message ); | |||
| } | |||
| return role; | |||
| } | |||
| /** | |||
| * Locates a type factory. | |||
| */ | |||
| protected final TypeFactory getTypeFactory( final String roleName ) | |||
| throws TaskException | |||
| { | |||
| try | |||
| { | |||
| final TypeManager typeManager = (TypeManager)getService( TypeManager.class ); | |||
| return typeManager.getFactory( roleName ); | |||
| } | |||
| catch( final TypeException te ) | |||
| { | |||
| final String message = REZ.getString( "container.no-factory.error", roleName ); | |||
| throw new TaskException( message, te ); | |||
| } | |||
| } | |||
| } | |||
| @@ -1,187 +0,0 @@ | |||
| /* | |||
| * 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.myrmidon.framework; | |||
| import org.apache.avalon.excalibur.i18n.ResourceManager; | |||
| import org.apache.avalon.excalibur.i18n.Resources; | |||
| import org.apache.avalon.framework.configuration.Configurable; | |||
| import org.apache.avalon.framework.configuration.Configuration; | |||
| import org.apache.avalon.framework.configuration.ConfigurationException; | |||
| import org.apache.avalon.framework.configuration.DefaultConfiguration; | |||
| import org.apache.myrmidon.api.TaskException; | |||
| /** | |||
| * Abstract task used to write tasks that delegate to facades | |||
| * such as Javac, Jspc and so forth. | |||
| * | |||
| * @author <a href="mailto:peter@apache.org">Peter Donald</a> | |||
| * @version $Revision$ $Date$ | |||
| */ | |||
| public abstract class AbstractFacadeTask | |||
| extends AbstractContainerTask | |||
| implements Configurable | |||
| { | |||
| private static final Resources REZ = | |||
| ResourceManager.getPackageResources( AbstractFacadeTask.class ); | |||
| /** | |||
| * The name of the attribute used to select specific | |||
| * implementation of facade. | |||
| */ | |||
| private final String m_selector; | |||
| /** | |||
| * The Class type for the facade (also used as the role | |||
| * when looking up TypeManager). | |||
| */ | |||
| private final Class m_facadeType; | |||
| /** | |||
| * The default name of implementation if none are specified. | |||
| */ | |||
| private final String m_defaultName; | |||
| /** | |||
| * The configuration used to configure the facade implementation. | |||
| */ | |||
| private Configuration m_configuration; | |||
| /** | |||
| * Create the facade task that works with specified facade class, | |||
| * using the selector attribute to find implementation or using | |||
| * defaultName if selector attribute not specified. | |||
| */ | |||
| protected AbstractFacadeTask( final String selector, | |||
| final Class facadeType, | |||
| final String defaultName ) | |||
| { | |||
| m_selector = selector; | |||
| m_facadeType = facadeType; | |||
| m_defaultName = defaultName; | |||
| } | |||
| /** | |||
| * Supply the configuration for this task. | |||
| */ | |||
| public void configure( final Configuration configuration ) | |||
| throws ConfigurationException | |||
| { | |||
| m_configuration = configuration; | |||
| } | |||
| /** | |||
| * Utility method to create and configure the facade | |||
| * implementation. | |||
| */ | |||
| protected Object prepareFacade() | |||
| throws TaskException | |||
| { | |||
| final Object facade = createFacade(); | |||
| configureFacade( facade ); | |||
| return facade; | |||
| } | |||
| /** | |||
| * Utility method to configure the specified facade. | |||
| * It will be configured according to normal resolution | |||
| * rules using the configuration data supplied to task | |||
| * minus the selector attribute if present. | |||
| */ | |||
| protected void configureFacade( final Object facade ) | |||
| throws TaskException | |||
| { | |||
| Configuration configuration = m_configuration; | |||
| if( null != m_selector && | |||
| null != m_configuration.getAttribute( m_selector, null ) ) | |||
| { | |||
| configuration = rebuildConfiguration( m_configuration, m_selector ); | |||
| } | |||
| try | |||
| { | |||
| configureElement( facade, m_facadeType, configuration ); | |||
| } | |||
| catch( final ConfigurationException ce ) | |||
| { | |||
| throw new TaskException( ce.getMessage(), ce ); | |||
| } | |||
| } | |||
| /** | |||
| * Rebuild the configuration tree with the attribute with specified | |||
| * name removed from top-level element. | |||
| */ | |||
| private Configuration rebuildConfiguration( final Configuration configuration, | |||
| final String attribute ) | |||
| { | |||
| final DefaultConfiguration newConfiguration = | |||
| new DefaultConfiguration( configuration.getName(), | |||
| configuration.getLocation() ); | |||
| //Add all the attributes from old configuration except the one | |||
| //that was used to select the particular implementation | |||
| final String[] names = configuration.getAttributeNames(); | |||
| for( int i = 0; i < names.length; i++ ) | |||
| { | |||
| final String name = names[ i ]; | |||
| if( !name.equals( attribute ) ) | |||
| { | |||
| final String value = configuration.getAttribute( name, null ); | |||
| newConfiguration.setAttribute( name, value ); | |||
| } | |||
| } | |||
| //Add all elements to new configuration element in the | |||
| //correct order | |||
| final Configuration[] children = configuration.getChildren(); | |||
| for( int i = 0; i < children.length; i++ ) | |||
| { | |||
| newConfiguration.addChild( children[ i ] ); | |||
| } | |||
| return newConfiguration; | |||
| } | |||
| /** | |||
| * Create the instance of the facade. It looks up the name | |||
| * of the implementation via the <code>getImplementation()</code> | |||
| * method and then creates a new instance from a TypeFactory | |||
| * using that name and the facadeType (specified in the | |||
| * constructor). | |||
| */ | |||
| protected Object createFacade() | |||
| throws TaskException | |||
| { | |||
| final String implementation = getImplementation(); | |||
| if( null == implementation ) | |||
| { | |||
| final String message = | |||
| REZ.getString( "facade.missing-impl.error", getContext().getName() ); | |||
| throw new TaskException( message ); | |||
| } | |||
| return newInstance( m_facadeType, implementation ); | |||
| } | |||
| /** | |||
| * Get the shortname of the implementation | |||
| * to use. It assumes that the implementation is registered in | |||
| * the TypeFactory under this shortname. | |||
| */ | |||
| protected String getImplementation() | |||
| { | |||
| if( null != m_selector ) | |||
| { | |||
| return m_configuration.getAttribute( m_selector, m_defaultName ); | |||
| } | |||
| else | |||
| { | |||
| return m_defaultName; | |||
| } | |||
| } | |||
| } | |||
| @@ -1,42 +0,0 @@ | |||
| /* | |||
| * 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.myrmidon.framework; | |||
| /** | |||
| * An abstract base class for all FileSets. | |||
| * FileSets represent a pattern anchored by a root. | |||
| * | |||
| * @author <a href="mailto:peter@apache.org">Peter Donald</a> | |||
| * @version $Revision$ $Date$ | |||
| */ | |||
| public class AbstractFileSet | |||
| extends PatternSet | |||
| { | |||
| private boolean m_defaultExcludes = true; | |||
| /** | |||
| * Add the default excludes to FileSet. | |||
| */ | |||
| public final void setDefaultExcludes( final boolean defaultExcludes ) | |||
| { | |||
| m_defaultExcludes = defaultExcludes; | |||
| } | |||
| public final boolean includeDefaultExcludes() | |||
| { | |||
| return m_defaultExcludes; | |||
| } | |||
| /** | |||
| * Merge specified PatternSet into this patternSet. | |||
| */ | |||
| public final void addPatternSet( final PatternSet set ) | |||
| { | |||
| append( set ); | |||
| } | |||
| } | |||
| @@ -1,71 +0,0 @@ | |||
| /* | |||
| * 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.myrmidon.framework; | |||
| import org.apache.myrmidon.api.AbstractTask; | |||
| /** | |||
| * An abstract base class for tasks that wish to operate on | |||
| * a set of files. This class is based on the ant1.x MatchingTask and | |||
| * should fullfill similar requirements. | |||
| * | |||
| * @author <a href="mailto:peter@apache.org">Peter Donald</a> | |||
| * @version $Revision$ $Date$ | |||
| */ | |||
| public abstract class AbstractMatchingTask | |||
| extends AbstractTask | |||
| { | |||
| private FileSet m_fileset = new FileSet(); | |||
| /** | |||
| * Retrieve fileset for Matching task. | |||
| */ | |||
| protected FileSet getFileSet() | |||
| { | |||
| return m_fileset; | |||
| } | |||
| /** | |||
| * The attribute that contains a list of itesm to be included. | |||
| */ | |||
| public void setIncludes( final String includes ) | |||
| { | |||
| m_fileset.setIncludes( includes ); | |||
| } | |||
| /** | |||
| * The attribute that contains a list of items to be excluded. | |||
| */ | |||
| public void setExcludes( final String excludes ) | |||
| { | |||
| m_fileset.setExcludes( excludes ); | |||
| } | |||
| /** | |||
| * Set this to true to use the defaul exclude patterns. | |||
| */ | |||
| public void setDefaultexcludes( final boolean useDefaultExcludes ) | |||
| { | |||
| m_fileset.setDefaultExcludes( useDefaultExcludes ); | |||
| } | |||
| public void addInclude( final Pattern pattern ) | |||
| { | |||
| m_fileset.addInclude( pattern ); | |||
| } | |||
| public void addExclude( final Pattern pattern ) | |||
| { | |||
| m_fileset.addExclude( pattern ); | |||
| } | |||
| public void addPatternSet( final PatternSet set ) | |||
| { | |||
| m_fileset.addPatternSet( set ); | |||
| } | |||
| } | |||
| @@ -1,93 +0,0 @@ | |||
| /* | |||
| * 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.myrmidon.framework; | |||
| import java.io.File; | |||
| import org.apache.avalon.excalibur.i18n.ResourceManager; | |||
| import org.apache.avalon.excalibur.i18n.Resources; | |||
| import org.apache.myrmidon.api.TaskException; | |||
| import org.apache.myrmidon.interfaces.deployer.Deployer; | |||
| import org.apache.myrmidon.interfaces.deployer.DeploymentException; | |||
| import org.apache.myrmidon.interfaces.deployer.TypeDefinition; | |||
| import org.apache.myrmidon.interfaces.deployer.TypeDeployer; | |||
| /** | |||
| * Abstract task to extend to define a type. | |||
| * | |||
| * TODO: Make this support classpath sub-element in future | |||
| * | |||
| * @author <a href="mailto:peter@apache.org">Peter Donald</a> | |||
| * @version $Revision$ $Date$ | |||
| */ | |||
| public abstract class AbstractTypeDef | |||
| extends AbstractContainerTask | |||
| { | |||
| private static final Resources REZ = | |||
| ResourceManager.getPackageResources( AbstractTypeDef.class ); | |||
| // TODO - replace lib with class-path | |||
| private File m_lib; | |||
| private String m_name; | |||
| private String m_classname; | |||
| protected void setName( final String name ) | |||
| { | |||
| m_name = name; | |||
| } | |||
| public void setClassname( final String classname ) | |||
| { | |||
| m_classname = classname; | |||
| } | |||
| public void setLib( final File lib ) | |||
| { | |||
| m_lib = lib; | |||
| } | |||
| protected final String getName() | |||
| { | |||
| return m_name; | |||
| } | |||
| protected final String getClassname() | |||
| { | |||
| return m_classname; | |||
| } | |||
| /** | |||
| * Executes the task. | |||
| */ | |||
| public void execute() | |||
| throws TaskException | |||
| { | |||
| if( null == m_lib ) | |||
| { | |||
| final String message = REZ.getString( "typedef.no-lib.error" ); | |||
| throw new TaskException( message ); | |||
| } | |||
| try | |||
| { | |||
| // Locate the deployer, and use it to deploy the type | |||
| final Deployer deployer = (Deployer)getService( Deployer.class ); | |||
| final TypeDeployer typeDeployer = deployer.createDeployer( m_lib ); | |||
| final TypeDefinition typeDef = createTypeDefinition(); | |||
| typeDeployer.deployType( typeDef ); | |||
| } | |||
| catch( DeploymentException e ) | |||
| { | |||
| throw new TaskException( e.getMessage(), e ); | |||
| } | |||
| } | |||
| /** | |||
| * Creates the definition for the type to be deployed. | |||
| */ | |||
| protected abstract TypeDefinition createTypeDefinition(); | |||
| } | |||
| @@ -1,85 +0,0 @@ | |||
| /* | |||
| * 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.myrmidon.framework; | |||
| import java.util.ArrayList; | |||
| import org.apache.myrmidon.api.TaskContext; | |||
| import org.apache.myrmidon.api.TaskException; | |||
| /** | |||
| * A mapper that applies a chain of mappers. | |||
| * | |||
| * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a> | |||
| * @version $Revision$ $Date$ | |||
| * | |||
| * @ant.type type="mapper" name="chain" | |||
| */ | |||
| public class ChainFileNameMapper | |||
| implements FileNameMapper | |||
| { | |||
| private final ArrayList m_mappers = new ArrayList(); | |||
| /** | |||
| * Adds a nested mapper. | |||
| */ | |||
| public void add( final FileNameMapper mapper ) | |||
| { | |||
| m_mappers.add( mapper ); | |||
| } | |||
| /** | |||
| * Returns an array containing the target filename(s) for the given source | |||
| * file. | |||
| */ | |||
| public String[] mapFileName( final String sourceFileName, | |||
| final TaskContext context ) | |||
| throws TaskException | |||
| { | |||
| ArrayList names = new ArrayList(); | |||
| names.add( sourceFileName ); | |||
| final int count = m_mappers.size(); | |||
| for( int i = 0; i < count; i++ ) | |||
| { | |||
| final FileNameMapper mapper = (FileNameMapper)m_mappers.get( i ); | |||
| names = mapNames( mapper, names, context ); | |||
| } | |||
| return (String[])names.toArray( new String[ names.size() ] ); | |||
| } | |||
| /** | |||
| * Maps a set of names. | |||
| */ | |||
| private ArrayList mapNames( final FileNameMapper mapper, | |||
| final ArrayList names, | |||
| final TaskContext context ) | |||
| throws TaskException | |||
| { | |||
| final ArrayList retval = new ArrayList(); | |||
| // Map each of the supplied names | |||
| final int count = names.size(); | |||
| for( int i = 0; i < count; i++ ) | |||
| { | |||
| final String name = (String)names.get( i ); | |||
| final String[] newNames = mapper.mapFileName( name, context ); | |||
| if( newNames == null ) | |||
| { | |||
| continue; | |||
| } | |||
| for( int j = 0; j < newNames.length; j++ ) | |||
| { | |||
| final String newName = newNames[ j ]; | |||
| retval.add( newName ); | |||
| } | |||
| } | |||
| return retval; | |||
| } | |||
| } | |||
| @@ -1,20 +0,0 @@ | |||
| /* | |||
| * 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.myrmidon.framework; | |||
| /** | |||
| * Base class for those classes that can appear inside the build file | |||
| * as stand alone data types. | |||
| * | |||
| * @author <a href="mailto:peter@apache.org">Peter Donald</a> | |||
| * @ant:role shorthand="data-type" | |||
| */ | |||
| public interface DataType | |||
| { | |||
| String ROLE = DataType.class.getName(); | |||
| } | |||
| @@ -1,44 +0,0 @@ | |||
| /* | |||
| * 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.myrmidon.framework; | |||
| import org.apache.myrmidon.api.TaskContext; | |||
| import org.apache.myrmidon.api.TaskException; | |||
| /** | |||
| * Interface to be used by SourceFileScanner. <p> | |||
| * | |||
| * Used to find the name of the target file(s) corresponding to a source file. | |||
| * </p> <p> | |||
| * | |||
| * The rule by which the file names are transformed is specified via the setFrom | |||
| * and setTo methods. The exact meaning of these is implementation dependent. | |||
| * </p> | |||
| * | |||
| * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a> | |||
| * | |||
| * @ant:role shorthand="mapper" | |||
| */ | |||
| public interface FileNameMapper | |||
| { | |||
| /** | |||
| * Returns an array containing the target filename(s) for the given source | |||
| * file. | |||
| * | |||
| * <p>if the given rule doesn't apply to the source file, implementation | |||
| * must return null. SourceFileScanner will then omit the source file in | |||
| * question.</p> | |||
| * | |||
| * @param sourceFileName the name of the source file relative to some given | |||
| * basedirectory. | |||
| * @param context the context to perform the mapping in. | |||
| * @return Description of the Returned Value | |||
| */ | |||
| String[] mapFileName( String sourceFileName, TaskContext context ) | |||
| throws TaskException; | |||
| } | |||
| @@ -1,38 +0,0 @@ | |||
| /* | |||
| * 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.myrmidon.framework; | |||
| import java.io.File; | |||
| /** | |||
| * A FileSet represents a set of files selected by patterns with a | |||
| * specified root. | |||
| * | |||
| * @author <a href="mailto:peter@apache.org">Peter Donald</a> | |||
| * @version $Revision$ $Date$ | |||
| * | |||
| * @ant.data-type name="fileset" | |||
| */ | |||
| public class FileSet | |||
| extends AbstractFileSet | |||
| { | |||
| private File m_dir; | |||
| /** | |||
| * Specify the base directory at which the file set is rooted. | |||
| */ | |||
| public final void setDir( File dir ) | |||
| { | |||
| m_dir = dir; | |||
| } | |||
| public final File getDir() | |||
| { | |||
| return m_dir; | |||
| } | |||
| } | |||
| @@ -1,77 +0,0 @@ | |||
| /* | |||
| * 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.myrmidon.framework; | |||
| import org.apache.avalon.framework.ValuedEnum; | |||
| /** | |||
| * Type safe wrapper class for Java Version enums. | |||
| * | |||
| * @author <a href="mailto:peter@apache.org">Peter Donald</a> | |||
| * @version $Revision$ $Date$ | |||
| */ | |||
| public final class JavaVersion | |||
| extends ValuedEnum | |||
| { | |||
| //standard enums for version of JVM | |||
| public static final JavaVersion JAVA1_0 = new JavaVersion( "Java 1.0", 100 ); | |||
| public static final JavaVersion JAVA1_1 = new JavaVersion( "Java 1.1", 110 ); | |||
| public static final JavaVersion JAVA1_2 = new JavaVersion( "Java 1.2", 120 ); | |||
| public static final JavaVersion JAVA1_3 = new JavaVersion( "Java 1.3", 130 ); | |||
| public static final JavaVersion JAVA1_4 = new JavaVersion( "Java 1.4", 140 ); | |||
| private static final JavaVersion CURRENT = determineCurrentJavaVersion(); | |||
| /** | |||
| * Method to retrieve the current JVM version. | |||
| * | |||
| * @return the current JVM version | |||
| */ | |||
| public static final JavaVersion getCurrentJavaVersion() | |||
| { | |||
| return CURRENT; | |||
| } | |||
| /** | |||
| * Private constructor so no instance except here can be defined. | |||
| * | |||
| * @param name the java version name | |||
| * @param value the version * 100 | |||
| */ | |||
| private JavaVersion( final String name, final int value ) | |||
| { | |||
| super( name, value ); | |||
| } | |||
| /** | |||
| * Helper method to retrieve current JVM version. | |||
| * | |||
| * @return the current JVM version | |||
| */ | |||
| private static final JavaVersion determineCurrentJavaVersion() | |||
| { | |||
| JavaVersion version = JavaVersion.JAVA1_0; | |||
| try | |||
| { | |||
| Class.forName( "java.lang.Void" ); | |||
| version = JAVA1_1; | |||
| Class.forName( "java.lang.ThreadLocal" ); | |||
| version = JAVA1_2; | |||
| Class.forName( "java.lang.StrictMath" ); | |||
| version = JAVA1_3; | |||
| Class.forName( "java.lang.CharSequence" ); | |||
| version = JAVA1_4; | |||
| } | |||
| catch( final ClassNotFoundException cnfe ) | |||
| { | |||
| } | |||
| return version; | |||
| } | |||
| } | |||
| @@ -1,132 +0,0 @@ | |||
| /* | |||
| * 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.myrmidon.framework; | |||
| import java.util.HashMap; | |||
| import java.util.Set; | |||
| import org.apache.avalon.framework.Enum; | |||
| import org.apache.myrmidon.api.TaskContext; | |||
| /** | |||
| * Type safe Enum for Log Levels and utility method | |||
| * for using enum to write to logger. | |||
| * | |||
| * @author <a href="mailto:peter@apache.org">Peter Donald</a> | |||
| * @version $Revision$ $Date$ | |||
| */ | |||
| public final class LogLevel | |||
| extends Enum | |||
| { | |||
| //Map for all the levels | |||
| private static final HashMap c_levels = new HashMap(); | |||
| //standard enums for version of JVM | |||
| public static final LogLevel ERROR = new LogLevel( "error" ); | |||
| public static final LogLevel WARN = new LogLevel( "warn" ); | |||
| public static final LogLevel INFO = new LogLevel( "info" ); | |||
| public static final LogLevel VERBOSE = new LogLevel( "verbose" ); | |||
| public static final LogLevel DEBUG = new LogLevel( "debug" ); | |||
| /** | |||
| * Retrieve the log level for the specified name. | |||
| * | |||
| * @param name the name of the LogLevel object to retrieve | |||
| * @returns The LogLevel for specified name or null | |||
| */ | |||
| public static LogLevel getByName( final String name ) | |||
| { | |||
| return (LogLevel)c_levels.get( name ); | |||
| } | |||
| /** | |||
| * Retrieve the names of all the LogLevels. | |||
| * | |||
| * @returns The names of all the LogLevels | |||
| */ | |||
| public static String[] getNames() | |||
| { | |||
| final Set keys = c_levels.keySet(); | |||
| return (String[])keys.toArray( new String[ keys.size() ] ); | |||
| } | |||
| /** | |||
| * Log a message. | |||
| * | |||
| * @param level the level to write the log message at. | |||
| * @param message the message to write. | |||
| */ | |||
| public static void log( final TaskContext context, | |||
| final LogLevel level, | |||
| final String message ) | |||
| { | |||
| if( ERROR == level ) | |||
| { | |||
| context.error( message ); | |||
| } | |||
| else if( WARN == level ) | |||
| { | |||
| context.warn( message ); | |||
| } | |||
| else if( INFO == level ) | |||
| { | |||
| context.info( message ); | |||
| } | |||
| else if( VERBOSE == level ) | |||
| { | |||
| context.verbose( message ); | |||
| } | |||
| else | |||
| { | |||
| context.debug( message ); | |||
| } | |||
| } | |||
| /** | |||
| * Log a message. | |||
| * | |||
| * @param level the level to write the log message at. | |||
| * @param message the message to write. | |||
| * @param throwable the throwable. | |||
| */ | |||
| public static void log( final TaskContext context, | |||
| final LogLevel level, | |||
| final String message, | |||
| final Throwable throwable ) | |||
| { | |||
| if( ERROR == level ) | |||
| { | |||
| context.error( message, throwable ); | |||
| } | |||
| else if( WARN == level ) | |||
| { | |||
| context.warn( message, throwable ); | |||
| } | |||
| else if( INFO == level ) | |||
| { | |||
| context.info( message, throwable ); | |||
| } | |||
| else if( VERBOSE == level ) | |||
| { | |||
| context.verbose( message, throwable ); | |||
| } | |||
| else | |||
| { | |||
| context.debug( message, throwable ); | |||
| } | |||
| } | |||
| /** | |||
| * Private constructor so no instance except here can be defined. | |||
| * | |||
| * @param name the name of Log Level | |||
| */ | |||
| private LogLevel( final String name ) | |||
| { | |||
| super( name, c_levels ); | |||
| } | |||
| } | |||
| @@ -1,142 +0,0 @@ | |||
| /* | |||
| * 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.myrmidon.framework; | |||
| import org.apache.avalon.excalibur.i18n.ResourceManager; | |||
| import org.apache.avalon.excalibur.i18n.Resources; | |||
| import org.apache.myrmidon.api.TaskContext; | |||
| import org.apache.myrmidon.api.TaskException; | |||
| import org.apache.myrmidon.framework.conditions.Condition; | |||
| import org.apache.myrmidon.framework.conditions.IsTrueCondition; | |||
| import org.apache.myrmidon.framework.conditions.NotCondition; | |||
| /** | |||
| * Basic data type for holding patterns. | |||
| * | |||
| * @author <a href="mailto:peter@apache.org">Peter Donald</a> | |||
| * @version $Revision$ $Date$ | |||
| * @ant.data-type name="pattern" | |||
| */ | |||
| public class Pattern | |||
| implements DataType | |||
| { | |||
| private static final Resources REZ = | |||
| ResourceManager.getPackageResources( Pattern.class ); | |||
| private String m_name; | |||
| private Condition m_condition; | |||
| public Pattern() | |||
| { | |||
| } | |||
| public Pattern( final String name ) | |||
| { | |||
| m_name = name; | |||
| } | |||
| /** | |||
| * Retrieve value of pattern. | |||
| * | |||
| * @return the value of pattern | |||
| */ | |||
| public String getName() | |||
| { | |||
| return m_name; | |||
| } | |||
| /** | |||
| * Get condition associated with pattern if any. | |||
| * | |||
| * @return the Condition | |||
| */ | |||
| public Condition getCondition() | |||
| { | |||
| return m_condition; | |||
| } | |||
| /** | |||
| * Setter method for name of pattern. | |||
| * | |||
| * @param name the name | |||
| */ | |||
| public void setName( final String name ) | |||
| { | |||
| m_name = name; | |||
| } | |||
| /** | |||
| * Set if clause on pattern. | |||
| * | |||
| * @param condition the condition | |||
| * @exception TaskException if an error occurs | |||
| */ | |||
| public void setIf( final String condition ) | |||
| throws TaskException | |||
| { | |||
| verifyConditionNull(); | |||
| m_condition = new IsTrueCondition( condition ); | |||
| } | |||
| /** | |||
| * Set unless clause of pattern. | |||
| * | |||
| * @param condition the unless clause | |||
| * @exception TaskException if an error occurs | |||
| */ | |||
| public void setUnless( final String condition ) | |||
| throws TaskException | |||
| { | |||
| verifyConditionNull(); | |||
| m_condition = new NotCondition( new IsTrueCondition( condition ) ); | |||
| } | |||
| public String evaluateName( final TaskContext context ) | |||
| { | |||
| try | |||
| { | |||
| final Condition condition = getCondition(); | |||
| final boolean result = ( condition == null || condition.evaluate( context ) ); | |||
| if( result ) | |||
| { | |||
| return getName(); | |||
| } | |||
| } | |||
| catch( final TaskException te ) | |||
| { | |||
| //ignore for the moment | |||
| } | |||
| return null; | |||
| } | |||
| public String toString() | |||
| { | |||
| String result = "Pattern['" + m_name + "',"; | |||
| if( null != m_condition ) | |||
| { | |||
| result = result + m_condition; | |||
| } | |||
| return result + "]"; | |||
| } | |||
| /** | |||
| * Utility method to make sure condition unset. | |||
| * Made so that it is not possible for both if and unless to be set. | |||
| * | |||
| * @exception TaskException if an error occurs | |||
| */ | |||
| private void verifyConditionNull() | |||
| throws TaskException | |||
| { | |||
| if( null != m_condition ) | |||
| { | |||
| final String message = REZ.getString( "pattern.ifelse-duplicate.error" ); | |||
| throw new TaskException( message ); | |||
| } | |||
| } | |||
| } | |||
| @@ -1,115 +0,0 @@ | |||
| /* | |||
| * 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.myrmidon.framework; | |||
| import java.util.ArrayList; | |||
| import java.util.StringTokenizer; | |||
| /** | |||
| * Named collection of include/exclude tags. <p> | |||
| * | |||
| * @author <a href="mailto:ajkuiper@wxs.nl">Arnout J. Kuiper</a> | |||
| * @author <a href="mailto:stefano@apache.org">Stefano Mazzocchi</a> | |||
| * @author <a href="mailto:rubys@us.ibm.com">Sam Ruby</a> | |||
| * @author <a href="mailto:jon@clearink.com">Jon S. Stevens</a> | |||
| * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a> | |||
| * @author <a href="mailto:peter@apache.org">Peter Donald</a> | |||
| * @version $Revision$ $Date$ | |||
| */ | |||
| public class PatternSet | |||
| { | |||
| private ArrayList m_includes = new ArrayList(); | |||
| private ArrayList m_excludes = new ArrayList(); | |||
| /** | |||
| * Sets the set of exclude patterns. Patterns may be separated by a comma or | |||
| * a space. | |||
| * | |||
| * @param excludes the string containing the exclude patterns | |||
| */ | |||
| public void setExcludes( final String excludes ) | |||
| { | |||
| final Pattern[] patterns = parsePatterns( excludes ); | |||
| for( int i = 0; i < patterns.length; i++ ) | |||
| { | |||
| addExclude( patterns[ i ] ); | |||
| } | |||
| } | |||
| /** | |||
| * Sets the set of include patterns. Patterns may be separated by a comma or | |||
| * a space. | |||
| * | |||
| * @param includes the string containing the include patterns | |||
| */ | |||
| public void setIncludes( final String includes ) | |||
| { | |||
| final Pattern[] patterns = parsePatterns( includes ); | |||
| for( int i = 0; i < patterns.length; i++ ) | |||
| { | |||
| addInclude( patterns[ i ] ); | |||
| } | |||
| } | |||
| /** | |||
| * add a name entry on the exclude list | |||
| */ | |||
| public void addExclude( final Pattern pattern ) | |||
| { | |||
| m_excludes.add( pattern ); | |||
| } | |||
| /** | |||
| * add a name entry on the include list | |||
| */ | |||
| public void addInclude( final Pattern pattern ) | |||
| { | |||
| m_includes.add( pattern ); | |||
| } | |||
| public final ArrayList getIncludes() | |||
| { | |||
| return m_includes; | |||
| } | |||
| public final ArrayList getExcludes() | |||
| { | |||
| return m_excludes; | |||
| } | |||
| /** | |||
| * Adds the patterns of the other instance to this set. | |||
| */ | |||
| public void append( final PatternSet other ) | |||
| { | |||
| m_includes.addAll( other.m_includes ); | |||
| m_excludes.addAll( other.m_excludes ); | |||
| } | |||
| public String toString() | |||
| { | |||
| return "PatternSet [ includes: " + m_includes + | |||
| " excludes: " + m_excludes + " ]"; | |||
| } | |||
| private Pattern[] parsePatterns( final String patternString ) | |||
| { | |||
| final ArrayList patterns = new ArrayList(); | |||
| if( patternString != null && patternString.length() > 0 ) | |||
| { | |||
| StringTokenizer tok = new StringTokenizer( patternString, ", ", false ); | |||
| while( tok.hasMoreTokens() ) | |||
| { | |||
| final Pattern pattern = new Pattern( tok.nextToken() ); | |||
| patterns.add( pattern ); | |||
| } | |||
| } | |||
| return (Pattern[])patterns.toArray( new Pattern[ patterns.size() ] ); | |||
| } | |||
| } | |||
| @@ -1,64 +0,0 @@ | |||
| /* | |||
| * 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.myrmidon.framework; | |||
| import java.util.ArrayList; | |||
| import java.util.Iterator; | |||
| import org.apache.myrmidon.api.TaskContext; | |||
| import org.apache.myrmidon.api.TaskException; | |||
| /** | |||
| * | |||
| * | |||
| * @author <a href="mailto:peter@apache.org">Peter Donald</a> | |||
| * @version $Revision$ $Date$ | |||
| */ | |||
| public class PatternUtil | |||
| { | |||
| /** | |||
| * Returns the filtered include patterns. | |||
| */ | |||
| public static String[] getIncludePatterns( final PatternSet set, | |||
| final TaskContext context ) | |||
| throws TaskException | |||
| { | |||
| return toArray( set.getIncludes(), context ); | |||
| } | |||
| public static String[] getExcludePatterns( final PatternSet set, | |||
| final TaskContext context ) | |||
| throws TaskException | |||
| { | |||
| return toArray( set.getExcludes(), context ); | |||
| } | |||
| /** | |||
| * Convert a vector of Pattern elements into an array of Strings. | |||
| */ | |||
| private static String[] toArray( final ArrayList list, final TaskContext context ) | |||
| { | |||
| if( list.size() == 0 ) | |||
| { | |||
| return null; | |||
| } | |||
| final ArrayList names = new ArrayList(); | |||
| final Iterator patterns = list.iterator(); | |||
| while( patterns.hasNext() ) | |||
| { | |||
| final Pattern pattern = (Pattern)patterns.next(); | |||
| final String result = pattern.evaluateName( context ); | |||
| if( null != result && result.length() > 0 ) | |||
| { | |||
| names.add( result ); | |||
| } | |||
| } | |||
| return (String[])names.toArray( new String[ names.size() ] ); | |||
| } | |||
| } | |||
| @@ -1,20 +0,0 @@ | |||
| container.null-value.error=Value ({0}) resolved to null. | |||
| container.bad-resolve.error=Error resolving value ({0}). | |||
| container.bad-config.error=Error converting value. | |||
| container.no-factory.error=Could not locate the type factory for role "{0}". | |||
| container.no-create-type.error=Could not create an instance of role "{0}" with type name "{1}". | |||
| container.no-create-type-for-type.error=Could not create an instance of class "{0}" with type name "{1}". | |||
| container.unknown-role-type.error=Could not determine the role for class "{0}". | |||
| typedef.no-lib.error=Must specify the lib parameter. | |||
| condition.no-resolve.error=Error resolving {0}. | |||
| pattern.ifelse-duplicate.error=Can only set one of if/else for pattern data type. | |||
| type.no-create.error=Unable to create datatype. | |||
| type.no-id.error=Id must be specified. | |||
| unknown-family=Don't know how to detect os family "{0}" | |||
| facade.missing-impl.error=Unable to determine the name of implementation for facade task "{0}". | |||
| @@ -1,32 +0,0 @@ | |||
| /* | |||
| * 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.myrmidon.framework; | |||
| import java.util.ArrayList; | |||
| import org.apache.avalon.framework.configuration.Configuration; | |||
| /** | |||
| * This object contains an ordered list of tasks. | |||
| * | |||
| * @author <a href="mailto:peter@apache.org">Peter Donald</a> | |||
| * @version $Revision$ $Date$ | |||
| */ | |||
| public class TaskList | |||
| { | |||
| private ArrayList m_tasks = new ArrayList(); | |||
| public void add( final Configuration task ) | |||
| { | |||
| m_tasks.add( task ); | |||
| } | |||
| public Configuration[] getTasks() | |||
| { | |||
| return (Configuration[])m_tasks.toArray( new Configuration[ m_tasks.size() ] ); | |||
| } | |||
| } | |||
| @@ -1,91 +0,0 @@ | |||
| /* | |||
| * 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.myrmidon.framework; | |||
| import org.apache.avalon.excalibur.i18n.ResourceManager; | |||
| import org.apache.avalon.excalibur.i18n.Resources; | |||
| import org.apache.avalon.framework.configuration.Configurable; | |||
| import org.apache.avalon.framework.configuration.Configuration; | |||
| import org.apache.avalon.framework.configuration.ConfigurationException; | |||
| import org.apache.avalon.framework.configuration.DefaultConfiguration; | |||
| import org.apache.myrmidon.api.TaskException; | |||
| /** | |||
| * This is the property "task" to declare a binding of a datatype to a name. | |||
| * | |||
| * @author <a href="mailto:peter@apache.org">Peter Donald</a> | |||
| * @version $Revision$ $Date$ | |||
| */ | |||
| public class TypeInstanceTask | |||
| extends AbstractContainerTask | |||
| implements Configurable | |||
| { | |||
| private static final Resources REZ = | |||
| ResourceManager.getPackageResources( TypeInstanceTask.class ); | |||
| private String m_id; | |||
| private Object m_value; | |||
| public void configure( final Configuration configuration ) | |||
| throws ConfigurationException | |||
| { | |||
| final DefaultConfiguration newConfiguration = | |||
| new DefaultConfiguration( configuration.getName(), configuration.getLocation() ); | |||
| final String[] attributes = configuration.getAttributeNames(); | |||
| for( int i = 0; i < attributes.length; i++ ) | |||
| { | |||
| final String name = attributes[ i ]; | |||
| final String value = configuration.getAttribute( name ); | |||
| if( name.equals( "id" ) || name.equals( "local-scope" ) ) | |||
| { | |||
| configureAttribute( this, name, value ); | |||
| } | |||
| else | |||
| { | |||
| newConfiguration.setAttribute( name, value ); | |||
| } | |||
| } | |||
| final Configuration[] children = configuration.getChildren(); | |||
| for( int i = 0; i < children.length; i++ ) | |||
| { | |||
| newConfiguration.addChild( children[ i ] ); | |||
| } | |||
| try | |||
| { | |||
| m_value = newInstance( DataType.ROLE, configuration.getName() ); | |||
| } | |||
| catch( final Exception e ) | |||
| { | |||
| final String message = REZ.getString( "type.no-create.error" ); | |||
| throw new ConfigurationException( message, e ); | |||
| } | |||
| configureElement( m_value, newConfiguration ); | |||
| } | |||
| public void setId( final String id ) | |||
| { | |||
| m_id = id; | |||
| } | |||
| public void execute() | |||
| throws TaskException | |||
| { | |||
| if( null == m_id ) | |||
| { | |||
| final String message = REZ.getString( "type.no-id.error" ); | |||
| throw new TaskException( message ); | |||
| } | |||
| getContext().setProperty( m_id, m_value ); | |||
| } | |||
| } | |||
| @@ -1,56 +0,0 @@ | |||
| /* | |||
| * 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.myrmidon.framework.conditions; | |||
| import java.util.ArrayList; | |||
| import org.apache.myrmidon.api.TaskContext; | |||
| import org.apache.myrmidon.api.TaskException; | |||
| /** | |||
| * <and> condition container. <p> | |||
| * | |||
| * Iterates over all conditions and returns false as soon as one evaluates to | |||
| * false. An empty and condition returns true.</p> | |||
| * | |||
| * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a> | |||
| * @version $Revision$ | |||
| * | |||
| * @ant.type type="condition" name="and" | |||
| */ | |||
| public class AndCondition | |||
| implements Condition | |||
| { | |||
| private final ArrayList m_conditions = new ArrayList(); | |||
| /** | |||
| * Adds a condition. | |||
| */ | |||
| public void add( final Condition condition ) | |||
| { | |||
| m_conditions.add( condition ); | |||
| } | |||
| /** | |||
| * Evaluates the condition. | |||
| * | |||
| */ | |||
| public boolean evaluate( final TaskContext context ) | |||
| throws TaskException | |||
| { | |||
| final int count = m_conditions.size(); | |||
| for( int i = 0; i < count; i++ ) | |||
| { | |||
| final Condition condition = (Condition)m_conditions.get( i ); | |||
| if( !condition.evaluate( context ) ) | |||
| { | |||
| return false; | |||
| } | |||
| } | |||
| return true; | |||
| } | |||
| } | |||
| @@ -1,32 +0,0 @@ | |||
| /* | |||
| * 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.myrmidon.framework.conditions; | |||
| import org.apache.myrmidon.api.TaskContext; | |||
| import org.apache.myrmidon.api.TaskException; | |||
| /** | |||
| * Class representing a condition. | |||
| * | |||
| * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a> | |||
| * @author <a href="mailto:peter@apache.org">Peter Donald</a> | |||
| * @version $Revision$ $Date$ | |||
| * | |||
| * @ant:role shorthand="condition" | |||
| */ | |||
| public interface Condition | |||
| { | |||
| /** | |||
| * Evaluates this condition. | |||
| * | |||
| * @param context | |||
| * The context to evaluate the condition in. | |||
| */ | |||
| boolean evaluate( final TaskContext context ) | |||
| throws TaskException; | |||
| } | |||
| @@ -1,66 +0,0 @@ | |||
| /* | |||
| * 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.myrmidon.framework.conditions; | |||
| import org.apache.avalon.excalibur.i18n.ResourceManager; | |||
| import org.apache.avalon.excalibur.i18n.Resources; | |||
| import org.apache.myrmidon.api.TaskContext; | |||
| import org.apache.myrmidon.api.TaskException; | |||
| /** | |||
| * A {@link Condition} that is true when a property is set. | |||
| * | |||
| * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a> | |||
| * @author <a href="mailto:peter@apache.org">Peter Donald</a> | |||
| * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a> | |||
| * @version $Revision$ $Date$ | |||
| * | |||
| * @ant.type type="condition" name="is-set" | |||
| */ | |||
| public class IsSetCondition | |||
| implements Condition | |||
| { | |||
| private static final Resources REZ = | |||
| ResourceManager.getPackageResources( IsSetCondition.class ); | |||
| private String m_property; | |||
| public IsSetCondition( final String propName ) | |||
| { | |||
| m_property = propName; | |||
| } | |||
| public IsSetCondition() | |||
| { | |||
| } | |||
| /** | |||
| * Set the property name to test. | |||
| */ | |||
| public void setProperty( final String propName ) | |||
| { | |||
| m_property = propName; | |||
| } | |||
| /** | |||
| * Evaluates the condition. | |||
| */ | |||
| public boolean evaluate( final TaskContext context ) | |||
| throws TaskException | |||
| { | |||
| if( m_property == null ) | |||
| { | |||
| final String message = REZ.getString( "isset.no-property.error" ); | |||
| throw new TaskException( message ); | |||
| } | |||
| // Resolve the condition | |||
| final Object object = context.getProperty( m_property ); | |||
| return ( object != null ); | |||
| } | |||
| } | |||
| @@ -1,84 +0,0 @@ | |||
| /* | |||
| * 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.myrmidon.framework.conditions; | |||
| import org.apache.avalon.excalibur.i18n.ResourceManager; | |||
| import org.apache.avalon.excalibur.i18n.Resources; | |||
| import org.apache.myrmidon.api.TaskContext; | |||
| import org.apache.myrmidon.api.TaskException; | |||
| import org.apache.aut.converter.Converter; | |||
| import org.apache.aut.converter.ConverterException; | |||
| /** | |||
| * A {@link Condition} that is true when a property is set, but not set to | |||
| * 'false'. | |||
| * | |||
| * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a> | |||
| * @author <a href="mailto:peter@apache.org">Peter Donald</a> | |||
| * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a> | |||
| * @version $Revision$ $Date$ | |||
| * | |||
| * @ant.type type="condition" name="is-true" | |||
| */ | |||
| public class IsTrueCondition | |||
| implements Condition | |||
| { | |||
| private static final Resources REZ = | |||
| ResourceManager.getPackageResources( IsTrueCondition.class ); | |||
| private String m_property; | |||
| public IsTrueCondition( final String propName ) | |||
| { | |||
| m_property = propName; | |||
| } | |||
| public IsTrueCondition() | |||
| { | |||
| } | |||
| /** | |||
| * Set the property name to test. | |||
| */ | |||
| public void setProperty( final String propName ) | |||
| { | |||
| m_property = propName; | |||
| } | |||
| /** | |||
| * Evaluates the condition. | |||
| */ | |||
| public boolean evaluate( final TaskContext context ) | |||
| throws TaskException | |||
| { | |||
| if( m_property == null ) | |||
| { | |||
| final String message = REZ.getString( "isset.no-property.error" ); | |||
| throw new TaskException( message ); | |||
| } | |||
| // Resolve the property name | |||
| final Object object = context.getProperty( m_property ); | |||
| if( object == null ) | |||
| { | |||
| return false; | |||
| } | |||
| // Convert value to boolean | |||
| try | |||
| { | |||
| final Converter converter = (Converter)context.getService( Converter.class ); | |||
| final Boolean value = (Boolean)converter.convert( Boolean.class, object, context ); | |||
| return value.booleanValue(); | |||
| } | |||
| catch( final ConverterException e ) | |||
| { | |||
| throw new TaskException( e.getMessage(), e ); | |||
| } | |||
| } | |||
| } | |||
| @@ -1,69 +0,0 @@ | |||
| /* | |||
| * 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.myrmidon.framework.conditions; | |||
| import org.apache.myrmidon.api.TaskContext; | |||
| import org.apache.myrmidon.api.TaskException; | |||
| import org.apache.avalon.excalibur.i18n.ResourceManager; | |||
| import org.apache.avalon.excalibur.i18n.Resources; | |||
| /** | |||
| * <not> condition. Evaluates to true if the single condition nested into | |||
| * it is false and vice versa. | |||
| * | |||
| * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a> | |||
| * @version $Revision$ | |||
| * | |||
| * @ant.type type="condition" name="not" | |||
| */ | |||
| public class NotCondition | |||
| implements Condition | |||
| { | |||
| private static final Resources REZ = | |||
| ResourceManager.getPackageResources( NotCondition.class ); | |||
| private Condition m_condition; | |||
| public NotCondition() | |||
| { | |||
| } | |||
| public NotCondition( final Condition condition ) | |||
| { | |||
| m_condition = condition; | |||
| } | |||
| /** | |||
| * Adds a nested condition. | |||
| */ | |||
| public void add( final Condition condition ) | |||
| throws TaskException | |||
| { | |||
| if( m_condition != null ) | |||
| { | |||
| final String message = REZ.getString( "not.too-many-conditions.error" ); | |||
| throw new TaskException( message ); | |||
| } | |||
| m_condition = condition; | |||
| } | |||
| /** | |||
| * Evaluates the condition. | |||
| */ | |||
| public boolean evaluate( final TaskContext context ) | |||
| throws TaskException | |||
| { | |||
| if( m_condition == null ) | |||
| { | |||
| final String message = REZ.getString( "not.no-condition.error" ); | |||
| throw new TaskException( message ); | |||
| } | |||
| return ! m_condition.evaluate( context ); | |||
| } | |||
| } | |||
| @@ -1,56 +0,0 @@ | |||
| /* | |||
| * 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.myrmidon.framework.conditions; | |||
| import java.util.ArrayList; | |||
| import org.apache.myrmidon.api.TaskContext; | |||
| import org.apache.myrmidon.api.TaskException; | |||
| /** | |||
| * <or> condition container. <p> | |||
| * | |||
| * Iterates over all conditions and returns true as soon as one evaluates to | |||
| * true. An empty container evaluates to true</p> | |||
| * | |||
| * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a> | |||
| * @version $Revision$ | |||
| * | |||
| * @ant.type type="condition" name="or" | |||
| */ | |||
| public class OrCondition | |||
| implements Condition | |||
| { | |||
| private final ArrayList m_conditions = new ArrayList(); | |||
| /** | |||
| * Adds a condition. | |||
| */ | |||
| public void add( final Condition condition ) | |||
| { | |||
| m_conditions.add( condition ); | |||
| } | |||
| /** | |||
| * Evaluates the condition. | |||
| * | |||
| */ | |||
| public boolean evaluate( final TaskContext context ) | |||
| throws TaskException | |||
| { | |||
| final int count = m_conditions.size(); | |||
| for( int i = 0; i < count; i++ ) | |||
| { | |||
| final Condition condition = (Condition)m_conditions.get( i ); | |||
| if( condition.evaluate( context ) ) | |||
| { | |||
| return true; | |||
| } | |||
| } | |||
| return (count == 0); | |||
| } | |||
| } | |||
| @@ -1,4 +0,0 @@ | |||
| isset.no-property.error=No property specified to test. | |||
| istrue.no-property.error=No property specified to test. | |||
| not.no-condition.error=No condition specified. | |||
| not.too-many-conditions.error=Too many conditions specified. | |||
| @@ -1,49 +0,0 @@ | |||
| /* | |||
| * 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.myrmidon.framework.factories; | |||
| import java.io.File; | |||
| import org.apache.aut.nativelib.impl.DefaultExecManager; | |||
| import org.apache.avalon.framework.context.Context; | |||
| import org.apache.avalon.framework.context.ContextException; | |||
| import org.apache.avalon.framework.context.Contextualizable; | |||
| import org.apache.myrmidon.interfaces.service.AntServiceException; | |||
| import org.apache.myrmidon.interfaces.service.ServiceFactory; | |||
| /** | |||
| * A Factory responsible for creating the ExecManager service. | |||
| * | |||
| * @author <a href="mailto:peter@apache.org">Peter Donald</a> | |||
| * @version $Revision$ $Date$ | |||
| */ | |||
| public class ExecManagerFactory | |||
| implements ServiceFactory, Contextualizable | |||
| { | |||
| private File m_homeDir; | |||
| public void contextualize( final Context context ) throws ContextException | |||
| { | |||
| m_homeDir = (File)context.get( "myrmidon.home" ); | |||
| } | |||
| /** | |||
| * Create the ExecManager Service. | |||
| */ | |||
| public Object createService() | |||
| throws AntServiceException | |||
| { | |||
| try | |||
| { | |||
| return new DefaultExecManager( m_homeDir ); | |||
| } | |||
| catch( final Exception ee ) | |||
| { | |||
| throw new AntServiceException( ee.getMessage(), ee ); | |||
| } | |||
| } | |||
| } | |||
| @@ -1,2 +0,0 @@ | |||
| missing-home-dir.error=Cannot locate antRun scripts: Property 'myrmidon.home' not specified | |||
| create-provider.error=Could not create file system provider "{0}". | |||
| @@ -1,93 +0,0 @@ | |||
| /* | |||
| * 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.myrmidon.framework.factories; | |||
| import org.apache.aut.vfs.FileSystemException; | |||
| import org.apache.aut.vfs.impl.DefaultFileSystemManager; | |||
| import org.apache.aut.vfs.provider.FileSystemProvider; | |||
| import org.apache.avalon.excalibur.i18n.ResourceManager; | |||
| import org.apache.avalon.excalibur.i18n.Resources; | |||
| import org.apache.avalon.framework.activity.Initializable; | |||
| import org.apache.avalon.framework.service.ServiceException; | |||
| import org.apache.avalon.framework.service.ServiceManager; | |||
| import org.apache.avalon.framework.service.Serviceable; | |||
| import org.apache.myrmidon.interfaces.type.TypeFactory; | |||
| import org.apache.myrmidon.interfaces.type.TypeManager; | |||
| /** | |||
| * The myrmidon FileSystemManager implementation. | |||
| * | |||
| * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a> | |||
| * @version $Revision$ $Date$ | |||
| */ | |||
| public class VfsManager | |||
| extends DefaultFileSystemManager | |||
| implements Serviceable, Initializable | |||
| { | |||
| private static final Resources REZ | |||
| = ResourceManager.getPackageResources( VfsManager.class ); | |||
| private TypeManager m_typeManager; | |||
| /** | |||
| * Locate the services used by this service. | |||
| */ | |||
| public void service( final ServiceManager serviceManager ) throws ServiceException | |||
| { | |||
| m_typeManager = (TypeManager)serviceManager.lookup( TypeManager.ROLE ); | |||
| } | |||
| /** | |||
| * Initialises this service. | |||
| */ | |||
| public void initialize() throws Exception | |||
| { | |||
| final TypeFactory factory = m_typeManager.getFactory( FileSystemProvider.ROLE ); | |||
| // TODO - make this list configurable | |||
| // Required providers | |||
| addProvider( factory, new String[]{"file"}, "file", false ); | |||
| addProvider( factory, new String[]{"zip", "jar"}, "zip", false ); | |||
| // Optional providers | |||
| addProvider( factory, new String[]{"smb"}, "smb", true ); | |||
| addProvider( factory, new String[]{"ftp"}, "ftp", true ); | |||
| } | |||
| /** | |||
| * Registers a file system provider. | |||
| */ | |||
| private void addProvider( final TypeFactory factory, | |||
| final String[] urlSchemes, | |||
| final String providerName, | |||
| final boolean ignoreIfNotPresent ) | |||
| throws FileSystemException | |||
| { | |||
| // Create an instance | |||
| if( ignoreIfNotPresent && !factory.canCreate( providerName ) ) | |||
| { | |||
| return; | |||
| } | |||
| final FileSystemProvider provider; | |||
| try | |||
| { | |||
| provider = (FileSystemProvider)factory.create( providerName ); | |||
| } | |||
| catch( Exception e ) | |||
| { | |||
| final String message = REZ.getString( "create-provider.error", providerName ); | |||
| throw new FileSystemException( message, e ); | |||
| } | |||
| // Register the provider | |||
| addProvider( urlSchemes, provider ); | |||
| } | |||
| } | |||
| @@ -1,36 +0,0 @@ | |||
| /* | |||
| * 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.myrmidon.framework.factories; | |||
| import org.apache.aut.vfs.FileSystemManager; | |||
| import org.apache.avalon.excalibur.i18n.ResourceManager; | |||
| import org.apache.avalon.excalibur.i18n.Resources; | |||
| import org.apache.myrmidon.interfaces.service.AntServiceException; | |||
| import org.apache.myrmidon.interfaces.service.ServiceFactory; | |||
| /** | |||
| * A factory that creates the {@link FileSystemManager} service. | |||
| * | |||
| * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a> | |||
| * @version $Revision$ $Date$ | |||
| */ | |||
| public class VfsManagerFactory | |||
| implements ServiceFactory | |||
| { | |||
| private static final Resources REZ | |||
| = ResourceManager.getPackageResources( VfsManagerFactory.class ); | |||
| /** | |||
| * Create a service that coresponds to this factory. | |||
| */ | |||
| public Object createService() | |||
| throws AntServiceException | |||
| { | |||
| return new VfsManager(); | |||
| } | |||
| } | |||
| @@ -1,39 +0,0 @@ | |||
| /* | |||
| * 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.myrmidon.framework.file; | |||
| import org.apache.myrmidon.api.TaskContext; | |||
| import org.apache.myrmidon.api.TaskException; | |||
| /** | |||
| * A PathElement made up of an array of strings. | |||
| * | |||
| * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a> | |||
| * @version $Revision$ $Date$ | |||
| */ | |||
| public class ArrayFileList | |||
| implements FileList | |||
| { | |||
| private final String[] m_parts; | |||
| public ArrayFileList( final String part ) | |||
| { | |||
| m_parts = new String[] { part } ; | |||
| } | |||
| public ArrayFileList( final String[] parts ) | |||
| { | |||
| m_parts = parts; | |||
| } | |||
| public String[] listFiles( final TaskContext context ) | |||
| throws TaskException | |||
| { | |||
| return m_parts; | |||
| } | |||
| } | |||
| @@ -1,31 +0,0 @@ | |||
| /* | |||
| * 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.myrmidon.framework.file; | |||
| import org.apache.myrmidon.api.TaskContext; | |||
| import org.apache.myrmidon.api.TaskException; | |||
| /** | |||
| * A list of files. | |||
| * | |||
| * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a> | |||
| * @version $Revision$ $Date$ | |||
| * | |||
| * @ant:role shorthand="path" | |||
| */ | |||
| public interface FileList | |||
| { | |||
| /** | |||
| * Returns the files in this list. | |||
| * | |||
| * @param context the context to use to evaluate the list. | |||
| * @return The names of the files in this list. All names are absolute paths. | |||
| */ | |||
| String[] listFiles( TaskContext context ) | |||
| throws TaskException; | |||
| } | |||
| @@ -1,48 +0,0 @@ | |||
| /* | |||
| * 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.myrmidon.framework.file; | |||
| import org.apache.aut.converter.AbstractConverter; | |||
| import org.apache.aut.converter.ConverterException; | |||
| import org.apache.aut.nativelib.PathUtil; | |||
| import org.apache.myrmidon.api.TaskContext; | |||
| import org.apache.myrmidon.api.TaskException; | |||
| import org.apache.myrmidon.framework.file.FileList; | |||
| /** | |||
| * Converters from FileList to String. | |||
| * | |||
| * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a> | |||
| * @version $Revision$ $Date$ | |||
| * | |||
| * @ant.converter source="org.apache.myrmidon.framework.file.FileList" destination="java.lang.String" | |||
| */ | |||
| public class FileListToStringConverter | |||
| extends AbstractConverter | |||
| { | |||
| public FileListToStringConverter() | |||
| { | |||
| super( FileList.class, String.class ); | |||
| } | |||
| protected Object convert( final Object original, final Object context ) | |||
| throws ConverterException | |||
| { | |||
| try | |||
| { | |||
| final TaskContext taskContext = (TaskContext)context; | |||
| final FileList fileList = (FileList)original; | |||
| final String[] files = fileList.listFiles( taskContext ); | |||
| return PathUtil.formatPath( files ); | |||
| } | |||
| catch( final TaskException e ) | |||
| { | |||
| throw new ConverterException( e.getMessage(), e ); | |||
| } | |||
| } | |||
| } | |||
| @@ -1,102 +0,0 @@ | |||
| /* | |||
| * 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.myrmidon.framework.file; | |||
| import org.apache.myrmidon.api.TaskContext; | |||
| import org.apache.myrmidon.api.TaskException; | |||
| import org.apache.myrmidon.interfaces.classloader.ClassLoaderManager; | |||
| import org.apache.myrmidon.interfaces.classloader.ClassLoaderException; | |||
| import org.apache.aut.nativelib.PathUtil; | |||
| import java.io.File; | |||
| import java.io.IOException; | |||
| import java.net.URL; | |||
| /** | |||
| * Utility methods for dealing with {@link FileList} objects. | |||
| * | |||
| * @author <a href="mailto:peter@apache.org">Peter Donald</a> | |||
| * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a> | |||
| * @version $Revision$ $Date$ | |||
| */ | |||
| public final class FileListUtil | |||
| { | |||
| private FileListUtil() | |||
| { | |||
| } | |||
| /** | |||
| * Formats a path into its native representation. | |||
| */ | |||
| public static String formatPath( final FileList path, final TaskContext context ) | |||
| throws TaskException | |||
| { | |||
| final String[] list = path.listFiles( context ); | |||
| return PathUtil.formatPath( list ); | |||
| } | |||
| /** | |||
| * Converts a path into an array of Files. | |||
| */ | |||
| public static File[] toFiles( final FileList path, final TaskContext context ) | |||
| throws TaskException | |||
| { | |||
| final String[] list = path.listFiles( context ); | |||
| final File[] result = new File[ list.length ]; | |||
| for( int i = 0; i < list.length; i++ ) | |||
| { | |||
| result[ i ] = new File( list[ i ] ); | |||
| } | |||
| return result; | |||
| } | |||
| /** | |||
| * Converts a path into an array of URLs - useful for building a ClassLoader. | |||
| */ | |||
| public static URL[] toURLs( final FileList path, final TaskContext context ) | |||
| throws TaskException | |||
| { | |||
| try | |||
| { | |||
| final String[] list = path.listFiles( context ); | |||
| final URL[] result = new URL[ list.length ]; | |||
| // path containing one or more elements | |||
| for( int i = 0; i < list.length; i++ ) | |||
| { | |||
| result[ i ] = new File( list[ i ] ).toURL(); | |||
| } | |||
| return result; | |||
| } | |||
| catch( final IOException ioe ) | |||
| { | |||
| throw new TaskException( "Malformed path entry.", ioe ); | |||
| } | |||
| } | |||
| /** | |||
| * Creates a ClassLoader from a class-path. | |||
| */ | |||
| public static ClassLoader createClassLoader( final FileList classpath, | |||
| final TaskContext context ) | |||
| throws TaskException | |||
| { | |||
| final File[] files = toFiles( classpath, context ); | |||
| final ClassLoaderManager manager = (ClassLoaderManager)context.getService( ClassLoaderManager.class ); | |||
| try | |||
| { | |||
| return manager.createClassLoader( files ); | |||
| } | |||
| catch( final ClassLoaderException e ) | |||
| { | |||
| throw new TaskException( e.getMessage(), e ); | |||
| } | |||
| } | |||
| } | |||
| @@ -1,36 +0,0 @@ | |||
| /* | |||
| * 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.myrmidon.framework.file; | |||
| import org.apache.myrmidon.api.TaskContext; | |||
| import org.apache.myrmidon.api.TaskException; | |||
| import org.apache.myrmidon.framework.file.FileList; | |||
| import org.apache.tools.todo.util.FileUtils; | |||
| /** | |||
| * A PathElement that is parsed from a string. | |||
| * | |||
| * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a> | |||
| * @version $Revision$ $Date$ | |||
| */ | |||
| public class ParsedPathElement | |||
| implements FileList | |||
| { | |||
| private final String m_path; | |||
| public ParsedPathElement( final String path ) | |||
| { | |||
| m_path = path; | |||
| } | |||
| public String[] listFiles( final TaskContext context ) | |||
| throws TaskException | |||
| { | |||
| return FileUtils.translatePath( context.getBaseDirectory(), m_path ); | |||
| } | |||
| } | |||
| @@ -1,178 +0,0 @@ | |||
| /* | |||
| * 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.myrmidon.framework.file; | |||
| import java.io.File; | |||
| import java.util.ArrayList; | |||
| import org.apache.myrmidon.api.TaskContext; | |||
| import org.apache.myrmidon.api.TaskException; | |||
| import org.apache.myrmidon.framework.DataType; | |||
| import org.apache.myrmidon.framework.FileSet; | |||
| import org.apache.myrmidon.framework.file.ArrayFileList; | |||
| import org.apache.myrmidon.framework.file.FileList; | |||
| import org.apache.tools.todo.util.FileUtils; | |||
| import org.apache.tools.todo.types.DirectoryScanner; | |||
| import org.apache.tools.todo.types.ScannerUtil; | |||
| /** | |||
| * This object represents a path as used by CLASSPATH or PATH environment | |||
| * variable. <p> | |||
| * | |||
| * <code> | |||
| * <sometask><br> | |||
| * <somepath><br> | |||
| * <pathelement location="/path/to/file.jar" /> | |||
| * <br> | |||
| * <pathelement | |||
| * path="/path/to/file2.jar:/path/to/class2;/path/to/class3" /><br> | |||
| * <pathelement location="/path/to/file3.jar" /> | |||
| * <br> | |||
| * <pathelement location="/path/to/file4.jar" /> | |||
| * <br> | |||
| * </somepath><br> | |||
| * </sometask><br> | |||
| * </code> <p> | |||
| * | |||
| * The object implemention <code>sometask</code> must provide a method called | |||
| * <code>createSomepath</code> which returns an instance of <code>Path</code>. | |||
| * Nested path definitions are handled by the Path object and must be labeled | |||
| * <code>pathelement</code>.<p> | |||
| * | |||
| * The path element takes a parameter <code>path</code> which will be parsed and | |||
| * split into single elements. It will usually be used to define a path from an | |||
| * environment variable. | |||
| * | |||
| * @author Thomas.Haas@softwired-inc.com | |||
| * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a> | |||
| * | |||
| * @ant.data-type name="path" | |||
| */ | |||
| public class Path | |||
| implements DataType, FileList | |||
| { | |||
| private final ArrayList m_elements = new ArrayList(); | |||
| public Path( final String path ) | |||
| { | |||
| add( path ); | |||
| } | |||
| public Path() | |||
| { | |||
| } | |||
| /** | |||
| * Adds a String to the ArrayList if it isn't already included. | |||
| */ | |||
| private void addUnlessPresent( final ArrayList list, final String entry ) | |||
| { | |||
| if( !list.contains( entry ) ) | |||
| { | |||
| list.add( entry ); | |||
| } | |||
| } | |||
| /** | |||
| * Adds an element to the path. | |||
| */ | |||
| public void setLocation( final File location ) | |||
| { | |||
| addLocation( location ); | |||
| } | |||
| /** | |||
| * Adds a element definition to the path. | |||
| * | |||
| * @param location the location of the element to add (must not be <code>null</code> | |||
| * nor empty. | |||
| */ | |||
| public void addLocation( final File location ) | |||
| { | |||
| final FileList pathElement = new ArrayFileList( location.getAbsolutePath() ); | |||
| m_elements.add( pathElement ); | |||
| } | |||
| /** | |||
| * Adds a nested <code><fileset></code> element. | |||
| */ | |||
| public void addFileset( final FileSet fileSet ) | |||
| { | |||
| m_elements.add( fileSet ); | |||
| } | |||
| /** | |||
| * Adds a path. | |||
| */ | |||
| public void setPath( final String path ) | |||
| { | |||
| add( path ); | |||
| } | |||
| /** | |||
| * Adds a path. | |||
| */ | |||
| public void add( final String path ) | |||
| { | |||
| final FileList pathElement = new ParsedPathElement( path ); | |||
| m_elements.add( pathElement ); | |||
| } | |||
| /** | |||
| * Adds a path. | |||
| */ | |||
| public void add( final String[] path ) | |||
| { | |||
| final FileList pathElement = new ArrayFileList( path ); | |||
| m_elements.add( pathElement ); | |||
| } | |||
| /** | |||
| * Adds a path. | |||
| */ | |||
| public void add( final FileList list ) | |||
| { | |||
| m_elements.add( list ); | |||
| } | |||
| /** | |||
| * Returns all path elements defined by this and nested path objects. | |||
| * The paths returned by this method are absolute. | |||
| */ | |||
| public String[] listFiles( final TaskContext context ) | |||
| throws TaskException | |||
| { | |||
| ArrayList result = new ArrayList( 2 * m_elements.size() ); | |||
| for( int i = 0; i < m_elements.size(); i++ ) | |||
| { | |||
| Object o = m_elements.get( i ); | |||
| if( o instanceof FileList ) | |||
| { | |||
| final FileList element = (FileList)o; | |||
| final String[] parts = element.listFiles( context ); | |||
| for( int j = 0; j < parts.length; j++ ) | |||
| { | |||
| addUnlessPresent( result, parts[ j ] ); | |||
| } | |||
| } | |||
| else if( o instanceof FileSet ) | |||
| { | |||
| final FileSet fs = (FileSet)o; | |||
| final DirectoryScanner ds = ScannerUtil.getDirectoryScanner( fs ); | |||
| final String[] s = ds.getIncludedFiles(); | |||
| final File dir = fs.getDir(); | |||
| for( int j = 0; j < s.length; j++ ) | |||
| { | |||
| File f = new File( dir, s[ j ] ); | |||
| String absolutePath = f.getAbsolutePath(); | |||
| addUnlessPresent( result, FileUtils.translateFile( absolutePath ) ); | |||
| } | |||
| } | |||
| } | |||
| return (String[])result.toArray( new String[ result.size() ] ); | |||
| } | |||
| } | |||
| @@ -1,48 +0,0 @@ | |||
| /* | |||
| * 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.myrmidon.framework.file; | |||
| import org.apache.aut.converter.AbstractConverter; | |||
| import org.apache.aut.converter.ConverterException; | |||
| import org.apache.myrmidon.framework.file.Path; | |||
| /** | |||
| * A converter from String to Path. | |||
| * | |||
| * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a> | |||
| * @version $Revision$ $Date$ | |||
| * | |||
| * @ant.converter source="java.lang.String" destination="org.apache.myrmidon.framework.file.Path" | |||
| */ | |||
| public class StringToPathConverter | |||
| extends AbstractConverter | |||
| { | |||
| /** | |||
| * Constructors a converter. | |||
| */ | |||
| public StringToPathConverter() | |||
| { | |||
| super( String.class, Path.class ); | |||
| } | |||
| /** | |||
| * Converts from String to Path | |||
| * | |||
| * @param original the original Object | |||
| * @param context the context in which to convert | |||
| * @return the converted object | |||
| */ | |||
| protected Object convert( final Object original, final Object context ) | |||
| throws ConverterException | |||
| { | |||
| String path = (String)original; | |||
| Path retval = new Path( path ); | |||
| return retval; | |||
| } | |||
| } | |||
| @@ -1,31 +0,0 @@ | |||
| /* | |||
| * 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.myrmidon.framework.filters; | |||
| import org.apache.myrmidon.api.TaskContext; | |||
| import org.apache.myrmidon.api.TaskException; | |||
| /** | |||
| * Filters lines of text. | |||
| * | |||
| * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a> | |||
| * @version $Revision$ $Date$ | |||
| * | |||
| * @ant:role shorthand="line-filter" | |||
| */ | |||
| public interface LineFilter | |||
| { | |||
| /** | |||
| * Filters a line of text. | |||
| * | |||
| * @param line the text to filter. | |||
| * @param context the context to use when filtering. | |||
| */ | |||
| void filterLine( StringBuffer line, TaskContext context ) | |||
| throws TaskException; | |||
| } | |||
| @@ -1,48 +0,0 @@ | |||
| /* | |||
| * 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.myrmidon.framework.filters; | |||
| import java.util.ArrayList; | |||
| import org.apache.myrmidon.api.TaskContext; | |||
| import org.apache.myrmidon.api.TaskException; | |||
| /** | |||
| * A collection of line filters. | |||
| * | |||
| * @ant.data-type name="filterset" | |||
| * @ant.type type="line-filter" name="filterset" | |||
| */ | |||
| public class LineFilterSet | |||
| implements LineFilter | |||
| { | |||
| private ArrayList m_filterSets = new ArrayList(); | |||
| public void add( final LineFilter filter ) | |||
| { | |||
| m_filterSets.add( filter ); | |||
| } | |||
| /** | |||
| * Filters a line of text. | |||
| * | |||
| * @param line the text to filter. | |||
| * @param context the context to use when filtering. | |||
| */ | |||
| public void filterLine( final StringBuffer line, final TaskContext context ) | |||
| throws TaskException | |||
| { | |||
| final int count = m_filterSets.size(); | |||
| for( int i = 0; i < count; i++ ) | |||
| { | |||
| final LineFilter filter = (LineFilter)m_filterSets.get( i ); | |||
| filter.filterLine( line, context ); | |||
| } | |||
| } | |||
| } | |||
| @@ -1,138 +0,0 @@ | |||
| /* | |||
| * 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.myrmidon.framework.filters; | |||
| import java.util.ArrayList; | |||
| import org.apache.myrmidon.api.TaskContext; | |||
| import org.apache.myrmidon.api.TaskException; | |||
| /** | |||
| * A line filter that replaces tokens. May have begintoken and endtokens defined. | |||
| * | |||
| * @author <A href="mailto:gholam@xtra.co.nz"> Michael McCallum </A> | |||
| * | |||
| * @ant.type type="line-filter" name="token-filter" | |||
| */ | |||
| public class TokenLineFilter | |||
| implements LineFilter | |||
| { | |||
| /** | |||
| * The default token start string | |||
| */ | |||
| private static final char[] DEFAULT_TOKEN_START = {'@'}; | |||
| /** | |||
| * The default token end string | |||
| */ | |||
| private static final char[] DEFAULT_TOKEN_END = {'@'}; | |||
| /** | |||
| * List of ordered filters and filter files. | |||
| */ | |||
| private ArrayList m_tokenSets = new ArrayList(); | |||
| /** | |||
| * Adds a TokenSet to this filter. | |||
| */ | |||
| public void add( final TokenSet tokens ) | |||
| { | |||
| m_tokenSets.add( tokens ); | |||
| } | |||
| /** | |||
| * Filters a line of text. | |||
| * | |||
| * @param line the text to filter. | |||
| * @param context the context to use when filtering. | |||
| */ | |||
| public void filterLine( final StringBuffer line, final TaskContext context ) | |||
| throws TaskException | |||
| { | |||
| int index = 0; | |||
| while( index < line.length() ) | |||
| { | |||
| // Find the start of the next token | |||
| final int startToken = indexOf( line, DEFAULT_TOKEN_START, index ); | |||
| if( startToken == -1 ) | |||
| { | |||
| break; | |||
| } | |||
| final int startTokenName = startToken + DEFAULT_TOKEN_START.length; | |||
| // Find the end of the next token | |||
| int endTokenName = indexOf( line, DEFAULT_TOKEN_END, startTokenName ); | |||
| if( endTokenName == -1 ) | |||
| { | |||
| break; | |||
| } | |||
| int endToken = endTokenName + DEFAULT_TOKEN_END.length; | |||
| if( endTokenName == startTokenName ) | |||
| { | |||
| // Empty token name - skip | |||
| index = endToken; | |||
| continue; | |||
| } | |||
| // Extract token name figure out the value | |||
| final String token = line.substring( startTokenName, endTokenName ); | |||
| final String value = getTokenValue( token, context ); | |||
| if( value == null ) | |||
| { | |||
| // Unknown token - skip | |||
| index = endToken; | |||
| continue; | |||
| } | |||
| // Replace token with its value | |||
| line.delete( startToken, endToken ); | |||
| line.insert( startToken, value ); | |||
| index = startToken + value.length(); | |||
| } | |||
| } | |||
| /** | |||
| * Returns the value of a token. | |||
| */ | |||
| private String getTokenValue( final String token, final TaskContext context ) | |||
| throws TaskException | |||
| { | |||
| String value = null; | |||
| final int count = m_tokenSets.size(); | |||
| for( int i = 0; value == null && i < count; i++ ) | |||
| { | |||
| final TokenSet tokenSet = (TokenSet)m_tokenSets.get( i ); | |||
| value = tokenSet.getValue( token, context ); | |||
| } | |||
| return value; | |||
| } | |||
| /** | |||
| * Returns the location of a string in a stringbuffer. | |||
| */ | |||
| private int indexOf( final StringBuffer buffer, | |||
| final char[] str, | |||
| final int index ) | |||
| { | |||
| final int maxIndex = buffer.length() - str.length + 1; | |||
| outer: for( int i = index; i < maxIndex; i++ ) | |||
| { | |||
| for( int j = 0; j < str.length; j++ ) | |||
| { | |||
| if( buffer.charAt( i + j ) != str[ j ] ) | |||
| { | |||
| continue outer; | |||
| } | |||
| } | |||
| return i; | |||
| } | |||
| return -1; | |||
| } | |||
| } | |||
| @@ -1,32 +0,0 @@ | |||
| /* | |||
| * 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.myrmidon.framework.filters; | |||
| import org.apache.myrmidon.api.TaskContext; | |||
| import org.apache.myrmidon.api.TaskException; | |||
| /** | |||
| * A set of tokens. | |||
| * | |||
| * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a> | |||
| * @version $Revision$ $Date$ | |||
| * | |||
| * @ant:role shorthand="token-set" | |||
| */ | |||
| public interface TokenSet | |||
| { | |||
| /** | |||
| * Evaluates the value for a token. | |||
| * | |||
| * @param token the token to evaluate. | |||
| * @param context the context to use to evaluate the token. | |||
| * @return the value for the token, or null if the token is unknown. | |||
| */ | |||
| String getValue( String token, TaskContext context ) | |||
| throws TaskException; | |||
| } | |||
| @@ -1,382 +0,0 @@ | |||
| /* | |||
| * 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.myrmidon.framework.java; | |||
| import java.io.File; | |||
| import java.lang.reflect.InvocationTargetException; | |||
| import java.lang.reflect.Method; | |||
| import org.apache.aut.nativelib.Os; | |||
| import org.apache.aut.nativelib.PathUtil; | |||
| import org.apache.avalon.excalibur.i18n.ResourceManager; | |||
| import org.apache.avalon.excalibur.i18n.Resources; | |||
| import org.apache.myrmidon.api.TaskContext; | |||
| import org.apache.myrmidon.api.TaskException; | |||
| import org.apache.myrmidon.framework.nativelib.Execute; | |||
| import org.apache.myrmidon.framework.file.Path; | |||
| import org.apache.myrmidon.framework.file.FileListUtil; | |||
| import org.apache.myrmidon.framework.nativelib.EnvironmentData; | |||
| import org.apache.tools.todo.types.SysProperties; | |||
| import org.apache.myrmidon.framework.nativelib.ArgumentList; | |||
| import org.apache.tools.todo.util.FileUtils; | |||
| /** | |||
| * A utility class that takes care of executing a Java application. This | |||
| * class can execute Java apps in the current JVM, or a forked JVM. | |||
| * | |||
| * <p>To execute a Java application, create an instance of this class, | |||
| * configure it, and call one of the following methods: | |||
| * <ul> | |||
| * <li>{@link #execute} | |||
| * <li>{@link #executeForked} | |||
| * <li>{@link #executeNonForked} | |||
| * </ul> | |||
| * | |||
| * @author thomas.haas@softwired-inc.com | |||
| * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a> | |||
| */ | |||
| public class ExecuteJava | |||
| { | |||
| private static final Resources REZ | |||
| = ResourceManager.getPackageResources( ExecuteJava.class ); | |||
| private final Path m_classPath = new Path(); | |||
| private final EnvironmentData m_sysProperties = new EnvironmentData(); | |||
| private final ArgumentList m_args = new ArgumentList(); | |||
| private final ArgumentList m_vmArgs = new ArgumentList(); | |||
| private boolean m_fork; | |||
| private File m_workingDirectory; | |||
| private File m_jar; | |||
| private String m_jvm; | |||
| private String m_className; | |||
| private String m_maxMemory; | |||
| private boolean m_ignoreReturnCode; | |||
| /** | |||
| * Sets the main class of the application. | |||
| */ | |||
| public void setClassName( final String className ) | |||
| { | |||
| m_className = className; | |||
| } | |||
| /** | |||
| * Sets the executable jar file to use to execute the application. | |||
| * Can only be used in forked mode. | |||
| */ | |||
| public void setJar( final File jar ) | |||
| { | |||
| m_jar = jar; | |||
| } | |||
| /** | |||
| * Enables forked mode. | |||
| */ | |||
| public void setFork( final boolean fork ) | |||
| { | |||
| m_fork = fork; | |||
| } | |||
| /** | |||
| * Sets the max memory allocation pool size to use when running the | |||
| * application. Only used in forked mode. | |||
| * | |||
| * @param maxMemory the maximum memory pool size, or null for the default. | |||
| */ | |||
| public void setMaxMemory( final String maxMemory ) | |||
| { | |||
| m_maxMemory = maxMemory; | |||
| } | |||
| /** | |||
| * Sets the working directory for the application. Only used in forked mode. | |||
| */ | |||
| public void setWorkingDirectory( final File workingDirectory ) | |||
| { | |||
| m_workingDirectory = workingDirectory; | |||
| } | |||
| /** | |||
| * Disables checking of the application's return code. Only used in forked | |||
| * mode. | |||
| * | |||
| * @param ignore If true, the return code of the application is ignored. | |||
| * If false, an exception is thrown if the application does | |||
| * no exit with a 0 return code. | |||
| */ | |||
| public void setIgnoreReturnCode( boolean ignore ) | |||
| { | |||
| m_ignoreReturnCode = ignore; | |||
| } | |||
| /** | |||
| * Sets the JVM executable to use to run the application in a forked JVM. | |||
| * | |||
| * @param jvm the path to the JVM executable, or null to use the default | |||
| * JVM executable. | |||
| */ | |||
| public void setJvm( final String jvm ) | |||
| { | |||
| m_jvm = jvm; | |||
| } | |||
| /** | |||
| * Returns the classpath that will be used to execute the application. | |||
| * | |||
| * @return the application's classpath. This path can be modified. | |||
| */ | |||
| public Path getClassPath() | |||
| { | |||
| return m_classPath; | |||
| } | |||
| /** | |||
| * Returns the system properties that will be used for the application. | |||
| * Only used in forked mode. | |||
| * | |||
| * @return the application's system properties. Can be modified. | |||
| */ | |||
| public EnvironmentData getSysProperties() | |||
| { | |||
| return m_sysProperties; | |||
| } | |||
| /** | |||
| * Returns the arguments that will be used for the application. | |||
| * | |||
| * @return the application's arguments. Can be modified. | |||
| */ | |||
| public ArgumentList getArguments() | |||
| { | |||
| return m_args; | |||
| } | |||
| /** | |||
| * Returns the JVM arguments that will be used to execute the application. | |||
| * Only used in forked mode. | |||
| * | |||
| * @return the JVM aguments. Can be modified. | |||
| */ | |||
| public ArgumentList getVmArguments() | |||
| { | |||
| return m_vmArgs; | |||
| } | |||
| /** | |||
| * Executes the application. | |||
| */ | |||
| public void execute( final TaskContext context ) | |||
| throws TaskException | |||
| { | |||
| if( m_fork ) | |||
| { | |||
| executeForked( context ); | |||
| } | |||
| else | |||
| { | |||
| executeNonForked( context ); | |||
| } | |||
| } | |||
| /** | |||
| * Executes the application in this JVM. | |||
| */ | |||
| public void executeNonForked( final TaskContext context ) | |||
| throws TaskException | |||
| { | |||
| if( m_className == null ) | |||
| { | |||
| final String message = REZ.getString( "executejava.no-classname.error" ); | |||
| throw new TaskException( message ); | |||
| } | |||
| if( m_jar != null ) | |||
| { | |||
| final String message = REZ.getString( "executejava.jar-no-fork.error" ); | |||
| throw new TaskException( message ); | |||
| } | |||
| if( m_vmArgs.getArguments().length > 0 ) | |||
| { | |||
| final String message = REZ.getString( "executejava.ignore-jvm-args.notice" ); | |||
| context.warn( message ); | |||
| } | |||
| if( m_workingDirectory != null ) | |||
| { | |||
| final String message = REZ.getString( "executejava.ignore-dir.notice" ); | |||
| context.warn( message ); | |||
| } | |||
| if( m_maxMemory != null ) | |||
| { | |||
| final String message = REZ.getString( "executejava.ignore-maxmem.notice" ); | |||
| context.warn( message ); | |||
| } | |||
| if( m_sysProperties.size() > 0 ) | |||
| { | |||
| final String message = REZ.getString( "executejava.ignore-sys-props.notice" ); | |||
| context.warn( message ); | |||
| } | |||
| final String[] args = m_args.getArguments(); | |||
| // Log message | |||
| if( context.isVerboseEnabled() ) | |||
| { | |||
| final String debugMessage | |||
| = REZ.getString( "executejava.exec-in-jvm.notice", | |||
| m_className, | |||
| FileUtils.formatCommandLine( args ) ); | |||
| context.verbose( debugMessage ); | |||
| } | |||
| // Locate the class | |||
| Class target; | |||
| try | |||
| { | |||
| final ClassLoader classLoader = FileListUtil.createClassLoader( m_classPath, context ); | |||
| target = classLoader.loadClass( m_className ); | |||
| } | |||
| catch( final Exception e ) | |||
| { | |||
| final String message = REZ.getString( "executejava.find-class.error", m_className ); | |||
| throw new TaskException( message, e ); | |||
| } | |||
| // Call the main method | |||
| try | |||
| { | |||
| final Class[] params = { args.getClass() }; | |||
| final Method main = target.getMethod( "main", params ); | |||
| main.invoke( null, new Object[] { args } ); | |||
| } | |||
| catch( final InvocationTargetException e ) | |||
| { | |||
| final Throwable t = e.getTargetException(); | |||
| final String message = REZ.getString( "executejava.execute-app.error", m_className ); | |||
| throw new TaskException( message, t ); | |||
| } | |||
| catch( final Exception e ) | |||
| { | |||
| final String message = REZ.getString( "executejava.execute-app.error", m_className ); | |||
| throw new TaskException( message, e ); | |||
| } | |||
| } | |||
| /** | |||
| * Executes the application in a separate JVM. | |||
| */ | |||
| public int executeForked( final TaskContext context ) | |||
| throws TaskException | |||
| { | |||
| // Validate | |||
| if( m_className != null && m_jar != null ) | |||
| { | |||
| final String message = REZ.getString( "executejava.class-and-jar.error" ); | |||
| throw new TaskException( message ); | |||
| } | |||
| if( m_className == null && m_jar == null ) | |||
| { | |||
| final String message = REZ.getString( "executejava.no-classname.error" ); | |||
| throw new TaskException( message ); | |||
| } | |||
| final Execute exe = new Execute(); | |||
| exe.setWorkingDirectory( m_workingDirectory ); | |||
| exe.setIgnoreReturnCode( m_ignoreReturnCode ); | |||
| // Setup the command line | |||
| // Executable name | |||
| if( m_jvm != null ) | |||
| { | |||
| exe.setExecutable( m_jvm ); | |||
| } | |||
| else | |||
| { | |||
| exe.setExecutable( getJavaExecutableName() ); | |||
| } | |||
| // JVM arguments | |||
| exe.addArguments( m_vmArgs ); | |||
| // Max memory size | |||
| if( m_maxMemory != null ) | |||
| { | |||
| exe.addArgument( "-Xmx" + m_maxMemory ); | |||
| } | |||
| // System properties | |||
| final String[] props = SysProperties.getJavaVariables( m_sysProperties ); | |||
| exe.addArguments( props ); | |||
| // Classpath | |||
| final String[] classpath = m_classPath.listFiles( context ); | |||
| if( classpath.length > 0 ) | |||
| { | |||
| exe.addArgument( "-classpath" ); | |||
| exe.addArgument( PathUtil.formatPath( classpath ) ); | |||
| } | |||
| // What to execute | |||
| if( m_jar != null ) | |||
| { | |||
| exe.addArgument( "-jar" ); | |||
| exe.addArgument( m_jar ); | |||
| } | |||
| else | |||
| { | |||
| exe.addArgument( m_className ); | |||
| } | |||
| // Java app arguments | |||
| exe.addArguments( m_args ); | |||
| // Execute | |||
| return exe.execute( context ); | |||
| } | |||
| /** | |||
| * Determines the executable name for the java command for this JVM. | |||
| * | |||
| * @todo Move this to somewhere in AUT. | |||
| */ | |||
| public static String getJavaExecutableName() | |||
| { | |||
| if( Os.isFamily( Os.OS_FAMILY_NETWARE ) ) | |||
| { | |||
| // NetWare may have a "java" in the JRE directory, but 99% of | |||
| // the time, you don't want to execute it -- Jeff Tulley | |||
| // <JTULLEY@novell.com> | |||
| return "java"; | |||
| } | |||
| // Figure out the basename | |||
| final String baseName; | |||
| if( Os.isFamily( Os.OS_FAMILY_WINDOWS) || Os.isFamily( Os.OS_FAMILY_DOS ) ) | |||
| { | |||
| baseName = "java.exe"; | |||
| } | |||
| else | |||
| { | |||
| baseName = "java"; | |||
| } | |||
| // Look for java in the ${java.home{/../bin directory. Unfortunately | |||
| // on Windows java.home doesn't always refer to the correct location, | |||
| // so we need to fall back to assuming java is somewhere on the | |||
| // PATH. | |||
| File javaExe = | |||
| new File( System.getProperty( "java.home" ) + "/../bin/" + baseName ); | |||
| if( javaExe.exists() ) | |||
| { | |||
| return javaExe.getAbsolutePath(); | |||
| } | |||
| else | |||
| { | |||
| return "java"; | |||
| } | |||
| } | |||
| } | |||
| @@ -1,80 +0,0 @@ | |||
| /* | |||
| * 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.myrmidon.framework.java; | |||
| import org.apache.myrmidon.framework.file.FileList; | |||
| import org.apache.myrmidon.framework.file.Path; | |||
| import org.apache.myrmidon.framework.FileSet; | |||
| import org.apache.myrmidon.api.TaskException; | |||
| import org.apache.myrmidon.api.TaskContext; | |||
| import org.apache.aut.nativelib.Os; | |||
| import java.util.Locale; | |||
| import java.io.File; | |||
| /** | |||
| * A FileList that evaluates to the runtime class-path for this JVM. | |||
| * | |||
| * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a> | |||
| * @version $Revision$ $Date$ | |||
| * | |||
| * @ant.type type="path" name="java-runtime" | |||
| */ | |||
| public class JavaRuntimeClassPath | |||
| implements FileList | |||
| { | |||
| /** | |||
| * Returns the files in this list. | |||
| * | |||
| * @param context the context to use to evaluate the list. | |||
| * @return The names of the files in this list. All names are absolute paths. | |||
| */ | |||
| public String[] listFiles( final TaskContext context ) | |||
| throws TaskException | |||
| { | |||
| final Path path = new Path(); | |||
| if( System.getProperty( "java.vendor" ).toLowerCase( Locale.US ).indexOf( "microsoft" ) >= 0 ) | |||
| { | |||
| // Pull in *.zip from packages directory | |||
| FileSet msZipFiles = new FileSet(); | |||
| msZipFiles.setDir( new File( System.getProperty( "java.home" ) + File.separator + "Packages" ) ); | |||
| msZipFiles.setIncludes( "*.ZIP" ); | |||
| path.addFileset( msZipFiles ); | |||
| } | |||
| else if( "Kaffe".equals( System.getProperty( "java.vm.name" ) ) ) | |||
| { | |||
| FileSet kaffeJarFiles = new FileSet(); | |||
| kaffeJarFiles.setDir( new File( System.getProperty( "java.home" ) | |||
| + File.separator + "share" | |||
| + File.separator + "kaffe" ) ); | |||
| kaffeJarFiles.setIncludes( "*.jar" ); | |||
| path.addFileset( kaffeJarFiles ); | |||
| } | |||
| else if( Os.isFamily( Os.OS_FAMILY_OSX ) ) | |||
| { | |||
| // MacOS X | |||
| final String classDir = System.getProperty( "java.home" ) + | |||
| File.separator + ".." + File.separator + "Classes"; | |||
| final File classes = new File( classDir, "classes.jar" ); | |||
| path.addLocation( classes ); | |||
| final File ui = new File( classDir, "ui.jar" ); | |||
| path.addLocation( ui ); | |||
| } | |||
| else | |||
| { | |||
| // JDK > 1.1 sets java.home to the JRE directory. | |||
| final String rt = System.getProperty( "java.home" ) + | |||
| File.separator + "lib" + File.separator + "rt.jar"; | |||
| final File rtJar = new File( rt ); | |||
| path.addLocation( rtJar ); | |||
| } | |||
| return path.listFiles( context ); | |||
| } | |||
| } | |||
| @@ -1,10 +0,0 @@ | |||
| executejava.exec-in-jvm.notice=Running in same VM: {0} {1}. | |||
| executejava.ignore-jvm-args.notice=JVM args are ignored when using non-forked mode. | |||
| executejava.ignore-dir.notice=Working directory is ignored when using non-forked mode. | |||
| executejava.ignore-maxmem.notice=Maximum memory pool size is ignored when using non-forked mode. | |||
| executejava.ignore-sys-props.notice=System properties are ignored when using non-forked mode. | |||
| executejava.no-classname.error=No class-name specified. | |||
| executejava.jar-no-fork.error=Cannot execute a jar in non-forked mode. | |||
| executejava.find-class.error=Could not find main class "{0}". | |||
| executejava.execute-app.error=Could not execute class "{0}". | |||
| executejava.class-and-jar.error=Cannot specify both a Jar file and a main class. | |||
| @@ -1,89 +0,0 @@ | |||
| /* | |||
| * 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.myrmidon.framework.nativelib; | |||
| import java.io.File; | |||
| import org.apache.myrmidon.api.TaskException; | |||
| import org.apache.myrmidon.framework.file.Path; | |||
| import org.apache.tools.todo.util.FileUtils; | |||
| /** | |||
| * Used for nested xml command line definitions. | |||
| */ | |||
| public class Argument | |||
| { | |||
| private String[] m_parts; | |||
| public Argument() | |||
| { | |||
| } | |||
| public Argument( final String value ) | |||
| { | |||
| setValue( value ); | |||
| } | |||
| public Argument( final File file ) | |||
| { | |||
| setFile( file ); | |||
| } | |||
| /** | |||
| * Sets a single commandline argument to the absolute filename of the | |||
| * given file. | |||
| * | |||
| * @param value a single commandline argument. | |||
| */ | |||
| public void setFile( final File value ) | |||
| { | |||
| m_parts = new String[]{value.getAbsolutePath()}; | |||
| } | |||
| /** | |||
| * Line to split into several commandline arguments. | |||
| * | |||
| * @param line line to split into several commandline arguments | |||
| */ | |||
| public void setLine( final String line ) | |||
| throws TaskException | |||
| { | |||
| m_parts = FileUtils.translateCommandline( line ); | |||
| } | |||
| /** | |||
| * Sets a single commandline argument and treats it like a PATH - | |||
| * ensures the right separator for the local platform is used. | |||
| * | |||
| * @param value a single commandline argument. | |||
| */ | |||
| public void setPath( final Path value ) throws TaskException | |||
| { | |||
| throw new TaskException( "Using a path not implemented." ); | |||
| //m_parts = new String[]{ PathUtil.formatPath( value ) }; | |||
| } | |||
| /** | |||
| * Sets a single commandline argument. | |||
| * | |||
| * @param value a single commandline argument. | |||
| */ | |||
| public void setValue( final String value ) | |||
| { | |||
| m_parts = new String[]{value}; | |||
| } | |||
| /** | |||
| * Returns the parts this Argument consists of. | |||
| * | |||
| * @return The Parts value | |||
| */ | |||
| public String[] getParts() | |||
| { | |||
| return m_parts; | |||
| } | |||
| } | |||
| @@ -1,94 +0,0 @@ | |||
| /* | |||
| * 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.myrmidon.framework.nativelib; | |||
| import java.util.ArrayList; | |||
| import java.io.File; | |||
| import org.apache.myrmidon.api.TaskException; | |||
| import org.apache.tools.todo.util.FileUtils; | |||
| /** | |||
| * A utility class to use to assemble a list of command-line arguments. | |||
| * | |||
| * @author thomas.haas@softwired-inc.com | |||
| * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a> | |||
| * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a> | |||
| * @version $Revision$ $Date$ | |||
| */ | |||
| public class ArgumentList | |||
| { | |||
| protected final ArrayList m_arguments = new ArrayList(); | |||
| /** | |||
| * Returns all arguments defined by <code>addLine</code>, <code>addValue</code> | |||
| * or the argument object. | |||
| * | |||
| * @return The Arguments value | |||
| */ | |||
| public String[] getArguments() | |||
| { | |||
| final int size = m_arguments.size(); | |||
| final ArrayList result = new ArrayList( size * 2 ); | |||
| for( int i = 0; i < size; i++ ) | |||
| { | |||
| final Argument arg = (Argument)m_arguments.get( i ); | |||
| final String[] s = arg.getParts(); | |||
| for( int j = 0; j < s.length; j++ ) | |||
| { | |||
| result.add( s[ j ] ); | |||
| } | |||
| } | |||
| final String[] res = new String[ result.size() ]; | |||
| return (String[])result.toArray( res ); | |||
| } | |||
| /** | |||
| * Sets the arguments, replacing the current value of this list. | |||
| */ | |||
| public void setArguments( final ArgumentList list ) | |||
| { | |||
| m_arguments.clear(); | |||
| addArguments( list ); | |||
| } | |||
| public void addArguments( final String[] args ) | |||
| { | |||
| for( int i = 0; i < args.length; i++ ) | |||
| { | |||
| addArgument( args[ i ] ); | |||
| } | |||
| } | |||
| public void addArguments( final ArgumentList args ) | |||
| { | |||
| addArguments( args.getArguments() ); | |||
| } | |||
| public void addArgument( final File argument ) | |||
| { | |||
| addArgument( new Argument( argument ) ); | |||
| } | |||
| public void addArgument( final String argument ) | |||
| { | |||
| addArgument( new Argument( argument ) ); | |||
| } | |||
| public void addArgument( final Argument argument ) | |||
| { | |||
| m_arguments.add( argument ); | |||
| } | |||
| public void addLine( final String line ) | |||
| throws TaskException | |||
| { | |||
| final String[] parts = FileUtils.translateCommandline( line ); | |||
| addArguments( parts ); | |||
| } | |||
| } | |||
| @@ -1,61 +0,0 @@ | |||
| /* | |||
| * 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.myrmidon.framework.nativelib; | |||
| /** | |||
| * Commandline objects help handling command lines specifying processes to | |||
| * execute. The class can be used to define a command line as nested elements or | |||
| * as a helper to define a command line by an application. <p> | |||
| * | |||
| * <code> | |||
| * <someelement><br> | |||
| * <acommandline executable="/executable/to/run"><br> | |||
| * <argument value="argument 1" /><br> | |||
| * <argument line="argument_1 argument_2 argument_3" | |||
| * /><br> | |||
| * <argument value="argument 4" /><br> | |||
| * </acommandline><br> | |||
| * </someelement><br> | |||
| * </code> The element <code>someelement</code> must provide a method <code>createAcommandline</code> | |||
| * which returns an instance of this class. | |||
| * | |||
| * @author thomas.haas@softwired-inc.com | |||
| * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a> | |||
| */ | |||
| public class Commandline | |||
| extends ArgumentList | |||
| { | |||
| private String m_executable; | |||
| /** | |||
| * Sets the executable to run. | |||
| */ | |||
| public void setExecutable( final String executable ) | |||
| { | |||
| m_executable = executable; | |||
| } | |||
| /** | |||
| * Returns the executable to run. | |||
| */ | |||
| public String getExecutable() | |||
| { | |||
| return m_executable; | |||
| } | |||
| /** | |||
| * Sets the commandline, replacing its current value. | |||
| */ | |||
| public void setCommandline( final Commandline command ) | |||
| { | |||
| m_executable = command.getExecutable(); | |||
| setArguments( command ); | |||
| } | |||
| } | |||
| @@ -1,56 +0,0 @@ | |||
| /* | |||
| * 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.myrmidon.framework.nativelib; | |||
| import java.util.ArrayList; | |||
| import java.util.Properties; | |||
| /** | |||
| * Wrapper for environment variables. | |||
| * | |||
| * @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a> | |||
| */ | |||
| public class EnvironmentData | |||
| { | |||
| protected final ArrayList m_variables = new ArrayList(); | |||
| public Properties getVariables() | |||
| { | |||
| final Properties environment = new Properties(); | |||
| final int size = m_variables.size(); | |||
| for( int i = 0; i < size; i++ ) | |||
| { | |||
| final EnvironmentVariable variable = (EnvironmentVariable)m_variables.get( i ); | |||
| environment.setProperty( variable.getKey(), variable.getValue() ); | |||
| } | |||
| return environment; | |||
| } | |||
| public void addVariable( EnvironmentVariable var ) | |||
| { | |||
| m_variables.add( var ); | |||
| } | |||
| public void addVariable( String key, String value ) | |||
| { | |||
| final EnvironmentVariable var = new EnvironmentVariable(); | |||
| var.setKey( key ); | |||
| var.setValue( value ); | |||
| addVariable( var ); | |||
| } | |||
| public void addVariables( EnvironmentData properties ) | |||
| { | |||
| m_variables.addAll( properties.m_variables ); | |||
| } | |||
| public int size() | |||
| { | |||
| return m_variables.size(); | |||
| } | |||
| } | |||
| @@ -1,49 +0,0 @@ | |||
| /* | |||
| * 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.myrmidon.framework.nativelib; | |||
| import java.io.File; | |||
| import org.apache.myrmidon.api.TaskException; | |||
| import org.apache.myrmidon.framework.file.Path; | |||
| public class EnvironmentVariable | |||
| { | |||
| private String m_key; | |||
| private String m_value; | |||
| public void setFile( final File file ) | |||
| { | |||
| m_value = file.getAbsolutePath(); | |||
| } | |||
| public void setKey( final String key ) | |||
| { | |||
| m_key = key; | |||
| } | |||
| public void setPath( final Path path ) throws TaskException | |||
| { | |||
| throw new TaskException( "Using a path not implemented." ); | |||
| //m_value = PathUtil.formatPath( path ); | |||
| } | |||
| public void setValue( final String value ) | |||
| { | |||
| m_value = value; | |||
| } | |||
| public String getKey() | |||
| { | |||
| return m_key; | |||
| } | |||
| public String getValue() | |||
| { | |||
| return m_value; | |||
| } | |||
| } | |||
| @@ -1,278 +0,0 @@ | |||
| /* | |||
| * 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.myrmidon.framework.nativelib; | |||
| import java.io.File; | |||
| import java.util.Properties; | |||
| import org.apache.aut.nativelib.ExecException; | |||
| import org.apache.aut.nativelib.ExecManager; | |||
| import org.apache.aut.nativelib.ExecMetaData; | |||
| import org.apache.aut.nativelib.ExecOutputHandler; | |||
| import org.apache.avalon.excalibur.i18n.ResourceManager; | |||
| import org.apache.avalon.excalibur.i18n.Resources; | |||
| import org.apache.myrmidon.api.TaskContext; | |||
| import org.apache.myrmidon.api.TaskException; | |||
| import org.apache.tools.todo.util.FileUtils; | |||
| /** | |||
| * This is a utility class designed to make executing native processes easier | |||
| * in the context of ant. | |||
| * | |||
| * <p>To execute a native process, configure an instance of this class, | |||
| * and then call its {@link #execute} method. | |||
| * | |||
| * @author <a href="mailto:peter@apache.org">Peter Donald</a> | |||
| * @author <a href="mailto:thomas.haas@softwired-inc.com">Thomas Haas</a> | |||
| * @version $Revision$ $Date$ | |||
| */ | |||
| public class Execute | |||
| extends Commandline | |||
| { | |||
| private static final Resources REZ | |||
| = ResourceManager.getPackageResources( Execute.class ); | |||
| private Properties m_environment = new Properties(); | |||
| private File m_workingDirectory; | |||
| private boolean m_newEnvironment; | |||
| private ExecOutputHandler m_handler; | |||
| private long m_timeout; | |||
| private int m_returnCode; | |||
| private boolean m_ignoreReturnCode; | |||
| /** | |||
| * Sets the timeout, in milliseconds, for the process. The process is | |||
| * forcibly shutdown after this time. Use 0 to allow the process to | |||
| * run forever. Default is 0. | |||
| * | |||
| * @param timeout the timeout, in milliseconds. | |||
| */ | |||
| public void setTimeout( final long timeout ) | |||
| { | |||
| m_timeout = timeout; | |||
| } | |||
| /** | |||
| * Sets the handler for the process' output and error streams. If not | |||
| * provided, the process' output and error are written to the log using | |||
| * the TaskContext's logging methods. | |||
| * | |||
| * @param handler the handler. | |||
| */ | |||
| public void setExecOutputHandler( final ExecOutputHandler handler ) | |||
| { | |||
| m_handler = handler; | |||
| } | |||
| /** | |||
| * Sets the environment to use for the process. | |||
| * | |||
| * @param environment a map from environment variable name to value. | |||
| */ | |||
| public void setEnvironment( final Properties environment ) | |||
| { | |||
| if( null == environment ) | |||
| { | |||
| throw new NullPointerException( "environment" ); | |||
| } | |||
| m_environment = environment; | |||
| } | |||
| /** | |||
| * If this variable is false then then the environment specified is | |||
| * added to the environment variables for current process. If this | |||
| * value is true then the specified environment replaces the environment | |||
| * for the command. Default is false. | |||
| */ | |||
| public void setNewenvironment( final boolean newEnvironment ) | |||
| { | |||
| m_newEnvironment = newEnvironment; | |||
| } | |||
| /** | |||
| * Sets the working directory of the process to execute. Default is the | |||
| * project's base directory. | |||
| * | |||
| * @param workingDirectory the working directory of the process. Use | |||
| * null for the project's base directory. | |||
| */ | |||
| public void setWorkingDirectory( final File workingDirectory ) | |||
| { | |||
| m_workingDirectory = workingDirectory; | |||
| } | |||
| /** | |||
| * Sets the expected return code of the process. If the process does not | |||
| * exit with this return code, and exception is thrown by {@link #execute}. | |||
| * Default is 0. | |||
| * | |||
| * @param returnCode the expected return code. | |||
| */ | |||
| public void setReturnCode( final int returnCode ) | |||
| { | |||
| m_returnCode = returnCode; | |||
| } | |||
| /** | |||
| * If set to true, the return code of the process is ignore. If false, | |||
| * it is compared against the expected return code. Default is false. | |||
| */ | |||
| public void setIgnoreReturnCode( final boolean ignore ) | |||
| { | |||
| m_ignoreReturnCode = ignore; | |||
| } | |||
| /** | |||
| * Runs a process defined by the command line and returns its exit status. | |||
| * | |||
| * @return the exit status of the subprocess. | |||
| */ | |||
| public int execute( final TaskContext context ) | |||
| throws TaskException | |||
| { | |||
| validate(); | |||
| try | |||
| { | |||
| // Build an output handler | |||
| final ExecOutputHandler handler = buildOutputHandler( context ); | |||
| // Build the command meta-info | |||
| final ExecManager execManager = (ExecManager)context.getService( ExecManager.class ); | |||
| final ExecMetaData metaData = buildExecMetaData( context, execManager ); | |||
| logExecDetails( metaData, context ); | |||
| // Execute the process and check return code | |||
| final int returnCode = execManager.execute( metaData, handler, m_timeout ); | |||
| checkReturnCode( returnCode ); | |||
| return returnCode; | |||
| } | |||
| catch( final Exception e ) | |||
| { | |||
| final String message = REZ.getString( "execute.failed.error", getExecutable() ); | |||
| throw new TaskException( message, e ); | |||
| } | |||
| } | |||
| /** | |||
| * Logs the details of the command. | |||
| */ | |||
| private void logExecDetails( final ExecMetaData metaData, | |||
| final TaskContext context ) | |||
| throws TaskException | |||
| { | |||
| if( context.isVerboseEnabled() ) | |||
| { | |||
| final String cmdline = FileUtils.formatCommandLine( metaData.getCommand() ); | |||
| final String message = REZ.getString( "execute.command.notice", cmdline ); | |||
| context.verbose( message ); | |||
| } | |||
| if( context.isDebugEnabled() ) | |||
| { | |||
| final String message = REZ.getString( "execute.env-vars.notice", metaData.getEnvironment() ); | |||
| context.debug( message ); | |||
| } | |||
| } | |||
| /** | |||
| * Vaidates the arguments. | |||
| */ | |||
| private void validate() throws TaskException | |||
| { | |||
| if( null == getExecutable() ) | |||
| { | |||
| final String message = REZ.getString( "execute.no-executable.error" ); | |||
| throw new TaskException( message ); | |||
| } | |||
| if( m_workingDirectory != null ) | |||
| { | |||
| if( !m_workingDirectory.exists() ) | |||
| { | |||
| final String message = REZ.getString( "execute.dir-noexist.error", m_workingDirectory ); | |||
| throw new TaskException( message ); | |||
| } | |||
| else if( !m_workingDirectory.isDirectory() ) | |||
| { | |||
| final String message = REZ.getString( "execute.dir-notdir.error", m_workingDirectory ); | |||
| throw new TaskException( message ); | |||
| } | |||
| } | |||
| } | |||
| /** | |||
| * Creates an output handler to use for the process' stdout and stderr. | |||
| */ | |||
| private ExecOutputHandler buildOutputHandler( final TaskContext context ) | |||
| { | |||
| ExecOutputHandler handler = m_handler; | |||
| if( handler == null ) | |||
| { | |||
| handler = new LoggingExecOutputHandler( context ); | |||
| } | |||
| return handler; | |||
| } | |||
| /** | |||
| * Utility method to verify that specified return code was the | |||
| * return code expected (if any). | |||
| */ | |||
| private void checkReturnCode( final int returnCode ) | |||
| throws TaskException | |||
| { | |||
| if( ! m_ignoreReturnCode && returnCode != m_returnCode ) | |||
| { | |||
| final String message = REZ.getString( "execute.bad-resultcode.error", | |||
| getExecutable(), | |||
| new Integer(returnCode) ); | |||
| throw new TaskException( message ); | |||
| } | |||
| } | |||
| /** | |||
| * Utility method to create an ExecMetaData object | |||
| * to pass to the ExecManager service. | |||
| */ | |||
| private ExecMetaData buildExecMetaData( final TaskContext context, | |||
| final ExecManager execManager ) | |||
| throws ExecException | |||
| { | |||
| // Build the command line | |||
| final String[] command = getCommandLine(); | |||
| // Build the environment | |||
| final Properties newEnvironment = new Properties(); | |||
| if( !m_newEnvironment ) | |||
| { | |||
| newEnvironment.putAll( execManager.getNativeEnvironment() ); | |||
| } | |||
| newEnvironment.putAll( m_environment ); | |||
| // Determine the working directory | |||
| File workingDir = m_workingDirectory; | |||
| if( workingDir == null ) | |||
| { | |||
| workingDir = context.getBaseDirectory(); | |||
| } | |||
| return new ExecMetaData( command, | |||
| newEnvironment, | |||
| workingDir ); | |||
| } | |||
| /** | |||
| * Builds the command line. | |||
| */ | |||
| private String[] getCommandLine() | |||
| { | |||
| final String[] args = getArguments(); | |||
| final String[] result = new String[ args.length + 1 ]; | |||
| result[ 0 ] = getExecutable().replace( '/', File.separatorChar ).replace( '\\', File.separatorChar ); | |||
| System.arraycopy( args, 0, result, 1, args.length ); | |||
| return result; | |||
| } | |||
| } | |||
| @@ -1,47 +0,0 @@ | |||
| /* | |||
| * 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.myrmidon.framework.nativelib; | |||
| import org.apache.aut.nativelib.ExecOutputHandler; | |||
| import org.apache.myrmidon.api.TaskContext; | |||
| /** | |||
| * An {@link org.apache.aut.nativelib.ExecOutputHandler} adaptor, that writes output to the logging | |||
| * methods of a {@link org.apache.myrmidon.api.TaskContext}. | |||
| * | |||
| * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a> | |||
| * @version $Revision$ $Date$ | |||
| */ | |||
| public class LoggingExecOutputHandler | |||
| implements ExecOutputHandler | |||
| { | |||
| private final TaskContext m_context; | |||
| public LoggingExecOutputHandler( final TaskContext context ) | |||
| { | |||
| m_context = context; | |||
| } | |||
| /** | |||
| * Receive notification about the process writing | |||
| * to standard output. | |||
| */ | |||
| public void stdout( final String line ) | |||
| { | |||
| m_context.info( line ); | |||
| } | |||
| /** | |||
| * Receive notification about the process writing | |||
| * to standard error. | |||
| */ | |||
| public void stderr( final String line ) | |||
| { | |||
| m_context.error( line ); | |||
| } | |||
| } | |||
| @@ -1,7 +0,0 @@ | |||
| execute.no-executable.error=No executable specified. | |||
| execute.dir-noexist.error=The specified working directory "{0}" does not exist. | |||
| execute.dir-notdir.error=The specified working directory "{0}" is not a directory. | |||
| execute.failed.error=Command "{0}" failed. | |||
| execute.bad-resultcode.error=Command "{0}" returned unexpected exit code {1}. | |||
| execute.command.notice=Executing: {0} | |||
| execute.env-vars.notice=Using environment: {0}. | |||