ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From adammurd...@apache.org
Subject cvs commit: jakarta-ant/proposal/myrmidon/src/java/org/apache/antlib/core AbstractAvailableCondition.java ClassAvailableCondition.java ResourceAvailableCondition.java Available.java
Date Tue, 19 Mar 2002 02:41:13 GMT
adammurdoch    02/03/18 18:41:13

  Added:       proposal/myrmidon/src/java/org/apache/antlib/core
                        AbstractAvailableCondition.java
                        ClassAvailableCondition.java
                        ResourceAvailableCondition.java
  Removed:     proposal/myrmidon/src/java/org/apache/antlib/core
                        Available.java
  Log:
  Split <available> condition into <class-available> and <resource-available>.
  
  Revision  Changes    Path
  1.1                  jakarta-ant/proposal/myrmidon/src/java/org/apache/antlib/core/AbstractAvailableCondition.java
  
  Index: AbstractAvailableCondition.java
  ===================================================================
  /*
   * 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: 1.1 $ $Date: 2002/03/19 02:41:13 $
   */
  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.1                  jakarta-ant/proposal/myrmidon/src/java/org/apache/antlib/core/ClassAvailableCondition.java
  
  Index: ClassAvailableCondition.java
  ===================================================================
  /*
   * 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;
          }
      }
  
  }
  
  
  
  1.1                  jakarta-ant/proposal/myrmidon/src/java/org/apache/antlib/core/ResourceAvailableCondition.java
  
  Index: ResourceAvailableCondition.java
  ===================================================================
  /*
   * 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;
      }
  }
  
  
  

--
To unsubscribe, e-mail:   <mailto:ant-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:ant-dev-help@jakarta.apache.org>


Mime
View raw message