/********************************************************* 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 sos.spooler.Job_impl;
import sos.spooler.Variable_set;
import sos.util.SOSLogger;
import sos.util.SOSSchedulerLogger;
import sos.util.SOSFileOperations;
/**
* This job copies one file or several files of a directory.
* It can be used standalone or as an order driven job.
*
* @author Florian Schreiber <fs@sos-berlin.com>
* @since 2006-11-01
*/
public class JobSchedulerCopyFile 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 source = null;
String target = null;
String fileSpec = ".*";
int flags = 0;
String replacing = null;
String replacement = null;
String minFileAge = "0";
String maxFileAge = "0";
String minFileSize = "-1";
String maxFileSize = "-1";
int skipFirstFiles = 0;
int skipLastFiles = 0;
boolean count_files = false;
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 = "source_file";
if ( params.value(name)!=null && params.value(name).length()>0 ) {
source = params.value(name);
// To make orderparams available for substitution in orderparam value
while (source.matches("^.*%[^%]+%.*$")) {
String p = source.replaceFirst("^.*%([^%]+)%.*$", "$1");
String s = params.var(p);
s = s.replace('\\','/');
source = source.replaceAll("%"+p+"%", s);
logger.debug("processing job parameter ["+name+"]: substitute %"+p+"% with "+s);
}
}
else
throw new Exception("job parameter is missing: ["+name+"]");
logger.info(".. job parameter ["+name+"]: " + source);
// optional parameters
name = "target_file";
if ( params.value(name)!=null && params.value(name).length()>0 ) {
target = params.value(name);
logger.info(".. job parameter ["+name+"]: " + target);
}
name = "file_spec";
if ( params.value(name)!=null && params.value(name).length()>0 ) {
fileSpec = params.value(name);
logger.info(".. job parameter ["+name+"]: " + fileSpec);
}
try {
name = "create_dir";
if ( params.value(name) != null && params.value(name).length() > 0 ) {
flags |= ( SOSFileOperations.toBoolean(params.value(name)) ) ? SOSFileOperations.CREATE_DIR : 0;
logger.info(".. job parameter ["+name+"]: " + params.value(name));
}
name = "gracious";
if ( params.value(name) != null && params.value(name).length() > 0 ) {
flags |= ( SOSFileOperations.toBoolean(params.value(name)) ) ? SOSFileOperations.GRACIOUS : 0;
logger.info(".. job parameter ["+name+"]: " + params.value(name));
}
name = "overwrite";
if ( params.value(name)!=null && params.value(name).length()>0 ) {
flags |= ( SOSFileOperations.toBoolean(params.value(name)) ) ? 0 : SOSFileOperations.NOT_OVERWRITE;
logger.info(".. job parameter ["+name+"]: " + params.value(name));
}
name = "recursive";
if ( params.value(name)!=null && params.value(name).length()>0 ) {
flags |= ( SOSFileOperations.toBoolean(params.value(name)) ) ? SOSFileOperations.RECURSIVE : 0;
logger.info(".. job parameter ["+name+"]: " + params.value(name));
}
name = "count_files";
if ( params.value(name)!=null && params.value(name).length()>0 ) {
count_files = SOSFileOperations.toBoolean(params.value(name));
logger.info(".. job parameter ["+name+"]: " + params.value(name));
if (spooler_job.order_queue() == null)
logger.warn("This is no order job. Job parameter ["+name+"] is only available for order jobs");
}
} catch (Exception x) {
throw new Exception("cannot evaluate job parameter ["+name+"]: " + x.getMessage());
}
name = "replacing";
if ( params.value(name)!=null && params.value(name).length()>0 ) {
replacing = params.value(name);
logger.info(".. job parameter ["+name+"]: " + replacing);
}
name = "replacement";
// Es muss unterschieden werden zwischen NULL und ""
String[] pArr = params.names().split(";");
for (int i=0; i<pArr.length; i++) {
if ( pArr[i].equals( name) )
replacement = params.value(name);
}
if ( replacement!=null )
logger.info(".. job parameter ["+name+"]: " + replacement);
if ( replacing != null && replacement == null ) {
throw new Exception("job parameter is missing for specified parameter [replacing]: [replacement]");
}
if ( replacing == null && replacement != null ) {
throw new Exception("job parameter is missing for specified parameter [replacement]: [replacing]");
}
name = "min_file_age";
if ( params.var(name)!=null && params.var(name).length()>0 ) {
minFileAge = params.var(name);
logger.info(".. job parameter ["+name+"]: " + minFileAge);
}
name = "max_file_age";
if ( params.var(name)!=null && params.var(name).length()>0 ) {
maxFileAge = params.var(name);
logger.info(".. job parameter ["+name+"]: " + maxFileAge);
}
name = "min_file_size";
if ( params.var(name)!=null && params.var(name).length()>0 ) {
minFileSize = params.var(name);
logger.info(".. job parameter ["+name+"]: " + minFileSize);
}
name = "max_file_size";
if ( params.var(name)!=null && params.var(name).length()>0 ) {
maxFileSize = params.var(name);
logger.info(".. job parameter ["+name+"]: " + maxFileSize);
}
name="skip_first_files";
if ( params.var(name)!=null && params.var(name).length()>0 ) {
try {
skipFirstFiles = Integer.parseInt(params.var(name));
} catch (Exception ex) {
throw new Exception("invalid, non-numeric value for parameter [" + name + "]: " + ex.getMessage());
}
logger.info(".. job parameter ["+name+"]: " + skipFirstFiles);
}
name="skip_last_files";
if ( params.var(name)!=null && params.var(name).length()>0 ) {
try {
skipLastFiles = Integer.parseInt(params.var(name));
} catch (Exception ex) {
throw new Exception("invalid, non-numeric value for parameter [" + name + "]: " + ex.getMessage());
}
logger.info(".. job parameter ["+name+"]: " + skipLastFiles);
}
// Methodenaufruf
int nrOfTransferedFiles = SOSFileOperations.copyFileCnt(source, target, fileSpec, flags,
java.util.regex.Pattern.CASE_INSENSITIVE, replacing, replacement,
minFileAge, maxFileAge, minFileSize, maxFileSize, skipFirstFiles, skipLastFiles, logger);
// Anzahl der �bertragenen Dateien wird in einen Order-Parameter geschrieben
if (count_files && spooler_job.order_queue() != null) {
spooler_task.order().params().set_var("scheduler_SOSFileOperations_file_count", String.valueOf(nrOfTransferedFiles));
}
rc = (nrOfTransferedFiles > 0);
if (!rc && params.value("gracious") != null && params.value("gracious").equalsIgnoreCase("all")) {
return (spooler_job.order_queue() != null);
} else {
return (spooler_job.order_queue() != null) ? rc : false;
}
} catch (Exception e) {
try {
logger.error("error occurred in JobSchedulerCopyFile: " + e.getMessage());
} catch(Exception x) {}
return false;
}
}
}