From 6a20789293e6588a94560a11697fb59dd0d9338c Mon Sep 17 00:00:00 2001 From: adammurdoch Date: Tue, 19 Mar 2002 02:41:13 +0000 Subject: [PATCH] Split condition into and . git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@271892 13f79535-47bb-0310-9956-ffa450edef68 --- .../core/AbstractAvailableCondition.java | 58 ++++++++ .../org/apache/antlib/core/Available.java | 136 ------------------ .../antlib/core/ClassAvailableCondition.java | 64 +++++++++ .../core/ResourceAvailableCondition.java | 61 ++++++++ 4 files changed, 183 insertions(+), 136 deletions(-) create mode 100644 proposal/myrmidon/src/java/org/apache/antlib/core/AbstractAvailableCondition.java delete mode 100644 proposal/myrmidon/src/java/org/apache/antlib/core/Available.java create mode 100644 proposal/myrmidon/src/java/org/apache/antlib/core/ClassAvailableCondition.java create mode 100644 proposal/myrmidon/src/java/org/apache/antlib/core/ResourceAvailableCondition.java diff --git a/proposal/myrmidon/src/java/org/apache/antlib/core/AbstractAvailableCondition.java b/proposal/myrmidon/src/java/org/apache/antlib/core/AbstractAvailableCondition.java new file mode 100644 index 000000000..7534f668a --- /dev/null +++ b/proposal/myrmidon/src/java/org/apache/antlib/core/AbstractAvailableCondition.java @@ -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 Adam Murdoch + * @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; + } +} diff --git a/proposal/myrmidon/src/java/org/apache/antlib/core/Available.java b/proposal/myrmidon/src/java/org/apache/antlib/core/Available.java deleted file mode 100644 index 8b7ebdcbb..000000000 --- a/proposal/myrmidon/src/java/org/apache/antlib/core/Available.java +++ /dev/null @@ -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 - * stefano@apache.org - * @author Magesh Umasankar - * - * @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; - } - } -} diff --git a/proposal/myrmidon/src/java/org/apache/antlib/core/ClassAvailableCondition.java b/proposal/myrmidon/src/java/org/apache/antlib/core/ClassAvailableCondition.java new file mode 100644 index 000000000..a1875a6c9 --- /dev/null +++ b/proposal/myrmidon/src/java/org/apache/antlib/core/ClassAvailableCondition.java @@ -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 + * stefano@apache.org + * @author Magesh Umasankar + * + * @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; + } + } + +} diff --git a/proposal/myrmidon/src/java/org/apache/antlib/core/ResourceAvailableCondition.java b/proposal/myrmidon/src/java/org/apache/antlib/core/ResourceAvailableCondition.java new file mode 100644 index 000000000..a0af0ca20 --- /dev/null +++ b/proposal/myrmidon/src/java/org/apache/antlib/core/ResourceAvailableCondition.java @@ -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 + * stefano@apache.org + * @author Magesh Umasankar + * + * @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; + } +}