git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@271892 13f79535-47bb-0310-9956-ffa450edef68master
| @@ -0,0 +1,58 @@ | |||||
| /* | |||||
| * Copyright (C) The Apache Software Foundation. All rights reserved. | |||||
| * | |||||
| * This software is published under the terms of the Apache Software License | |||||
| * version 1.1, a copy of which has been included with this distribution in | |||||
| * the LICENSE.txt file. | |||||
| */ | |||||
| package org.apache.antlib.core; | |||||
| import org.apache.myrmidon.framework.conditions.Condition; | |||||
| import org.apache.myrmidon.api.TaskException; | |||||
| import org.apache.tools.todo.types.Path; | |||||
| import org.apache.tools.todo.types.PathUtil; | |||||
| import java.net.URL; | |||||
| import java.net.URLClassLoader; | |||||
| /** | |||||
| * An abstract condition which checks for the availability of a particular | |||||
| * resource in a classpath. | |||||
| * | |||||
| * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a> | |||||
| * @version $Revision$ $Date$ | |||||
| */ | |||||
| public abstract class AbstractAvailableCondition | |||||
| implements Condition | |||||
| { | |||||
| private Path m_classpath = new Path(); | |||||
| /** | |||||
| * Adds a classpath element. | |||||
| */ | |||||
| public void addClasspath( final Path classpath ) | |||||
| throws TaskException | |||||
| { | |||||
| m_classpath.addPath( classpath ); | |||||
| } | |||||
| /** | |||||
| * Builds the ClassLoader to use to check resources. | |||||
| */ | |||||
| protected ClassLoader buildClassLoader() throws TaskException | |||||
| { | |||||
| final URL[] urls = PathUtil.toURLs( m_classpath ); | |||||
| final ClassLoader classLoader; | |||||
| if( urls.length > 0 ) | |||||
| { | |||||
| classLoader = new URLClassLoader( urls ); | |||||
| } | |||||
| else | |||||
| { | |||||
| // TODO - using system classloader is kinda useless now, because | |||||
| // the system classpath contains almost nothing. Should be using | |||||
| // the 'common' classloader instead | |||||
| classLoader = ClassLoader.getSystemClassLoader(); | |||||
| } | |||||
| return classLoader; | |||||
| } | |||||
| } | |||||
| @@ -1,136 +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.antlib.core; | |||||
| import java.net.URL; | |||||
| import java.net.URLClassLoader; | |||||
| import org.apache.myrmidon.api.TaskContext; | |||||
| import org.apache.myrmidon.api.TaskException; | |||||
| import org.apache.myrmidon.framework.conditions.Condition; | |||||
| import org.apache.tools.todo.types.Path; | |||||
| import org.apache.tools.todo.types.PathUtil; | |||||
| /** | |||||
| * A condition that evaluates to true if the requested class or resource | |||||
| * is available at runtime. | |||||
| * | |||||
| * @author Stefano Mazzocchi <a href="mailto:stefano@apache.org"> | |||||
| * stefano@apache.org</a> | |||||
| * @author <a href="mailto:umagesh@apache.org">Magesh Umasankar</a> | |||||
| * | |||||
| * @ant:type type="condition" name="available" | |||||
| */ | |||||
| public class Available | |||||
| implements Condition | |||||
| { | |||||
| private String m_classname; | |||||
| private Path m_classpath; | |||||
| private ClassLoader m_classLoader; | |||||
| private String m_resource; | |||||
| /** | |||||
| * Sets the name of the class to search for. | |||||
| */ | |||||
| public void setClassname( final String classname ) | |||||
| { | |||||
| if( !"".equals( classname ) ) | |||||
| { | |||||
| m_classname = classname; | |||||
| } | |||||
| } | |||||
| /** | |||||
| * Adds a classpath element. | |||||
| */ | |||||
| public void addClasspath( final Path classpath ) | |||||
| throws TaskException | |||||
| { | |||||
| if( m_classpath == null ) | |||||
| { | |||||
| m_classpath = classpath; | |||||
| } | |||||
| else | |||||
| { | |||||
| m_classpath.addPath( classpath ); | |||||
| } | |||||
| } | |||||
| /** | |||||
| * Sets the name of the resource to look for. | |||||
| */ | |||||
| public void setResource( final String resource ) | |||||
| { | |||||
| m_resource = resource; | |||||
| } | |||||
| /** | |||||
| * Evaluates the condition. | |||||
| */ | |||||
| public boolean evaluate( final TaskContext context ) | |||||
| throws TaskException | |||||
| { | |||||
| if( m_classname == null && m_resource == null ) | |||||
| { | |||||
| throw new TaskException( "At least one of (classname|file|resource) is required" ); | |||||
| } | |||||
| if( m_classpath != null ) | |||||
| { | |||||
| final URL[] urls = PathUtil.toURLs( m_classpath ); | |||||
| m_classLoader = new URLClassLoader( urls ); | |||||
| } | |||||
| if( ( m_classname != null ) && !checkClass( m_classname ) ) | |||||
| { | |||||
| return false; | |||||
| } | |||||
| if( ( m_resource != null ) && !checkResource( m_resource ) ) | |||||
| { | |||||
| return false; | |||||
| } | |||||
| return true; | |||||
| } | |||||
| private boolean checkClass( String classname ) | |||||
| { | |||||
| try | |||||
| { | |||||
| final ClassLoader classLoader = getClassLoader(); | |||||
| classLoader.loadClass( classname ); | |||||
| return true; | |||||
| } | |||||
| catch( ClassNotFoundException e ) | |||||
| { | |||||
| return false; | |||||
| } | |||||
| catch( NoClassDefFoundError e ) | |||||
| { | |||||
| return false; | |||||
| } | |||||
| } | |||||
| private boolean checkResource( String resource ) | |||||
| { | |||||
| final ClassLoader classLoader = getClassLoader(); | |||||
| return ( null != classLoader.getResourceAsStream( resource ) ); | |||||
| } | |||||
| private ClassLoader getClassLoader() | |||||
| { | |||||
| if( null == m_classLoader ) | |||||
| { | |||||
| return ClassLoader.getSystemClassLoader(); | |||||
| } | |||||
| else | |||||
| { | |||||
| return m_classLoader; | |||||
| } | |||||
| } | |||||
| } | |||||
| @@ -0,0 +1,64 @@ | |||||
| /* | |||||
| * Copyright (C) The Apache Software Foundation. All rights reserved. | |||||
| * | |||||
| * This software is published under the terms of the Apache Software License | |||||
| * version 1.1, a copy of which has been included with this distribution in | |||||
| * the LICENSE.txt file. | |||||
| */ | |||||
| package org.apache.antlib.core; | |||||
| import org.apache.myrmidon.api.TaskContext; | |||||
| import org.apache.myrmidon.api.TaskException; | |||||
| import org.apache.myrmidon.framework.conditions.Condition; | |||||
| /** | |||||
| * A condition that evaluates to true if the requested class is available | |||||
| * at runtime. | |||||
| * | |||||
| * @author Stefano Mazzocchi <a href="mailto:stefano@apache.org"> | |||||
| * stefano@apache.org</a> | |||||
| * @author <a href="mailto:umagesh@apache.org">Magesh Umasankar</a> | |||||
| * | |||||
| * @ant:type type="condition" name="class-available" | |||||
| */ | |||||
| public class ClassAvailableCondition | |||||
| extends AbstractAvailableCondition | |||||
| implements Condition | |||||
| { | |||||
| private String m_classname; | |||||
| /** | |||||
| * Sets the name of the class to search for. | |||||
| */ | |||||
| public void setClassname( final String classname ) | |||||
| { | |||||
| m_classname = classname; | |||||
| } | |||||
| /** | |||||
| * Evaluates the condition. | |||||
| */ | |||||
| public boolean evaluate( final TaskContext context ) | |||||
| throws TaskException | |||||
| { | |||||
| if( m_classname == null ) | |||||
| { | |||||
| throw new TaskException( "Classname not specified." ); | |||||
| } | |||||
| // Build the classloader to use to check resources | |||||
| final ClassLoader classLoader = buildClassLoader(); | |||||
| // Do the check | |||||
| try | |||||
| { | |||||
| classLoader.loadClass( m_classname ); | |||||
| return true; | |||||
| } | |||||
| catch( final Exception e ) | |||||
| { | |||||
| return false; | |||||
| } | |||||
| } | |||||
| } | |||||
| @@ -0,0 +1,61 @@ | |||||
| /* | |||||
| * Copyright (C) The Apache Software Foundation. All rights reserved. | |||||
| * | |||||
| * This software is published under the terms of the Apache Software License | |||||
| * version 1.1, a copy of which has been included with this distribution in | |||||
| * the LICENSE.txt file. | |||||
| */ | |||||
| package org.apache.antlib.core; | |||||
| import java.io.InputStream; | |||||
| import org.apache.avalon.excalibur.io.IOUtil; | |||||
| import org.apache.myrmidon.api.TaskContext; | |||||
| import org.apache.myrmidon.api.TaskException; | |||||
| import org.apache.myrmidon.framework.conditions.Condition; | |||||
| /** | |||||
| * A condition that evaluates to true if the requested resource is available | |||||
| * at runtime. | |||||
| * | |||||
| * @author Stefano Mazzocchi <a href="mailto:stefano@apache.org"> | |||||
| * stefano@apache.org</a> | |||||
| * @author <a href="mailto:umagesh@apache.org">Magesh Umasankar</a> | |||||
| * | |||||
| * @ant:type type="condition" name="resource-available" | |||||
| */ | |||||
| public class ResourceAvailableCondition | |||||
| extends AbstractAvailableCondition | |||||
| implements Condition | |||||
| { | |||||
| private String m_resource; | |||||
| /** | |||||
| * Sets the name of the resource to look for. | |||||
| */ | |||||
| public void setResource( final String resource ) | |||||
| { | |||||
| m_resource = resource; | |||||
| } | |||||
| /** | |||||
| * Evaluates the condition. | |||||
| */ | |||||
| public boolean evaluate( final TaskContext context ) | |||||
| throws TaskException | |||||
| { | |||||
| if( m_resource == null ) | |||||
| { | |||||
| throw new TaskException( "Resource was not specified." ); | |||||
| } | |||||
| // Check whether the resource is available | |||||
| final ClassLoader classLoader = buildClassLoader(); | |||||
| final InputStream instr = classLoader.getResourceAsStream( m_resource ); | |||||
| if( instr != null ) | |||||
| { | |||||
| IOUtil.shutdownStream( instr ); | |||||
| return true; | |||||
| } | |||||
| return false; | |||||
| } | |||||
| } | |||||