Package sos.scheduler.CheckRunHistory

Source Code of sos.scheduler.CheckRunHistory.JobSchedulerCheckRunHistory

/********************************************************* begin of preamble
**
** Copyright (C) 2003-2010 Software- und Organisations-Service GmbH.
** All rights reserved.
**
** This file may be used under the terms of either the
**
**   GNU General Public License version 2.0 (GPL)
**
**   as published by the Free Software Foundation
**   http://www.gnu.org/licenses/gpl-2.0.txt and appearing in the file
**   LICENSE.GPL included in the packaging of this file.
**
** or the
** 
**   Agreement for Purchase and Licensing
**
**   as offered by Software- und Organisations-Service GmbH
**   in the respective terms of supply that ship with this file.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
** IS" AND ANY EXPRESS 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 COPYRIGHT OWNER OR 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.
********************************************************** end of preamble*/
package sos.scheduler.CheckRunHistory;
import java.math.BigInteger;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import sos.scheduler.job.IJSCommands;
import sos.spooler.Mail;
import sos.spooler.Spooler;
import sos.util.SOSDate;

import com.sos.JSHelper.Basics.JSJobUtilities;
import com.sos.JSHelper.Basics.JSToolBox;
import com.sos.JSHelper.Exceptions.JobSchedulerException;
import com.sos.localization.Messages;
import com.sos.scheduler.model.SchedulerObjectFactory;
import com.sos.scheduler.model.answers.Answer;
import com.sos.scheduler.model.answers.ERROR;
import com.sos.scheduler.model.answers.HistoryEntry;
import com.sos.scheduler.model.commands.JSCmdShowHistory;
import com.sos.scheduler.model.commands.ShowHistory;
import com.sos.scheduler.model.exceptions.JSCommandErrorException;

/**
* \class     JobSchedulerCheckRunHistory - Workerclass for "Check the last job run"
*
* \brief AdapterClass of JobSchedulerCheckRunHistory for the SOSJobScheduler
*
* This Class JobSchedulerCheckRunHistory is the worker-class.
*

*
* see \see J:\E\java\development\com.sos.scheduler\src\sos\scheduler\jobdoc\JobSchedulerCheckRunHistory.xml for (more) details.
*
* \verbatim ;
* mechanicaly created by C:\Users\KB\eclipse\xsl\JSJobDoc2JSWorkerClass.xsl from http://www.sos-berlin.com at 20110224143604
* \endverbatim
*/
public class JobSchedulerCheckRunHistory extends JSToolBox implements JSJobUtilities, IJSCommands {
  private final String              conClassName    = "JobSchedulerCheckRunHistory";            //$NON-NLS-1$
  private static Logger              logger        = Logger.getLogger(JobSchedulerCheckRunHistory.class);
  protected JobSchedulerCheckRunHistoryOptions  objOptions      = null;
  private JSJobUtilities              objJSJobUtilities  = this;
  private IJSCommands                objJSCommands    = this;

  /**
   *
   * \brief JobSchedulerCheckRunHistory
   *
   * \details
   *
   */
  public JobSchedulerCheckRunHistory() {
    super();
  }

  /**
   *
   * \brief Options - returns the JobSchedulerCheckRunHistoryOptionClass
   *
   * \details
   * The JobSchedulerCheckRunHistoryOptionClass is used as a Container for all Options (Settings) which are
   * needed.
   * 
   * \return JobSchedulerCheckRunHistoryOptions
   *
   */
  public JobSchedulerCheckRunHistoryOptions Options() {
    @SuppressWarnings("unused")//$NON-NLS-1$
    final String conMethodName = conClassName + "::Options"; //$NON-NLS-1$
    if (objOptions == null) {
      objOptions = new JobSchedulerCheckRunHistoryOptions();
    }
    return objOptions;
  }

  /**
   *
   * \brief Options - set the JobSchedulerCheckRunHistoryOptionClass
   *
   * \details
   * The JobSchedulerCheckRunHistoryOptionClass is used as a Container for all Options (Settings) which are
   * needed.
   * 
   * \return JobSchedulerCheckRunHistoryOptions
   *
   */
  public JobSchedulerCheckRunHistoryOptions Options(final JobSchedulerCheckRunHistoryOptions pobjOptions) {
    @SuppressWarnings("unused")//$NON-NLS-1$
    final String conMethodName = conClassName + "::Options"; //$NON-NLS-1$
    objOptions = pobjOptions;
    return objOptions;
  }

  /**
   *
   * \brief Execute - Start the Execution of JobSchedulerCheckRunHistory
   *
   * \details
   *
   * For more details see
   *
   * \see JobSchedulerAdapterClass
   * \see JobSchedulerCheckRunHistoryMain
   *
   * \return JobSchedulerCheckRunHistory
   *
   * @return
   */
  public JobSchedulerCheckRunHistory Execute() throws Exception {
    final String conMethodName = conClassName + "::Execute"; //$NON-NLS-1$
    logger.debug(String.format(Messages.getMsg("JSJ-I-110"), conMethodName));
    try {
      Object objSp = objJSCommands.getSpoolerObject();
      boolean flgRunAsSchedulerAPIJob = objSp != null;
     
      if(!flgRunAsSchedulerAPIJob) {
        Options().SchedulerHostName.isMandatory(true);
        Options().SchedulerPort.isMandatory(true);
      }
     
      Options().CheckMandatory();
      logger.debug(Options().toString());
     
      @SuppressWarnings("unused")
      String strOperation = Options().operation.Value();
      String strJobName = Options().JobName.Value();
      String strMessage = Options().message.Value();
      String strMailTo  = Options().mail_to.Value();
      String strMailCc  = Options().mail_cc.Value();
      String strMailBcc = Options().mail_bcc.Value();
     
      strMessage = String.format("check %1$s: %2$s", strJobName, myReplaceAll(strMessage,"\\[?JOB_NAME\\]?", strJobName));
      Date objDateStartTime = Options().start_time.getDateObject();
     
      if(flgRunAsSchedulerAPIJob) {
        Spooler objSpooler = (Spooler) objSp;
        Mail objMail = objSpooler.log().mail();
        if(isNotEmpty(strMailTo)) {
          objMail.set_to(strMailTo);
        }
        if(isNotEmpty(strMailCc)) {
          objMail.set_cc(strMailCc);
        }
        if(isNotEmpty(strMailBcc)) {
          objMail.set_bcc(strMailBcc);
        }
        if(isNotEmpty(strMessage)) {
          objMail.set_subject(strMessage);
        }
      }
     
      SchedulerObjectFactory objJSFactory = new SchedulerObjectFactory();
      objJSFactory.initMarshaller(ShowHistory.class);
      JSCmdShowHistory objShowHistory = objJSFactory.createShowHistory();
      objShowHistory.setJob(strJobName);
      objShowHistory.setPrev(BigInteger.valueOf(1));
      Answer objAnswer = null;
     
      if(flgRunAsSchedulerAPIJob) {
        logger.debug("... runs as Job Scheduler API job");
        String strShowHistoryXML = objShowHistory.toXMLString();
        String answerXML = objJSCommands.executeXML(strShowHistoryXML);
        objAnswer = objShowHistory.getAnswer(answerXML);
      }
      else {
        logger.debug("... runs as command line job");
        objJSFactory.Options().ServerName.Value(Options().SchedulerHostName.Value());
        objJSFactory.Options().PortNumber.value(Options().SchedulerPort.value());
        objShowHistory.run();
        objAnswer = objShowHistory.getAnswer();
      }
     
      if(objAnswer != null) {
        ERROR objError = objAnswer.getERROR();
        if(objError != null) {
          throw new JSCommandErrorException(objError.getText());
        }
        List<HistoryEntry> objHistoryEntries = objAnswer.getHistory().getHistoryEntry();
        if(objHistoryEntries.size() == 0) {
          logger.error(String.format("no history entry found for %1$s", strJobName));
          throw new JobSchedulerException(String.format("no history entry found for %1$s", strJobName));
        }
        else {
          HistoryEntry objHistoryEntry = objHistoryEntries.get(0);
          String strErrorText = objHistoryEntry.getErrorText();
          String strEndTime = objHistoryEntry.getEndTime();
          Date objDateEndTime = SOSDate.getDate(strEndTime, SOSDate.dateTimeFormat);       
          boolean flgRunSuccessful = isEmpty(strErrorText);
          boolean flgRunTooLate = objDateEndTime.before(objDateStartTime);
         
          if(flgRunTooLate || !flgRunSuccessful ) {
            logger.info(String.format("last run of %1$s on %2$s", strJobName, strEndTime));
            if(!flgRunSuccessful) {
              logger.info(String.format("with error: %1$s", strErrorText));
              logger.error(String.format("%1$s %n%2$s", strMessage, strErrorText));
              throw new JobSchedulerException(String.format("%1$s %n%2$s", strMessage, strErrorText));
            }
            else {
              logger.error(String.format("%1$s", strMessage));
              throw new JobSchedulerException(String.format("%1$s", strMessage));
            }
          }
          else {
            logger.info(String.format("last successfully run of %1$s on %2$s", strJobName, strEndTime));
          }
        }
      }
      else {
        throw new JobSchedulerException("Job Scheduler isn't responding");
      }
    }
    catch (Exception e) {
//      e.printStackTrace(System.err);
      logger.error(String.format(Messages.getMsg("JSJ-I-107"), conMethodName), e);
      throw e;
    }
    finally {
      logger.debug(String.format(Messages.getMsg("JSJ-I-111"), conMethodName));
    }
    return this;
  }

  public void init() {
    @SuppressWarnings("unused")//$NON-NLS-1$
    final String conMethodName = conClassName + "::init"; //$NON-NLS-1$
    doInitialize();
  }

  private void doInitialize() {
  } // doInitialize

  public String myReplaceAll(String pstrSourceString, String pstrReplaceWhat, String pstrReplaceWith) {
    String newReplacement = pstrReplaceWith.replaceAll("\\$", "\\\\\\$");
    return pstrSourceString.replaceAll("(?m)" + pstrReplaceWhat, newReplacement);
  }

  /**
   *
   * \brief replaceSchedulerVars
   *
   * \details
   * Dummy-Method to make sure, that there is always a valid Instance for the JSJobUtilities.
   * \return
   *
   * @param isWindows
   * @param pstrString2Modify
   * @return
   */
  @Override
  public String replaceSchedulerVars(boolean isWindows, String pstrString2Modify) {
    logger.debug("replaceSchedulerVars as Dummy-call executed. No Instance of JobUtilites specified.");
    return pstrString2Modify;
  }

  /**
   *
   * \brief setJSParam
   *
   * \details
   * Dummy-Method to make shure, that there is always a valid Instance for the JSJobUtilities.
   * \return
   *
   * @param pstrKey
   * @param pstrValue
   */
  @Override
  public void setJSParam(String pstrKey, String pstrValue) {
  }

  @Override
  public void setJSParam(String pstrKey, StringBuffer pstrValue) {
  }

  /**
   *
   * \brief setJSJobUtilites
   *
   * \details
   * The JobUtilities are a set of methods used by the SSH-Job or can be used be other, similar, job-
   * implementations.
   *
   * \return void
   *
   * @param pobjJSJobUtilities
   */
  public void setJSJobUtilites(JSJobUtilities pobjJSJobUtilities) {
    if (pobjJSJobUtilities == null) {
      objJSJobUtilities = this;
    }
    else {
      objJSJobUtilities = pobjJSJobUtilities;
    }
    logger.debug("objJSJobUtilities = " + objJSJobUtilities.getClass().getName());
  }

  /**
   *
   * \brief setJSCommands
   *
   * \details
   *
   * \return void
   *
   * @param pobjJSCommands
   */
  @SuppressWarnings("null")
  public void setJSCommands(IJSCommands pobjJSCommands) {
    if (pobjJSCommands == null) {
      objJSCommands = this;
    }
    else {
      objJSCommands = pobjJSCommands;
    }
    logger.debug("pobjJSCommands = " + pobjJSCommands.getClass().getName());
  }

  @Override
  public String getCurrentNodeName() {
    // TODO Auto-generated method stub
    return null;
  }

  @Override
  public Object getSpoolerObject() {
    return null;
  }

  @Override
  public String executeXML(String pstrJSXmlCommand) {
    return null;
  }

} // class JobSchedulerCheckRunHistory
TOP

Related Classes of sos.scheduler.CheckRunHistory.JobSchedulerCheckRunHistory

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.