/********************************************************* 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*/
/*
* JobSchedulerCreateMD5Job.java
* Created on 18.06.2008
*
*/
package sos.scheduler.file;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import sos.scheduler.job.JobSchedulerJob;
import sos.spooler.Variable_set;
import sos.util.SOSCrypt;
public class JobSchedulerMD5File extends JobSchedulerJob {
public boolean spooler_process() throws Exception {
String name = "";
String md5Extension = ".md5";
String fileName = "";
String mode = "create";
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 ) {
fileName = params.var(name);
// To make orderparams available for substitution in orderparam value
while (fileName.matches("^.*%[^%]+%.*$")) {
String p = fileName.replaceFirst("^.*%([^%]+)%.*$", "$1");
String s = params.var(p);
s = s.replace('\\','/');
fileName = fileName.replaceAll("%"+p+"%", s);
getLogger().debug("processing job parameter ["+name+"]: substitute %"+p+"% with "+s);
}
}
else
throw new Exception("job parameter is missing: ["+name+"]");
getLogger().info(".. job parameter ["+name+"]: " + fileName);
name = "md5_suffix";
if ( params.var(name)!=null && params.var(name).length()>0 ){
md5Extension = params.var(name);
getLogger().info(".. job parameter ["+name+"]: " + md5Extension);
}
name = "mode";
if ( params.var(name)!=null && params.var(name).length()>0 ){
mode = params.var(name);
getLogger().info(".. job parameter ["+name+"]: " + mode);
}
File file = new File(fileName);
if (!file.canRead()){
getLogger().warn("Failed to read file: "+file.getAbsolutePath());
return false;
}
File md5File = new File(file.getAbsolutePath()+md5Extension);
String fileMD5 = SOSCrypt.MD5encrypt(file);
getLogger().info("md5 of "+file.getAbsolutePath()+": "+fileMD5);
if (mode.equalsIgnoreCase("create")){
getLogger().debug1("creating md5 file: "+md5File.getAbsolutePath());
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(md5File)));
out.write(fileMD5);
out.close();
} else{
getLogger().debug1("checking md5 file: "+md5File.getAbsolutePath());
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(md5File)));
String md5fromFile = in.readLine();
if (md5fromFile!=null){
// get only 1st part in case of md5sum format
md5fromFile = md5fromFile.split("\\s+")[0];
}else md5fromFile="";
in.close();
getLogger().debug3("md5 from "+md5File.getAbsolutePath()+": "+md5fromFile);
if(md5fromFile.equalsIgnoreCase(fileMD5)){
getLogger().info("md5 checksums are equal.");
} else{
getLogger().warn("md5 checksums are different.");
return false;
}
}
return (spooler_job.order_queue() != null);
} catch (Exception e) {
try {
getLogger().error("error occurred in JobSchedulerCreateMD5File: " + e.getMessage());
} catch(Exception x) {}
return false;
}
}
}