You are viewing a plain text version of this content. The canonical link for it is here.
Posted to tdk-dev@turbine.apache.org by jv...@apache.org on 2002/02/12 15:32:42 UTC

cvs commit: jakarta-turbine-tdk/src/tdk/task/org/apache/tdk CreatePath.java

jvanzyl     02/02/12 06:32:42

  Added:       src/tdk/task/org/apache/tdk CreatePath.java
  Log:
  - adding a small task that will create a path from a file with a list
    of specified jar locations.
  
  Revision  Changes    Path
  1.1                  jakarta-turbine-tdk/src/tdk/task/org/apache/tdk/CreatePath.java
  
  Index: CreatePath.java
  ===================================================================
  package org.apache.tdk.task;
  
  /*
   *  The Apache Software License, Version 1.1
   *
   *  Copyright (c) 2001-2002 The Apache Software Foundation.  All rights
   *  reserved.
   *
   *  Redistribution and use in source and binary forms, with or without
   *  modification, are permitted provided that the following conditions
   *  are met:
   *
   *  1. Redistributions of source code must retain the above copyright
   *  notice, this list of conditions and the following disclaimer.
   *
   *  2. Redistributions in binary form must reproduce the above copyright
   *  notice, this list of conditions and the following disclaimer in
   *  the documentation and/or other materials provided with the
   *  distribution.
   *
   *  3. The end-user documentation included with the redistribution, if
   *  any, must include the following acknowlegement:
   *  "This product includes software developed by the
   *  Apache Software Foundation (http://www.apache.org/)."
   *  Alternately, this acknowlegement may appear in the software itself,
   *  if and wherever such third-party acknowlegements normally appear.
   *
   *  4. The names "The Jakarta Project", "Ant", and "Apache Software
   *  Foundation" must not be used to endorse or promote products derived
   *  from this software without prior written permission. For written
   *  permission, please contact apache@apache.org.
   *
   *  5. Products derived from this software may not be called "Apache"
   *  nor may "Apache" appear in their names without prior written
   *  permission of the Apache Group.
   *
   *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   *  DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   *  ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   *  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   *  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   *  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   *  SUCH DAMAGE.
   *  ====================================================================
   *
   *  This software consists of voluntary contributions made by many
   *  individuals on behalf of the Apache Software Foundation.  For more
   *  information on the Apache Software Foundation, please see
   *  <http://www.apache.org/>.
   */
  
  import java.io.BufferedReader;
  import java.io.File;
  import java.io.FileReader;
  import java.io.IOException;
  
  import org.apache.tools.ant.Task;
  import org.apache.tools.ant.Project;
  import org.apache.tools.ant.BuildException;
  
  import org.apache.tools.ant.types.Path;
  import org.apache.tools.ant.types.Reference;
  
  /**
   * Task that creates a referable path based on the contents of a descriptor
   * file. By referable we mean that we are emulating the following
   * construct:
   *
   * <pre>
   * <path id="classpath">
   *   <pathelement location="location1.jar"/>
   * </path>
   * </pre>
   *
   * You might have the following descriptor file:
   *
   * <pre>
   * ---------------------------------------------------
   * -- B U I L D
   * ---------------------------------------------------
   * A.jar
   * B.jar
   * 
   * ---------------------------------------------------
   * -- T E S T I N G
   * ---------------------------------------------------
   * C.jar
   * D.jar
   * </pre>
   *
   * 
   * Using the following construct:
   *
   * <pre>
   * <create-path
   *   pathId="classpath"
   *   pathDescriptor="deps.list"
   *   basedir="${lib.repo}"
   * />
   * </pre>
   *
   * You will create the following:
   *
   * A.jar:B.jar:C.jar:D.jar
   *
   * And this path can be referenced by using a <code>refid</code>
   * in subsequent tasks that can utilize references to paths.
   *
   * @author <a href="mailto:jason@zenplex.com">Jason van Zyl</a>
   * @version $Id: CreatePath.java,v 1.1 2002/02/12 14:32:42 jvanzyl Exp $
   *
   */
  public class CreatePath 
      extends Task
  {
      /**
       * source file, usually null
       */
      private File pathDescriptor = null;
  
      /**
       * what to do when it goes pear-shaped
       */
      private boolean failOnError = true;
  
      /**
       * Base directory used to prefix the entries in the path descriptor.
       */
      private File baseDir;
      
      /**
       * Path that is created from the elements of the path descriptor.
       */
      private Path path;
  
      /**
       * The id to use when creating a reference to the created path
       * in the project.
       */
      private String pathId;
  
      /**
       * Default constructor
       */
      public CreatePath()
      {
          path = new Path(project);
      }
      
      /**
       * Set the base directory used to prefix the entries in the 
       * path descriptor.
       *
       * @param baseDir Used as a prefix for each entry in the path
       *                descriptor.
       */
      public void setBasedir(File baseDir)
      {
          this.baseDir = baseDir;
      }
  
      /**
       * Id to use for creating a reference to the path
       * in the project.
       *
       * @param pathId Reference identifier to use in the call
       *               to project.addReference(pathId, path).
       */
      public void setPathid(String pathId)
      {
          this.pathId = pathId;
      }
  
      /**
       * Sets the pathDescriptor attribute.
       *
       * @param pathDescriptor The new pathDescriptor value
       */
      public void setPathdescriptor(File pathDescriptor)
      {
          this.pathDescriptor = pathDescriptor;
      }
  
      /**
       * Sets the Failonerror attribute of the LoadFile object
       *
       * @param fail The new Failonerror value
       */
      public void setFailonerror(boolean fail)
      {
          failOnError = fail;
      }
  
      /**
       * read in a source file to a property
       *
       * @exception BuildException if something goes wrong with the build
       */
      public void execute() 
          throws BuildException
      {
          // Make sure that we have a valid path descriptor.
          if (pathDescriptor == null)
          {
              throw new BuildException("Path descriptor file not defined");
          }
  
          String line;
          BufferedReader in = null;
          try
          {
              in = new BufferedReader(new FileReader(pathDescriptor));
              while ((line = in.readLine()) != null)
              {
                  line = line.trim();
  
                  // Allow comments and blank lines to be placed in the
                  // payload descriptor.
                  if (line.startsWith("#") || line.startsWith("--") || line.length() < 1)
                  {
                      continue;
                  }
  
                  // Create a new path for this JAR entry. This path will be
                  // added to the Ant project as a reference so that the 'refid'
                  // can be used in <classpath> constructs.
                  Path p = new Path(project);
                  
                  // If a base directory is specified then prefix this path
                  // with that specified base directory.
                  if (baseDir != null)
                  {
                      p.setPath(new File(baseDir, line).getAbsolutePath());
                  }
                  else
                  {
                      p.setPath(line);
                  }
                  
                  // We want all the paths specified in the descriptor file to
                  // be available by refid so we will append each of the individual
                  // paths to the top-level path in much the same way that the
                  // core <path> construct works.
                  path.append(p);
              }
              
              // Add the top-level path as a reference to the project.
              project.addReference(pathId, path);
          }
          catch (IOException ioe)
          {
              String message = "Unable to load file: " + ioe.toString();
              if (failOnError)
              {
                  throw new BuildException(message, ioe, location);
              }
              else
              {
                  log(message, Project.MSG_ERR);
              }
          }
          finally 
          { 
              try
              {
                  if (in != null)
                  {
                            in.close();
                  }
              }
              catch (IOException ioex)
              {
              }
          } 
      }
  }
  
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>