« Extending TN's bizdocToRecord | Main | NerdDinner »
Thursday
30Apr2009

writeFile service for webMethods Integration Server

Curiously, Integration Server doesn't come with a service to write to a file. People have shared their implementations on wMUsers.com and elsewhere. The professional services group at webMethods provides several file writing services in their PSUtilties package.

Here's another implementation. It writes fileData to the fully qualified fileName. The fileData is appended if the append var is set to "true".

The fileData can be a String, a byte array or a java.io.InputStream. If fileData is an InputStream it is not closed. 

// IS Java service
//
//
public static final void writeFile (IData pipeline)
    throws ServiceException
{
  // --- <> ---
  // @sigtype java 3.5
  // [i] field:0:required fileName
  // [i] object:0:required fileData
  // [i] field:0:required append {"false","true"}
  java.io.FileOutputStream outFile = null;
  IDataCursor idc = pipeline.getCursor();
  boolean append = IDataUtil.getBoolean(idc, "append");

  try
  {
    String fileName;
    byte fileData[] = null;
    java.io.InputStream is = null;

    //Get the file name
    if (idc.last("fileName"))
    {    
      fileName = IDataUtil.getString(idc);

      //Get the data
      if (idc.last("fileData"))
      {    
        Object val = idc.getValue();
        if(val instanceof java.io.InputStream)
        {
          is = (java.io.InputStream)val;
        }
        else if(val instanceof String)
        {    
          fileData = IDataUtil.getString(idc).getBytes();
        }
        else if(val instanceof byte[])
        {
          fileData = (byte[])idc.getValue();
        }
        else
        {
          throw new ServiceException("Unsupported type.");
        }

        //Start the output stream
        outFile = new java.io.FileOutputStream(fileName, append);
        if(fileData != null)
        {
          outFile.write (fileData);
        }
        else
        {
          //outFile.write("STREAM".getBytes());
          fileData = new byte[1024];
          int bytesread = 0;
          while((bytesread=is.read(fileData)) > 0)
          {
            outFile.write(fileData, 0, bytesread);
          }
        }
        outFile.flush();
        outFile.close();
      }
    }
  }
  catch( Exception ex )
  {
    if(outFile != null)
    {
      try
      {
        outFile.close();
        outFile = null;
      }
      catch (java.io.IOException ignored) { }
    }
    throw (ServiceException)ex;
  }

  idc.destroy();
  // --- <> ---
}

Place this within your own utilities package to use how you see fit.

PrintView Printer Friendly Version

EmailEmail Article to Friend

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>