package com.ximple.eofms.jobs; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.nio.ByteBuffer; import java.sql.SQLException; import java.util.ArrayList; import java.util.StringTokenizer; import org.apache.commons.logging.Log; import org.quartz.Job; import org.quartz.JobDataMap; import org.quartz.JobDetail; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import com.vividsolutions.jts.util.Assert; import oracle.sql.BLOB; public abstract class AbstractOracleDatabaseJob implements Job { private static final String SHPDATA_DIR = "SHPDATA_DIR"; private static final String CONFSHPFILTER = "SHPFILTER_CONF"; private static final String SPATAILSCHEMA = "ORGSCHEMA"; private static final String CONVERTDB = "CONVERTDB"; private static final String CONVERTFILE = "CONVERTFILE"; private static final String ORAHOST = "ORAHOST"; private static final String ORAINST = "ORAINST"; private static final String ORAPORT = "ORAPORT"; private static final String ORAUSER = "ORAUSER"; private static final String ORAPASS = "ORAPASS"; private static final String TESTMODE = "TESTMODE"; private static final String TESTCOUNT = "TESTCOUNT"; protected String _dataPath; protected String _filterPath; protected String _oracleHost; protected String _oracleInstance; protected String _oraclePort; protected String _username; protected String _password; protected String _convertDB; protected String _convertFile; protected ArrayList _orgSchema = new ArrayList(); protected boolean _testMode = false; protected int _testCount = -1; public abstract void execute(JobExecutionContext context) throws JobExecutionException; public Log getLogger() { return null; } protected void extractJobConfiguration(JobDetail jobDetail) throws JobExecutionException { // The directory to scan is stored in the job map JobDataMap dataMap = jobDetail.getJobDataMap(); _dataPath = dataMap.getString(SHPDATA_DIR); _filterPath = dataMap.getString(CONFSHPFILTER); _oracleHost = dataMap.getString(ORAHOST); _oracleInstance = dataMap.getString(ORAINST); _oraclePort = dataMap.getString(ORAPORT); _username = dataMap.getString(ORAUSER); _password = dataMap.getString(ORAPASS); _convertDB = dataMap.getString(CONVERTDB); _convertFile = dataMap.getString(CONVERTFILE); String strSchema = dataMap.getString(SPATAILSCHEMA); StringTokenizer st = new StringTokenizer(strSchema, ","); while (st.hasMoreTokens()) { String aSchema = st.nextToken().trim(); _orgSchema.add(aSchema); } _testMode = dataMap.getBooleanFromString(TESTMODE); _testCount = dataMap.getIntFromString(TESTCOUNT); // Validate the required input if (_dataPath == null) { Log logger = getLogger(); if (logger != null) { logger.warn("Cannot found data directory in configarion."); } throw new JobExecutionException("Directory not configured"); } // Make sure the directory exists File dir = new File(_dataPath); if (!dir.exists()) { Log logger = getLogger(); if (logger != null) { logger.warn("Cannot found data directory in file system.[" + _dataPath + "]"); } throw new JobExecutionException("Invalid Dir " + _dataPath); } if (_oracleHost == null) { throw new JobExecutionException("Unknown Oracle Host."); } if (_oracleInstance == null) { throw new JobExecutionException("Unknown Oracle Instance."); } if (_username == null) { throw new JobExecutionException("Unknown Oracle Username."); } if (_password == null) { throw new JobExecutionException("Unknown Oracle Password."); } if (_orgSchema == null) { throw new JobExecutionException("Unknown Spatial Database Schema."); } } protected abstract AbstractOracleJobContext prepareJobContext(String filterPath); protected static byte[] getBytesFromBLOB(BLOB blob) throws SQLException { byte[] raw = null; // BLOB blob = (BLOB) rs.getBlob(1); int optimalSize = blob.getChunkSize(); byte[] chunk = new byte[optimalSize]; InputStream is = blob.getBinaryStream(0); ByteBuffer buffer = null; // ByteBuffer.allocate(optimalSize); int len; try { while ((len = (is.read(chunk))) != -1) { if (buffer != null) { buffer.limit(buffer.limit() + len); } else { buffer = ByteBuffer.allocate(len); } buffer.put(chunk); } is.close(); assert buffer != null; buffer.position(0); raw = buffer.array(); } catch (IOException e) { e.printStackTrace(); // To change body of catch statement use File | Settings | File Templates. Assert.shouldNeverReachHere(); } return raw; } public String getDataPath() { return _dataPath; } public String getFilterPath() { return _filterPath; } public String getOracleHost() { return _oracleHost; } public String getOracleInstance() { return _oracleInstance; } public String getOraclePort() { return _oraclePort; } public String getUsername() { return _username; } public String getPassword() { return _password; } public ArrayList getOriginSchema() { return _orgSchema; } public boolean isTestMode() { return _testMode; } public int getTestCount() { return _testCount; } }