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
|
*
|
8
|
* '$Author: berkley $'
|
9
|
* '$Date: 2010-07-27 11:04:52 -0700 (Tue, 27 Jul 2010) $'
|
10
|
* '$Revision: 5457 $'
|
11
|
*
|
12
|
* This program is free software; you can redistribute it and/or modify
|
13
|
* it under the terms of the GNU General Public License as published by
|
14
|
* the Free Software Foundation; either version 2 of the License, or
|
15
|
* (at your option) any later version.
|
16
|
*
|
17
|
* This program is distributed in the hope that it will be useful,
|
18
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
19
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
20
|
* GNU General Public License for more details.
|
21
|
*
|
22
|
* You should have received a copy of the GNU General Public License
|
23
|
* along with this program; if not, write to the Free Software
|
24
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
25
|
*/
|
26
|
|
27
|
package edu.ucsb.nceas.metacat.replication;
|
28
|
|
29
|
import edu.ucsb.nceas.metacat.CatalogMessageHandler;
|
30
|
import edu.ucsb.nceas.metacat.DBUtil;
|
31
|
import edu.ucsb.nceas.metacat.DocInfoHandler;
|
32
|
import edu.ucsb.nceas.metacat.DocumentImpl;
|
33
|
import edu.ucsb.nceas.metacat.DocumentImplWrapper;
|
34
|
import edu.ucsb.nceas.metacat.EventLog;
|
35
|
import edu.ucsb.nceas.metacat.accesscontrol.AccessControlForSingleFile;
|
36
|
import edu.ucsb.nceas.metacat.accesscontrol.XMLAccessDAO;
|
37
|
import edu.ucsb.nceas.metacat.database.DBConnection;
|
38
|
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
|
39
|
import edu.ucsb.nceas.metacat.database.DatabaseService;
|
40
|
import edu.ucsb.nceas.metacat.properties.PropertyService;
|
41
|
import edu.ucsb.nceas.metacat.shared.HandlerException;
|
42
|
import edu.ucsb.nceas.metacat.util.MetacatUtil;
|
43
|
import edu.ucsb.nceas.metacat.IdentifierManager;
|
44
|
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
|
45
|
|
46
|
import java.sql.*;
|
47
|
import java.util.*;
|
48
|
import java.util.Date;
|
49
|
import java.io.*;
|
50
|
import java.net.*;
|
51
|
import java.text.*;
|
52
|
|
53
|
import org.apache.log4j.Logger;
|
54
|
import org.xml.sax.ContentHandler;
|
55
|
import org.xml.sax.ErrorHandler;
|
56
|
import org.xml.sax.InputSource;
|
57
|
import org.xml.sax.SAXException;
|
58
|
import org.xml.sax.XMLReader;
|
59
|
import org.xml.sax.helpers.XMLReaderFactory;
|
60
|
import org.xml.sax.helpers.DefaultHandler;
|
61
|
|
62
|
|
63
|
|
64
|
/**
|
65
|
* This class handles deltaT replication checking. Whenever this TimerTask
|
66
|
* is fired it checks each server in xml_replication for updates and updates
|
67
|
* the local db as needed.
|
68
|
*/
|
69
|
public class ReplicationHandler extends TimerTask
|
70
|
{
|
71
|
int serverCheckCode = 1;
|
72
|
ReplicationServerList serverList = null;
|
73
|
//PrintWriter out;
|
74
|
// private static final AbstractDatabase dbAdapter = MetacatUtil.dbAdapter;
|
75
|
private static Logger logReplication = Logger.getLogger("ReplicationLogging");
|
76
|
private static Logger logMetacat = Logger.getLogger(ReplicationHandler.class);
|
77
|
private static Logger logD1 = Logger.getLogger("DataOneLogger");
|
78
|
|
79
|
private static int DOCINSERTNUMBER = 1;
|
80
|
private static int DOCERRORNUMBER = 1;
|
81
|
private static int REVINSERTNUMBER = 1;
|
82
|
private static int REVERRORNUMBER = 1;
|
83
|
|
84
|
private static int _xmlDocQueryCount = 0;
|
85
|
private static int _xmlRevQueryCount = 0;
|
86
|
private static long _xmlDocQueryTime = 0;
|
87
|
private static long _xmlRevQueryTime = 0;
|
88
|
|
89
|
|
90
|
public ReplicationHandler()
|
91
|
{
|
92
|
//this.out = o;
|
93
|
serverList = new ReplicationServerList();
|
94
|
}
|
95
|
|
96
|
public ReplicationHandler(int serverCheckCode)
|
97
|
{
|
98
|
//this.out = o;
|
99
|
this.serverCheckCode = serverCheckCode;
|
100
|
serverList = new ReplicationServerList();
|
101
|
}
|
102
|
|
103
|
/**
|
104
|
* Method that implements TimerTask.run(). It runs whenever the timer is
|
105
|
* fired.
|
106
|
*/
|
107
|
public void run()
|
108
|
{
|
109
|
//find out the last_checked time of each server in the server list and
|
110
|
//send a query to each server to see if there are any documents in
|
111
|
//xml_documents with an update_date > last_checked
|
112
|
|
113
|
//if serverList is null, metacat don't need to replication
|
114
|
if (serverList==null||serverList.isEmpty())
|
115
|
{
|
116
|
return;
|
117
|
}
|
118
|
updateCatalog();
|
119
|
update();
|
120
|
//conn.close();
|
121
|
}
|
122
|
|
123
|
/**
|
124
|
* Method that uses revision tagging for replication instead of update_date.
|
125
|
*/
|
126
|
private void update()
|
127
|
{
|
128
|
|
129
|
_xmlDocQueryCount = 0;
|
130
|
_xmlRevQueryCount = 0;
|
131
|
_xmlDocQueryTime = 0;
|
132
|
_xmlRevQueryTime = 0;
|
133
|
/*
|
134
|
Pseudo-algorithm
|
135
|
- request a doc list from each server in xml_replication
|
136
|
- check the rev number of each of those documents agains the
|
137
|
documents in the local database
|
138
|
- pull any documents that have a lesser rev number on the local server
|
139
|
from the remote server
|
140
|
- delete any documents that still exist in the local xml_documents but
|
141
|
are in the deletedDocuments tag of the remote host response.
|
142
|
- update last_checked to keep track of the last time it was checked.
|
143
|
(this info is theoretically not needed using this system but probably
|
144
|
should be kept anyway)
|
145
|
*/
|
146
|
|
147
|
ReplicationServer replServer = null; // Variable to store the
|
148
|
// ReplicationServer got from
|
149
|
// Server list
|
150
|
String server = null; // Variable to store server name
|
151
|
// String update;
|
152
|
Vector<String> responses = new Vector<String>();
|
153
|
URL u;
|
154
|
long replicationStartTime = System.currentTimeMillis();
|
155
|
long timeToGetServerList = 0;
|
156
|
|
157
|
//Check for every server in server list to get updated list and put
|
158
|
// them in to response
|
159
|
long startTimeToGetServers = System.currentTimeMillis();
|
160
|
for (int i=0; i<serverList.size(); i++)
|
161
|
{
|
162
|
// Get ReplicationServer object from server list
|
163
|
replServer = serverList.serverAt(i);
|
164
|
// Get server name from ReplicationServer object
|
165
|
server = replServer.getServerName().trim();
|
166
|
String result = null;
|
167
|
logReplication.info("ReplicationHandler.update - full update started to: " + server);
|
168
|
// Send command to that server to get updated docid information
|
169
|
try
|
170
|
{
|
171
|
u = new URL("https://" + server + "?server="
|
172
|
+MetacatUtil.getLocalReplicationServerName()+"&action=update");
|
173
|
logReplication.info("ReplicationHandler.update - Sending infomation " +u.toString());
|
174
|
result = ReplicationService.getURLContent(u);
|
175
|
}
|
176
|
catch (Exception e)
|
177
|
{
|
178
|
logMetacat.error("ReplicationHandler.update - " + ReplicationService.METACAT_REPL_ERROR_MSG);
|
179
|
logReplication.error( "ReplicationHandler.update - Failed to get updated doc list "+
|
180
|
"for server " + server + " because "+e.getMessage());
|
181
|
continue;
|
182
|
}
|
183
|
|
184
|
//logReplication.info("ReplicationHandler.update - docid: "+server+" "+result);
|
185
|
//check if result have error or not, if has skip it.
|
186
|
if (result.indexOf("<error>")!=-1 && result.indexOf("</error>")!=-1)
|
187
|
{
|
188
|
logMetacat.error("ReplicationHandler.update - " + ReplicationService.METACAT_REPL_ERROR_MSG);
|
189
|
logReplication.error( "ReplicationHandler.update - Failed to get updated doc list "+
|
190
|
"for server " + server + " because "+result);
|
191
|
continue;
|
192
|
}
|
193
|
//Add result to vector
|
194
|
responses.add(result);
|
195
|
}
|
196
|
timeToGetServerList = System.currentTimeMillis() - startTimeToGetServers;
|
197
|
|
198
|
//make sure that there is updated file list
|
199
|
//If response is null, metacat don't need do anything
|
200
|
if (responses==null || responses.isEmpty())
|
201
|
{
|
202
|
logMetacat.error("ReplicationHandler.update - " + ReplicationService.METACAT_REPL_ERROR_MSG);
|
203
|
logReplication.info( "ReplicationHandler.update - No updated doc list for "+
|
204
|
"every server and failed to replicate");
|
205
|
return;
|
206
|
}
|
207
|
|
208
|
|
209
|
//logReplication.info("ReplicationHandler.update - Responses from remote metacat about updated "+
|
210
|
// "document information: "+ responses.toString());
|
211
|
|
212
|
long totalServerListParseTime = 0;
|
213
|
// go through response vector(it contains updated vector and delete vector
|
214
|
for(int i=0; i<responses.size(); i++)
|
215
|
{
|
216
|
long startServerListParseTime = System.currentTimeMillis();
|
217
|
XMLReader parser;
|
218
|
ReplMessageHandler message = new ReplMessageHandler();
|
219
|
try
|
220
|
{
|
221
|
parser = initParser(message);
|
222
|
}
|
223
|
catch (Exception e)
|
224
|
{
|
225
|
logMetacat.error("ReplicationHandler.update - " + ReplicationService.METACAT_REPL_ERROR_MSG);
|
226
|
logReplication.error("ReplicationHandler.update - Failed to replicate becaue couldn't " +
|
227
|
" initParser for message and " +e.getMessage());
|
228
|
// stop replication
|
229
|
return;
|
230
|
}
|
231
|
|
232
|
try
|
233
|
{
|
234
|
parser.parse(new InputSource(
|
235
|
new StringReader(
|
236
|
(String)(responses.elementAt(i)))));
|
237
|
}
|
238
|
catch(Exception e)
|
239
|
{
|
240
|
logMetacat.error("ReplicationHandler.update - " + ReplicationService.METACAT_REPL_ERROR_MSG);
|
241
|
logReplication.error("ReplicationHandler.update - Couldn't parse one responses "+
|
242
|
"because "+ e.getMessage());
|
243
|
continue;
|
244
|
}
|
245
|
//v is the list of updated documents
|
246
|
Vector<Vector<String>> updateList = new Vector<Vector<String>>(message.getUpdatesVect());
|
247
|
logReplication.info("ReplicationHandler.update - The document list size is "+updateList.size()+ " from "+message.getServerName());
|
248
|
//System.out.println("v: " + v.toString());
|
249
|
//d is the list of deleted documents
|
250
|
Vector<Vector<String>> deleteList = new Vector<Vector<String>>(message.getDeletesVect());
|
251
|
//System.out.println("d: " + d.toString());
|
252
|
logReplication.info("ReplicationHandler.update - Update vector size: "+ updateList.size()+" from "+message.getServerName());
|
253
|
logReplication.info("ReplicationHandler.update - Delete vector size: "+ deleteList.size()+" from "+message.getServerName());
|
254
|
logReplication.info("ReplicationHandler.update - The delete document list size is "+deleteList.size()+" from "+message.getServerName());
|
255
|
// go though every element in updated document vector
|
256
|
handleDocList(updateList, DocumentImpl.DOCUMENTTABLE);
|
257
|
//handle deleted docs
|
258
|
for(int k=0; k<deleteList.size(); k++)
|
259
|
{ //delete the deleted documents;
|
260
|
Vector<String> w = new Vector<String>(deleteList.elementAt(k));
|
261
|
String docId = (String)w.elementAt(0);
|
262
|
try
|
263
|
{
|
264
|
handleDeleteSingleDocument(docId, server);
|
265
|
}
|
266
|
catch (Exception ee)
|
267
|
{
|
268
|
continue;
|
269
|
}
|
270
|
}//for delete docs
|
271
|
|
272
|
// handle replicate doc in xml_revision
|
273
|
Vector<Vector<String>> revisionList = new Vector<Vector<String>>(message.getRevisionsVect());
|
274
|
logReplication.info("ReplicationHandler.update - The revision document list size is "+revisionList.size()+ " from "+message.getServerName());
|
275
|
handleDocList(revisionList, DocumentImpl.REVISIONTABLE);
|
276
|
DOCINSERTNUMBER = 1;
|
277
|
DOCERRORNUMBER = 1;
|
278
|
REVINSERTNUMBER = 1;
|
279
|
REVERRORNUMBER = 1;
|
280
|
|
281
|
totalServerListParseTime += (System.currentTimeMillis() - startServerListParseTime);
|
282
|
}//for response
|
283
|
|
284
|
//updated last_checked
|
285
|
for (int i=0;i<serverList.size(); i++)
|
286
|
{
|
287
|
// Get ReplicationServer object from server list
|
288
|
replServer = serverList.serverAt(i);
|
289
|
try
|
290
|
{
|
291
|
updateLastCheckTimeForSingleServer(replServer);
|
292
|
}
|
293
|
catch(Exception e)
|
294
|
{
|
295
|
continue;
|
296
|
}
|
297
|
}//for
|
298
|
|
299
|
long replicationEndTime = System.currentTimeMillis();
|
300
|
logMetacat.debug("ReplicationHandler.update - Total replication time: " +
|
301
|
(replicationEndTime - replicationStartTime));
|
302
|
logMetacat.debug("ReplicationHandler.update - time to get server list: " +
|
303
|
timeToGetServerList);
|
304
|
logMetacat.debug("ReplicationHandler.update - server list parse time: " +
|
305
|
totalServerListParseTime);
|
306
|
logMetacat.debug("ReplicationHandler.update - 'in xml_documents' total query count: " +
|
307
|
_xmlDocQueryCount);
|
308
|
logMetacat.debug("ReplicationHandler.update - 'in xml_documents' total query time: " +
|
309
|
_xmlDocQueryTime + " ms");
|
310
|
logMetacat.debug("ReplicationHandler.update - 'in xml_revisions' total query count: " +
|
311
|
_xmlRevQueryCount);
|
312
|
logMetacat.debug("ReplicationHandler.update - 'in xml_revisions' total query time: " +
|
313
|
_xmlRevQueryTime + " ms");;
|
314
|
|
315
|
}//update
|
316
|
|
317
|
/* Handle replicate single xml document*/
|
318
|
private void handleSingleXMLDocument(String remoteserver, String actions,
|
319
|
String accNumber, String tableName)
|
320
|
throws HandlerException
|
321
|
{
|
322
|
DBConnection dbConn = null;
|
323
|
int serialNumber = -1;
|
324
|
try
|
325
|
{
|
326
|
// Get DBConnection from pool
|
327
|
dbConn=DBConnectionPool.
|
328
|
getDBConnection("ReplicationHandler.handleSingleXMLDocument");
|
329
|
serialNumber=dbConn.getCheckOutSerialNumber();
|
330
|
//if the document needs to be updated or inserted, this is executed
|
331
|
String readDocURLString = "https://" + remoteserver + "?server="+
|
332
|
MetacatUtil.getLocalReplicationServerName()+"&action=read&docid="+accNumber;
|
333
|
readDocURLString = MetacatUtil.replaceWhiteSpaceForURL(readDocURLString);
|
334
|
URL u = new URL(readDocURLString);
|
335
|
|
336
|
// Get docid content
|
337
|
String newxmldoc = ReplicationService.getURLContent(u);
|
338
|
// If couldn't get skip it
|
339
|
if ( newxmldoc.indexOf("<error>")!= -1 && newxmldoc.indexOf("</error>")!=-1)
|
340
|
{
|
341
|
throw new HandlerException("ReplicationHandler.handleSingleXMLDocument - " + newxmldoc);
|
342
|
}
|
343
|
//logReplication.info("xml documnet:");
|
344
|
//logReplication.info(newxmldoc);
|
345
|
|
346
|
// Try get the docid info from remote server
|
347
|
DocInfoHandler dih = new DocInfoHandler();
|
348
|
XMLReader docinfoParser = initParser(dih);
|
349
|
String docInfoURLStr = "https://" + remoteserver +
|
350
|
"?server="+MetacatUtil.getLocalReplicationServerName()+
|
351
|
"&action=getdocumentinfo&docid="+accNumber;
|
352
|
docInfoURLStr = MetacatUtil.replaceWhiteSpaceForURL(docInfoURLStr);
|
353
|
URL docinfoUrl = new URL(docInfoURLStr);
|
354
|
logReplication.info("ReplicationHandler.handleSingleXMLDocument - Sending message: " +
|
355
|
docinfoUrl.toString());
|
356
|
String docInfoStr = ReplicationService.getURLContent(docinfoUrl);
|
357
|
docinfoParser.parse(new InputSource(new StringReader(docInfoStr)));
|
358
|
Hashtable<String, String> docinfoHash = dih.getDocInfo();
|
359
|
// Get home server of the docid
|
360
|
String docHomeServer = docinfoHash.get("home_server");
|
361
|
logReplication.info("ReplicationHandler.handleSingleXMLDocument - doc home server in repl: "+docHomeServer);
|
362
|
String createdDate = docinfoHash.get("date_created");
|
363
|
String updatedDate = docinfoHash.get("date_updated");
|
364
|
//docid should include rev number too
|
365
|
/*String accnum=docId+util.getProperty("document.accNumSeparator")+
|
366
|
(String)docinfoHash.get("rev");*/
|
367
|
logReplication.info("ReplicationHandler.handleSingleXMLDocument - docid in repl: "+accNumber);
|
368
|
String docType = docinfoHash.get("doctype");
|
369
|
logReplication.info("ReplicationHandler.handleSingleXMLDocument - doctype in repl: "+docType);
|
370
|
|
371
|
String parserBase = null;
|
372
|
// this for eml2 and we need user eml2 parser
|
373
|
if (docType != null && (docType.trim()).equals(DocumentImpl.EML2_0_0NAMESPACE))
|
374
|
{
|
375
|
parserBase = DocumentImpl.EML200;
|
376
|
}
|
377
|
else if (docType != null && (docType.trim()).equals(DocumentImpl.EML2_0_1NAMESPACE))
|
378
|
{
|
379
|
parserBase = DocumentImpl.EML200;
|
380
|
}
|
381
|
else if (docType != null && (docType.trim()).equals(DocumentImpl.EML2_1_0NAMESPACE))
|
382
|
{
|
383
|
parserBase = DocumentImpl.EML210;
|
384
|
}
|
385
|
// Write the document into local host
|
386
|
DocumentImplWrapper wrapper = new DocumentImplWrapper(parserBase, false);
|
387
|
String newDocid = wrapper.writeReplication(dbConn,
|
388
|
newxmldoc,
|
389
|
docinfoHash.get("public_access"),
|
390
|
null, /* the dtd text */
|
391
|
actions,
|
392
|
accNumber,
|
393
|
docinfoHash.get("user_owner"),
|
394
|
null, /* null for groups[] */
|
395
|
docHomeServer,
|
396
|
remoteserver, tableName, true,// true is for time replication
|
397
|
createdDate,
|
398
|
updatedDate);
|
399
|
|
400
|
//process extra access rules
|
401
|
Vector<XMLAccessDAO> xmlAccessDAOList = dih.getAccessControlList();
|
402
|
if (xmlAccessDAOList != null) {
|
403
|
AccessControlForSingleFile acfsf = new AccessControlForSingleFile(accNumber);
|
404
|
for (XMLAccessDAO xmlAccessDAO : xmlAccessDAOList) {
|
405
|
if (!acfsf.accessControlExists(xmlAccessDAO)) {
|
406
|
acfsf.insertPermissions(xmlAccessDAO);
|
407
|
}
|
408
|
}
|
409
|
}
|
410
|
|
411
|
//process guid
|
412
|
logReplication.debug("Processing guid information from docinfoHash: " + docinfoHash.toString());
|
413
|
String guid = docinfoHash.get("guid");
|
414
|
String docName = docinfoHash.get("docName");
|
415
|
System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%guid passed from docinfo hash: " + guid);
|
416
|
IdentifierManager idman = IdentifierManager.getInstance();
|
417
|
if(guid != null && !idman.identifierExists(guid))
|
418
|
{ //if the guid was passed in, put it in the identifiers table
|
419
|
logReplication.debug("Creating guid/docid mapping for docid " +
|
420
|
docinfoHash.get("docid") + " and guid: " + guid);
|
421
|
System.out.println("docname: " + docName);
|
422
|
if(docName.trim().equals("systemMetadata"))
|
423
|
{
|
424
|
System.out.println("creating mapping for systemMetadata: guid: " + guid + " localId: " + docinfoHash.get("docid"));
|
425
|
idman.createSystemMetadataMapping(guid, docinfoHash.get("docid"));
|
426
|
}
|
427
|
else
|
428
|
{
|
429
|
System.out.println("creating mapping: guid: " + guid + " localId: " + docinfoHash.get("docid"));
|
430
|
idman.createMapping(guid, docinfoHash.get("docid"));
|
431
|
}
|
432
|
}
|
433
|
else
|
434
|
{
|
435
|
logReplication.debug("No guid information was included with the replicated document");
|
436
|
}
|
437
|
|
438
|
if(guid != null)
|
439
|
{
|
440
|
logReplication.info("replicate localId:" + accNumber + " guid:" + guid);
|
441
|
}
|
442
|
|
443
|
logReplication.info("ReplicationHandler.handleSingleXMLDocument - Successfully replicated doc " + accNumber);
|
444
|
if (tableName.equals(DocumentImpl.DOCUMENTTABLE))
|
445
|
{
|
446
|
logReplication.info("ReplicationHandler.handleSingleXMLDocument - " + DOCINSERTNUMBER + " Wrote xml doc " + accNumber +
|
447
|
" into "+tableName + " from " +
|
448
|
remoteserver);
|
449
|
DOCINSERTNUMBER++;
|
450
|
}
|
451
|
else
|
452
|
{
|
453
|
logReplication.info("ReplicationHandler.handleSingleXMLDocument - " +REVINSERTNUMBER + " Wrote xml doc " + accNumber +
|
454
|
" into "+tableName + " from " +
|
455
|
remoteserver);
|
456
|
REVINSERTNUMBER++;
|
457
|
}
|
458
|
String ip = getIpFromURL(u);
|
459
|
EventLog.getInstance().log(ip, ReplicationService.REPLICATIONUSER, accNumber, actions);
|
460
|
|
461
|
|
462
|
}//try
|
463
|
catch(Exception e)
|
464
|
{
|
465
|
|
466
|
if (tableName.equals(DocumentImpl.DOCUMENTTABLE))
|
467
|
{
|
468
|
logMetacat.error("ReplicationHandler.handleSingleXMLDocument - " + ReplicationService.METACAT_REPL_ERROR_MSG);
|
469
|
logReplication.error("ReplicationHandler.handleSingleXMLDocument - " +DOCERRORNUMBER + " Failed to write xml doc " + accNumber +
|
470
|
" into "+tableName + " from " +
|
471
|
remoteserver + " because "+e.getMessage());
|
472
|
DOCERRORNUMBER++;
|
473
|
}
|
474
|
else
|
475
|
{
|
476
|
logMetacat.error("ReplicationHandler.handleSingleXMLDocument - " + ReplicationService.METACAT_REPL_ERROR_MSG);
|
477
|
logReplication.error("ReplicationHandler.handleSingleXMLDocument - " +REVERRORNUMBER + " Failed to write xml doc " + accNumber +
|
478
|
" into "+tableName + " from " +
|
479
|
remoteserver +" because "+e.getMessage());
|
480
|
REVERRORNUMBER++;
|
481
|
}
|
482
|
logMetacat.error("ReplicationHandler.handleSingleXMLDocument - " + ReplicationService.METACAT_REPL_ERROR_MSG);
|
483
|
logReplication.error("ReplicationHandler.handleSingleXMLDocument - Failed to write doc " + accNumber +
|
484
|
" into db because " +e.getMessage());
|
485
|
throw new HandlerException("ReplicationHandler.handleSingleXMLDocument - generic exception "
|
486
|
+ "writing Replication: " +e.getMessage());
|
487
|
}
|
488
|
finally
|
489
|
{
|
490
|
//return DBConnection
|
491
|
DBConnectionPool.returnDBConnection(dbConn, serialNumber);
|
492
|
}//finally
|
493
|
logD1.info("replication.create localId:" + accNumber);
|
494
|
}
|
495
|
|
496
|
|
497
|
|
498
|
/* Handle replicate single xml document*/
|
499
|
private void handleSingleDataFile(String remoteserver, String actions,
|
500
|
String accNumber, String tableName)
|
501
|
throws HandlerException
|
502
|
{
|
503
|
logReplication.info("ReplicationHandler.handleSingleDataFile - Try to replicate data file: " + accNumber);
|
504
|
DBConnection dbConn = null;
|
505
|
int serialNumber = -1;
|
506
|
try
|
507
|
{
|
508
|
// Get DBConnection from pool
|
509
|
dbConn=DBConnectionPool.
|
510
|
getDBConnection("ReplicationHandler.handleSinlgeDataFile");
|
511
|
serialNumber=dbConn.getCheckOutSerialNumber();
|
512
|
// Try get docid info from remote server
|
513
|
DocInfoHandler dih = new DocInfoHandler();
|
514
|
XMLReader docinfoParser = initParser(dih);
|
515
|
String docInfoURLString = "https://" + remoteserver +
|
516
|
"?server="+MetacatUtil.getLocalReplicationServerName()+
|
517
|
"&action=getdocumentinfo&docid="+accNumber;
|
518
|
docInfoURLString = MetacatUtil.replaceWhiteSpaceForURL(docInfoURLString);
|
519
|
URL docinfoUrl = new URL(docInfoURLString);
|
520
|
|
521
|
String docInfoStr = ReplicationService.getURLContent(docinfoUrl);
|
522
|
docinfoParser.parse(new InputSource(new StringReader(docInfoStr)));
|
523
|
Hashtable<String, String> docinfoHash = dih.getDocInfo();
|
524
|
// Get docid owner
|
525
|
String user = docinfoHash.get("user_owner");
|
526
|
// Get docid name (such as acl or dataset)
|
527
|
String docName = docinfoHash.get("docname");
|
528
|
// Get doc type (eml public id)
|
529
|
String docType = docinfoHash.get("doctype");
|
530
|
// Get docid home sever. it might be different to remoteserver
|
531
|
// because of hub feature
|
532
|
String docHomeServer = docinfoHash.get("home_server");
|
533
|
String createdDate = docinfoHash.get("date_created");
|
534
|
String updatedDate = docinfoHash.get("date_updated");
|
535
|
//docid should include rev number too
|
536
|
/*String accnum=docId+util.getProperty("document.accNumSeparator")+
|
537
|
(String)docinfoHash.get("rev");*/
|
538
|
|
539
|
|
540
|
String datafilePath = PropertyService.getProperty("application.datafilepath");
|
541
|
// Get data file content
|
542
|
String readDataURLString = "https://" + remoteserver + "?server="+
|
543
|
MetacatUtil.getLocalReplicationServerName()+
|
544
|
"&action=readdata&docid="+accNumber;
|
545
|
readDataURLString = MetacatUtil.replaceWhiteSpaceForURL(readDataURLString);
|
546
|
URL u = new URL(readDataURLString);
|
547
|
InputStream input = u.openStream();
|
548
|
//register data file into xml_documents table and wite data file
|
549
|
//into file system
|
550
|
if ( input != null)
|
551
|
{
|
552
|
DocumentImpl.writeDataFileInReplication(input,
|
553
|
datafilePath,
|
554
|
docName,docType,
|
555
|
accNumber, user,
|
556
|
docHomeServer,
|
557
|
remoteserver,
|
558
|
tableName,
|
559
|
true, //true means timed replication
|
560
|
createdDate,
|
561
|
updatedDate);
|
562
|
|
563
|
//process extra access rules
|
564
|
Vector<XMLAccessDAO> xmlAccessDAOList = dih.getAccessControlList();
|
565
|
if (xmlAccessDAOList != null) {
|
566
|
AccessControlForSingleFile acfsf = new AccessControlForSingleFile(accNumber);
|
567
|
for (XMLAccessDAO xmlAccessDAO : xmlAccessDAOList) {
|
568
|
if (!acfsf.accessControlExists(xmlAccessDAO)) {
|
569
|
acfsf.insertPermissions(xmlAccessDAO);
|
570
|
}
|
571
|
}
|
572
|
}
|
573
|
|
574
|
logReplication.info("ReplicationHandler.handleSingleDataFile - Successfully to write datafile " + accNumber);
|
575
|
/*MetacatReplication.replLog("wrote datafile " + accNumber + " from " +
|
576
|
remote server);*/
|
577
|
if (tableName.equals(DocumentImpl.DOCUMENTTABLE))
|
578
|
{
|
579
|
logReplication.info("ReplicationHandler.handleSingleDataFile - " + DOCINSERTNUMBER + " Wrote data file" + accNumber +
|
580
|
" into "+tableName + " from " +
|
581
|
remoteserver);
|
582
|
DOCINSERTNUMBER++;
|
583
|
}
|
584
|
else
|
585
|
{
|
586
|
logReplication.info("ReplicationHandler.handleSingleDataFile - " + REVINSERTNUMBER + " Wrote data file" + accNumber +
|
587
|
" into "+tableName + " from " +
|
588
|
remoteserver);
|
589
|
REVINSERTNUMBER++;
|
590
|
}
|
591
|
String ip = getIpFromURL(u);
|
592
|
EventLog.getInstance().log(ip, ReplicationService.REPLICATIONUSER, accNumber, actions);
|
593
|
|
594
|
}//if
|
595
|
else
|
596
|
{
|
597
|
logReplication.info("ReplicationHandler.handleSingleDataFile - Couldn't open the data file: " + accNumber);
|
598
|
throw new HandlerException("ReplicationHandler.handleSingleDataFile - Couldn't open the data file: " + accNumber);
|
599
|
}//else
|
600
|
|
601
|
}//try
|
602
|
catch(Exception e)
|
603
|
{
|
604
|
/*MetacatReplication.replErrorLog("Failed to try wrote data file " + accNumber +
|
605
|
" because " +e.getMessage());*/
|
606
|
if (tableName.equals(DocumentImpl.DOCUMENTTABLE))
|
607
|
{
|
608
|
logMetacat.error("ReplicationHandler.handleSingleDataFile - " + ReplicationService.METACAT_REPL_ERROR_MSG);
|
609
|
logReplication.error("ReplicationHandler.handleSingleDataFile - " + DOCERRORNUMBER + " Failed to write data file " + accNumber +
|
610
|
" into " + tableName + " from " +
|
611
|
remoteserver + " because " + e.getMessage());
|
612
|
DOCERRORNUMBER++;
|
613
|
}
|
614
|
else
|
615
|
{
|
616
|
logMetacat.error("ReplicationHandler.handleSingleDataFile - " + ReplicationService.METACAT_REPL_ERROR_MSG);
|
617
|
logReplication.error("ReplicationHandler.handleSingleDataFile - " + REVERRORNUMBER + " Failed to write data file" + accNumber +
|
618
|
" into " + tableName + " from " +
|
619
|
remoteserver +" because "+ e.getMessage());
|
620
|
REVERRORNUMBER++;
|
621
|
}
|
622
|
logMetacat.error("ReplicationHandler.handleSingleDataFile - " + ReplicationService.METACAT_REPL_ERROR_MSG);
|
623
|
logReplication.error("ReplicationHandler.handleSingleDataFile - Failed to try wrote datafile " + accNumber +
|
624
|
" because " + e.getMessage());
|
625
|
throw new HandlerException("ReplicationHandler.handleSingleDataFile - generic exception "
|
626
|
+ "writing Replication: " + e.getMessage());
|
627
|
}
|
628
|
finally
|
629
|
{
|
630
|
//return DBConnection
|
631
|
DBConnectionPool.returnDBConnection(dbConn, serialNumber);
|
632
|
}//finally
|
633
|
logD1.info("replication.create localId:" + accNumber);
|
634
|
}
|
635
|
|
636
|
|
637
|
|
638
|
/* Handle delete single document*/
|
639
|
private void handleDeleteSingleDocument(String docId, String notifyServer)
|
640
|
throws HandlerException
|
641
|
{
|
642
|
logReplication.info("ReplicationHandler.handleDeleteSingleDocument - Try delete doc: "+docId);
|
643
|
DBConnection dbConn = null;
|
644
|
int serialNumber = -1;
|
645
|
try
|
646
|
{
|
647
|
// Get DBConnection from pool
|
648
|
dbConn=DBConnectionPool.
|
649
|
getDBConnection("ReplicationHandler.handleDeleteSingleDoc");
|
650
|
serialNumber=dbConn.getCheckOutSerialNumber();
|
651
|
if(!alreadyDeleted(docId))
|
652
|
{
|
653
|
|
654
|
//because delete method docid should have rev number
|
655
|
//so we just add one for it. This rev number is no sence.
|
656
|
String accnum=docId+PropertyService.getProperty("document.accNumSeparator")+"1";
|
657
|
//System.out.println("accnum: "+accnum);
|
658
|
DocumentImpl.delete(accnum, null, null, notifyServer);
|
659
|
logReplication.info("ReplicationHandler.handleDeleteSingleDocument - Successfully deleted doc " + docId);
|
660
|
logReplication.info("ReplicationHandler.handleDeleteSingleDocument - Doc " + docId + " deleted");
|
661
|
URL u = new URL("https://"+notifyServer);
|
662
|
String ip = getIpFromURL(u);
|
663
|
EventLog.getInstance().log(ip, ReplicationService.REPLICATIONUSER, docId, "delete");
|
664
|
}
|
665
|
|
666
|
}//try
|
667
|
catch(Exception e)
|
668
|
{
|
669
|
logMetacat.error("ReplicationHandler.handleDeleteSingleDocument - " + ReplicationService.METACAT_REPL_ERROR_MSG);
|
670
|
logReplication.error("ReplicationHandler.handleDeleteSingleDocument - Failed to delete doc " + docId +
|
671
|
" in db because because " + e.getMessage());
|
672
|
throw new HandlerException("ReplicationHandler.handleDeleteSingleDocument - generic exception "
|
673
|
+ "when handling document: " + e.getMessage());
|
674
|
}
|
675
|
finally
|
676
|
{
|
677
|
//return DBConnection
|
678
|
DBConnectionPool.returnDBConnection(dbConn, serialNumber);
|
679
|
}//finally
|
680
|
logD1.info("replication.handleDeleteSingleDocument localId:" + docId);
|
681
|
}
|
682
|
|
683
|
/* Handle updateLastCheckTimForSingleServer*/
|
684
|
private void updateLastCheckTimeForSingleServer(ReplicationServer repServer)
|
685
|
throws HandlerException
|
686
|
{
|
687
|
String server = repServer.getServerName();
|
688
|
DBConnection dbConn = null;
|
689
|
int serialNumber = -1;
|
690
|
PreparedStatement pstmt = null;
|
691
|
try
|
692
|
{
|
693
|
// Get DBConnection from pool
|
694
|
dbConn=DBConnectionPool.
|
695
|
getDBConnection("ReplicationHandler.updateLastCheckTimeForServer");
|
696
|
serialNumber=dbConn.getCheckOutSerialNumber();
|
697
|
|
698
|
logReplication.info("ReplicationHandler.updateLastCheckTimeForSingleServer - Try to update last_check for server: "+server);
|
699
|
// Get time from remote server
|
700
|
URL dateurl = new URL("https://" + server + "?server="+
|
701
|
MetacatUtil.getLocalReplicationServerName()+"&action=gettime");
|
702
|
String datexml = ReplicationService.getURLContent(dateurl);
|
703
|
logReplication.info("ReplicationHandler.updateLastCheckTimeForSingleServer - datexml: "+datexml);
|
704
|
if (datexml!=null && !datexml.equals(""))
|
705
|
{
|
706
|
String datestr = datexml.substring(11, datexml.indexOf('<', 11));
|
707
|
StringBuffer sql = new StringBuffer();
|
708
|
/*sql.append("update xml_replication set last_checked = to_date('");
|
709
|
sql.append(datestr).append("', 'YY-MM-DD HH24:MI:SS') where ");
|
710
|
sql.append("server like '").append(server).append("'");*/
|
711
|
sql.append("update xml_replication set last_checked = ");
|
712
|
sql.append(DatabaseService.getInstance().getDBAdapter().toDate(datestr, "MM/DD/YY HH24:MI:SS"));
|
713
|
sql.append(" where server like '").append(server).append("'");
|
714
|
pstmt = dbConn.prepareStatement(sql.toString());
|
715
|
|
716
|
pstmt.executeUpdate();
|
717
|
dbConn.commit();
|
718
|
pstmt.close();
|
719
|
logReplication.info("ReplicationHandler.updateLastCheckTimeForSingleServer - last_checked updated to "+datestr+" on "
|
720
|
+ server);
|
721
|
}//if
|
722
|
else
|
723
|
{
|
724
|
|
725
|
logReplication.info("ReplicationHandler.updateLastCheckTimeForSingleServer - Failed to update last_checked for server " +
|
726
|
server + " in db because couldn't get time "
|
727
|
);
|
728
|
throw new Exception("Couldn't get time for server "+ server);
|
729
|
}
|
730
|
|
731
|
}//try
|
732
|
catch(Exception e)
|
733
|
{
|
734
|
logMetacat.error("ReplicationHandler.updateLastCheckTimeForSingleServer - " + ReplicationService.METACAT_REPL_ERROR_MSG);
|
735
|
logReplication.error("ReplicationHandler.updateLastCheckTimeForSingleServer - Failed to update last_checked for server " +
|
736
|
server + " in db because because " + e.getMessage());
|
737
|
throw new HandlerException("ReplicationHandler.updateLastCheckTimeForSingleServer - "
|
738
|
+ "Error updating last checked time: " + e.getMessage());
|
739
|
}
|
740
|
finally
|
741
|
{
|
742
|
//return DBConnection
|
743
|
DBConnectionPool.returnDBConnection(dbConn, serialNumber);
|
744
|
}//finally
|
745
|
}
|
746
|
|
747
|
|
748
|
|
749
|
/**
|
750
|
* updates xml_catalog with entries from other servers.
|
751
|
*/
|
752
|
private void updateCatalog()
|
753
|
{
|
754
|
logReplication.info("ReplicationHandler.updateCatalog - Start of updateCatalog");
|
755
|
// ReplicationServer object in server list
|
756
|
ReplicationServer replServer = null;
|
757
|
PreparedStatement pstmt = null;
|
758
|
String server = null;
|
759
|
|
760
|
|
761
|
// Go through each ReplicationServer object in sererlist
|
762
|
for (int j=0; j<serverList.size(); j++)
|
763
|
{
|
764
|
Vector<Vector<String>> remoteCatalog = new Vector<Vector<String>>();
|
765
|
Vector<String> publicId = new Vector<String>();
|
766
|
try
|
767
|
{
|
768
|
// Get ReplicationServer object from server list
|
769
|
replServer = serverList.serverAt(j);
|
770
|
// Get server name from the ReplicationServer object
|
771
|
server = replServer.getServerName();
|
772
|
// Try to get catalog
|
773
|
URL u = new URL("https://" + server + "?server="+
|
774
|
MetacatUtil.getLocalReplicationServerName()+"&action=getcatalog");
|
775
|
logReplication.info("ReplicationHandler.updateCatalog - sending message " + u.toString());
|
776
|
String catxml = ReplicationService.getURLContent(u);
|
777
|
|
778
|
// Make sure there are not error, no empty string
|
779
|
if (catxml.indexOf("error")!=-1 || catxml==null||catxml.equals(""))
|
780
|
{
|
781
|
throw new Exception("Couldn't get catalog list form server " +server);
|
782
|
}
|
783
|
logReplication.debug("ReplicationHandler.updateCatalog - catxml: " + catxml);
|
784
|
CatalogMessageHandler cmh = new CatalogMessageHandler();
|
785
|
XMLReader catparser = initParser(cmh);
|
786
|
catparser.parse(new InputSource(new StringReader(catxml)));
|
787
|
//parse the returned catalog xml and put it into a vector
|
788
|
remoteCatalog = cmh.getCatalogVect();
|
789
|
|
790
|
// Make sure remoteCatalog is not empty
|
791
|
if (remoteCatalog.isEmpty())
|
792
|
{
|
793
|
throw new Exception("Couldn't get catalog list form server " +server);
|
794
|
}
|
795
|
|
796
|
String localcatxml = ReplicationService.getCatalogXML();
|
797
|
|
798
|
// Make sure local catalog is no empty
|
799
|
if (localcatxml==null||localcatxml.equals(""))
|
800
|
{
|
801
|
throw new Exception("Couldn't get catalog list form server " +server);
|
802
|
}
|
803
|
|
804
|
cmh = new CatalogMessageHandler();
|
805
|
catparser = initParser(cmh);
|
806
|
catparser.parse(new InputSource(new StringReader(localcatxml)));
|
807
|
Vector<Vector<String>> localCatalog = cmh.getCatalogVect();
|
808
|
|
809
|
//now we have the catalog from the remote server and this local server
|
810
|
//we now need to compare the two and merge the differences.
|
811
|
//the comparison is base on the public_id fields which is the 4th
|
812
|
//entry in each row vector.
|
813
|
publicId = new Vector<String>();
|
814
|
for(int i=0; i<localCatalog.size(); i++)
|
815
|
{
|
816
|
Vector<String> v = new Vector<String>(localCatalog.elementAt(i));
|
817
|
logReplication.info("ReplicationHandler.updateCatalog - v1: " + v.toString());
|
818
|
publicId.add(new String((String)v.elementAt(3)));
|
819
|
//System.out.println("adding " + (String)v.elementAt(3));
|
820
|
}
|
821
|
}//try
|
822
|
catch (Exception e)
|
823
|
{
|
824
|
logMetacat.error("ReplicationHandler.updateCatalog - " + ReplicationService.METACAT_REPL_ERROR_MSG);
|
825
|
logReplication.error("ReplicationHandler.updateCatalog - Failed to update catalog for server "+
|
826
|
server + " because " +e.getMessage());
|
827
|
}//catch
|
828
|
|
829
|
for(int i=0; i<remoteCatalog.size(); i++)
|
830
|
{
|
831
|
// DConnection
|
832
|
DBConnection dbConn = null;
|
833
|
// DBConnection checkout serial number
|
834
|
int serialNumber = -1;
|
835
|
try
|
836
|
{
|
837
|
dbConn=DBConnectionPool.
|
838
|
getDBConnection("ReplicationHandler.updateCatalog");
|
839
|
serialNumber=dbConn.getCheckOutSerialNumber();
|
840
|
Vector<String> v = remoteCatalog.elementAt(i);
|
841
|
//System.out.println("v2: " + v.toString());
|
842
|
//System.out.println("i: " + i);
|
843
|
//System.out.println("remoteCatalog.size(): " + remoteCatalog.size());
|
844
|
//System.out.println("publicID: " + publicId.toString());
|
845
|
logReplication.info
|
846
|
("ReplicationHandler.updateCatalog - v.elementAt(3): " + (String)v.elementAt(3));
|
847
|
if(!publicId.contains(v.elementAt(3)))
|
848
|
{ //so we don't have this public id in our local table so we need to
|
849
|
//add it.
|
850
|
//System.out.println("in if");
|
851
|
StringBuffer sql = new StringBuffer();
|
852
|
sql.append("insert into xml_catalog (entry_type, source_doctype, ");
|
853
|
sql.append("target_doctype, public_id, system_id) values (?,?,?,");
|
854
|
sql.append("?,?)");
|
855
|
//System.out.println("sql: " + sql.toString());
|
856
|
pstmt = dbConn.prepareStatement(sql.toString());
|
857
|
pstmt.setString(1, (String)v.elementAt(0));
|
858
|
pstmt.setString(2, (String)v.elementAt(1));
|
859
|
pstmt.setString(3, (String)v.elementAt(2));
|
860
|
pstmt.setString(4, (String)v.elementAt(3));
|
861
|
pstmt.setString(5, (String)v.elementAt(4));
|
862
|
pstmt.execute();
|
863
|
pstmt.close();
|
864
|
logReplication.info("ReplicationHandler.updateCatalog - Success fully to insert new publicid "+
|
865
|
(String)v.elementAt(3) + " from server"+server);
|
866
|
}
|
867
|
}
|
868
|
catch(Exception e)
|
869
|
{
|
870
|
logMetacat.error("ReplicationHandler.updateCatalog - " + ReplicationService.METACAT_REPL_ERROR_MSG);
|
871
|
logReplication.error("ReplicationHandler.updateCatalog - Failed to update catalog for server "+
|
872
|
server + " because " +e.getMessage());
|
873
|
}//catch
|
874
|
finally
|
875
|
{
|
876
|
DBConnectionPool.returnDBConnection(dbConn, serialNumber);
|
877
|
}//finally
|
878
|
}//for remote catalog
|
879
|
}//for server list
|
880
|
logReplication.info("End of updateCatalog");
|
881
|
}
|
882
|
|
883
|
/**
|
884
|
* Method that returns true if docid has already been "deleted" from metacat.
|
885
|
* This method really implements a truth table for deleted documents
|
886
|
* The table is (a docid in one of the tables is represented by the X):
|
887
|
* xml_docs xml_revs deleted?
|
888
|
* ------------------------------------
|
889
|
* X X FALSE
|
890
|
* X _ FALSE
|
891
|
* _ X TRUE
|
892
|
* _ _ TRUE
|
893
|
*/
|
894
|
private static boolean alreadyDeleted(String docid) throws HandlerException
|
895
|
{
|
896
|
DBConnection dbConn = null;
|
897
|
int serialNumber = -1;
|
898
|
PreparedStatement pstmt = null;
|
899
|
try
|
900
|
{
|
901
|
dbConn=DBConnectionPool.
|
902
|
getDBConnection("ReplicationHandler.alreadyDeleted");
|
903
|
serialNumber=dbConn.getCheckOutSerialNumber();
|
904
|
boolean xml_docs = false;
|
905
|
boolean xml_revs = false;
|
906
|
|
907
|
StringBuffer sb = new StringBuffer();
|
908
|
sb.append("select docid from xml_revisions where docid like '");
|
909
|
sb.append(docid).append("'");
|
910
|
pstmt = dbConn.prepareStatement(sb.toString());
|
911
|
pstmt.execute();
|
912
|
ResultSet rs = pstmt.getResultSet();
|
913
|
boolean tablehasrows = rs.next();
|
914
|
if(tablehasrows)
|
915
|
{
|
916
|
xml_revs = true;
|
917
|
}
|
918
|
|
919
|
sb = new StringBuffer();
|
920
|
sb.append("select docid from xml_documents where docid like '");
|
921
|
sb.append(docid).append("'");
|
922
|
pstmt.close();
|
923
|
pstmt = dbConn.prepareStatement(sb.toString());
|
924
|
//increase usage count
|
925
|
dbConn.increaseUsageCount(1);
|
926
|
pstmt.execute();
|
927
|
rs = pstmt.getResultSet();
|
928
|
tablehasrows = rs.next();
|
929
|
pstmt.close();
|
930
|
if(tablehasrows)
|
931
|
{
|
932
|
xml_docs = true;
|
933
|
}
|
934
|
|
935
|
if(xml_docs && xml_revs)
|
936
|
{
|
937
|
return false;
|
938
|
}
|
939
|
else if(xml_docs && !xml_revs)
|
940
|
{
|
941
|
return false;
|
942
|
}
|
943
|
else if(!xml_docs && xml_revs)
|
944
|
{
|
945
|
return true;
|
946
|
}
|
947
|
else if(!xml_docs && !xml_revs)
|
948
|
{
|
949
|
return true;
|
950
|
}
|
951
|
}
|
952
|
catch(Exception e)
|
953
|
{
|
954
|
logMetacat.error("ReplicationHandler.alreadyDeleted - " + ReplicationService.METACAT_REPL_ERROR_MSG);
|
955
|
logReplication.error("ReplicationHandler.alreadyDeleted - general error in alreadyDeleted: " +
|
956
|
e.getMessage());
|
957
|
throw new HandlerException("ReplicationHandler.alreadyDeleted - general error: "
|
958
|
+ e.getMessage());
|
959
|
}
|
960
|
finally
|
961
|
{
|
962
|
try
|
963
|
{
|
964
|
pstmt.close();
|
965
|
}//try
|
966
|
catch (SQLException ee)
|
967
|
{
|
968
|
logMetacat.error("ReplicationHandler.alreadyDeleted - " + ReplicationService.METACAT_REPL_ERROR_MSG);
|
969
|
logReplication.error("ReplicationHandler.alreadyDeleted - Error in replicationHandler.alreadyDeleted "+
|
970
|
"to close pstmt: "+ee.getMessage());
|
971
|
throw new HandlerException("ReplicationHandler.alreadyDeleted - SQL error when closing prepared statement: "
|
972
|
+ ee.getMessage());
|
973
|
}//catch
|
974
|
finally
|
975
|
{
|
976
|
DBConnectionPool.returnDBConnection(dbConn, serialNumber);
|
977
|
}//finally
|
978
|
}//finally
|
979
|
return false;
|
980
|
}
|
981
|
|
982
|
|
983
|
/**
|
984
|
* Method to initialize the message parser
|
985
|
*/
|
986
|
public static XMLReader initParser(DefaultHandler dh)
|
987
|
throws HandlerException
|
988
|
{
|
989
|
XMLReader parser = null;
|
990
|
|
991
|
try {
|
992
|
ContentHandler chandler = dh;
|
993
|
|
994
|
// Get an instance of the parser
|
995
|
String parserName = PropertyService.getProperty("xml.saxparser");
|
996
|
parser = XMLReaderFactory.createXMLReader(parserName);
|
997
|
|
998
|
// Turn off validation
|
999
|
parser.setFeature("http://xml.org/sax/features/validation", false);
|
1000
|
|
1001
|
parser.setContentHandler((ContentHandler)chandler);
|
1002
|
parser.setErrorHandler((ErrorHandler)chandler);
|
1003
|
|
1004
|
} catch (SAXException se) {
|
1005
|
throw new HandlerException("ReplicationHandler.initParser - Sax error when "
|
1006
|
+ " initializing parser: " + se.getMessage());
|
1007
|
} catch (PropertyNotFoundException pnfe) {
|
1008
|
throw new HandlerException("ReplicationHandler.initParser - Property error when "
|
1009
|
+ " getting parser name: " + pnfe.getMessage());
|
1010
|
}
|
1011
|
|
1012
|
return parser;
|
1013
|
}
|
1014
|
|
1015
|
/**
|
1016
|
* This method will combine given time string(in short format) to current
|
1017
|
* date. If the given time (e.g 10:00 AM) passed the current time (e.g 2:00
|
1018
|
* PM Aug 21, 2005), then the time will set to second day, 10:00 AM Aug 22,
|
1019
|
* 2005. If the given time (e.g 10:00 AM) haven't passed the current time
|
1020
|
* (e.g 8:00 AM Aug 21, 2005) The time will set to be 10:00 AM Aug 21, 2005.
|
1021
|
*
|
1022
|
* @param givenTime
|
1023
|
* the format should be "10:00 AM " or "2:00 PM"
|
1024
|
* @return
|
1025
|
* @throws Exception
|
1026
|
*/
|
1027
|
public static Date combinateCurrentDateAndGivenTime(String givenTime) throws HandlerException
|
1028
|
{
|
1029
|
try {
|
1030
|
Date givenDate = parseTime(givenTime);
|
1031
|
Date newDate = null;
|
1032
|
Date now = new Date();
|
1033
|
String currentTimeString = getTimeString(now);
|
1034
|
Date currentTime = parseTime(currentTimeString);
|
1035
|
if ( currentTime.getTime() >= givenDate.getTime())
|
1036
|
{
|
1037
|
logReplication.info("ReplicationHandler.combinateCurrentDateAndGivenTime - Today already pass the given time, we should set it as tomorrow");
|
1038
|
String dateAndTime = getDateString(now) + " " + givenTime;
|
1039
|
Date combinationDate = parseDateTime(dateAndTime);
|
1040
|
// new date should plus 24 hours to make is the second day
|
1041
|
newDate = new Date(combinationDate.getTime()+24*3600*1000);
|
1042
|
}
|
1043
|
else
|
1044
|
{
|
1045
|
logReplication.info("ReplicationHandler.combinateCurrentDateAndGivenTime - Today haven't pass the given time, we should it as today");
|
1046
|
String dateAndTime = getDateString(now) + " " + givenTime;
|
1047
|
newDate = parseDateTime(dateAndTime);
|
1048
|
}
|
1049
|
logReplication.warn("ReplicationHandler.combinateCurrentDateAndGivenTime - final setting time is "+ newDate.toString());
|
1050
|
return newDate;
|
1051
|
} catch (ParseException pe) {
|
1052
|
throw new HandlerException("ReplicationHandler.combinateCurrentDateAndGivenTime - "
|
1053
|
+ "parsing error: " + pe.getMessage());
|
1054
|
}
|
1055
|
}
|
1056
|
|
1057
|
/*
|
1058
|
* parse a given string to Time in short format. For example, given time is
|
1059
|
* 10:00 AM, the date will be return as Jan 1 1970, 10:00 AM
|
1060
|
*/
|
1061
|
private static Date parseTime(String timeString) throws ParseException
|
1062
|
{
|
1063
|
DateFormat format = DateFormat.getTimeInstance(DateFormat.SHORT);
|
1064
|
Date time = format.parse(timeString);
|
1065
|
logReplication.info("ReplicationHandler.parseTime - Date string is after parse a time string "
|
1066
|
+time.toString());
|
1067
|
return time;
|
1068
|
|
1069
|
}
|
1070
|
|
1071
|
/*
|
1072
|
* Parse a given string to date and time. Date format is long and time
|
1073
|
* format is short.
|
1074
|
*/
|
1075
|
private static Date parseDateTime(String timeString) throws ParseException
|
1076
|
{
|
1077
|
DateFormat format = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.SHORT);
|
1078
|
Date time = format.parse(timeString);
|
1079
|
logReplication.info("ReplicationHandler.parseDateTime - Date string is after parse a time string "+
|
1080
|
time.toString());
|
1081
|
return time;
|
1082
|
}
|
1083
|
|
1084
|
/*
|
1085
|
* Get a date string from a Date object. The date format will be long
|
1086
|
*/
|
1087
|
private static String getDateString(Date now)
|
1088
|
{
|
1089
|
DateFormat df = DateFormat.getDateInstance(DateFormat.LONG);
|
1090
|
String s = df.format(now);
|
1091
|
logReplication.info("ReplicationHandler.getDateString - Today is " + s);
|
1092
|
return s;
|
1093
|
}
|
1094
|
|
1095
|
/*
|
1096
|
* Get a time string from a Date object, the time format will be short
|
1097
|
*/
|
1098
|
private static String getTimeString(Date now)
|
1099
|
{
|
1100
|
DateFormat df = DateFormat.getTimeInstance(DateFormat.SHORT);
|
1101
|
String s = df.format(now);
|
1102
|
logReplication.info("ReplicationHandler.getTimeString - Time is " + s);
|
1103
|
return s;
|
1104
|
}
|
1105
|
|
1106
|
|
1107
|
/*
|
1108
|
* This method will go through the docid list both in xml_Documents table
|
1109
|
* and in xml_revisions table @author tao
|
1110
|
*/
|
1111
|
private void handleDocList(Vector<Vector<String>> docList, String tableName) {
|
1112
|
boolean dataFile = false;
|
1113
|
for (int j = 0; j < docList.size(); j++) {
|
1114
|
// initial dataFile is false
|
1115
|
dataFile = false;
|
1116
|
// w is information for one document, information contain
|
1117
|
// docid, rev, server or datafile.
|
1118
|
Vector<String> w = new Vector<String>(docList.elementAt(j));
|
1119
|
// Check if the vector w contain "datafile"
|
1120
|
// If it has, this document is data file
|
1121
|
try {
|
1122
|
if (w.contains((String) PropertyService.getProperty("replication.datafileflag"))) {
|
1123
|
dataFile = true;
|
1124
|
}
|
1125
|
} catch (PropertyNotFoundException pnfe) {
|
1126
|
logMetacat.error("ReplicationHandler.handleDocList - " + ReplicationService.METACAT_REPL_ERROR_MSG);
|
1127
|
logReplication.error("ReplicationHandler.handleDocList - Could not retrieve data file flag property. "
|
1128
|
+ "Leaving as false: " + pnfe.getMessage());
|
1129
|
}
|
1130
|
// System.out.println("w: " + w.toString());
|
1131
|
// Get docid
|
1132
|
String docid = (String) w.elementAt(0);
|
1133
|
logReplication.info("docid: " + docid);
|
1134
|
// Get revision number
|
1135
|
int rev = Integer.parseInt((String) w.elementAt(1));
|
1136
|
logReplication.info("rev: " + rev);
|
1137
|
// Get remote server name (it is may not be doc home server because
|
1138
|
// the new hub feature
|
1139
|
String remoteServer = (String) w.elementAt(2);
|
1140
|
remoteServer = remoteServer.trim();
|
1141
|
|
1142
|
try {
|
1143
|
if (tableName.equals(DocumentImpl.DOCUMENTTABLE)) {
|
1144
|
handleDocInXMLDocuments(docid, rev, remoteServer, dataFile);
|
1145
|
} else if (tableName.equals(DocumentImpl.REVISIONTABLE)) {
|
1146
|
handleDocInXMLRevisions(docid, rev, remoteServer, dataFile);
|
1147
|
} else {
|
1148
|
continue;
|
1149
|
}
|
1150
|
|
1151
|
} catch (Exception e) {
|
1152
|
logMetacat.error("ReplicationHandler.handleDocList - " + ReplicationService.METACAT_REPL_ERROR_MSG);
|
1153
|
logReplication.error("ReplicationHandler.handleDocList - error to handle update doc in " + tableName
|
1154
|
+ " in time replication" + e.getMessage());
|
1155
|
continue;
|
1156
|
}
|
1157
|
|
1158
|
if (_xmlDocQueryCount > 0 && (_xmlDocQueryCount % 100) == 0) {
|
1159
|
logMetacat.debug("ReplicationHandler.update - xml_doc query count: " + _xmlDocQueryCount +
|
1160
|
", xml_doc avg query time: " + (_xmlDocQueryTime / _xmlDocQueryCount));
|
1161
|
}
|
1162
|
|
1163
|
if (_xmlRevQueryCount > 0 && (_xmlRevQueryCount % 100) == 0) {
|
1164
|
logMetacat.debug("ReplicationHandler.update - xml_rev query count: " + _xmlRevQueryCount +
|
1165
|
", xml_rev avg query time: " + (_xmlRevQueryTime / _xmlRevQueryCount));
|
1166
|
}
|
1167
|
|
1168
|
}// for update docs
|
1169
|
|
1170
|
}
|
1171
|
|
1172
|
/*
|
1173
|
* This method will handle doc in xml_documents table.
|
1174
|
*/
|
1175
|
private void handleDocInXMLDocuments(String docid, int rev, String remoteServer, boolean dataFile)
|
1176
|
throws HandlerException
|
1177
|
{
|
1178
|
// compare the update rev and local rev to see what need happen
|
1179
|
int localrev = -1;
|
1180
|
String action = null;
|
1181
|
boolean flag = false;
|
1182
|
try
|
1183
|
{
|
1184
|
long docQueryStartTime = System.currentTimeMillis();
|
1185
|
localrev = DBUtil.getLatestRevisionInDocumentTable(docid);
|
1186
|
long docQueryEndTime = System.currentTimeMillis();
|
1187
|
_xmlDocQueryTime += (docQueryEndTime - docQueryStartTime);
|
1188
|
_xmlDocQueryCount++;
|
1189
|
}
|
1190
|
catch (SQLException e)
|
1191
|
{
|
1192
|
logMetacat.error("ReplicationHandler.handleDocInXMLDocuments - " + ReplicationService.METACAT_REPL_ERROR_MSG);
|
1193
|
logReplication.error("ReplicationHandler.handleDocInXMLDocuments - Local rev for docid "+ docid + " could not "+
|
1194
|
" be found because " + e.getMessage());
|
1195
|
logReplication.error("ReplicationHandler.handleDocInXMLDocuments - " + DOCERRORNUMBER+"Docid "+ docid + " could not be "+
|
1196
|
"written because error happend to find it's local revision");
|
1197
|
DOCERRORNUMBER++;
|
1198
|
throw new HandlerException ("ReplicationHandler.handleDocInXMLDocuments - Local rev for docid "+ docid + " could not "+
|
1199
|
" be found: " + e.getMessage());
|
1200
|
}
|
1201
|
logReplication.info("ReplicationHandler.handleDocInXMLDocuments - Local rev for docid "+ docid + " is "+
|
1202
|
localrev);
|
1203
|
|
1204
|
//check the revs for an update because this document is in the
|
1205
|
//local DB, it might be out of date.
|
1206
|
if (localrev == -1)
|
1207
|
{
|
1208
|
// check if the revision is in the revision table
|
1209
|
Vector<Integer> localRevVector = null;
|
1210
|
try {
|
1211
|
long revQueryStartTime = System.currentTimeMillis();
|
1212
|
localRevVector = DBUtil.getRevListFromRevisionTable(docid);
|
1213
|
long revQueryEndTime = System.currentTimeMillis();
|
1214
|
_xmlRevQueryTime += (revQueryEndTime - revQueryStartTime);
|
1215
|
_xmlRevQueryCount++;
|
1216
|
} catch (SQLException sqle) {
|
1217
|
throw new HandlerException("ReplicationHandler.handleDocInXMLDocuments - SQL error "
|
1218
|
+ " when getting rev list for docid: " + docid + " : " + sqle.getMessage());
|
1219
|
}
|
1220
|
if (localRevVector != null && localRevVector.contains(new Integer(rev)))
|
1221
|
{
|
1222
|
// this version was deleted, so don't need replicate
|
1223
|
flag = false;
|
1224
|
}
|
1225
|
else
|
1226
|
{
|
1227
|
//insert this document as new because it is not in the local DB
|
1228
|
action = "INSERT";
|
1229
|
flag = true;
|
1230
|
}
|
1231
|
}
|
1232
|
else
|
1233
|
{
|
1234
|
if(localrev == rev)
|
1235
|
{
|
1236
|
// Local meatacat has the same rev to remote host, don't need
|
1237
|
// update and flag set false
|
1238
|
flag = false;
|
1239
|
}
|
1240
|
else if(localrev < rev)
|
1241
|
{
|
1242
|
//this document needs to be updated so send an read request
|
1243
|
action = "UPDATE";
|
1244
|
flag = true;
|
1245
|
}
|
1246
|
}
|
1247
|
|
1248
|
String accNumber = null;
|
1249
|
try {
|
1250
|
accNumber = docid + PropertyService.getProperty("document.accNumSeparator") + rev;
|
1251
|
} catch (PropertyNotFoundException pnfe) {
|
1252
|
throw new HandlerException("ReplicationHandler.handleDocInXMLDocuments - error getting "
|
1253
|
+ "account number separator : " + pnfe.getMessage());
|
1254
|
}
|
1255
|
// this is non-data file
|
1256
|
if(flag && !dataFile)
|
1257
|
{
|
1258
|
try
|
1259
|
{
|
1260
|
handleSingleXMLDocument(remoteServer, action, accNumber, DocumentImpl.DOCUMENTTABLE);
|
1261
|
}
|
1262
|
catch(HandlerException he)
|
1263
|
{
|
1264
|
// skip this document
|
1265
|
throw he;
|
1266
|
}
|
1267
|
}//if for non-data file
|
1268
|
|
1269
|
// this is for data file
|
1270
|
if(flag && dataFile)
|
1271
|
{
|
1272
|
try
|
1273
|
{
|
1274
|
handleSingleDataFile(remoteServer, action, accNumber, DocumentImpl.DOCUMENTTABLE);
|
1275
|
}
|
1276
|
catch(HandlerException he)
|
1277
|
{
|
1278
|
// skip this data file
|
1279
|
throw he;
|
1280
|
}
|
1281
|
|
1282
|
}//for data file
|
1283
|
}
|
1284
|
|
1285
|
/*
|
1286
|
* This method will handle doc in xml_documents table.
|
1287
|
*/
|
1288
|
private void handleDocInXMLRevisions(String docid, int rev, String remoteServer, boolean dataFile)
|
1289
|
throws HandlerException
|
1290
|
{
|
1291
|
// compare the update rev and local rev to see what need happen
|
1292
|
logReplication.info("ReplicationHandler.handleDocInXMLRevisions - In handle repliation revsion table");
|
1293
|
logReplication.info("ReplicationHandler.handleDocInXMLRevisions - the docid is "+ docid);
|
1294
|
logReplication.info("ReplicationHandler.handleDocInXMLRevisions - The rev is "+rev);
|
1295
|
Vector<Integer> localrev = null;
|
1296
|
String action = "INSERT";
|
1297
|
boolean flag = false;
|
1298
|
try
|
1299
|
{
|
1300
|
long revQueryStartTime = System.currentTimeMillis();
|
1301
|
localrev = DBUtil.getRevListFromRevisionTable(docid);
|
1302
|
long revQueryEndTime = System.currentTimeMillis();
|
1303
|
_xmlRevQueryTime += (revQueryEndTime - revQueryStartTime);
|
1304
|
_xmlRevQueryCount++;
|
1305
|
}
|
1306
|
catch (SQLException sqle)
|
1307
|
{
|
1308
|
logMetacat.error("ReplicationHandler.handleDocInXMLDocuments - " + ReplicationService.METACAT_REPL_ERROR_MSG);
|
1309
|
logReplication.error("ReplicationHandler.handleDocInXMLRevisions - Local rev for docid "+ docid + " could not "+
|
1310
|
" be found because " + sqle.getMessage());
|
1311
|
REVERRORNUMBER++;
|
1312
|
throw new HandlerException ("ReplicationHandler.handleDocInXMLRevisions - SQL exception getting rev list: "
|
1313
|
+ sqle.getMessage());
|
1314
|
}
|
1315
|
logReplication.info("ReplicationHandler.handleDocInXMLRevisions - rev list in xml_revision table for docid "+ docid + " is "+
|
1316
|
localrev.toString());
|
1317
|
|
1318
|
// if the rev is not in the xml_revision, we need insert it
|
1319
|
if (!localrev.contains(new Integer(rev)))
|
1320
|
{
|
1321
|
flag = true;
|
1322
|
}
|
1323
|
|
1324
|
String accNumber = null;
|
1325
|
try {
|
1326
|
accNumber = docid + PropertyService.getProperty("document.accNumSeparator") + rev;
|
1327
|
} catch (PropertyNotFoundException pnfe) {
|
1328
|
throw new HandlerException("ReplicationHandler.handleDocInXMLRevisions - error getting "
|
1329
|
+ "account number separator : " + pnfe.getMessage());
|
1330
|
}
|
1331
|
// this is non-data file
|
1332
|
if(flag && !dataFile)
|
1333
|
{
|
1334
|
try
|
1335
|
{
|
1336
|
|
1337
|
handleSingleXMLDocument(remoteServer, action, accNumber, DocumentImpl.REVISIONTABLE);
|
1338
|
}
|
1339
|
catch(HandlerException he)
|
1340
|
{
|
1341
|
// skip this document
|
1342
|
throw he;
|
1343
|
}
|
1344
|
}//if for non-data file
|
1345
|
|
1346
|
// this is for data file
|
1347
|
if(flag && dataFile)
|
1348
|
{
|
1349
|
try
|
1350
|
{
|
1351
|
handleSingleDataFile(remoteServer, action, accNumber, DocumentImpl.REVISIONTABLE);
|
1352
|
}
|
1353
|
catch(HandlerException he)
|
1354
|
{
|
1355
|
// skip this data file
|
1356
|
throw he;
|
1357
|
}
|
1358
|
|
1359
|
}//for data file
|
1360
|
}
|
1361
|
|
1362
|
/*
|
1363
|
* Return a ip address for given url
|
1364
|
*/
|
1365
|
private String getIpFromURL(URL url)
|
1366
|
{
|
1367
|
String ip = null;
|
1368
|
try
|
1369
|
{
|
1370
|
InetAddress address = InetAddress.getByName(url.getHost());
|
1371
|
ip = address.getHostAddress();
|
1372
|
}
|
1373
|
catch(UnknownHostException e)
|
1374
|
{
|
1375
|
logMetacat.error("ReplicationHandler.getIpFromURL - " + ReplicationService.METACAT_REPL_ERROR_MSG);
|
1376
|
logReplication.error("ReplicationHandler.getIpFromURL - Error in get ip address for host: "
|
1377
|
+e.getMessage());
|
1378
|
}
|
1379
|
|
1380
|
return ip;
|
1381
|
}
|
1382
|
|
1383
|
}
|
1384
|
|