You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by mc...@apache.org on 2003/03/06 13:00:53 UTC

cvs commit: avalon-sandbox/assembly/src/java/org/apache/avalon/assembly/engine/model Classpath.java

mcconnell    2003/03/06 04:00:52

  Added:       assembly/src/java/org/apache/avalon/assembly/engine/model
                        Classpath.java
  Log:
  A classpath meta-data directive that captures the classpath descriptor meta-info together with a base directory for classpath resolution.
  
  Revision  Changes    Path
  1.1                  avalon-sandbox/assembly/src/java/org/apache/avalon/assembly/engine/model/Classpath.java
  
  Index: Classpath.java
  ===================================================================
  /* ==================================================================== 
   * The Apache Software License, Version 1.1 
   * 
   * Copyright (c) 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 acknowledgment:
   *    "This product includes software developed by the
   *    Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software 
   *    itself, if and wherever such third-party acknowledgments  
   *    normally appear.
   *
   * 4. The names "Jakarta", "Avalon", 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 name, without prior written
   *    permission of the Apache Software Foundation.
   *
   * 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/>.
   */ 
  
  package org.apache.avalon.assembly.engine.model;
  
  import java.io.File;
  import java.io.Serializable;
  import java.util.ArrayList;
  import java.util.List;
  import java.net.URL;
  import java.net.MalformedURLException;
  
  /**
   * <p>A classpath directive that describes a scoped set of jar files and 
   * a relative base directory.
   *
   * @see ClasspathDescriptor
   * @author <a href="mailto:mcconnell@apache.org">Stephen McConnell</a>
   * @version $Revision: 1.1 $ $Date: 2003/03/06 12:00:51 $
   */
  public class Classpath implements Serializable
  {
      /**
       * The classpath descriptor.
       */
      private final ClasspathDescriptor m_classpath;
  
      /**
       * The directory from which classpath entries will be resolved.
       */
      private final File m_base;
  
      /**
       * Create a new Classpath instance.
       * @param base the base directory from which classpath fileset directories 
       *   shall be resolved.
       * @param classpath the classpath descriptor
       */
      public Classpath( File base, ClasspathDescriptor classpath )
      {
          m_base = base;
          m_classpath = classpath;
      }
  
      /**
       * Create a new Classpath instance.
       * @param a url from which the base directory will be resolved
       * @param classpath the classpath descriptor
       * @exception IllegalArgumentException if the base directory cannot 
       *   be resolved from the supplied URL 
       */
      public Classpath( URL url, ClasspathDescriptor classpath )
      {
          m_base = getBaseDirectory( url );
          m_classpath = classpath;
      }
  
      /**
       * Return the classpath descriptor.
       *
       * @return the classpath descriptor
       */
      public ClasspathDescriptor getClasspathDescriptor()
      {
          return m_classpath;
      }
  
      /**
       * Expand a classpath to an array of URLS.
       * @param home the base directory from which relative classpath entries
       * will be resolved.
       */
      public URL[] expand( )
      {
          List list = new ArrayList();
          FilesetDescriptor[] dirs = m_classpath.getFilesetDescriptors();
          for( int i = 0; i < dirs.length; i++ )
          {
              FilesetDescriptor descriptor = dirs[ i ];
              File anchor = new File( m_base, descriptor.getBaseDirectory() );
              if( !anchor.exists() )
              {
                  final String error =
                    "Fileset base directory does not exist: " 
                    + anchor;
                  throw new ClasspathRuntimeException( error );
              }
  
              IncludeDescriptor[] includes = descriptor.getIncludeDescriptors();
              for( int j = 0; j < includes.length; j++ )
              {
                  String inc = includes[ j ].getFile();
                  try
                  {
                      URL url = new File( anchor, inc ).getCanonicalFile().toURL();
                      list.add( url );
                  }
                  catch( Throwable e )
                  {
                      final String error = 
                        "Error processing a classpath include: " 
                        + inc;
                      throw new ClasspathRuntimeException( error, e );
                  }
              }
          }
          return (URL[]) list.toArray( new URL[0] );
      }
  
     /**
      * Resolves a supplied URL to a directory.
      * @param url the url to resolve
      * @return the base directory file
      * @exception IllegalArgumentException if the supplied URL 
      *    cannot be resolved to a directory
      */
      private static File getBaseDirectory( final URL url )
      {
          File base = null;
          if( url.getProtocol().equals( "file" ) )
          {
              File file = new File( url.toString().substring( 5 ) );
              if( file.isFile() )
              {
                  base = file.getParentFile();
              }
              else
              {
                  base = file;
              }
          }
          if( base.isDirectory() )
          {
              return base;
          }
          else
          {
              final String error = 
                "Cannot resolve base directory from supplied URL: " + url;
              throw new IllegalArgumentException( error );
          }
      }
  
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: cvs-unsubscribe@avalon.apache.org
For additional commands, e-mail: cvs-help@avalon.apache.org