/********************************************************* 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 removes one file or several files from the file system.
* It can be used standalone or as an order driven job.
*
* @author Florian Schreiber <fs@sos-berlin.com>
* @since 2006-11-01
*/
public class JobSchedulerRemoveFile 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 = ".*";
int flags = 0;
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 = "file";
if ( params.value("file") != null && params.value("file").length()>0 ) {
file = params.value("file");
// 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 [file]: substitute %"+p+"% with "+s);
}
logger.info(".. job parameter [file]: " + file);
}
else if ( params.value("source_file") != null && params.value("source_file").length() > 0 ) {
file = params.value("source_file");
// 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 [source_file]: substitute %"+p+"% with "+s);
}
logger.info(".. job parameter [source_file]: " + file);
}
else {
throw new Exception("job parameter is missing: ["+name+"]");
}
// optional parameters
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 = "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 = "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 = "remove_dir";
if ( params.value(name) != null && params.value(name).length() > 0 ) {
flags |= ( SOSFileOperations.toBoolean(params.value(name)) ) ? SOSFileOperations.REMOVE_DIR : 0;
logger.info(".. job parameter ["+name+"]: " + params.value(name));
}
name = "wipe";
if ( params.var(name)!=null && params.var(name).length()>0 ) {
flags |= ( SOSFileOperations.toBoolean(params.var(name)) ) ? SOSFileOperations.WIPE : 0;
logger.info(".. job parameter ["+name+"]: " + params.var(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 = "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 nrOfRemovedObjects = SOSFileOperations.removeFileCnt(file, fileSpec, flags, java.util.regex.Pattern.CASE_INSENSITIVE, 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(nrOfRemovedObjects));
}
rc = (nrOfRemovedObjects > 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 JobSchedulerRemoveFile: " + 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 {
}
}
}