/********************************************************* 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 com.sos.scheduler.engine.kernel.scripting;
/**
* \file APIModuleInstance.java
* \brief implementation of the script api from the job scheduler
*
* \class ModuleInstance
* \brief implementation of the script api from the job scheduler
*
* \details
* This class is a general implementation of the scheduler script api for using with
* all script languages supported by the javax interface for scripting.
*
* see http://java.sun.com/developer/technicalArticles/J2SE/Desktop/scripting/
* see https://scripting.dev.java.net/
*
* \author Stefan Schaedlich
* \version 1.0 - 2010-12-17
* <div class="sos_branding">
* <p>(c) 2010 SOS GmbH - Berlin (<a style='color:silver' href='http://www.sos-berlin.com'>http://www.sos-berlin.com</a>)</p>
* </div>
*/
//import com.sos.JSHelper.Logging.JobSchedulerLog4JAppender;
//import org.apache.log4j.Appender;
import java.util.Arrays;
import java.util.List;
import org.apache.log4j.Appender;
import sos.spooler.Log;
import sos.util.SOSSchedulerLogger;
import com.sos.JSHelper.Logging.JobSchedulerLog4JAppender;
import com.sos.scheduler.engine.cplusplus.runtime.annotation.ForCpp;
@ForCpp
public class APIModuleInstance extends ScriptInstance implements APIModule {
private final static String LANGUAGE_PREFIX = "javax.script:";
private final static String SCHEDULER_INIT = "spooler_init";
private final static String SCHEDULER_OPEN = "spooler_open";
private final static String SCHEDULER_CLOSE = "spooler_close";
private final static String SCHEDULER_ON_SUCCESS = "spooler_on_success";
private final static String SCHEDULER_EXIT = "spooler_exit";
private final static String SCHEDULER_ON_ERROR = "spooler_on_error";
private final static String SCHEDULER_PROCESS = "spooler_process";
private final String schedulerLanguageId;
private final JobSchedulerLog4JAppender jsAppender;
/**
* These are the optional methods of the scheduler script api.
*/
List<String> optionalMethods = Arrays.asList(SCHEDULER_INIT, SCHEDULER_OPEN,
SCHEDULER_CLOSE, SCHEDULER_ON_SUCCESS, SCHEDULER_EXIT, SCHEDULER_ON_ERROR);
public APIModuleInstance(String scriptlanguage, String sourcecode) {
super(getScriptLanguage(scriptlanguage));
JobSchedulerLog4JAppender bapn = null;
Appender stdoutAppender = logger.getAppender("scheduler");
if (stdoutAppender instanceof JobSchedulerLog4JAppender) {
bapn = (JobSchedulerLog4JAppender) stdoutAppender;
logger.info("LOG-I-0020: JobSchedulerLog4JAppender is configured as log4j-appender");
}
jsAppender = bapn;
setSourceCode(sourcecode);
schedulerLanguageId = scriptlanguage;
}
/**
* \brief the script language to use \return String
*/
private static String getScriptLanguage(String scriptlanguage) {
return scriptlanguage.toLowerCase().replace(LANGUAGE_PREFIX, "");
}
/**
* \brief the content from the language attribute of the script element
* \details In case of javascript it will return 'javax.script:javascript'
*
* \return String
*/
public String getSchedulerLanguageId() {
return schedulerLanguageId;
}
@Override
public void addObject(Object object, String name) {
if (object instanceof Log) {
Log log = (Log) object; // log object of the scheduler-task
if (jsAppender != null) {
try {
SOSSchedulerLogger l = new SOSSchedulerLogger(log);
jsAppender.setSchedulerLogger( l );
} catch (Exception e) {
logger.error("LOG-E-0120: job scheduler log object could not set in log4j properties");
e.printStackTrace();
}
}
super.addObject(logger, "logger");
}
super.addObject(object, name);
}
/**
* \brief call a script function \details It's just the same like the call
* method of the superclass, but the error handling is different. The JS
* calls the script for any function of the api (scheduler_init, scheduler_open
* etc.), but it is not necessary the functions are present in the script.
*
* scheduler_process has to be present. If not the whole script will run
* without functions.
*
* \return Object - with the result of the script
*
* @param functionname
*/
@Override
public Object call(String rawfunctionname, Object[] params) {
Object result = null;
ScriptFunction fobj = new ScriptFunction(rawfunctionname);
String function = fobj.getNativeFunctionName();
logger.info("call for function " + function);
if ( fobj.isFunction(getSourcecode())) {
try {
result = super.call(function, params);
} catch (NoSuchMethodException e) {
logger.error("the function " + function + " does not exist.");
}
} else {
if (function.equals(SCHEDULER_PROCESS)) {
result = super.call();
result = (result == null) ? false : result;
}
}
return result;
}
@Override
public Object call(String rawfunctionname) {
return call(rawfunctionname, new Object[] {});
}
/**
* \brief dummy for checking the existence of a function
* \details
*
* @param name
* @return true
*/
@Override
public boolean nameExists(String name) {
return true;
}
}