1
|
/**
|
2
|
* '$RCSfile$'
|
3
|
* Purpose: A class to asyncronously do delta-T replication checking
|
4
|
* Copyright: 2000 Regents of the University of California and the
|
5
|
* National Center for Ecological Analysis and Synthesis
|
6
|
* Authors: Chad Berkley
|
7
|
* Release: @release@
|
8
|
*
|
9
|
* '$Author: tao $'
|
10
|
* '$Date: 2003-02-04 15:51:25 -0800 (Tue, 04 Feb 2003) $'
|
11
|
* '$Revision: 1387 $'
|
12
|
*
|
13
|
* This program is free software; you can redistribute it and/or modify
|
14
|
* it under the terms of the GNU General Public License as published by
|
15
|
* the Free Software Foundation; either version 2 of the License, or
|
16
|
* (at your option) any later version.
|
17
|
*
|
18
|
* This program is distributed in the hope that it will be useful,
|
19
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
20
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
21
|
* GNU General Public License for more details.
|
22
|
*
|
23
|
* You should have received a copy of the GNU General Public License
|
24
|
* along with this program; if not, write to the Free Software
|
25
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
26
|
*/
|
27
|
|
28
|
package edu.ucsb.nceas.metacat;
|
29
|
|
30
|
import java.sql.*;
|
31
|
import java.util.*;
|
32
|
import java.lang.Thread;
|
33
|
import java.io.*;
|
34
|
import java.net.*;
|
35
|
import java.text.*;
|
36
|
import org.xml.sax.AttributeList;
|
37
|
import org.xml.sax.ContentHandler;
|
38
|
import org.xml.sax.DTDHandler;
|
39
|
import org.xml.sax.EntityResolver;
|
40
|
import org.xml.sax.ErrorHandler;
|
41
|
import org.xml.sax.InputSource;
|
42
|
import org.xml.sax.XMLReader;
|
43
|
import org.xml.sax.SAXException;
|
44
|
import org.xml.sax.SAXParseException;
|
45
|
import org.xml.sax.helpers.XMLReaderFactory;
|
46
|
import org.xml.sax.helpers.DefaultHandler;
|
47
|
|
48
|
|
49
|
|
50
|
/**
|
51
|
* This class handles deltaT replication checking. Whenever this TimerTask
|
52
|
* is fired it checks each server in xml_replication for updates and updates
|
53
|
* the local db as needed.
|
54
|
*/
|
55
|
public class ReplicationHandler extends TimerTask
|
56
|
{
|
57
|
int serverCheckCode = 1;
|
58
|
MetaCatUtil util = new MetaCatUtil();
|
59
|
ReplicationServerList serverList = null;
|
60
|
PrintWriter out;
|
61
|
|
62
|
public ReplicationHandler(PrintWriter o)
|
63
|
{
|
64
|
this.out = o;
|
65
|
serverList = new ReplicationServerList();
|
66
|
}
|
67
|
|
68
|
public ReplicationHandler(PrintWriter o, int serverCheckCode)
|
69
|
{
|
70
|
this.out = o;
|
71
|
this.serverCheckCode = serverCheckCode;
|
72
|
serverList = new ReplicationServerList();
|
73
|
}
|
74
|
|
75
|
/**
|
76
|
* Method that implements TimerTask.run(). It runs whenever the timer is
|
77
|
* fired.
|
78
|
*/
|
79
|
public void run()
|
80
|
{
|
81
|
//find out the last_checked time of each server in the server list and
|
82
|
//send a query to each server to see if there are any documents in
|
83
|
//xml_documents with an update_date > last_checked
|
84
|
try
|
85
|
{
|
86
|
//if serverList is null, metacat don't need to replication
|
87
|
if (serverList==null||serverList.isEmpty())
|
88
|
{
|
89
|
return;
|
90
|
}
|
91
|
updateCatalog();
|
92
|
update();
|
93
|
//conn.close();
|
94
|
}//try
|
95
|
catch (Exception e)
|
96
|
{
|
97
|
MetaCatUtil.debugMessage("Error in replicationHandler.run():"
|
98
|
+e.getMessage(), 30);
|
99
|
e.printStackTrace();
|
100
|
}//catch
|
101
|
}
|
102
|
|
103
|
/**
|
104
|
* Method that uses revision taging for replication instead of update_date.
|
105
|
*/
|
106
|
private void update()
|
107
|
{
|
108
|
/*
|
109
|
Pseudo-algorithm
|
110
|
- request a doc list from each server in xml_replication
|
111
|
- check the rev number of each of those documents agains the
|
112
|
documents in the local database
|
113
|
- pull any documents that have a lesser rev number on the local server
|
114
|
from the remote server
|
115
|
- delete any documents that still exist in the local xml_documents but
|
116
|
are in the deletedDocuments tag of the remote host response.
|
117
|
- update last_checked to keep track of the last time it was checked.
|
118
|
(this info is theoretically not needed using this system but probably
|
119
|
should be kept anyway)
|
120
|
*/
|
121
|
|
122
|
DBConnection dbConn = null;
|
123
|
int serialNumber = -1;
|
124
|
|
125
|
ReplicationServer replServer = null; // Variable to store the
|
126
|
// ReplicationServer got from
|
127
|
// Server list
|
128
|
String server; // Variable to store server name
|
129
|
String update;
|
130
|
ReplMessageHandler message = new ReplMessageHandler();
|
131
|
Vector responses = new Vector();
|
132
|
PreparedStatement pstmt = null;
|
133
|
ResultSet rs;
|
134
|
boolean tablehasrows;
|
135
|
boolean flag=false; // If a document need to update
|
136
|
boolean dataFile=false;
|
137
|
String action = new String();
|
138
|
XMLReader parser;
|
139
|
URL u;
|
140
|
|
141
|
try
|
142
|
{
|
143
|
// Get DBConnection from pool
|
144
|
dbConn=DBConnectionPool.
|
145
|
getDBConnection("ReplicationHandler.update");
|
146
|
serialNumber=dbConn.getCheckOutSerialNumber();
|
147
|
MetaCatUtil.debugMessage("init parser", 50);
|
148
|
parser = initParser(message);
|
149
|
//Check for every server in server list
|
150
|
for (int i=0; i<serverList.size(); i++)
|
151
|
{
|
152
|
// Get ReplicationServer object from server list
|
153
|
replServer = serverList.serverAt(i);
|
154
|
// Get server name from ReplicationServer object
|
155
|
server = replServer.getServerName();
|
156
|
MetacatReplication.replLog("full update started to: " + server);
|
157
|
// Send command to that server to get updated docid information
|
158
|
u = new URL("https://" + server + "?server="
|
159
|
+util.getLocalReplicationServerName()+"&action=update");
|
160
|
MetaCatUtil.debugMessage("Sending infomation " +u.toString(), 50);
|
161
|
// Get information from remote server
|
162
|
String result = null;
|
163
|
try
|
164
|
{
|
165
|
result = MetacatReplication.getURLContent(u);
|
166
|
}//try
|
167
|
catch (Exception e)
|
168
|
{
|
169
|
|
170
|
MetaCatUtil.debugMessage("Error in get docid information from "+
|
171
|
server+" :"+e.getMessage(), 30);
|
172
|
continue;
|
173
|
}//catch
|
174
|
MetaCatUtil.debugMessage("docid: "+server+" "+result, 50);
|
175
|
//check if result have error or not, if has skip it.
|
176
|
if (result.indexOf("error")!=-1)
|
177
|
{
|
178
|
continue;
|
179
|
}
|
180
|
responses.add(result);
|
181
|
}
|
182
|
|
183
|
//make sure that there is updated file list
|
184
|
//If response is null, metacat don't need do anything
|
185
|
if (responses==null||responses.isEmpty())
|
186
|
{
|
187
|
return;
|
188
|
}
|
189
|
|
190
|
|
191
|
MetaCatUtil.debugMessage("Responses from remote metacat about updated "+
|
192
|
"document information: "+ responses.toString(), 30);
|
193
|
|
194
|
for(int i=0; i<responses.size(); i++)
|
195
|
{ //check each server for updated files
|
196
|
parser.parse(new InputSource(
|
197
|
new StringReader(
|
198
|
(String)(responses.elementAt(i)))));
|
199
|
//v is the list of updated documents
|
200
|
Vector v = new Vector(message.getUpdatesVect());
|
201
|
//System.out.println("v: " + v.toString());
|
202
|
//d is the list of deleted documents
|
203
|
Vector d = new Vector(message.getDeletesVect());
|
204
|
//System.out.println("d: " + d.toString());
|
205
|
MetaCatUtil.debugMessage("Update vector size: "+ v.size(), 40);
|
206
|
MetaCatUtil.debugMessage("Delete vector size: "+ d.size(),40);
|
207
|
//check the revs in u to see if there are any newer ones than
|
208
|
//in the local DB.
|
209
|
for(int j=0; j<v.size(); j++)
|
210
|
{
|
211
|
//initial dataFile is false
|
212
|
dataFile=false;
|
213
|
//w is information for one document, information contain
|
214
|
//docid, rev, server or datafile.
|
215
|
Vector w = new Vector((Vector)(v.elementAt(j)));
|
216
|
//Check if the vector w contain "datafile"
|
217
|
//If it has, this document is data file
|
218
|
if (w.contains((String)util.getOption("datafileflag")))
|
219
|
{
|
220
|
dataFile=true;
|
221
|
}
|
222
|
//System.out.println("w: " + w.toString());
|
223
|
// Get docid
|
224
|
String docid = (String)w.elementAt(0);
|
225
|
MetaCatUtil.debugMessage("docid: " + docid, 40);
|
226
|
// Get revision number
|
227
|
int rev = Integer.parseInt((String)w.elementAt(1));
|
228
|
MetaCatUtil.debugMessage("rev: " + rev, 40);
|
229
|
// Get remote server name (it is may not be doc homeserver because
|
230
|
// the new hub feature
|
231
|
String remoteServer = (String)w.elementAt(2);
|
232
|
|
233
|
// Choose revision number from local host
|
234
|
pstmt = dbConn.prepareStatement("select rev from xml_documents where "
|
235
|
+ "docid like '" + docid + "'");
|
236
|
pstmt.execute();
|
237
|
rs = pstmt.getResultSet();
|
238
|
tablehasrows = rs.next();
|
239
|
if(tablehasrows)
|
240
|
{ //check the revs for an update because this document is in the
|
241
|
//local DB, it might be out of date.
|
242
|
int localrev = rs.getInt(1);
|
243
|
if(localrev == rev)
|
244
|
{
|
245
|
// Local meatacat has the same rev to remote host, don't need
|
246
|
// update and flag set false
|
247
|
flag = false;
|
248
|
}
|
249
|
else if(localrev < rev)
|
250
|
{//this document needs to be updated so send an read request
|
251
|
action = "UPDATE";
|
252
|
flag = true;
|
253
|
}
|
254
|
}
|
255
|
else
|
256
|
{ //insert this document as new because it is not in the local DB
|
257
|
action = "INSERT";
|
258
|
flag = true;
|
259
|
}
|
260
|
|
261
|
// Close result set
|
262
|
rs.close();
|
263
|
// Close preparedstatement
|
264
|
pstmt.close();
|
265
|
|
266
|
// this is non-data file
|
267
|
if(flag && !dataFile)
|
268
|
{ //if the document needs to be updated or inserted, this is executed
|
269
|
u = new URL("https://" + remoteServer + "?server="+
|
270
|
util.getLocalReplicationServerName()+"&action=read&docid="+docid);
|
271
|
//System.out.println("Sending message: " + u.toString());
|
272
|
// Get docid content
|
273
|
String newxmldoc = MetacatReplication.getURLContent(u);
|
274
|
// If couldn't get skip it
|
275
|
if ( newxmldoc.indexOf("error")!= -1)
|
276
|
{
|
277
|
continue;
|
278
|
}
|
279
|
MetaCatUtil.debugMessage("xml documnet:", 50);
|
280
|
MetaCatUtil.debugMessage(newxmldoc, 50);
|
281
|
|
282
|
// Try get the docid info from remote server
|
283
|
DocInfoHandler dih = new DocInfoHandler();
|
284
|
XMLReader docinfoParser = initParser(dih);
|
285
|
URL docinfoUrl = new URL("https://" + remoteServer +
|
286
|
"?server="+util.getLocalReplicationServerName()+
|
287
|
"&action=getdocumentinfo&docid="+docid);
|
288
|
MetaCatUtil.debugMessage("Sending message: " +
|
289
|
docinfoUrl.toString(), 50);
|
290
|
String docInfoStr = MetacatReplication.getURLContent(docinfoUrl);
|
291
|
docinfoParser.parse(new InputSource(new StringReader(docInfoStr)));
|
292
|
Hashtable docinfoHash = dih.getDocInfo();
|
293
|
// Get home server of the docid
|
294
|
String docHomeServer = (String)docinfoHash.get("home_server");
|
295
|
|
296
|
//docid should include rev number too
|
297
|
String accnum=docid+util.getOption("accNumSeparator")+
|
298
|
(String)docinfoHash.get("rev");
|
299
|
try
|
300
|
{
|
301
|
// Write the document into local host
|
302
|
String newDocid = DocumentImplWrapper.writeReplication(dbConn,
|
303
|
new StringReader(newxmldoc),
|
304
|
(String)docinfoHash.get("public_access"),
|
305
|
null, /* the dtd text */
|
306
|
action,
|
307
|
accnum,
|
308
|
(String)docinfoHash.get("user_owner"),
|
309
|
null, /* null for groups[] */
|
310
|
docHomeServer,
|
311
|
remoteServer);
|
312
|
//increase usage
|
313
|
dbConn.increaseUsageCount(1);
|
314
|
MetacatReplication.replLog("wrote doc " + docid + " from " +
|
315
|
remoteServer);
|
316
|
/*System.out.println("wrote doc " + docid + " from " +
|
317
|
docServer);*/
|
318
|
}
|
319
|
catch(Exception e)
|
320
|
{
|
321
|
System.out.println("error writing document in " +
|
322
|
"ReplicationHandler.update: " + docid +
|
323
|
": " + e.getMessage());
|
324
|
}
|
325
|
}//if for non-data file
|
326
|
|
327
|
// this is for data file
|
328
|
if(flag && dataFile)
|
329
|
{
|
330
|
//if the document needs to be updated or inserted, this is executed
|
331
|
|
332
|
// Try get docid info from remote server
|
333
|
DocInfoHandler dih = new DocInfoHandler();
|
334
|
XMLReader docinfoParser = initParser(dih);
|
335
|
URL docinfoUrl = new URL("https://" + remoteServer +
|
336
|
"?server="+util.getLocalReplicationServerName()+
|
337
|
"&action=getdocumentinfo&docid="+docid);
|
338
|
|
339
|
String docInfoStr = MetacatReplication.getURLContent(docinfoUrl);
|
340
|
docinfoParser.parse(new InputSource(new StringReader(docInfoStr)));
|
341
|
Hashtable docinfoHash = dih.getDocInfo();
|
342
|
// Get doicd owner
|
343
|
String user = (String)docinfoHash.get("user_owner");
|
344
|
// Get docid name (such as acl or dataset)
|
345
|
String docName = (String)docinfoHash.get("docname");
|
346
|
// Get doc type (eml public id)
|
347
|
String docType = (String)docinfoHash.get("doctype");
|
348
|
// Get docid home sever. it might be different to remoteserver
|
349
|
// becuause of hub feature
|
350
|
String docHomeServer = (String)docinfoHash.get("home_server");
|
351
|
|
352
|
//System.out.println("updating doc: " + docid +" action: "+ action);
|
353
|
//docid should include rev number too
|
354
|
String accnum=docid+util.getOption("accNumSeparator")+
|
355
|
(String)docinfoHash.get("rev");
|
356
|
String datafilePath = util.getOption("datafilepath");
|
357
|
// Get data file content
|
358
|
u = new URL("https://" + remoteServer + "?server="+
|
359
|
util.getLocalReplicationServerName()+
|
360
|
"&action=readdata&docid="+accnum);
|
361
|
|
362
|
try
|
363
|
{
|
364
|
//register data file into xml_documents table and wite data file
|
365
|
//into file system
|
366
|
if (u.openStream() != null)
|
367
|
{
|
368
|
DocumentImpl.writeDataFileInReplication(u.openStream(),
|
369
|
datafilePath,
|
370
|
docName,docType,
|
371
|
accnum, user,
|
372
|
docHomeServer,
|
373
|
remoteServer);
|
374
|
|
375
|
MetacatReplication.replLog("wrote doc " + docid + " from " +
|
376
|
remoteServer);
|
377
|
}//if
|
378
|
else
|
379
|
{
|
380
|
// If get null, skip it
|
381
|
continue;
|
382
|
}//else
|
383
|
|
384
|
}//try
|
385
|
catch(Exception e)
|
386
|
{
|
387
|
System.out.println("error writing document in " +
|
388
|
"ReplicationHandler.update: " + docid +
|
389
|
": " + e.getMessage());
|
390
|
}//catch
|
391
|
}//for datafile
|
392
|
}//for update docs
|
393
|
|
394
|
//handle deleted docs
|
395
|
for(int k=0; k<d.size(); k++)
|
396
|
{ //delete the deleted documents;
|
397
|
Vector w = new Vector((Vector)d.elementAt(k));
|
398
|
String docid = (String)w.elementAt(0);
|
399
|
if(!alreadyDeleted(docid))
|
400
|
{
|
401
|
|
402
|
//because delete method docid should have rev number
|
403
|
//so we just add one for it. This rev number is no sence.
|
404
|
String accnum=docid+util.getOption("accNumSeparator")+"1";
|
405
|
//System.out.println("accnum: "+accnum);
|
406
|
DocumentImpl.delete(accnum, null, null);
|
407
|
MetacatReplication.replLog("doc " + docid + " deleted");
|
408
|
}
|
409
|
}//for delete docs
|
410
|
}//for
|
411
|
|
412
|
//updated last_checked
|
413
|
for (int i=0;i<serverList.size(); i++)
|
414
|
{
|
415
|
// Get ReplicationServer object from server list
|
416
|
replServer = serverList.serverAt(i);
|
417
|
// Get server name from ReplicationServer object
|
418
|
server = replServer.getServerName();
|
419
|
// Get time from remote server
|
420
|
URL dateurl = new URL("https://" + server + "?server="+
|
421
|
util.getLocalReplicationServerName()+"&action=gettime");
|
422
|
String datexml = MetacatReplication.getURLContent(dateurl);
|
423
|
MetaCatUtil.debugMessage("datexml: "+datexml, 40);
|
424
|
if (datexml!=null && !datexml.equals(""))
|
425
|
{
|
426
|
String datestr = datexml.substring(11, datexml.indexOf('<', 11));
|
427
|
StringBuffer sql = new StringBuffer();
|
428
|
sql.append("update xml_replication set last_checked = to_date('");
|
429
|
sql.append(datestr).append("', 'YY-MM-DD HH24:MI:SS') where ");
|
430
|
sql.append("server like '").append(server).append("'");
|
431
|
//System.out.println("sql: " + sql.toString());
|
432
|
pstmt.close();
|
433
|
pstmt = dbConn.prepareStatement(sql.toString());
|
434
|
//increase usage count
|
435
|
dbConn.increaseUsageCount(1);
|
436
|
pstmt.executeUpdate();
|
437
|
dbConn.commit();
|
438
|
MetaCatUtil.debugMessage("last_checked updated to "+datestr+" on "
|
439
|
+ server, 45);
|
440
|
}//if
|
441
|
}//for
|
442
|
}//try
|
443
|
catch(Exception e)
|
444
|
{
|
445
|
/*System.out.println("error in ReplicationHandler.update: " +
|
446
|
e.getMessage());*/
|
447
|
e.printStackTrace(System.out);
|
448
|
}
|
449
|
finally
|
450
|
{
|
451
|
try
|
452
|
{
|
453
|
|
454
|
pstmt.close();
|
455
|
|
456
|
}//try
|
457
|
catch(Exception ee)
|
458
|
{
|
459
|
MetaCatUtil.debugMessage("Error in ReplicationHandler.update to close"+
|
460
|
" pstmt: "+ee.getMessage(), 30);
|
461
|
}//catch
|
462
|
finally
|
463
|
{
|
464
|
DBConnectionPool.returnDBConnection(dbConn, serialNumber);
|
465
|
}//finally
|
466
|
}//finally
|
467
|
}//update
|
468
|
|
469
|
/**
|
470
|
* updates xml_catalog with entries from other servers.
|
471
|
*/
|
472
|
private void updateCatalog()
|
473
|
{
|
474
|
// DConnection
|
475
|
DBConnection dbConn = null;
|
476
|
// DBConnection checkout serial number
|
477
|
int serialNumber = -1;
|
478
|
// ReplicationServer object in server list
|
479
|
ReplicationServer replServer = null;
|
480
|
PreparedStatement pstmt = null;
|
481
|
try
|
482
|
{
|
483
|
|
484
|
dbConn=DBConnectionPool.
|
485
|
getDBConnection("ReplicationHandler.updateCatalog");
|
486
|
serialNumber=dbConn.getCheckOutSerialNumber();
|
487
|
String server;
|
488
|
|
489
|
// Go through each ReplicationServer object in sererlist
|
490
|
for (int j=0; j<serverList.size(); j++)
|
491
|
{
|
492
|
// Get ReplicationServer object from server list
|
493
|
replServer = serverList.serverAt(j);
|
494
|
// Get server name from the ReplicationServer object
|
495
|
server = replServer.getServerName();
|
496
|
// Try to get catalog
|
497
|
URL u = new URL("https://" + server + "?server="+
|
498
|
util.getLocalReplicationServerName()+"&action=getcatalog");
|
499
|
MetaCatUtil.debugMessage("sending message " + u.toString(), 50);
|
500
|
String catxml = MetacatReplication.getURLContent(u);
|
501
|
|
502
|
// Make sure there are not error, no empty string
|
503
|
if (catxml.indexOf("error")!=-1 || catxml==null||catxml.equals(""))
|
504
|
{
|
505
|
continue;
|
506
|
}
|
507
|
MetaCatUtil.debugMessage("catxml: " + catxml, 50);
|
508
|
CatalogMessageHandler cmh = new CatalogMessageHandler();
|
509
|
XMLReader catparser = initParser(cmh);
|
510
|
catparser.parse(new InputSource(new StringReader(catxml)));
|
511
|
//parse the returned catalog xml and put it into a vector
|
512
|
Vector remoteCatalog = cmh.getCatalogVect();
|
513
|
|
514
|
// Makse sure remoteCatalog is not empty
|
515
|
if (remoteCatalog.isEmpty())
|
516
|
{
|
517
|
continue;
|
518
|
}
|
519
|
|
520
|
String localcatxml = MetacatReplication.getCatalogXML();
|
521
|
|
522
|
// Make sure local catalog is no empty
|
523
|
if (localcatxml==null||localcatxml.equals(""))
|
524
|
{
|
525
|
continue;
|
526
|
}
|
527
|
|
528
|
cmh = new CatalogMessageHandler();
|
529
|
catparser = initParser(cmh);
|
530
|
catparser.parse(new InputSource(new StringReader(localcatxml)));
|
531
|
Vector localCatalog = cmh.getCatalogVect();
|
532
|
|
533
|
//now we have the catalog from the remote server and this local server
|
534
|
//we now need to compare the two and merge the differences.
|
535
|
//the comparison is base on the public_id fields which is the 4th
|
536
|
//entry in each row vector.
|
537
|
Vector publicId = new Vector();
|
538
|
for(int i=0; i<localCatalog.size(); i++)
|
539
|
{
|
540
|
Vector v = new Vector((Vector)localCatalog.elementAt(i));
|
541
|
MetaCatUtil.debugMessage("v1: " + v.toString(), 50);
|
542
|
publicId.add(new String((String)v.elementAt(3)));
|
543
|
//System.out.println("adding " + (String)v.elementAt(3));
|
544
|
}
|
545
|
|
546
|
for(int i=0; i<remoteCatalog.size(); i++)
|
547
|
{
|
548
|
Vector v = (Vector)remoteCatalog.elementAt(i);
|
549
|
//System.out.println("v2: " + v.toString());
|
550
|
//System.out.println("i: " + i);
|
551
|
//System.out.println("remoteCatalog.size(): " + remoteCatalog.size());
|
552
|
//System.out.println("publicID: " + publicId.toString());
|
553
|
MetaCatUtil.debugMessage
|
554
|
("v.elementAt(3): " + (String)v.elementAt(3), 50);
|
555
|
if(!publicId.contains(v.elementAt(3)))
|
556
|
{ //so we don't have this public id in our local table so we need to
|
557
|
//add it.
|
558
|
//System.out.println("in if");
|
559
|
StringBuffer sql = new StringBuffer();
|
560
|
sql.append("insert into xml_catalog (entry_type, source_doctype, ");
|
561
|
sql.append("target_doctype, public_id, system_id) values (?,?,?,");
|
562
|
sql.append("?,?)");
|
563
|
//System.out.println("sql: " + sql.toString());
|
564
|
pstmt = dbConn.prepareStatement(sql.toString());
|
565
|
//increase usage count 1
|
566
|
dbConn.increaseUsageCount(1);
|
567
|
pstmt.setString(1, (String)v.elementAt(0));
|
568
|
pstmt.setString(2, (String)v.elementAt(1));
|
569
|
pstmt.setString(3, (String)v.elementAt(2));
|
570
|
pstmt.setString(4, (String)v.elementAt(3));
|
571
|
pstmt.setString(5, (String)v.elementAt(4));
|
572
|
pstmt.execute();
|
573
|
pstmt.close();
|
574
|
}
|
575
|
}
|
576
|
}
|
577
|
|
578
|
}
|
579
|
catch(Exception e)
|
580
|
{
|
581
|
System.out.println("error in ReplicationHandler.updateCatalog: " +
|
582
|
e.getMessage());
|
583
|
e.printStackTrace(System.out);
|
584
|
}//catch
|
585
|
finally
|
586
|
{
|
587
|
DBConnectionPool.returnDBConnection(dbConn, serialNumber);
|
588
|
}//finall
|
589
|
}
|
590
|
|
591
|
/**
|
592
|
* Method that returns true if docid has already been "deleted" from metacat.
|
593
|
* This method really implements a truth table for deleted documents
|
594
|
* The table is (a docid in one of the tables is represented by the X):
|
595
|
* xml_docs xml_revs deleted?
|
596
|
* ------------------------------------
|
597
|
* X X FALSE
|
598
|
* X _ FALSE
|
599
|
* _ X TRUE
|
600
|
* _ _ TRUE
|
601
|
*/
|
602
|
private static boolean alreadyDeleted(String docid)
|
603
|
{
|
604
|
DBConnection dbConn = null;
|
605
|
int serialNumber = -1;
|
606
|
PreparedStatement pstmt = null;
|
607
|
try
|
608
|
{
|
609
|
dbConn=DBConnectionPool.
|
610
|
getDBConnection("ReplicationHandler.alreadyDeleted");
|
611
|
serialNumber=dbConn.getCheckOutSerialNumber();
|
612
|
boolean xml_docs = false;
|
613
|
boolean xml_revs = false;
|
614
|
|
615
|
StringBuffer sb = new StringBuffer();
|
616
|
sb.append("select docid from xml_revisions where docid like '");
|
617
|
sb.append(docid).append("'");
|
618
|
pstmt = dbConn.prepareStatement(sb.toString());
|
619
|
pstmt.execute();
|
620
|
ResultSet rs = pstmt.getResultSet();
|
621
|
boolean tablehasrows = rs.next();
|
622
|
if(tablehasrows)
|
623
|
{
|
624
|
xml_revs = true;
|
625
|
}
|
626
|
|
627
|
sb = new StringBuffer();
|
628
|
sb.append("select docid from xml_documents where docid like '");
|
629
|
sb.append(docid).append("'");
|
630
|
pstmt.close();
|
631
|
pstmt = dbConn.prepareStatement(sb.toString());
|
632
|
//increase usage count
|
633
|
dbConn.increaseUsageCount(1);
|
634
|
pstmt.execute();
|
635
|
rs = pstmt.getResultSet();
|
636
|
tablehasrows = rs.next();
|
637
|
pstmt.close();
|
638
|
if(tablehasrows)
|
639
|
{
|
640
|
xml_docs = true;
|
641
|
}
|
642
|
|
643
|
if(xml_docs && xml_revs)
|
644
|
{
|
645
|
return false;
|
646
|
}
|
647
|
else if(xml_docs && !xml_revs)
|
648
|
{
|
649
|
return false;
|
650
|
}
|
651
|
else if(!xml_docs && xml_revs)
|
652
|
{
|
653
|
return true;
|
654
|
}
|
655
|
else if(!xml_docs && !xml_revs)
|
656
|
{
|
657
|
return true;
|
658
|
}
|
659
|
}
|
660
|
catch(Exception e)
|
661
|
{
|
662
|
System.out.println("error in ReplicationHandler.alreadyDeleted: " +
|
663
|
e.getMessage());
|
664
|
e.printStackTrace(System.out);
|
665
|
}
|
666
|
finally
|
667
|
{
|
668
|
try
|
669
|
{
|
670
|
pstmt.close();
|
671
|
}//try
|
672
|
catch (SQLException ee)
|
673
|
{
|
674
|
MetaCatUtil.debugMessage("Error in replicationHandler.alreadyDeleted "+
|
675
|
"to close pstmt: "+ee.getMessage(), 30);
|
676
|
}//catch
|
677
|
finally
|
678
|
{
|
679
|
DBConnectionPool.returnDBConnection(dbConn, serialNumber);
|
680
|
}//finally
|
681
|
}//finally
|
682
|
return false;
|
683
|
}
|
684
|
|
685
|
|
686
|
/**
|
687
|
* Method to initialize the message parser
|
688
|
*/
|
689
|
public static XMLReader initParser(DefaultHandler dh)
|
690
|
throws Exception
|
691
|
{
|
692
|
XMLReader parser = null;
|
693
|
|
694
|
try {
|
695
|
ContentHandler chandler = dh;
|
696
|
|
697
|
// Get an instance of the parser
|
698
|
MetaCatUtil util = new MetaCatUtil();
|
699
|
String parserName = util.getOption("saxparser");
|
700
|
parser = XMLReaderFactory.createXMLReader(parserName);
|
701
|
|
702
|
// Turn off validation
|
703
|
parser.setFeature("http://xml.org/sax/features/validation", false);
|
704
|
|
705
|
parser.setContentHandler((ContentHandler)chandler);
|
706
|
parser.setErrorHandler((ErrorHandler)chandler);
|
707
|
|
708
|
} catch (Exception e) {
|
709
|
throw e;
|
710
|
}
|
711
|
|
712
|
return parser;
|
713
|
}
|
714
|
|
715
|
|
716
|
}
|
717
|
|