/********************************************************* 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.managed.configuration;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.Vector;
import java.util.regex.Pattern;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import sos.net.SOSMail;
import sos.spooler.Monitor_impl;
import sos.spooler.Variable_set;
import sos.util.SOSFile;
import sos.util.SOSLogger;
import sos.util.SOSSchedulerLogger;
import sos.xml.SOSXMLXPath;
import sun.security.action.GetLongAction;
/**
* <p>ConfigurationBaseMonitor provides functions for Monitor-Scripts</p>
*
*
* @author andreas.pueschel@sos-berlin.com
* @since 1.0 2006-10-05
*/
public class ConfigurationBaseMonitor extends Monitor_impl {
/** Logging */
private SOSSchedulerLogger sosLogger = null;
/** Attribut: configuration: Job-Konfiguration im XML-Format */
private Document configuration = null;
private StringBuffer configurationBuffer = null;
private String configurationPath = "";
private String configurationFilename = "";
/** Liste der Parameternamen */
private Vector orderParameterKeys = null;
private TreeMap envvars = null;
private TreeMap additional_envvars = null;
private boolean envVarsCaseSensitive = true;
/**
* Initialize order configuration
*/
public void initConfiguration() throws Exception {
if (spooler_job.order_queue() != null) {
if (spooler_task.order().params().value("configuration_path") != null && spooler_task.order().params().value("configuration_path").length() > 0)
this.setConfigurationPath(spooler_task.order().params().value("configuration_path"));
if (spooler_task.order().params().value("configuration_file") != null && spooler_task.order().params().value("configuration_file").length() > 0)
this.setConfigurationFilename(spooler_task.order().params().value("configuration_file"));
}
this.initConfiguration(this.getConfigurationPath(), this.getConfigurationFilename());
}
/**
* Initialize order configuration
*/
public void initConfiguration(String configurationPath, String configurationFilename) throws Exception {
if (spooler_job.order_queue() != null) {
if (spooler_task.order().params().value("configuration_path") != null && spooler_task.order().params().value("configuration_path").length() > 0)
this.setConfigurationPath(spooler_task.order().params().value("configuration_path"));
}
if (configurationFilename.startsWith(".") || configurationFilename.startsWith("/") || configurationFilename.startsWith("\\")
|| configurationFilename.indexOf(":") > -1 || configurationPath == null || configurationPath.length() == 0) {
this.initConfiguration(configurationFilename);
} else {
this.initConfiguration(configurationPath
+ ( (!configurationPath.endsWith("/") && !configurationPath.endsWith("\\")) ? "/" : "")
+ configurationFilename);
}
}
/**
* Initialize order configuration
*/
public void initConfiguration(String configurationFilename) throws Exception {
FileInputStream fis = null;
try { // to retrieve configuration from file
if (spooler_task.job().order_queue() != null) {
if (configurationFilename == null || configurationFilename.length() == 0)
throw new Exception("no configuration filename was specified");
File configurationFile = new File(configurationFilename);
if (!configurationFile.exists()) {
throw new Exception("configuration file not found: " + configurationFile.getCanonicalPath());
} else if (!configurationFile.canRead()) {
throw new Exception("configuration file is not accessible: " + configurationFile.getCanonicalPath());
}
fis = new FileInputStream(configurationFile);
BufferedInputStream in = new BufferedInputStream(fis);
byte inBuffer[] = new byte[1024];
int inBytesRead;
this.configurationBuffer = new StringBuffer();
spooler_log.debug3("...Building configuration buffer");
while ((inBytesRead = in.read(inBuffer)) != -1) {
this.configurationBuffer.append(new String(inBuffer, 0, inBytesRead));
//ur: Warum das ganze XML hier ausgeben? spooler_log.debug9("..." + new String(inBuffer, 0, inBytesRead));
}
spooler_task.order().set_xml_payload(this.configurationBuffer.toString());
spooler_log.debug3("...Setting scheduler_order_configuration_loaded=true");
spooler_task.order().params().set_var("scheduler_order_configuration_loaded", "true");
spooler_log.debug3("...Setting configuration_file=" + configurationFilename);
spooler_task.order().params().set_var("configuration_file", configurationFilename);
}
} catch (Exception e) {
this.getLogger().warn("error occurred initializing configuration: " + e.getMessage());
throw new Exception(this.getLogger().getWarning());
} finally {
try { if (fis != null) { fis.close(); fis = null; } } catch (Exception ex) {}
}
}
/**
* Initialize order configuration
*/
public Document prepareConfiguration() throws Exception {
String nodeQuery = "";
String payload = "";
additional_envvars = null;
try { // to fetch the order configuration
this.orderParameterKeys = new Vector();
if (spooler_task.job().order_queue() != null) {
if (spooler_task.order().xml_payload() == null || spooler_task.order().xml_payload().length() == 0
|| spooler_task.order().params() == null
|| spooler_task.order().params().value("scheduler_order_configuration_loaded") == null
|| spooler_task.order().params().value("scheduler_order_configuration_loaded").length() == 0 )
this.initConfiguration();
if (spooler_task.order().xml_payload() == null)
throw new Exception("monitor:no configuration was specified for this order: " + spooler_task.order().id());
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
docFactory.setNamespaceAware(false);
docFactory.setValidating(false);
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
//this.setConfiguration(docBuilder.parse(new ByteArrayInputStream(spooler_task.order().xml_payload().getBytes())));
payload = spooler_task.order().xml_payload();
if (!payload.startsWith("<?xml ")) {
payload = "<?xml version='1.0' encoding='ISO-8859-1'?>" + payload;
}
this.setConfiguration(docBuilder.parse(new ByteArrayInputStream(payload.getBytes())));
SOSXMLXPath xpath = new SOSXMLXPath(new StringBuffer(payload));
NodeList nodeList = null;
NamedNodeMap nodeMapSettings = null;
nodeQuery = "//settings/log_level";
Node nodeSettings = xpath.selectSingleNode(nodeQuery);
if (nodeSettings != null) {
nodeMapSettings = nodeSettings.getAttributes();
if (nodeMapSettings != null && nodeMapSettings.getNamedItem("value") != null) {
this.getLogger().debug1("Log Level is: " + nodeMapSettings.getNamedItem("value").getNodeValue());
this.getLogger().setLogLevel(this.logLevel2Int(nodeMapSettings.getNamedItem("value").getNodeValue()));
}
}
this.setEnvVars();
String env = "";
boolean globalEnv = false;
// add attributes from configuration
// this.getLogger().debug7("adding parameters from configuration: " + spooler_task.order().xml_payload());
// look up the configuration for the all states
nodeQuery = "//job_chain[@name='" + spooler_task.order().job_chain().name() + "']/order";
this.getLogger().debug9("monitor: lookup order for job chain: " + nodeQuery + "/params/param");
Node nodeParams = xpath.selectSingleNode(nodeQuery + "/params");
if (nodeParams == null || !nodeParams.hasChildNodes()) {
nodeQuery = "//application[@name='" + spooler_task.order().job_chain().name() + "']/order";
this.getLogger().debug9("lookup order query for application: " + nodeQuery);
nodeParams = xpath.selectSingleNode(nodeQuery + "/params");
}
if (nodeParams != null && nodeParams.hasAttributes()) {
NamedNodeMap nodeMapParams = nodeParams.getAttributes();
if (nodeMapParams != null && nodeMapParams.getNamedItem("env") != null) {
env = nodeMapParams.getNamedItem("env").getNodeValue();
this.getLogger().debug3(".. parameter section with env=" + env + " found");
globalEnv = (env.equalsIgnoreCase("yes") || env.equals("1") || env.equalsIgnoreCase("on") || env.equalsIgnoreCase("true"));
}
}
nodeList = xpath.selectNodeList(nodeQuery + "/params/param");
for (int i=0; i<nodeList.getLength(); i++) {
Node node = nodeList.item(i);
String nodeValue = "";
String nodeName = "";
if (node.getNodeName().equalsIgnoreCase("param")) {
NamedNodeMap nodeMap = node.getAttributes();
if (nodeMap != null && nodeMap.getNamedItem("name") != null) {
nodeName = nodeMap.getNamedItem("name").getNodeValue();
if (nodeMap.getNamedItem("value") != null) {
nodeValue = nodeMap.getNamedItem("value").getNodeValue();
} else {
NodeList children = node.getChildNodes();
for (int j=0; j<children.getLength(); j++) {
Node item = children.item(j);
switch (item.getNodeType())
{
case Node.TEXT_NODE:
case Node.CDATA_SECTION_NODE:
nodeValue += item.getNodeValue();
}
}
}
this.getLogger().debug3(".. monitor: global configuration parameter [" + nodeName + "]: " + nodeValue );
spooler_task.order().params().set_var(nodeName, nodeValue);
if (globalEnv || nodeMap.getNamedItem("env") != null) {
if (nodeMap.getNamedItem("env") != null) env = nodeMap.getNamedItem("env").getNodeValue();
boolean setEnv = globalEnv || (env.equalsIgnoreCase("yes") || env.equals("1") || env.equalsIgnoreCase("on") || env.equalsIgnoreCase("true"));
if (setEnv) {
if (this.additional_envvars == null) this.additional_envvars = new TreeMap();
this.additional_envvars.put(nodeName, nodeValue);
}
}
}
}
}
// add additional envvars parameter
if (additional_envvars!=null){
Iterator iter = additional_envvars.keySet().iterator();
String envNames = "";
while (iter.hasNext()) {
String envName = (String) iter.next();
envNames += envName;
if (iter.hasNext()) envNames+=";";
}
this.orderParameterKeys.add("scheduler_order_additional_envvars");
spooler_task.order().params().set_var("scheduler_order_additional_envvars", envNames);
}
// look up the configuration for the order state
nodeQuery = "//job_chain[@name='" + spooler_task.order().job_chain().name() + "']/order/process[@state='" + spooler_task.order().state() + "']";
this.getLogger().debug9("monitor: lookup order node query for job chain: " + nodeQuery + "/params/param");
nodeList = xpath.selectNodeList(nodeQuery + "/params/param");
nodeParams = xpath.selectSingleNode(nodeQuery + "/params");
if (nodeList == null || nodeList.getLength() == 0) {
nodeQuery = "//application[@name='" + spooler_task.order().job_chain().name() + "']/order/process[@state='" + spooler_task.order().state() + "']";
this.getLogger().debug9("monitor: lookup order node query for application: " + nodeQuery + "/params/param");
nodeList = xpath.selectNodeList(nodeQuery + "/params/param");
nodeParams = xpath.selectSingleNode(nodeQuery + "/params");
}
// look for global env at this state
globalEnv = false;
if (nodeParams != null && nodeParams.hasAttributes()) {
NamedNodeMap nodeMapParams = nodeParams.getAttributes();
if (nodeMapParams != null && nodeMapParams.getNamedItem("env") != null) {
env = nodeMapParams.getNamedItem("env").getNodeValue();
this.getLogger().debug3(".. parameter section with env=" + env + " found");
globalEnv = (env.equalsIgnoreCase("yes") || env.equals("1") || env.equalsIgnoreCase("on") || env.equalsIgnoreCase("true"));
}
}
for (int i=0; i<nodeList.getLength(); i++) {
Node node = nodeList.item(i);
this.getLogger().debug7("---->" + node.getNodeName());
if (node.getNodeName().equalsIgnoreCase("param")) {
NamedNodeMap nodeMap = node.getAttributes();
boolean hidden = false;
if (nodeMap.getNamedItem("hidden")!=null && nodeMap.getNamedItem("hidden").getNodeValue().equalsIgnoreCase("true")) hidden = true;
if (nodeMap != null && nodeMap.getNamedItem("name") != null) {
String value="";
if (nodeMap.getNamedItem("value") != null) {
value = nodeMap.getNamedItem("value").getNodeValue();
} else {
NodeList children = node.getChildNodes();
for (int j=0; j<children.getLength(); j++) {
Node item = children.item(j);
switch (item.getNodeType())
{
case Node.TEXT_NODE:
case Node.CDATA_SECTION_NODE:
value += item.getNodeValue();
}
}
}
if (hidden){
this.getLogger().debug3(".. configuration parameter [" + nodeMap.getNamedItem("name").getNodeValue() + "]: *****");
}else{
this.getLogger().debug3(".. configuration parameter [" + nodeMap.getNamedItem("name").getNodeValue() + "]: " + value);
}
spooler_task.order().params().set_var(nodeMap.getNamedItem("name").getNodeValue(), value);
this.orderParameterKeys.add(nodeMap.getNamedItem("name").getNodeValue());
// set additional environment variables for parameters of this state
if (globalEnv || nodeMap.getNamedItem("env") != null) {
if (nodeMap.getNamedItem("env") != null) env = nodeMap.getNamedItem("env").getNodeValue();
boolean setEnv = globalEnv || (env.equalsIgnoreCase("yes") || env.equals("1") || env.equalsIgnoreCase("on") || env.equalsIgnoreCase("true"));
if (setEnv) {
if (this.additional_envvars == null) this.additional_envvars = new TreeMap();
this.additional_envvars.put(nodeMap.getNamedItem("name").getNodeValue(), value);
}
}
}
}
}
getLogger().debug3("Looking for global parameters from scheduler variables");
Variable_set globalVariables = spooler.create_variable_set();
globalVariables.merge(spooler.variables());
globalVariables.merge(spooler_task.params());
if (globalVariables.value("global_configuration_params")!=null && globalVariables.value("global_configuration_params").length()>0){
String globalFile = globalVariables.value("global_configuration_params");
getLogger().debug3("Reading global parameters from "+globalFile);
SOSXMLXPath globalXPath = new SOSXMLXPath(globalFile);
NodeList globalParams = globalXPath.selectNodeList("//params/param");
for (int i=0; i<globalParams.getLength(); i++) {
Node node = globalParams.item(i);
if (node.getNodeName().equalsIgnoreCase("param")) {
NamedNodeMap nodeMap = node.getAttributes();
boolean hidden = false;
if (nodeMap.getNamedItem("hidden")!=null && nodeMap.getNamedItem("hidden").getNodeValue().equalsIgnoreCase("true")) hidden = true;
if (nodeMap != null && nodeMap.getNamedItem("name") != null) {
String value="";
if (nodeMap.getNamedItem("value") != null) {
value = nodeMap.getNamedItem("value").getNodeValue();
} else {
NodeList children = node.getChildNodes();
for (int j=0; j<children.getLength(); j++) {
Node item = children.item(j);
switch (item.getNodeType())
{
case Node.TEXT_NODE:
case Node.CDATA_SECTION_NODE:
value += item.getNodeValue();
}
}
}
if (hidden){
this.getLogger().debug3(".. configuration parameter [" + nodeMap.getNamedItem("name").getNodeValue() + "]: *****");
}else{
this.getLogger().debug3(".. configuration parameter [" + nodeMap.getNamedItem("name").getNodeValue() + "]: " + value);
}
globalVariables.set_var(nodeMap.getNamedItem("name").getNodeValue(), value);
// set additional environment variables for parameters of this state
if (globalEnv || nodeMap.getNamedItem("env") != null) {
if (nodeMap.getNamedItem("env") != null) env = nodeMap.getNamedItem("env").getNodeValue();
boolean setEnv = globalEnv || (env.equalsIgnoreCase("yes") || env.equals("1") || env.equalsIgnoreCase("on") || env.equalsIgnoreCase("true"));
if (setEnv) {
if (this.additional_envvars == null) this.additional_envvars = new TreeMap();
this.additional_envvars.put(nodeMap.getNamedItem("name").getNodeValue(), value);
}
}
}
}
}
//globalParams
}
String[] parameterNames = spooler_task.order().params().names().split(";");
Pattern paramPattern = Pattern.compile("(?m)^.*\\$\\{.*\\}.*$");
int metaTrials=0;
while (metaTrials<=1){
for(int i=0; i<parameterNames.length; i++) {// iterate over parameters and try to substitute
String currParam = String.valueOf ( spooler_task.order().params().value(parameterNames[i]) );
//if ( currParam != null && currParam.matches("^.*\\$\\{.*\\}.*$")) {
if ( currParam != null && paramPattern.matcher(currParam).find()) {
boolean parameterFound = false;
boolean globalParameterFound = false;
boolean envFound = false;
boolean additionalEnvFound = false;
String parameterValue = spooler_task.order().params().value(parameterNames[i]);
int trials=0;
while (parameterValue.indexOf("${") != -1 && trials <= 1) {
this.getLogger().debug6("substitution trials: " + trials + " --> " + parameterValue);
for(int j=0; j<parameterNames.length; j++) {// iterate over other parameters and try to replace
this.getLogger().debug9("parameterNames[j]=" + parameterNames[j] + " -->" + contains(parameterValue,"${" + parameterNames[j] + "}", false));
if (!parameterNames[i].equals(parameterNames[j]) && (contains(parameterValue,"${" + parameterNames[j].toUpperCase() + "}",false) || contains(parameterValue,"${basename:" + parameterNames[j] + "}",false) || contains(parameterValue,"${file_content:" + parameterNames[j] + "}",false))) {
String jParameterValue = spooler_task.order().params().value(parameterNames[j]);
if (parameterValue.indexOf("${basename:"+parameterNames[j]+"}") != -1) {
parameterValue = myReplaceAll(parameterValue,"(?i)\\$\\{basename:" + parameterNames[j] + "\\}", new File(spooler_task.order().params().value(parameterNames[j])).getName().replaceAll("[\\\\]", "\\\\\\\\"));
parameterFound = true;
trials=0;
} else if(parameterValue.indexOf("${file_content:"+parameterNames[j]+"}") != -1){
if (jParameterValue.indexOf("${") != -1){
getLogger().debug9("file_content parameter still contains other parameters.");
metaTrials=0;
}else{
File contentFile = new File(spooler_task.order().params().value(parameterNames[j]));
String fileContent = "";
try{
fileContent = SOSFile.readFile(contentFile);
} catch(Exception e){
getLogger().warn("Failed to read file: "+contentFile.getAbsolutePath());
}
parameterValue = myReplaceAll(parameterValue,"(?i)\\$\\{file_content:" + parameterNames[j] + "\\}", fileContent.replaceAll("[\\\\]", "\\\\\\\\"));
parameterFound = true;
trials=0;
}
} else {
parameterValue = myReplaceAll(parameterValue,"(?i)\\$\\{" + parameterNames[j] + "\\}", spooler_task.order().params().value(parameterNames[j]).replaceAll("[\\\\]", "\\\\\\\\"));
parameterFound = true;
trials=0;
}
}
}
trials++;
}
if (globalVariables.count()>0){
String[] globalNames = globalVariables.names().split(";");
for (int j = 0; j < globalNames.length; j++) {
String globalName = globalNames[j];
String jParameterValue = globalVariables.value(globalName);
this.getLogger().debug9("globalNames[j]=" + globalNames[j] + " -->" + contains(parameterValue,"${" + globalNames[j] + "}", false));
if ((contains(parameterValue,"${" + globalName.toUpperCase() + "}",false) || contains(parameterValue,"${basename:" + globalName + "}",false) || contains(parameterValue,"${file_content:" + globalName + "}",false))) {
if (parameterValue.indexOf("${basename:") != -1) {
parameterValue = myReplaceAll(parameterValue,"(?i)\\$\\{basename:" + globalName + "\\}", new File(globalVariables.value(globalName)).getName().replaceAll("[\\\\]", "\\\\\\\\"));
} else if(parameterValue.indexOf("${file_content:"+globalName+"}") != -1){
if (jParameterValue.indexOf("${") != -1){
getLogger().debug9("file_content parameter still contains other parameters.");
metaTrials=0;
}else{
File contentFile = new File(globalVariables.value(globalName));
String fileContent = "";
try{
fileContent = SOSFile.readFile(contentFile);
} catch(Exception e){
getLogger().warn("Failed to read file: "+contentFile.getAbsolutePath());
}
parameterValue = myReplaceAll(parameterValue,"(?i)\\$\\{file_content:" + globalName + "\\}", fileContent.replaceAll("[\\\\]", "\\\\\\\\"));
}
} else {
parameterValue = myReplaceAll(parameterValue,"(?i)\\$\\{" + globalName + "\\}", globalVariables.value(globalName).replaceAll("[\\\\]", "\\\\\\\\"));
}
globalParameterFound = true;
}
}
}
String casePrefix="";
if (!envVarsCaseSensitive) casePrefix = "(?i)";
if (this.envvars != null) {
Iterator envIterator = this.envvars.keySet().iterator();
while(envIterator.hasNext()) {
Object envName = envIterator.next();
Object envValue = this.envvars.get(envName.toString());
if (contains(parameterValue,"${" + envName.toString() + "}",envVarsCaseSensitive)) {
parameterValue = myReplaceAll(parameterValue, casePrefix+"\\$\\{" + envName.toString() + "\\}", envValue.toString().replaceAll("[\\\\]", "\\\\\\\\"));
envFound = true;
} else if (contains(parameterValue,"${basename:" + envName.toString() + "}",envVarsCaseSensitive)) {
parameterValue = myReplaceAll(parameterValue, casePrefix+"\\$\\{basename:" + envName.toString() + "\\}", new File(envValue.toString()).getName().replaceAll("[\\\\]", "\\\\\\\\"));
envFound = true;
}
}
}
if (this.additional_envvars != null) {
Iterator envIterator = this.additional_envvars.keySet().iterator();
while(envIterator.hasNext()) {
Object envName = envIterator.next();
Object envValue = this.additional_envvars.get(envName.toString());
if ( contains(parameterValue,"${" + envName + "}", envVarsCaseSensitive)) {
parameterValue = myReplaceAll(parameterValue, casePrefix+"\\$\\{" + envName.toString() + "\\}", envValue.toString().replaceAll("[\\\\]", "\\\\\\\\"));
additionalEnvFound = true;
} else if ( contains(parameterValue,"${basename:" + envName.toString() + "}", envVarsCaseSensitive)) {
parameterValue = myReplaceAll(parameterValue, casePrefix+"\\$\\{basename:" + envName.toString() + "\\}", new File(envValue.toString()).getName().replaceAll("[\\\\]", "\\\\\\\\"));
additionalEnvFound = true;
} else if(contains(parameterValue,"${file_content:" + envName.toString() + "}", envVarsCaseSensitive)){
if (envValue.toString().indexOf("${") != -1){
getLogger().debug9("file_content parameter still contains other parameters.");
metaTrials=0;
}else{
File contentFile = new File(envValue.toString());
String fileContent = "";
try{
fileContent = SOSFile.readFile(contentFile);
} catch(Exception e){
getLogger().warn("Failed to read file: "+contentFile.getAbsolutePath());
}
parameterValue = myReplaceAll(parameterValue,casePrefix+"\\$\\{file_content:" + envName.toString() + "\\}", fileContent.replaceAll("[\\\\]", "\\\\\\\\"));
additionalEnvFound = true;
}
}
}
}
if (parameterFound) {
this.getLogger().debug3("parameter substitution [" + parameterNames[i] + "]: " + parameterValue);
spooler_task.order().params().set_var(parameterNames[i], parameterValue);
}
if (globalParameterFound) {
this.getLogger().debug3("global parameter substitution [" + parameterNames[i] + "]: " + parameterValue);
spooler_task.order().params().set_var(parameterNames[i], parameterValue);
}
if (envFound) {
this.getLogger().debug3("environment variable substitution [" + parameterNames[i] + "]: " + parameterValue);
spooler_task.order().params().set_var(parameterNames[i], parameterValue);
}
if (additionalEnvFound) {
this.getLogger().debug3("additional environment variable substitution [" + parameterNames[i] + "]: " + parameterValue);
spooler_task.order().params().set_var(parameterNames[i], parameterValue);
}
}
}
metaTrials++;
}
getLogger().debug1("Merged order parameters after substitutions:");
ConfigurationBaseMonitor.logParameters(spooler_task.order().params(), this.getLogger());
}
return this.getConfiguration();
} catch (Exception e) {
this.getLogger().warn("Monitor: error occurred preparing configuration: " + e.getMessage());
throw new Exception(this.getLogger().getWarning());
}
}
public static void logParameters(Variable_set params, SOSLogger logger){
String[] names = params.names().split(";");
for (int i = 0; i < names.length; i++) {
try{
logger.debug1(".. parameter ["+names[i]+"]: "+params.value(names[i]));
} catch(Exception e){
// no exception handling here
}
}
}
/**
* cleanup order parameters
*/
public void cleanupConfiguration() throws Exception {
try {
if (this.orderParameterKeys != null) {
Variable_set resultParameters = spooler.create_variable_set();
String[] parameterNames = spooler_task.order().params().names().split(";");
for(int i=0; i<parameterNames.length; i++) {
if (!this.orderParameterKeys.contains(parameterNames[i])) {
resultParameters.set_var(parameterNames[i], spooler_task.order().params().value(parameterNames[i]));
}
}
spooler_task.order().set_params(resultParameters);
}
} catch (Exception e) {
throw new Exception("error occurred in monitor on cleanup: " + e.getMessage());
}
}
/**
* sendet Mail mit den Einstellungen des Job Schedulers
*
* @param recipient
* @param subject
* @param body
* @throws Exception
*/
public void sendMail(String recipient, String recipientCC, String recipientBCC, String subject, String body) throws Exception {
try {
SOSMail sosMail = new SOSMail(spooler_log.mail().smtp());
sosMail.setQueueDir(spooler_log.mail().queue_dir());
sosMail.setFrom(spooler_log.mail().from());
sosMail.setContentType("text/plain");
sosMail.setEncoding("Base64");
String recipients[] = recipient.split(",");
for(int i=0; i<recipients.length; i++) {
if (i==0) sosMail.setReplyTo(recipients[i].trim());
sosMail.addRecipient(recipients[i].trim());
}
String recipientsCC[] = recipientCC.split(",");
for(int i=0; i<recipientsCC.length; i++) {
sosMail.addCC(recipientsCC[i].trim());
}
String recipientsBCC[] = recipientBCC.split(",");
for(int i=0; i<recipientsBCC.length; i++) {
sosMail.addBCC(recipientsBCC[i].trim());
}
sosMail.setSubject(subject);
sosMail.setBody(body);
sosMail.setSOSLogger(this.getLogger());
this.getLogger().info("sending mail: \n" + sosMail.dumpMessageAsString());
if (!sosMail.send()){
this.getLogger().warn("mail server is unavailable, mail for recipient [" + recipient + "] is queued in local directory [" + sosMail.getQueueDir() + "]:" + sosMail.getLastError());
}
sosMail.clearRecipients();
} catch (Exception e) {
throw new Exception("error occurred in monitor sending mai: " + e.getMessage());
}
}
/**
* @return Returns the sosLogger.
*/
public SOSSchedulerLogger getLogger() {
return sosLogger;
}
/**
* @param sosLogger
* The sosLogger to set.
*/
public void setLogger(SOSSchedulerLogger sosLogger) {
this.sosLogger = sosLogger;
}
/**
* @return Returns the configuration.
*/
public Document getConfiguration() {
return configuration;
}
/**
* @param configuration The configuration to set.
*/
public void setConfiguration(Document configuration) {
this.configuration = configuration;
}
/**
* @return Returns the configurationPath.
*/
public String getConfigurationPath() {
return configurationPath;
}
/**
* @param configurationPath The configurationPath to set.
*/
public void setConfigurationPath(String configurationPath) {
this.configurationPath = configurationPath;
}
/**
* @return Returns the configurationFilename.
*/
public String getConfigurationFilename() {
return configurationFilename;
}
/**
* @param configurationFilename The configurationFilename to set.
*/
public void setConfigurationFilename(String configurationFilename) {
this.configurationFilename = configurationFilename;
}
private void setEnvVars() throws Exception {
String OS = System.getProperty("os.name").toLowerCase();
boolean win = false;
this.envvars = new TreeMap();
if ( (OS.indexOf("nt") > -1)
|| (OS.indexOf("windows") > -1)) {
win=true;
envVarsCaseSensitive = false;
}
Variable_set env = spooler_task.create_subprocess().env();
this.getLogger().debug9("environment variable names: " + env.names());
StringTokenizer t = new StringTokenizer(env.names(), ";");
while (t.hasMoreTokens()) {
String envname = t.nextToken();
if (envname != null) {
String envvalue = env.value(envname);
if (win) {
this.getLogger().debug9("set environment variable: " + envname.toUpperCase() + "=" + envvalue);
this.envvars.put(envname.toUpperCase(), envvalue);
} else {
this.getLogger().debug9("set environment variable: " + envname + "=" + envvalue);
this.envvars.put(envname, envvalue);
}
}
}
}
private int logLevel2Int(String l) {
HashMap levels = new HashMap();
if (l == null) {
return this.getLogger().getLogLevel();
} else {
levels.put("info", "10");
levels.put("warn", "11");
levels.put("error", "12");
levels.put("debug1", "1");
levels.put("debug2", "2");
levels.put("debug3", "3");
levels.put("debug4", "4");
levels.put("debug5", "5");
levels.put("debug6", "6");
levels.put("debug7", "7");
levels.put("debug8", "8");
levels.put("debug9", "9");
if (levels.get(l) != null) {
return Integer.parseInt(levels.get(l).toString());
} else {
return this.getLogger().getLogLevel();
}
}
}
public static String myReplaceAll(String source, String what, String replacement) {
String newReplacement = replacement.replaceAll("\\$", "\\\\\\$");
newReplacement = newReplacement.replaceAll("\"", "");
return source.replaceAll("(?m)"+what, newReplacement);
}
public static boolean contains(String searchString, String otherString, boolean caseSensitive){
if (caseSensitive) return (searchString.indexOf(otherString)!=-1);
else return (searchString.toUpperCase().indexOf(otherString.toUpperCase())!=-1);
}
}