donaldp     01/08/29 08:48:00

  Modified:    
proposal/myrmidon/src/java/org/apache/myrmidon/components/workspace
                        DefaultWorkspace.java
  Added:       
proposal/myrmidon/src/java/org/apache/myrmidon/components/workspace
                        DefaultTaskContext.java
  Removed:     proposal/myrmidon/src/java/org/apache/myrmidon/api
                        DefaultTaskContext.java
  Log:
  Moved DefaultTaskContext class to workspace package as it is only used in 
that package.
  
  Revision  Changes    Path
  1.6       +0 -1      
jakarta-ant/proposal/myrmidon/src/java/org/apache/myrmidon/components/workspace/DefaultWorkspace.java
  
  Index: DefaultWorkspace.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-ant/proposal/myrmidon/src/java/org/apache/myrmidon/components/workspace/DefaultWorkspace.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- DefaultWorkspace.java     2001/08/29 15:34:58     1.5
  +++ DefaultWorkspace.java     2001/08/29 15:48:00     1.6
  @@ -28,7 +28,6 @@
   import org.apache.avalon.framework.parameters.Parameters;
   import org.apache.log.Hierarchy;
   import org.apache.log.Logger;
  -import org.apache.myrmidon.api.DefaultTaskContext;
   import org.apache.myrmidon.api.TaskContext;
   import org.apache.myrmidon.api.TaskException;
   import org.apache.myrmidon.components.deployer.DefaultDeployer;
  
  
  
  1.1                  
jakarta-ant/proposal/myrmidon/src/java/org/apache/myrmidon/components/workspace/DefaultTaskContext.java
  
  Index: DefaultTaskContext.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 file.
   */
  package org.apache.myrmidon.components.workspace;
  
  import java.io.File;
  import java.util.Map;
  import org.apache.avalon.excalibur.io.FileUtil;
  import org.apache.avalon.excalibur.property.PropertyException;
  import org.apache.avalon.excalibur.property.PropertyUtil;
  import org.apache.avalon.framework.context.ContextException;
  import org.apache.avalon.framework.context.DefaultContext;
  import org.apache.myrmidon.api.JavaVersion;
  import org.apache.myrmidon.api.TaskContext;
  import org.apache.myrmidon.api.TaskException;
  
  /**
   * Default implementation of TaskContext.
   *
   * @author <a href="mailto:[EMAIL PROTECTED]">Peter Donald</a>
   */
  public class DefaultTaskContext
      extends DefaultContext
      implements TaskContext
  {
      /**
       * Constructor for Context with no parent contexts.
       */
      public DefaultTaskContext( final Map contextData )
      {
          super( contextData );
      }
  
      /**
       * Constructor for Context with no parent contexts.
       */
      public DefaultTaskContext()
      {
          this( (TaskContext)null );
      }
  
      /**
       * Constructor.
       */
      public DefaultTaskContext( final TaskContext parent )
      {
          super( parent );
      }
  
      /**
       * Retrieve JavaVersion running under.
       *
       * @return the version of JVM
       */
      public JavaVersion getJavaVersion()
      {
          try { return (JavaVersion)get( JAVA_VERSION ); }
          catch( final ContextException ce )
          {
              throw new IllegalStateException( "No JavaVersion in Context" );
          }
      }
  
  
      /**
       * Retrieve Name of tasklet.
       *
       * @return the name
       */
      public String getName()
      {
          try { return (String)get( NAME ); }
          catch( final ContextException ce )
          {
              throw new IllegalStateException( "No Name in Context" );
          }
      }
  
      /**
       * Retrieve base directory.
       *
       * @return the base directory
       */
      public File getBaseDirectory()
      {
          try { return (File)get( BASE_DIRECTORY ); }
          catch( final ContextException ce )
          {
              throw new IllegalStateException( "No Base Directory in Context" );
          }
      }
  
      /**
       * Resolve filename.
       * This involves resolving it against baseDirectory and
       * removing ../ and ./ references. It also means formatting
       * it appropriately for the particular OS (ie different OS have
       * different volumes, file conventions etc)
       *
       * @param filename the filename to resolve
       * @return the resolved filename
       */
      public File resolveFile( final String filename )
      {
          return FileUtil.resolveFile( getBaseDirectory(), filename );
      }
  
      /**
       * Retrieve property for name.
       *
       * @param name the name of property
       * @return the value of the property
       */
      public Object getProperty( final String name )
      {
          try { return get( name ); }
          catch( final ContextException ce )
          {
              return null;
          }
      }
  
      /**
       * Set property value in current context.
       *
       * @param name the name of property
       * @param value the value of property
       */
      public void setProperty( final String name, final Object value )
          throws TaskException
      {
          setProperty( name, value, CURRENT );
      }
  
      /**
       * Set property value.
       *
       * @param property the property
       */
      public void setProperty( final String name, final Object value, final 
ScopeEnum scope )
          throws TaskException
      {
          checkPropertyValid( name, value );
  
          if( CURRENT == scope ) put( name, value );
          else if( PARENT == scope )
          {
              if( null == getParent() )
              {
                  throw new TaskException( "Can't set a property with parent 
scope when context " +
                                           " has no parent" );
              }
              else
              {
                  ((TaskContext)getParent()).setProperty( name, value );
              }
          }
          else if( TOP_LEVEL == scope )
          {
              DefaultTaskContext context = this;
  
              while( null != context.getParent() )
              {
                  context = (DefaultTaskContext)context.getParent();
              }
  
              context.put( name, value );
          }
          else
          {
              throw new IllegalStateException( "Unknown property scope! (" + 
scope + ")" );
          }
      }
  
      /**
       * Create a Child Context.
       * This allows separate hierarchly contexts to be easily constructed.
       *
       * @param name the name of sub-context
       * @return the created TaskContext
       * @exception TaskException if an error occurs
       */
      public TaskContext createSubContext( final String name )
          throws TaskException
      {
          final DefaultTaskContext context = new DefaultTaskContext( this );
  
          context.setProperty( TaskContext.NAME, getName() + "." + name );
          context.setProperty( TaskContext.BASE_DIRECTORY, getBaseDirectory() );
          context.setProperty( TaskContext.JAVA_VERSION, getJavaVersion() );
  
          return context;
      }
  
      /**
       * Make sure property is valid if it is one of the "magic" properties.
       *
       * @param name the name of property
       * @param value the value of proeprty
       * @exception TaskException if an error occurs
       */
      protected void checkPropertyValid( final String name, final Object value )
          throws TaskException
      {
          if( BASE_DIRECTORY.equals( name ) && !( value instanceof File ) )
          {
              throw new TaskException( "Property " + BASE_DIRECTORY +
                                       " must have a value of type " +
                                       File.class.getName() );
          }
          else if( NAME.equals( name ) && !( value instanceof String ) )
          {
              throw new TaskException( "Property " + NAME +
                                       " must have a value of type " +
                                       String.class.getName() );
          }
          else if( JAVA_VERSION.equals( name ) && !( value instanceof 
JavaVersion ) )
          {
              throw new TaskException( "Property " + JAVA_VERSION +
                                       " must have a value of type " +
                                       JavaVersion.class.getName() );
          }
      }
  }
  
  
  

Reply via email to