Package com.acelet.s.job

Source Code of com.acelet.s.job.OperatingSystemJob

/* Copyright 1999-2008 Acelet.org. All rights reserved. GPL v2 license */
/** @author Wei Jiang */

package com.acelet.s.job;

import java.io.*;
import java.util.*;

import com.acelet.lib.Execute;
import com.acelet.lib.Kit;
import com.acelet.s.task.*;
import com.acelet.lib.Common;

public class OperatingSystemJob extends Job {
  protected String[] commandArray;
  protected String[] envArray;
  protected String dirName;
  protected Execute execute;

  public OperatingSystemJob() {
  }

  public void init(AbstractTask abstractTask, Object object) throws Exception
    init(abstractTask);

    CommandObject commandObject = new CommandObject();
    commandObject.fromXmlText(jobXmlString);

    String command = commandObject.command;
    StringTokenizer st = new StringTokenizer(command, " ");
    int size = st.countTokens();
    commandArray = new String[size];
    for (int i = 0; i < size; i++)
      commandArray[i] = st.nextToken();

    String env = commandObject.env;
    StringTokenizer envStringTokenizer = new StringTokenizer(env, ",");
    int count = envStringTokenizer.countTokens();
    if (count > 0) {
      envArray = new String[count];
      for (int i = 0; i < count; i++)
        envArray[i] = envStringTokenizer.nextToken().trim();
    }

    dirName = new String(commandObject.dirName);
    if (dirName.trim().length() == 0)
      dirName = null;
  }

  public void kill() throws Exception {
    isKilled = true;

    if (execute != null) {
      execute.kill();
    }
  }

  public void run() {
    try {
      StringBuffer errBuffer = new StringBuffer("");
      StringBuffer outBuffer = new StringBuffer("");

      execute = new Execute();
      int exitCode = execute.execute(commandArray, envArray, dirName, errBuffer, outBuffer);

      String error = errBuffer.toString().trim();
      if (error.length() > 0)
        error += "; ";

      if (exitCode != 0) {
        error = exitCode + "; " + error;
        anyError = true;
      }

      resultObject = error + outBuffer.toString();
    } catch (Throwable th) {
      anyError = true;
      throwable = th;
      resultObject = th;
    }
  }
}
TOP

Related Classes of com.acelet.s.job.OperatingSystemJob

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.