1
|
/**
|
2
|
* '$RCSfile$'
|
3
|
* Copyright: 2004 University of New Mexico and the
|
4
|
* Regents of the University of California
|
5
|
*
|
6
|
* '$Author: jones $'
|
7
|
* '$Date: 2004-04-01 16:41:58 -0800 (Thu, 01 Apr 2004) $'
|
8
|
* '$Revision: 2094 $'
|
9
|
*
|
10
|
* This program is free software; you can redistribute it and/or modify
|
11
|
* it under the terms of the GNU General Public License as published by
|
12
|
* the Free Software Foundation; either version 2 of the License, or
|
13
|
* (at your option) any later version.
|
14
|
*
|
15
|
* This program is distributed in the hope that it will be useful,
|
16
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
17
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
18
|
* GNU General Public License for more details.
|
19
|
*
|
20
|
* You should have received a copy of the GNU General Public License
|
21
|
* along with this program; if not, write to the Free Software
|
22
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
23
|
*/
|
24
|
|
25
|
package edu.ucsb.nceas.metacat.harvesterClient;
|
26
|
|
27
|
import com.oreilly.servlet.MailMessage;
|
28
|
import java.io.File;
|
29
|
import java.io.FileInputStream;
|
30
|
import java.io.IOException;
|
31
|
import java.io.PrintStream;
|
32
|
import java.sql.Connection;
|
33
|
import java.sql.DriverManager;
|
34
|
import java.sql.ResultSet;
|
35
|
import java.sql.SQLException;
|
36
|
import java.sql.SQLWarning;
|
37
|
import java.sql.Statement;
|
38
|
import java.util.ArrayList;
|
39
|
import java.text.SimpleDateFormat;
|
40
|
import java.util.Date;
|
41
|
import java.util.Properties;
|
42
|
|
43
|
import edu.ucsb.nceas.metacat.client.Metacat;
|
44
|
import edu.ucsb.nceas.metacat.client.MetacatFactory;
|
45
|
import edu.ucsb.nceas.metacat.client.MetacatInaccessibleException;
|
46
|
|
47
|
/**
|
48
|
* Harvester is the main class for the Harvester application. The main
|
49
|
* method creates a single Harvester object which drives the application.
|
50
|
*
|
51
|
* @author costa
|
52
|
*
|
53
|
*/
|
54
|
public class Harvester {
|
55
|
|
56
|
/*
|
57
|
* Class fields
|
58
|
*/
|
59
|
private static final String marker =
|
60
|
"*****************************************************************************";
|
61
|
|
62
|
|
63
|
/*
|
64
|
* Class methods
|
65
|
*/
|
66
|
|
67
|
|
68
|
/**
|
69
|
* Constructor. Creates a new instance of Harvester.
|
70
|
*/
|
71
|
public Harvester() {
|
72
|
}
|
73
|
|
74
|
|
75
|
/**
|
76
|
* Loads Harvester properties from a configuration file.
|
77
|
*/
|
78
|
private static Properties loadProperties() {
|
79
|
File configfile = new File("../../lib/harvester", "harvester.properties");
|
80
|
Properties properties = new Properties();
|
81
|
|
82
|
try {
|
83
|
properties.load(new FileInputStream(configfile));
|
84
|
}
|
85
|
catch (IOException e) {
|
86
|
System.out.println("IOException: " + e.getMessage());
|
87
|
System.exit(1);
|
88
|
}
|
89
|
|
90
|
return properties;
|
91
|
}
|
92
|
|
93
|
|
94
|
/**
|
95
|
* Harvester main method.
|
96
|
*
|
97
|
* @param args the command line arguments
|
98
|
* @throws SAXException
|
99
|
* @throws IOException
|
100
|
* @throws ParserConfigurationException
|
101
|
*/
|
102
|
public static void main(String[] args) {
|
103
|
Integer delayDefault = new Integer(0); // Default number of hours delay
|
104
|
int delay = delayDefault.intValue(); // Delay in hours before first harvest
|
105
|
Integer d; // Used for determining delay
|
106
|
long delta; // endTime - startTime
|
107
|
long endTime; // time that a harvest completes
|
108
|
Harvester harvester; // object for a single harvest run
|
109
|
Integer maxHarvestsDefault = new Integer(30); // Default max harvests
|
110
|
int maxHarvests = maxHarvestsDefault.intValue(); // Max number of harvests
|
111
|
Integer mh; // used in determining max harvests
|
112
|
int nHarvests = 0; // counts the number of harvest runs
|
113
|
final long oneHour = (60 * 60 * 1000);
|
114
|
final long oneSecond = 1000;
|
115
|
Integer periodDefault = new Integer(24); // Default hours between harvests
|
116
|
int period = periodDefault.intValue(); // Hours between harvests
|
117
|
Integer p; // Used in determining the period
|
118
|
Properties properties;
|
119
|
long startTime; // time that a harvest starts
|
120
|
|
121
|
System.out.println(marker);
|
122
|
System.out.println("Starting Harvester");
|
123
|
properties = Harvester.loadProperties();
|
124
|
properties.list(System.out);
|
125
|
|
126
|
// Parse the delay property. Use default if necessary.
|
127
|
try {
|
128
|
d = Integer.valueOf(properties.getProperty(
|
129
|
"delay",
|
130
|
delayDefault.toString()
|
131
|
));
|
132
|
delay = d.intValue();
|
133
|
}
|
134
|
catch (NumberFormatException e) {
|
135
|
System.out.println("NumberFormatException: Error parsing delay: " +
|
136
|
e.getMessage());
|
137
|
System.out.println("Defaulting to delay=" + delayDefault);
|
138
|
delay = delayDefault.intValue();
|
139
|
}
|
140
|
|
141
|
// Parse the maxHarvests property. Use default if necessary.
|
142
|
try {
|
143
|
mh = Integer.valueOf(properties.getProperty(
|
144
|
"maxHarvests",
|
145
|
maxHarvestsDefault.toString()
|
146
|
));
|
147
|
maxHarvests = mh.intValue();
|
148
|
}
|
149
|
catch (NumberFormatException e) {
|
150
|
System.out.println("NumberFormatException: Error parsing maxHarvests: " +
|
151
|
e.getMessage());
|
152
|
System.out.println("Defaulting to maxHarvests=" + maxHarvestsDefault);
|
153
|
maxHarvests = maxHarvestsDefault.intValue();
|
154
|
}
|
155
|
|
156
|
// Parse the period property. Use default if necessary.
|
157
|
try {
|
158
|
p = Integer.valueOf(properties.getProperty("period",
|
159
|
periodDefault.toString()
|
160
|
));
|
161
|
period = p.intValue();
|
162
|
}
|
163
|
catch (NumberFormatException e) {
|
164
|
System.out.println("NumberFormatException: Error parsing period: " +
|
165
|
e.getMessage());
|
166
|
System.out.println("Defaulting to period=" + periodDefault);
|
167
|
period = periodDefault.intValue();
|
168
|
}
|
169
|
|
170
|
// Sleep for delay number of hours prior to starting first harvest
|
171
|
if (delay > 0) {
|
172
|
try {
|
173
|
System.out.print("First harvest will begin in " + delay);
|
174
|
if (delay == 1) {
|
175
|
System.out.println(" hour.");
|
176
|
}
|
177
|
else {
|
178
|
System.out.println(" hours.");
|
179
|
}
|
180
|
//Thread.sleep(delay * oneHour);
|
181
|
Thread.sleep(delay * oneSecond);
|
182
|
}
|
183
|
catch (InterruptedException e) {
|
184
|
System.err.println("InterruptedException: " + e.getMessage());
|
185
|
System.exit(1);
|
186
|
}
|
187
|
}
|
188
|
|
189
|
// Repeat a new harvest once every period number of hours, until we reach
|
190
|
// the maximum number of harvests. Subtract delta from the time period so
|
191
|
// that each harvest will start at a fixed interval.
|
192
|
//
|
193
|
while (nHarvests < maxHarvests) {
|
194
|
nHarvests++;
|
195
|
startTime = System.currentTimeMillis();
|
196
|
harvester = new Harvester(); // New object for this harvest
|
197
|
harvester.startup(nHarvests, maxHarvests); // Start up Harvester
|
198
|
harvester.readHarvestSiteSchedule(); // Read the database table
|
199
|
harvester.harvest(); // Harvest the documents
|
200
|
harvester.shutdown(); // Shut down Harvester
|
201
|
endTime = System.currentTimeMillis();
|
202
|
delta = endTime - startTime;
|
203
|
|
204
|
if (nHarvests < maxHarvests) {
|
205
|
try {
|
206
|
// Thread.sleep((period * oneHour) - delta);
|
207
|
Thread.sleep((period * oneSecond) - delta);
|
208
|
}
|
209
|
catch (InterruptedException e) {
|
210
|
System.err.println("InterruptedException: " + e.getMessage());
|
211
|
System.exit(1);
|
212
|
}
|
213
|
}
|
214
|
}
|
215
|
}
|
216
|
|
217
|
|
218
|
/*
|
219
|
* Object fields
|
220
|
*/
|
221
|
|
222
|
/** Database connection */
|
223
|
Connection conn;
|
224
|
|
225
|
/** Used during development to determine whether to connect to metacat
|
226
|
* Sometimes it's useful to test parts of the code without actually
|
227
|
* connecting to Metacat.
|
228
|
*/
|
229
|
private boolean connectToMetacat;
|
230
|
|
231
|
/** Highest DETAIL_LOG_ID primary key in the HARVEST_DETAIL_LOG table */
|
232
|
private int detailLogID;
|
233
|
|
234
|
/** Email address of the Harvester Administrator */
|
235
|
private String harvesterAdministrator;
|
236
|
|
237
|
/** Highest HARVEST_LOG_ID primary key in the HARVEST_LOG table */
|
238
|
private int harvestLogID;
|
239
|
|
240
|
/** End time of this harvest session */
|
241
|
private Date harvestEndTime;
|
242
|
|
243
|
/** List of HarvestLog objects. Stores log entries for report generation. */
|
244
|
private ArrayList harvestLogList = new ArrayList();
|
245
|
|
246
|
/** List of HarvestSiteSchedule objects */
|
247
|
private ArrayList harvestSiteScheduleList = new ArrayList();
|
248
|
|
249
|
/** Start time of this harvest session */
|
250
|
private Date harvestStartTime;
|
251
|
|
252
|
/** Number of days to save log records. Any that are older are purged. */
|
253
|
int logPeriod;
|
254
|
|
255
|
/** Metacat client object */
|
256
|
Metacat metacat;
|
257
|
|
258
|
/** SMTP server for sending mail messages */
|
259
|
String smtpServer;
|
260
|
|
261
|
|
262
|
/*
|
263
|
* Object methods
|
264
|
*/
|
265
|
|
266
|
/**
|
267
|
* Creates a new HarvestLog object and adds it to the harvestLogList.
|
268
|
*
|
269
|
* @param status the status of the harvest operation
|
270
|
* @param message the message text of the harvest operation
|
271
|
* @param harvestOperationCode the harvest operation code
|
272
|
* @param siteScheduleID the siteScheduleID for which this operation was
|
273
|
* performed. 0 indicates that the operation did not
|
274
|
* involve a particular harvest site.
|
275
|
* @param harvestDocument the associated HarvestDocument object. May be null.
|
276
|
* @param errorMessage additional error message pertaining to document
|
277
|
* error.
|
278
|
*/
|
279
|
void addLogEntry(int status,
|
280
|
String message,
|
281
|
String harvestOperationCode,
|
282
|
int siteScheduleID,
|
283
|
HarvestDocument harvestDocument,
|
284
|
String errorMessage
|
285
|
) {
|
286
|
HarvestLog harvestLog;
|
287
|
|
288
|
/* If there is no associated harvest document, call the basic constructor;
|
289
|
* else call the extended constructor.
|
290
|
*/
|
291
|
if (harvestDocument == null) {
|
292
|
harvestLog = new HarvestLog(this, harvestStartTime, status, message,
|
293
|
harvestOperationCode, siteScheduleID);
|
294
|
}
|
295
|
else {
|
296
|
harvestLog = new HarvestLog(this, harvestStartTime, status, message,
|
297
|
harvestOperationCode, siteScheduleID,
|
298
|
harvestDocument, errorMessage);
|
299
|
}
|
300
|
|
301
|
harvestLogList.add(harvestLog);
|
302
|
}
|
303
|
|
304
|
|
305
|
/**
|
306
|
* Determines whether Harvester should attempt to connect to Metacat.
|
307
|
* Used during development and testing.
|
308
|
*
|
309
|
* @return true if Harvester should connect, otherwise false
|
310
|
*/
|
311
|
boolean connectToMetacat () {
|
312
|
return connectToMetacat;
|
313
|
}
|
314
|
|
315
|
|
316
|
/**
|
317
|
* Normalizes text prior to insertion into the HARVEST_LOG or
|
318
|
* HARVEST_DETAIL_LOG tables. In particular, replaces the single quote
|
319
|
* character with the double quote character. This prevents SQL errors
|
320
|
* involving words that contain single quotes. Also removes \n and \r
|
321
|
* characters from the text.
|
322
|
*
|
323
|
* @param text the original string
|
324
|
* @return a string containing the normalized text
|
325
|
*/
|
326
|
String dequoteText(String text) {
|
327
|
char c;
|
328
|
StringBuffer stringBuffer = new StringBuffer();
|
329
|
|
330
|
for (int i = 0; i < text.length(); i++) {
|
331
|
c = text.charAt(i);
|
332
|
switch (c) {
|
333
|
case '\'':
|
334
|
stringBuffer.append('\"');
|
335
|
break;
|
336
|
case '\r':
|
337
|
case '\n':
|
338
|
break;
|
339
|
default:
|
340
|
stringBuffer.append(c);
|
341
|
break;
|
342
|
}
|
343
|
}
|
344
|
|
345
|
return stringBuffer.toString();
|
346
|
}
|
347
|
|
348
|
|
349
|
/**
|
350
|
* Gets the current value of the detailLogID for storage as a primary key in
|
351
|
* the DETAIL_LOG_ID field of the HARVEST_DETAIL_LOG table.
|
352
|
*
|
353
|
* @return the current value of the detailLogID
|
354
|
*/
|
355
|
int getDetailLogID() {
|
356
|
int currentValue = detailLogID;
|
357
|
|
358
|
detailLogID++;
|
359
|
return currentValue;
|
360
|
}
|
361
|
|
362
|
|
363
|
/**
|
364
|
* Gets the current value of the harvestLogID for storage as a primary key in
|
365
|
* the HARVEST_LOG_ID field of the HARVEST_LOG table.
|
366
|
*
|
367
|
* @return the current value of the detailLogID
|
368
|
*/
|
369
|
int getHarvestLogID() {
|
370
|
int currentValue = harvestLogID;
|
371
|
|
372
|
harvestLogID++;
|
373
|
return currentValue;
|
374
|
}
|
375
|
|
376
|
|
377
|
/**
|
378
|
* Gets the maximum value of an integer field from a table.
|
379
|
*
|
380
|
* @param tableName the database table name
|
381
|
* @param fieldName the field name of the integer field in the table
|
382
|
* @return the maximum integer stored in the fieldName field of tableName
|
383
|
*/
|
384
|
private int getMaxValue(String tableName, String fieldName) {
|
385
|
int maxValue = 100;
|
386
|
int fieldValue;
|
387
|
String query = "SELECT " + fieldName + " FROM " + tableName;
|
388
|
Statement stmt;
|
389
|
|
390
|
try {
|
391
|
stmt = conn.createStatement();
|
392
|
ResultSet rs = stmt.executeQuery(query);
|
393
|
|
394
|
while (rs.next()) {
|
395
|
fieldValue = rs.getInt(fieldName);
|
396
|
maxValue = Math.max(maxValue, fieldValue);
|
397
|
}
|
398
|
|
399
|
stmt.close();
|
400
|
}
|
401
|
catch(SQLException ex) {
|
402
|
System.out.println("SQLException: " + ex.getMessage());
|
403
|
}
|
404
|
|
405
|
return maxValue;
|
406
|
}
|
407
|
|
408
|
|
409
|
/**
|
410
|
* Gets the minimum value of an integer field from a table.
|
411
|
*
|
412
|
* @param tableName the database table name
|
413
|
* @param fieldName the field name of the integer field in the table
|
414
|
* @return the minimum integer stored in the fieldName field of tableName
|
415
|
*/
|
416
|
private int getMinValue(String tableName, String fieldName) {
|
417
|
int minValue = 0;
|
418
|
int fieldValue;
|
419
|
String query = "SELECT " + fieldName + " FROM " + tableName;
|
420
|
Statement stmt;
|
421
|
|
422
|
try {
|
423
|
stmt = conn.createStatement();
|
424
|
ResultSet rs = stmt.executeQuery(query);
|
425
|
|
426
|
while (rs.next()) {
|
427
|
fieldValue = rs.getInt(fieldName);
|
428
|
|
429
|
if (minValue == 0) {
|
430
|
minValue = fieldValue;
|
431
|
}
|
432
|
else {
|
433
|
minValue = Math.min(minValue, fieldValue);
|
434
|
}
|
435
|
}
|
436
|
|
437
|
stmt.close();
|
438
|
}
|
439
|
catch(SQLException ex) {
|
440
|
System.out.println("SQLException: " + ex.getMessage());
|
441
|
}
|
442
|
|
443
|
return minValue;
|
444
|
}
|
445
|
|
446
|
|
447
|
/**
|
448
|
* For every Harvest site schedule in the database, harvest the
|
449
|
* documents for that site if they are due to be harvested.
|
450
|
*
|
451
|
* @throws SAXException
|
452
|
* @throws IOException
|
453
|
* @throws ParserConfigurationException
|
454
|
*/
|
455
|
private void harvest() {
|
456
|
HarvestSiteSchedule harvestSiteSchedule;
|
457
|
|
458
|
for (int i = 0; i < harvestSiteScheduleList.size(); i++) {
|
459
|
harvestSiteSchedule = (HarvestSiteSchedule)harvestSiteScheduleList.get(i);
|
460
|
harvestSiteSchedule.harvestDocumentList();
|
461
|
}
|
462
|
}
|
463
|
|
464
|
|
465
|
/**
|
466
|
* Initializes the detailLogID and harvestLogID values to their current
|
467
|
* maximums + 1.
|
468
|
*/
|
469
|
private void initLogIDs() {
|
470
|
detailLogID = getMaxValue("HARVEST_DETAIL_LOG", "DETAIL_LOG_ID") + 1;
|
471
|
harvestLogID = getMaxValue("HARVEST_LOG", "HARVEST_LOG_ID") + 1;
|
472
|
}
|
473
|
|
474
|
|
475
|
/**
|
476
|
* Prints all harvest log entries for this harvest run.
|
477
|
*
|
478
|
* @param out the PrintStream object to write to
|
479
|
*/
|
480
|
private void printHarvestLog(PrintStream out) {
|
481
|
HarvestLog harvestLog;
|
482
|
|
483
|
for (int i = 0; i < harvestLogList.size(); i++) {
|
484
|
harvestLog = (HarvestLog) harvestLogList.get(i);
|
485
|
harvestLog.printOutput(out);
|
486
|
}
|
487
|
}
|
488
|
|
489
|
|
490
|
/**
|
491
|
* Prints the site schedule data for a given site.
|
492
|
*
|
493
|
* @param out the PrintStream to write to
|
494
|
* @param siteScheduleID the primary key in the HARVEST_SITE_SCHEDULE table
|
495
|
*/
|
496
|
void printHarvestSiteSchedule(PrintStream out, int siteScheduleID) {
|
497
|
HarvestSiteSchedule harvestSiteSchedule;
|
498
|
|
499
|
for (int i = 0; i < harvestSiteScheduleList.size(); i++) {
|
500
|
harvestSiteSchedule = (HarvestSiteSchedule)harvestSiteScheduleList.get(i);
|
501
|
if (harvestSiteSchedule.siteScheduleID == siteScheduleID) {
|
502
|
harvestSiteSchedule.printOutput(out);
|
503
|
}
|
504
|
}
|
505
|
}
|
506
|
|
507
|
|
508
|
/**
|
509
|
* Prunes old records from the HARVEST_DETAIL_LOG table. Records are
|
510
|
* removed if the HARVEST_LOG_ID foreign key is less than the lowest
|
511
|
* HARVEST_LOG_ID primary key in the HARVEST_LOG table.
|
512
|
*/
|
513
|
private void pruneHarvestDetailLog() {
|
514
|
String deleteString;
|
515
|
int minHarvestLogID;
|
516
|
int recordsDeleted;
|
517
|
Statement stmt;
|
518
|
|
519
|
minHarvestLogID = getMinValue("HARVEST_LOG", "HARVEST_LOG_ID");
|
520
|
deleteString = "DELETE FROM HARVEST_DETAIL_LOG WHERE HARVEST_LOG_ID < " +
|
521
|
minHarvestLogID;
|
522
|
|
523
|
try {
|
524
|
System.out.print("Pruning log entries from HARVEST_DETAIL_LOG: ");
|
525
|
stmt = conn.createStatement();
|
526
|
recordsDeleted = stmt.executeUpdate(deleteString);
|
527
|
System.out.println(recordsDeleted + " records deleted");
|
528
|
stmt.close();
|
529
|
}
|
530
|
catch(SQLException e) {
|
531
|
System.out.println("SQLException: " + e.getMessage());
|
532
|
}
|
533
|
}
|
534
|
|
535
|
|
536
|
/**
|
537
|
* Prunes old records from the HARVEST_LOG table. Records are removed if
|
538
|
* their HARVEST_DATE is older than a given number of days, as stored in the
|
539
|
* logPeriod object field.
|
540
|
*/
|
541
|
private void pruneHarvestLog() {
|
542
|
long currentTime = harvestStartTime.getTime(); // time in milliseconds
|
543
|
Date dateLastLog; // Prune everything prior to this date
|
544
|
String deleteString;
|
545
|
long delta;
|
546
|
final long millisecondsPerDay = (1000 * 60 * 60 * 24);
|
547
|
int recordsDeleted;
|
548
|
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MMM-yyyy");
|
549
|
String dateString;
|
550
|
Statement stmt;
|
551
|
long timeLastLog = 0;
|
552
|
|
553
|
delta = logPeriod * millisecondsPerDay;
|
554
|
deleteString = "DELETE FROM HARVEST_LOG WHERE HARVEST_DATE < ";
|
555
|
timeLastLog = currentTime - delta;
|
556
|
dateLastLog = new Date(timeLastLog);
|
557
|
dateString = "'" + simpleDateFormat.format(dateLastLog) + "'";
|
558
|
deleteString += dateString;
|
559
|
|
560
|
try {
|
561
|
System.out.print("Pruning log entries from HARVEST_LOG: ");
|
562
|
stmt = conn.createStatement();
|
563
|
recordsDeleted = stmt.executeUpdate(deleteString);
|
564
|
System.out.println(recordsDeleted + " records deleted");
|
565
|
stmt.close();
|
566
|
}
|
567
|
catch (SQLException e) {
|
568
|
System.out.println("SQLException: " + e.getMessage());
|
569
|
}
|
570
|
}
|
571
|
|
572
|
|
573
|
/**
|
574
|
* Reads the HARVEST_SITE_SCHEDULE table in the database, creating
|
575
|
* a HarvestSiteSchedule object for each row in the table.
|
576
|
*/
|
577
|
private void readHarvestSiteSchedule() {
|
578
|
HarvestSiteSchedule harvestSiteSchedule;
|
579
|
ResultSet rs;
|
580
|
SQLWarning warn;
|
581
|
Statement stmt;
|
582
|
|
583
|
String contactEmail;
|
584
|
String dateLastHarvest;
|
585
|
String dateNextHarvest;
|
586
|
String documentListURL;
|
587
|
String ldapDN;
|
588
|
String ldapPwd;
|
589
|
int siteScheduleID;
|
590
|
String unit;
|
591
|
int updateFrequency;
|
592
|
|
593
|
try {
|
594
|
// Read the HARVEST_SITE_SCHEDULE table
|
595
|
stmt = conn.createStatement();
|
596
|
rs = stmt.executeQuery("SELECT * FROM HARVEST_SITE_SCHEDULE");
|
597
|
warn = rs.getWarnings();
|
598
|
|
599
|
if (warn != null) {
|
600
|
System.out.println("\n---Warning---\n");
|
601
|
|
602
|
while (warn != null) {
|
603
|
System.out.println("Message: " + warn.getMessage());
|
604
|
System.out.println("SQLState: " + warn.getSQLState());
|
605
|
System.out.print("Vendor error code: ");
|
606
|
System.out.println(warn.getErrorCode());
|
607
|
System.out.println("");
|
608
|
warn = warn.getNextWarning();
|
609
|
}
|
610
|
}
|
611
|
|
612
|
while (rs.next()) {
|
613
|
siteScheduleID = rs.getInt("SITE_SCHEDULE_ID");
|
614
|
documentListURL = rs.getString("DOCUMENTLISTURL");
|
615
|
ldapDN = rs.getString("LDAPDN");
|
616
|
ldapPwd = rs.getString("LDAPPWD");
|
617
|
dateNextHarvest = rs.getString("DATENEXTHARVEST");
|
618
|
dateLastHarvest = rs.getString("DATELASTHARVEST");
|
619
|
updateFrequency = rs.getInt("UPDATEFREQUENCY");
|
620
|
unit = rs.getString("UNIT");
|
621
|
contactEmail = rs.getString("CONTACT_EMAIL");
|
622
|
|
623
|
warn = rs.getWarnings();
|
624
|
|
625
|
if (warn != null) {
|
626
|
System.out.println("\n---Warning---\n");
|
627
|
|
628
|
while (warn != null) {
|
629
|
System.out.println("Message: " + warn.getMessage());
|
630
|
System.out.println("SQLState: " + warn.getSQLState());
|
631
|
System.out.print("Vendor error code: ");
|
632
|
System.out.println(warn.getErrorCode());
|
633
|
System.out.println("");
|
634
|
warn = warn.getNextWarning();
|
635
|
}
|
636
|
}
|
637
|
|
638
|
harvestSiteSchedule = new HarvestSiteSchedule(this,
|
639
|
siteScheduleID,
|
640
|
documentListURL,
|
641
|
ldapDN,
|
642
|
ldapPwd,
|
643
|
dateNextHarvest,
|
644
|
dateLastHarvest,
|
645
|
updateFrequency,
|
646
|
unit,
|
647
|
contactEmail
|
648
|
);
|
649
|
harvestSiteScheduleList.add(harvestSiteSchedule);
|
650
|
}
|
651
|
|
652
|
rs.close();
|
653
|
stmt.close();
|
654
|
}
|
655
|
catch (SQLException e) {
|
656
|
System.out.println("Database access failed " + e);
|
657
|
System.exit(1);
|
658
|
}
|
659
|
|
660
|
}
|
661
|
|
662
|
|
663
|
/**
|
664
|
* Sends a report to the Harvester administrator. The report prints each log
|
665
|
* entry pertaining to this harvest run.
|
666
|
*/
|
667
|
void reportToAdministrator() {
|
668
|
PrintStream body;
|
669
|
String from = harvesterAdministrator;
|
670
|
MailMessage msg;
|
671
|
String subject = "Report from Metacat Harvester";
|
672
|
String to = harvesterAdministrator;
|
673
|
|
674
|
if (!to.equals("")) {
|
675
|
System.out.println("Sending report to Harvester Administrator at address "
|
676
|
+ harvesterAdministrator);
|
677
|
|
678
|
try {
|
679
|
msg = new MailMessage(smtpServer);
|
680
|
msg.from(from);
|
681
|
msg.to(to);
|
682
|
msg.setSubject(subject);
|
683
|
body = msg.getPrintStream();
|
684
|
printHarvestLog(body);
|
685
|
msg.sendAndClose();
|
686
|
}
|
687
|
catch (IOException e) {
|
688
|
System.out.println("There was a problem sending email to " + to);
|
689
|
System.out.println("IOException: " + e.getMessage());
|
690
|
}
|
691
|
}
|
692
|
}
|
693
|
|
694
|
|
695
|
/**
|
696
|
* Shuts down Harvester. Performs cleanup operations such as logging out
|
697
|
* of Metacat and disconnecting from the database.
|
698
|
*/
|
699
|
private void shutdown() {
|
700
|
// Log shutdown operation
|
701
|
System.out.println("Shutting Down Harvester");
|
702
|
addLogEntry(0, "Shutting Down Harvester", "HarvesterShutdown", 0, null, "");
|
703
|
pruneHarvestLog();
|
704
|
pruneHarvestDetailLog();
|
705
|
|
706
|
try {
|
707
|
// Close the database connection
|
708
|
System.out.println("Closing the database connection");
|
709
|
conn.close();
|
710
|
}
|
711
|
catch (SQLException e) {
|
712
|
System.out.println("Database access failed " + e);
|
713
|
}
|
714
|
|
715
|
printHarvestLog(System.out); // Print log to standard output
|
716
|
reportToAdministrator(); // Send a copy of the log to harvester admin
|
717
|
}
|
718
|
|
719
|
|
720
|
/**
|
721
|
* Initializes Harvester at startup. Connects to the database and to Metacat.
|
722
|
*
|
723
|
* @param nHarvests the nth harvest
|
724
|
* @param maxHarvests the maximum number of harvests that this process
|
725
|
* can run
|
726
|
*/
|
727
|
private void startup(int nHarvests, int maxHarvests) {
|
728
|
Boolean ctm;
|
729
|
String dbDriver;
|
730
|
Integer lp;
|
731
|
String metacatURL;
|
732
|
Date now;
|
733
|
String password;
|
734
|
Properties properties;
|
735
|
//String response;
|
736
|
String sessionId;
|
737
|
String timestamp;
|
738
|
String url;
|
739
|
String user;
|
740
|
String userName = System.getProperty("user.name");
|
741
|
SQLWarning warn;
|
742
|
|
743
|
// Log startup operation
|
744
|
System.out.println(Harvester.marker);
|
745
|
now = new Date();
|
746
|
timestamp = now.toString() + ": ";
|
747
|
System.out.println(timestamp + "Starting Next Harvest (" +
|
748
|
nHarvests + "/" + maxHarvests + ")");
|
749
|
properties = loadProperties();
|
750
|
ctm = Boolean.valueOf(properties.getProperty("connectToMetacat", "true"));
|
751
|
connectToMetacat = ctm.booleanValue();
|
752
|
dbDriver = properties.getProperty("dbDriver");
|
753
|
harvesterAdministrator = properties.getProperty("harvesterAdministrator");
|
754
|
|
755
|
try {
|
756
|
lp = Integer.valueOf(properties.getProperty("logPeriod", "90"));
|
757
|
logPeriod = lp.intValue();
|
758
|
}
|
759
|
catch (NumberFormatException e) {
|
760
|
System.err.println("NumberFormatException: Error parsing logPeriod " +
|
761
|
logPeriod + e.getMessage());
|
762
|
System.err.println("Defaulting to logPeriod of 90 days");
|
763
|
logPeriod = 90;
|
764
|
}
|
765
|
|
766
|
metacatURL = properties.getProperty("metacatURL");
|
767
|
password = properties.getProperty("password");
|
768
|
smtpServer = properties.getProperty("smtpServer", "localhost");
|
769
|
url = properties.getProperty("url");
|
770
|
user = properties.getProperty("user");
|
771
|
|
772
|
// Load the jdbc driver
|
773
|
try {
|
774
|
Class.forName(dbDriver);
|
775
|
}
|
776
|
catch (ClassNotFoundException e) {
|
777
|
System.out.println("Can't load driver " + e);
|
778
|
System.exit(1);
|
779
|
}
|
780
|
|
781
|
// Make the database connection
|
782
|
try {
|
783
|
System.out.println("Getting connection to Harvester tables");
|
784
|
conn = DriverManager.getConnection(url, user, password);
|
785
|
|
786
|
// If a SQLWarning object is available, print its warning(s).
|
787
|
// There may be multiple warnings chained.
|
788
|
warn = conn.getWarnings();
|
789
|
|
790
|
if (warn != null) {
|
791
|
while (warn != null) {
|
792
|
System.out.println("SQLState: " + warn.getSQLState());
|
793
|
System.out.println("Message: " + warn.getMessage());
|
794
|
System.out.println("Vendor: " + warn.getErrorCode());
|
795
|
System.out.println("");
|
796
|
warn = warn.getNextWarning();
|
797
|
}
|
798
|
}
|
799
|
}
|
800
|
catch (SQLException e) {
|
801
|
System.out.println("Database access failed " + e);
|
802
|
System.exit(1);
|
803
|
}
|
804
|
|
805
|
initLogIDs();
|
806
|
harvestStartTime = new Date();
|
807
|
addLogEntry(0, "Starting Up Harvester", "HarvesterStartup", 0, null, "");
|
808
|
|
809
|
if (connectToMetacat()) {
|
810
|
try {
|
811
|
System.out.println("Connecting to Metacat: " + metacatURL);
|
812
|
metacat = MetacatFactory.createMetacatConnection(metacatURL);
|
813
|
}
|
814
|
catch (MetacatInaccessibleException e) {
|
815
|
System.out.println("Metacat connection failed." + e.getMessage());
|
816
|
}
|
817
|
catch (Exception e) {
|
818
|
System.out.println("Metacat connection failed." + e.getMessage());
|
819
|
}
|
820
|
}
|
821
|
}
|
822
|
|
823
|
|
824
|
}
|