craigmcc    01/08/13 14:17:08

  Added:       workflow/src/java/org/apache/commons/workflow/io
                        DisplayStep.java GetStep.java PeekStep.java
                        ReadStep.java WriteStep.java
  Log:
  Initial check-in of the workflow management system proposal.
  
  Revision  Changes    Path
  1.1                  
jakarta-commons-sandbox/workflow/src/java/org/apache/commons/workflow/io/DisplayStep.java
  
  Index: DisplayStep.java
  ===================================================================
  /*
   * $Header: 
/home/cvs/jakarta-commons-sandbox/workflow/src/java/org/apache/commons/workflow/io/DisplayStep.java,v
 1.1 2001/08/13 21:17:08 craigmcc Exp $
   * $Revision: 1.1 $
   * $Date: 2001/08/13 21:17:08 $
   *
   * ====================================================================
   * 
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2001 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", "Commons", 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 [EMAIL PROTECTED]
   *
   * 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/>.
   *
   */ 
  
  package org.apache.commons.workflow.io;
  
  
  import org.apache.commons.workflow.Context;
  import org.apache.commons.workflow.StepException;
  import org.apache.commons.workflow.base.BaseStep;
  
  
  /**
   * <p>Print the value of the specified bean in the specified scope to
   * standard ouput.</p>
   *
   * <p><strong>WARNING</strong> - This will probably be
   * replaced later by a more general purpose input/output mechanism.</p>
   *
   * <p>Supported Attributes:</p>
   * <ul>
   * <li><strong>name</strong> - Name under which the requested bean is retrieved
   *     in the specified scope.</li>
   * <li><strong>scope</strong> - Identifier of the scope in which to search for
   *     this bean, or omitted to search for the bean in any scope.</li>
   * </ul>
   *
   * @version $Revision: 1.1 $ $Date: 2001/08/13 21:17:08 $
   * @author Craig R. McClanahan
   */
  
  public class DisplayStep extends BaseStep {
  
  
      // ------------------------------------------------------------- Properties
  
  
      /**
       * The name of the bean to be copied.
       */
      protected String name = null;
  
      public String getName() {
          return (this.name);
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
  
      /**
       * The scope containing the bean to be copied.
       */
      public String scope = null;
  
      public String getScope() {
          return (this.scope);
      }
  
      public void setScope(String scope) {
          this.scope = scope;
      }
  
  
      // --------------------------------------------------------- Public Methods
  
  
      /**
       * Perform the executable actions related to this Step, in the context of
       * the specified Context.
       *
       * @param context The Context that is tracking our execution state
       *
       * @exception StepException if a processing error has occurred
       */
      public void execute(Context context) throws StepException {
  
          // Identify the scope we are interested in
          int scopeId = -1;
          if (scope != null) {
              scopeId = context.getScopeId(scope);
              if (scopeId < 0)
                  throw new StepException("Cannot find scope '" + scope + "'",
                                          this);
          }
  
          // Retrieve the specified bean value
          Object value = null;
          if (scopeId < 0)
              value = context.get(name);
          else
              value = context.get(name, scopeId);
          if (value == null)
              throw new StepException("Cannot find bean '" + name + "'", this);
  
          // Write the bean value to standard output
          System.out.println(value);
  
      }
  
  
  }
  
  
  
  1.1                  
jakarta-commons-sandbox/workflow/src/java/org/apache/commons/workflow/io/GetStep.java
  
  Index: GetStep.java
  ===================================================================
  /*
   * $Header: 
/home/cvs/jakarta-commons-sandbox/workflow/src/java/org/apache/commons/workflow/io/GetStep.java,v
 1.1 2001/08/13 21:17:08 craigmcc Exp $
   * $Revision: 1.1 $
   * $Date: 2001/08/13 21:17:08 $
   *
   * ====================================================================
   * 
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2001 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", "Commons", 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 [EMAIL PROTECTED]
   *
   * 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/>.
   *
   */ 
  
  package org.apache.commons.workflow.io;
  
  
  import java.io.BufferedInputStream;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  import java.io.IOException;
  import java.net.MalformedURLException;
  import java.net.URL;
  import java.net.URLConnection;
  import java.util.EmptyStackException;
  import org.apache.commons.workflow.Context;
  import org.apache.commons.workflow.StepException;
  import org.apache.commons.workflow.base.BaseStep;
  import org.apache.commons.workflow.util.WorkflowUtils;
  
  
  /**
   * <p>Retrieve the contents of a specified URL resource, and push the
   * contents as a String object onto the evaluation stack.</p>
   *
   * <p>Supported Attributes:</p>
   * <ul>
   * <li><strong>url</strong> - URL of the resource to be retrieved, or
   *     omitted to pop a computed String value from the top of the
   *     evaluation stack.</li>
   * </ul>
   *
   * <strong>DESIGN QUESTION - What about binary content?</strong>
   *
   * @version $Revision: 1.1 $ $Date: 2001/08/13 21:17:08 $
   * @author Craig R. McClanahan
   */
  
  public class GetStep extends BaseStep {
  
  
      // ------------------------------------------------------------- Properties
  
  
      /**
       * The URL of the resource to be retrieved.
       */
      protected String url = null;
  
      public String getUrl() {
          return (this.url);
      }
  
      public void setUrl(String url) {
          this.url = url;
      }
  
  
      // --------------------------------------------------------- Public Methods
  
  
      /**
       * Perform the executable actions related to this Step, in the context of
       * the specified Context.
       *
       * @param context The Context that is tracking our execution state
       *
       * @exception StepException if a processing error has occurred
       */
      public void execute(Context context) throws StepException {
  
          // Get the remote URL we will be contacting
          Object remote = this.url;
          URL remoteURL = null;
          if (remote == null) {
              try {
                  remote = (String) context.pop();
              } catch (EmptyStackException e) {
                  throw new StepException("Evaluation stack is empty", this);
              }
          }
          if (remote instanceof URL) {
              remoteURL = (URL) remote;
          } else if (remote instanceof String) {
              try {
                  remoteURL = new URL((String) remote);
              } catch (MalformedURLException e) {
                  throw new StepException("Invalid URL '" + remote + "'",
                                          e, this);
              }
          } else {
              try {
                  remoteURL = new URL(remote.toString());
              } catch (MalformedURLException e) {
                  throw new StepException("Invalid URL '" + remote + "'",
                                          e, this);
              }
          }
  
          // Define variables we will need later
          URLConnection conn = null;
          InputStream is = null;
          BufferedInputStream bis = null;
          InputStreamReader isr = null;
          StringBuffer sb = null;
          StepException se = null;
  
          try {
  
              // Open a connection to the specified URL
              conn = remoteURL.openConnection();
              conn.setDoInput(true);
              conn.setDoOutput(false);
              conn.connect();
              int contentLength = conn.getContentLength();
              if (contentLength < 2048)
                  contentLength = 2048;
              sb = new StringBuffer(contentLength);
  
              // Parse the character encoding to be used
              String contentType = conn.getContentType();
              String encoding =
                  WorkflowUtils.parseCharacterEncoding(contentType);
  
              // Construct a suitable InputStreamReader
              is = conn.getInputStream();
              bis = new BufferedInputStream(is, 2048);
              if (encoding == null)
                  isr = new InputStreamReader(bis);
              else
                  isr = new InputStreamReader(bis, encoding);
  
              // Copy all characters from this resource
              while (true) {
                  int ch = isr.read();
                  if (ch < 0)
                      break;
                  sb.append((char) ch);
              }
  
              // Close the input file
              isr.close();
              isr = null;
              bis = null;
              is = null;
  
          } catch (IOException e) {
  
              se = new StepException("IOException processing '" + remoteURL +
                                     "'", e, this);
  
          } finally {
  
              if (isr != null) {
                  try {
                      isr.close();
                  } catch (Throwable t) {
                      ;
                  }
              } else if (bis != null) {
                  try {
                      bis.close();
                  } catch (Throwable t) {
                      ;
                  }
              } else if (is != null) {
                  try {
                      is.close();
                  } catch (Throwable t) {
                      ;
                  }
              }
  
          }
  
          // Push results or throw exception as appropriate
          if (se != null)
              throw se;
          context.push(sb.toString());
  
      }
  
  
  }
  
  
  
  1.1                  
jakarta-commons-sandbox/workflow/src/java/org/apache/commons/workflow/io/PeekStep.java
  
  Index: PeekStep.java
  ===================================================================
  /*
   * $Header: 
/home/cvs/jakarta-commons-sandbox/workflow/src/java/org/apache/commons/workflow/io/PeekStep.java,v
 1.1 2001/08/13 21:17:08 craigmcc Exp $
   * $Revision: 1.1 $
   * $Date: 2001/08/13 21:17:08 $
   *
   * ====================================================================
   * 
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2001 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", "Commons", 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 [EMAIL PROTECTED]
   *
   * 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/>.
   *
   */ 
  
  package org.apache.commons.workflow.io;
  
  
  import java.util.EmptyStackException;
  import org.apache.commons.workflow.Context;
  import org.apache.commons.workflow.StepException;
  import org.apache.commons.workflow.base.BaseStep;
  
  
  /**
   * <p>Write the top value from the evaluation stack to standard output,
   * without removing it.</p>
   *
   * <p><strong>WARNING</strong> - This will probably be
   * replaced later by a more general purpose input/output mechanism.</p>
   *
   * @version $Revision: 1.1 $ $Date: 2001/08/13 21:17:08 $
   * @author Craig R. McClanahan
   */
  
  public class PeekStep extends BaseStep {
  
  
      // --------------------------------------------------------- Public Methods
  
  
      /**
       * Perform the executable actions related to this Step, in the context of
       * the specified Context.
       *
       * @param context The Context that is tracking our execution state
       *
       * @exception StepException if a processing error has occurred
       */
      public void execute(Context context) throws StepException {
  
          // Retrieve the top value from the evaluation stack
          Object value = null;
          try {
              value = context.pop();
          } catch (EmptyStackException e) {
              throw new StepException("Evaluation stack is empty", e, this);
          }
  
          // Display it to standard output
          System.out.println(value);
  
      }
  
  
  }
  
  
  
  1.1                  
jakarta-commons-sandbox/workflow/src/java/org/apache/commons/workflow/io/ReadStep.java
  
  Index: ReadStep.java
  ===================================================================
  /*
   * $Header: 
/home/cvs/jakarta-commons-sandbox/workflow/src/java/org/apache/commons/workflow/io/ReadStep.java,v
 1.1 2001/08/13 21:17:08 craigmcc Exp $
   * $Revision: 1.1 $
   * $Date: 2001/08/13 21:17:08 $
   *
   * ====================================================================
   * 
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2001 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", "Commons", 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 [EMAIL PROTECTED]
   *
   * 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/>.
   *
   */ 
  
  package org.apache.commons.workflow.io;
  
  import java.io.BufferedInputStream;
  import java.io.FileInputStream;
  import java.io.InputStreamReader;
  import java.io.IOException;
  import org.apache.commons.workflow.Context;
  import org.apache.commons.workflow.StepException;
  import org.apache.commons.workflow.base.BaseStep;
  
  
  /**
   * <p>Read the contents of the specified file from the filesystem, and
   * push the contents as a String object onto the evaluation stack.</p>
   *
   * <p>Supported Attributes:</p>
   * <ul>
   * <li><strong>encoding</strong> - Character encoding in which to interpret
   *     the characters in the specified file, or omitted for the platform
   *     default encoding.</li>
   * <li><strong>file</strong> - Relative or absolute operating system pathname
   *     whose contents are to be read.</li>
   * </ul>
   *
   * <strong>DESIGN QUESTION - What about binary content?</strong>
   *
   * @version $Revision: 1.1 $ $Date: 2001/08/13 21:17:08 $
   * @author Craig R. McClanahan
   */
  
  public class ReadStep extends BaseStep {
  
  
      // ------------------------------------------------------------- Properties
  
  
      /**
       * The character encoding used to interpret the contents of this file.
       */
      protected String encoding = null;
  
      public String getEncoding() {
          return (this.encoding);
      }
  
      public void setEncoding(String encoding) {
          this.encoding = encoding;
      }
  
  
      /**
       * The relative or absolute pathname of the operating system file.
       */
      protected String file = null;
  
      public String getFile() {
          return (this.file);
      }
  
      public void setFile(String file) {
          this.file = file;
      }
  
  
      // --------------------------------------------------------- Public Methods
  
  
      /**
       * Perform the executable actions related to this Step, in the context of
       * the specified Context.
       *
       * @param context The Context that is tracking our execution state
       *
       * @exception StepException if a processing error has occurred
       */
      public void execute(Context context) throws StepException {
  
          // Define variables we will need later
          FileInputStream fis = null;
          BufferedInputStream bis = null;
          InputStreamReader isr = null;
          StringBuffer sb = new StringBuffer();
          StepException se = null;
  
          try {
  
              // Construct a suitable InputStreamReader
              fis = new FileInputStream(file);
              bis = new BufferedInputStream(fis, 2048);
              if (encoding == null)
                  isr = new InputStreamReader(bis);
              else
                  isr = new InputStreamReader(bis, encoding);
  
              // Copy all characters from this file
              while (true) {
                  int ch = isr.read();
                  if (ch < 0)
                      break;
                  sb.append((char) ch);
              }
  
              // Close the input file
              isr.close();
              isr = null;
              bis = null;
              fis = null;
  
          } catch (IOException e) {
  
              se = new StepException("IOException processing '" + file + "'",
                                     e, this);
  
          } finally {
  
              if (isr != null) {
                  try {
                      isr.close();
                  } catch (Throwable t) {
                      ;
                  }
              } else if (bis != null) {
                  try {
                      bis.close();
                  } catch (Throwable t) {
                      ;
                  }
              } else if (fis != null) {
                  try {
                      fis.close();
                  } catch (Throwable t) {
                      ;
                  }
              }
  
          }
  
          // Push results or throw exception as appropriate
          if (se != null)
              throw se;
          context.push(sb.toString());
  
      }
  
  
  }
  
  
  
  1.1                  
jakarta-commons-sandbox/workflow/src/java/org/apache/commons/workflow/io/WriteStep.java
  
  Index: WriteStep.java
  ===================================================================
  /*
   * $Header: 
/home/cvs/jakarta-commons-sandbox/workflow/src/java/org/apache/commons/workflow/io/WriteStep.java,v
 1.1 2001/08/13 21:17:08 craigmcc Exp $
   * $Revision: 1.1 $
   * $Date: 2001/08/13 21:17:08 $
   *
   * ====================================================================
   * 
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2001 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", "Commons", 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 [EMAIL PROTECTED]
   *
   * 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/>.
   *
   */ 
  
  package org.apache.commons.workflow.io;
  
  import java.io.BufferedOutputStream;
  import java.io.File;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.io.OutputStreamWriter;
  import java.util.EmptyStackException;
  import org.apache.commons.workflow.Context;
  import org.apache.commons.workflow.StepException;
  import org.apache.commons.workflow.base.BaseStep;
  
  
  /**
   * <p>Pop the top value from the evaluation stack, and write its contents
   * as a string to the specified file in the filesystem (replacing any
   * previous contents in that file).</p>
   *
   * <p>Supported Attributes:</p>
   * <ul>
   * <li><strong>encoding</strong> - Character encoding in which to write
   *     the characters to the specified file, or omitted for the platform
   *     default encoding.</li>
   * <li><strong>file</strong> - Relative or absolute operating system pathname
   *     whose contents are to be written.</li>
   * </ul>
   *
   * <strong>DESIGN QUESTION - What about binary content?</strong>
   *
   * @version $Revision: 1.1 $ $Date: 2001/08/13 21:17:08 $
   * @author Craig R. McClanahan
   */
  
  public class WriteStep extends BaseStep {
  
  
      // ------------------------------------------------------------- Properties
  
  
      /**
       * The character encoding used to write the contents of this file.
       */
      protected String encoding = null;
  
      public String getEncoding() {
          return (this.encoding);
      }
  
      public void setEncoding(String encoding) {
          this.encoding = encoding;
      }
  
  
      /**
       * The relative or absolute pathname of the operating system file.
       */
      protected String file = null;
  
      public String getFile() {
          return (this.file);
      }
  
      public void setFile(String file) {
          this.file = file;
      }
  
  
      // --------------------------------------------------------- Public Methods
  
  
      /**
       * Perform the executable actions related to this Step, in the context of
       * the specified Context.
       *
       * @param context The Context that is tracking our execution state
       *
       * @exception StepException if a processing error has occurred
       */
      public void execute(Context context) throws StepException {
  
          // Pop the top value from the evaluation stack
          Object value = null;
          try {
              value = context.pop();
          } catch (EmptyStackException e) {
              throw new StepException("Evaluation stack is empty", e, this);
          }
          String string = null;
          if (value instanceof String)
              string = (String) value;
          else
              string = value.toString();
  
          // Define variables we will need later
          FileOutputStream fos = null;
          BufferedOutputStream bos = null;
          OutputStreamWriter osw = null;
          StepException se = null;
  
          try {
  
              // Construct a suitable OutputStreamWriter
              fos = new FileOutputStream(file);
              bos = new BufferedOutputStream(fos, 2048);
              if (encoding == null)
                  osw = new OutputStreamWriter(bos);
              else
                  osw = new OutputStreamWriter(bos, encoding);
  
              // Copy all characters to this file
              osw.write(string, 0, string.length());
  
              // Close the output file
              osw.flush();
              osw.close();
              osw = null;
              bos = null;
              fos = null;
  
          } catch (IOException e) {
  
              se = new StepException("IOException processing '" + file + "'",
                                     e, this);
  
          } finally {
  
              if (osw != null) {
                  try {
                      osw.close();
                  } catch (Throwable t) {
                      ;
                  }
              } else if (bos != null) {
                  try {
                      bos.close();
                  } catch (Throwable t) {
                      ;
                  }
              } else if (fos != null) {
                  try {
                      fos.close();
                  } catch (Throwable t) {
                      ;
                  }
              }
  
              if (se != null)
                  (new File(file)).delete();
  
          }
  
      }
  
  
  }
  
  
  

Reply via email to