/********************************************************* 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.file;
import java.util.regex.Pattern;
import sos.spooler.Job_impl;
import sos.spooler.Variable_set;
import sos.util.SOSLogger;
import sos.util.SOSSchedulerLogger;
import sos.util.SOSFileOperations;
/**
* This job checks for file existence of one file or several files of a directory.
* It can be used standalone or as an order driven job.
*
* @author Uwe Risse <uwe.risse@sos-berlin.com>
* @since 2006-12-08
*/
public class JobSchedulerCanWrite extends Job_impl {
private SOSLogger logger = null;
/**
* Implementierung f�r Spooler API.
* Initialisierungmethode f�r den Scheduler.
* @return boolean
*/
public boolean spooler_init() {
try {
try {
this.logger = new SOSSchedulerLogger(this.spooler_log);
} catch (Exception e) {
throw new Exception("error occurred instantiating logger: " + e.getMessage());
}
return true;
} catch (Exception e) {
try {
if ( logger!=null ) logger.error("error occurred in spooler_init(): " + e.getMessage());
} catch (Exception x) {}
return false;
}
}
/**
* Implementierung f�r Spooler API. L�uft bis return = false
* @return boolean
*/
public boolean spooler_process() {
boolean rc = false;
String name = null;
String file = null;
String fileSpec = null;
try {
// Job oder Order
Variable_set params = spooler.create_variable_set();
if (spooler_task.params() != null) params.merge(spooler_task.params());
if (spooler_job.order_queue() != null && spooler_task.order().params() != null)
params.merge(spooler_task.order().params());
// mandatory parameters
name = "file";
if ( params.var(name)!=null && params.var(name).length()>0 )
file = params.var(name);
else
throw new Exception("job parameter is missing: ["+name+"]");
//To make orderparams available for substitution in orderparam value
while (file.matches("^.*%[^%]+%.*$")) {
String p = file.replaceFirst("^.*%([^%]+)%.*$", "$1");
String s = params.var(p);
s = s.replace('\\','/');
file = file.replaceAll("%"+p+"%", s);
logger.debug("processing job parameter ["+name+"]: substitute %"+p+"% with "+s);
}
logger.info(".. job parameter ["+name+"]: " + file);
// optional parameters
name = "file_spec";
if ( params.var(name)!=null && params.var(name).length()>0 ) {
fileSpec = params.var(name);
logger.info(".. job parameter ["+name+"]: " + fileSpec);
}
rc = SOSFileOperations.canWrite(file, fileSpec, Pattern.CASE_INSENSITIVE, logger);
if (!rc && params.value("gracious") != null && params.value("gracious").equalsIgnoreCase("all")) {
return (spooler_job.order_queue() != null);
}
else
if (!rc && params.value("gracious") != null &&
(params.value("gracious").equalsIgnoreCase("yes") || params.value("gracious").equalsIgnoreCase("true") || params.value("gracious").equals("1") ) ) {
return false;
}
else
if ( !rc ) {
throw new Exception("check failed for file: " + file);
}
else {
return (spooler_job.order_queue() != null) ? rc : false;
}
} catch (Exception e) {
try {
logger.error("error occurred in JobSchedulerCanWrite: " + e.getMessage());
} catch(Exception x) {}
return false;
}
}
/**
* Implementierung f�r Spooler API. Wird beim Entladen aufgerufen und
* schlie�t die Datenbankverbindung.
*/
public void spooler_exit() {
try {
} catch (Exception e) {
// no error processing at job level
} finally {
}
}
}