forked from geodmms/xdgnjobs

?? ?
2009-05-04 8062b71a50590bff18ace076f1fc6e4bb34b26c3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
package com.ximple.eofms.jobs;
 
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.BufferOverflowException;
import java.nio.ByteBuffer;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.TreeMap;
 
import org.apache.commons.logging.Log;
import org.geotools.data.DataStore;
import org.geotools.data.oracle.OracleDataStore;
import org.geotools.data.oracle.OracleDataStoreFactory;
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;
 
import com.ximple.eofms.jobs.context.AbstractOracleJobContext;
 
public abstract class AbstractOracleDatabaseJob implements Job {
    /**
     * The Oracle driver class name
     */
    private static final String JDBC_DRIVER = "oracle.jdbc.driver.OracleDriver";
 
    private static final String ORACLE_URL = "jdbc:oracle:thin:@";
    private static final String PROPUsrKey = "user";
    private static final String PROPPassKey = "password";
 
    private static final String JOBDATA_DIR = "JOBDATA_DIR";
    private static final String CONFELMSFILTER = "ELMSFILTER_CONF";
    private static final String SPATAILSCHEMA = "ORGSCHEMA";
    private static final String CONVERTDB = "CONVERTDB";
    private static final String CONVERTFILE = "CONVERTFILE";
    private static final String CONVERTELEMIN = "CONVERTELEMIN";
    private static final String CREATEDUMMY = "CREATEDUMMY";
    private static final String ELEMLOG = "ELEMLOG";
    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";
    private static final String COPYCONNECTIVITYMODE = "COPYCONNECTIVITYMODE";
    private static final String PROFILEMODE = "PROFILEMODE";
    private static final String USEEPSG3826 = "USEEPSG3826";
 
    protected static OracleDataStoreFactory dataStoreFactory = new OracleDataStoreFactory();
 
    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 String _convertElementIn;
    protected String _elementLogging;
    protected String _createDummy;
    protected ArrayList<String> _orgSchema = new ArrayList<String>();
    protected boolean _testMode = false;
    protected boolean _copyConnectivityMode = false;
    protected boolean _profileMode = false;
    protected boolean _useEPSG3826 = true;
    protected boolean _useTransform = true;
 
    protected int _testCount = -1;
    protected OracleDataStore sourceDataStore;
    private boolean driverFound = true;
 
    private long _processTime;
    private long _updateTime;
    private long _processTimeStart;
    private long _updateTimeStart;
 
    protected AbstractOracleDatabaseJob() {
        try {
            Class.forName(JDBC_DRIVER);
        } catch (Throwable t) {
            // must be running off dummy jar!
            driverFound = false;
        }
    }
 
    public abstract void execute(JobExecutionContext context) throws JobExecutionException;
 
    public abstract Log getLogger();
 
    protected void extractJobConfiguration(JobDetail jobDetail) throws JobExecutionException {
        // The directory to scan is stored in the job map
        JobDataMap dataMap = jobDetail.getJobDataMap();
        _dataPath = dataMap.getString(JOBDATA_DIR);
        _filterPath = dataMap.getString(CONFELMSFILTER);
        _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);
        _convertElementIn = dataMap.getString(CONVERTELEMIN);
        _elementLogging = dataMap.getString(ELEMLOG);
        _createDummy = dataMap.getString(CREATEDUMMY);
 
        Log logger = getLogger();
        /*
        logger.info("JOBDATA_DIR=" + _dataPath);
        logger.info("CONFELMSFILTER=" + _filterPath);
        logger.info("ORAHOST=" + _oracleHost);
        logger.info("ORAINST=" + _oracleInstance);
        logger.info("ORAPORT=" + _oraclePort);
        logger.info("ORAUSER=" + _username);
        logger.info("ORAPASS=" + _password);
        logger.info("CONVERTDB=" + _convertDB);
        logger.info("CONVERTFILE=" + _convertFile);
        logger.info("CONVERTELEMIN=" + _convertElementIn);
        logger.info("ELEMLOG=" + _elementLogging);
        */
 
        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);
        _copyConnectivityMode = dataMap.getBooleanFromString(COPYCONNECTIVITYMODE);
        _profileMode = dataMap.getBooleanFromString(PROFILEMODE);
        _useEPSG3826 = dataMap.getBooleanFromString(USEEPSG3826);
 
        // Validate the required input
        if (_dataPath == null) {
            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()) {
            logger = getLogger();
            if (logger != null) {
                logger.warn("Cannot found data directory in file system.[" + _dataPath + "]");
            }
            throw new JobExecutionException("Invalid Dir " + _dataPath);
        }
 
        if (_oracleHost == null) {
            logger.warn("OracleHOST is null");
            throw new JobExecutionException("Unknown Oracle Host.");
        }
        if (_oracleInstance == null) {
            logger.warn("OracleINSTANCE is null");
            throw new JobExecutionException("Unknown Oracle Instance.");
        }
        if (_username == null) {
            logger.warn("OracleUSER is null");
            throw new JobExecutionException("Unknown Oracle Username.");
        }
        if (_password == null) {
            logger.warn("OraclePASS is null");
            throw new JobExecutionException("Unknown Oracle Password.");
        }
        if (_orgSchema == null) {
            logger.warn("OracleSchema is null");
            throw new JobExecutionException("Unknown Spatial Database Schema.");
        }
    }
 
    protected abstract AbstractOracleJobContext prepareJobContext(String targetSchemaName, String filterPath,
                                                                  boolean profileMode,
                                                                  boolean useTransform, boolean useEPSG3826);
 
    protected byte[] getBytesFromBLOB(BLOB blob) throws SQLException, BufferOverflowException {
        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 = 0;
 
        try {
            while ((len = (is.read(chunk))) != -1) {
                if (buffer != null) {
                    buffer.limit(buffer.limit() + len);
                } else {
                    buffer = ByteBuffer.allocate(len);
                }
 
                buffer.put(chunk, 0, len);
            }
 
            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();
        } catch (BufferOverflowException e) {
            getLogger().warn("BufferOverflowException:: len = " + len + "buffer.limit=" + buffer.limit());
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                getLogger().warn("InputStream cannot close", e);
            }
            ;
        }
 
        return raw;
    }
 
    public boolean isDriverFound() {
        return driverFound;
    }
 
    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<String> getOriginSchema() {
        return _orgSchema;
    }
 
    public boolean isTestMode() {
        return _testMode;
    }
 
    public int getTestCount() {
        return _testCount;
    }
 
    public String getConvertDB() {
        return _convertDB;
    }
 
    public void setConvertDB(String convertDB) {
        _convertDB = convertDB;
    }
 
    public String getConvertFile() {
        return _convertFile;
    }
 
    public void setConvertFile(String convertFile) {
        _convertFile = convertFile;
    }
 
    public String getConvertElementIn() {
        return _convertElementIn;
    }
 
    public void setConvertElementIn(String convertElementIn) {
        _convertElementIn = convertElementIn;
    }
 
    public boolean checkConvertDB() {
        return _convertDB != null && !_convertDB.equalsIgnoreCase("false") &&
            !_convertDB.equalsIgnoreCase("no") && !_convertDB.equalsIgnoreCase("0");
    }
 
    public boolean checkConvertFile() {
        return _convertFile != null && !_convertFile.equalsIgnoreCase("false") &&
            !_convertFile.equalsIgnoreCase("no") && !_convertFile.equalsIgnoreCase("0");
    }
 
    public boolean checkConvertElementIn() {
        return _convertElementIn != null && !_convertElementIn.equalsIgnoreCase("false") &&
            !_convertElementIn.equalsIgnoreCase("no") && !_convertElementIn.equalsIgnoreCase("0");
    }
 
    public String getElementLogging() {
        return _elementLogging;
    }
 
    public void setElementLogging(String elementLogging) {
        this._elementLogging = elementLogging;
    }
 
    public boolean checkElementLogging() {
        return _elementLogging != null && !_elementLogging.equalsIgnoreCase("false") &&
            !_elementLogging.equalsIgnoreCase("no") && !_elementLogging.equalsIgnoreCase("0");
    }
 
    public boolean checkCreateDummy() {
        return _createDummy != null && !_createDummy.equalsIgnoreCase("false") &&
            !_createDummy.equalsIgnoreCase("no") && !_createDummy.equalsIgnoreCase("0");
    }
 
    public boolean isCopyConnectivityMode() {
        return _copyConnectivityMode;
    }
 
    public boolean isProfileMode() {
        return _profileMode;
    }
 
    public boolean isTransformed() {
        return _useTransform;
    }
 
    public boolean isEPSG3826() {
        return _useEPSG3826;
    }
 
    public DataStore getSourceDataStore() {
        return sourceDataStore;
    }
 
    protected void createSourceDataStore() throws JobExecutionException {
        if (sourceDataStore != null) {
            sourceDataStore.dispose();
            sourceDataStore = null;
        }
 
        if (!isDriverFound()) {
            throw new JobExecutionException("Oracle JDBC Driver not found.-" + JDBC_DRIVER);
        }
        Map<String, String> map = new TreeMap<String, String>();
        map.put("host", _oracleHost);
        map.put("port", _oraclePort);
        map.put("instance", _oracleInstance);
        map.put("user", _username);
        map.put("passwd", _password);
        map.put("dbtype", "oracle");
        map.put("alias", _oracleInstance);
        map.put("namespace", null);
        if (!map.containsKey(OracleDataStoreFactory.MAXCONN.key)) {
            map.put(OracleDataStoreFactory.MAXCONN.key, "5");
        }
        if (!map.containsKey(OracleDataStoreFactory.MINCONN.key)) {
            map.put(OracleDataStoreFactory.MINCONN.key, "1");
        }
 
        if (!dataStoreFactory.canProcess(map)) {
            getLogger().warn("cannot process properties-");
            throw new JobExecutionException("cannot process properties-");
        }
        try {
            sourceDataStore = (OracleDataStore) dataStoreFactory.createDataStore(map);
        } catch (IOException e) {
            getLogger().warn(e.getMessage(), e);
            throw new JobExecutionException(e.getMessage(), e);
        }
    }
 
    protected void disconnect() {
        // ConnectionPoolManager manager = ConnectionPoolManager.getInstance();
        // manager.closeAll();
        if (sourceDataStore != null) {
            sourceDataStore.dispose();
            sourceDataStore = null;
        }
    }
 
    public final void accumulateProcessTime() {
        _processTime += System.currentTimeMillis() - _processTimeStart;
    }
 
    public final void accumulateUpdateTime() {
        _updateTime += System.currentTimeMillis() - _updateTimeStart;
    }
 
    public long getProcessTime() {
        return _processTime;
    }
 
    public long getUpdateTime() {
        return _updateTime;
    }
 
    public final void markProcessTime() {
        _processTimeStart = System.currentTimeMillis();
    }
 
    public final void markUpdateTime() {
        _updateTimeStart = System.currentTimeMillis();
    }
 
    public final void resetProcessTime() {
        _processTime = 0;
    }
 
    public final void resetUpdateTime() {
        _updateTime = 0;
    }
}