Project

General

Profile

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: 2011-02-03 15:25:41 -0800 (Thu, 03 Feb 2011) $'
10
 * '$Revision: 5895 $'
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
      else if (docType != null && (docType.trim()).equals(DocumentImpl.EML2_1_1NAMESPACE))
386
      {
387
        parserBase = DocumentImpl.EML210;
388
      }
389
      // Write the document into local host
390
      DocumentImplWrapper wrapper = new DocumentImplWrapper(parserBase, false);
391
      String newDocid = wrapper.writeReplication(dbConn,
392
                              newxmldoc,
393
                              docinfoHash.get("public_access"),
394
                              null,  /* the dtd text */
395
                              actions,
396
                              accNumber,
397
                              docinfoHash.get("user_owner"),
398
                              null, /* null for groups[] */
399
                              docHomeServer,
400
                              remoteserver, tableName, true,// true is for time replication 
401
                              createdDate,
402
                              updatedDate);
403
      
404
      //process extra access rules 
405
      Vector<XMLAccessDAO> xmlAccessDAOList = dih.getAccessControlList();
406
      if (xmlAccessDAOList != null) {
407
      	AccessControlForSingleFile acfsf = new AccessControlForSingleFile(accNumber);
408
      	for (XMLAccessDAO xmlAccessDAO : xmlAccessDAOList) {
409
      		if (!acfsf.accessControlExists(xmlAccessDAO)) {
410
      			acfsf.insertPermissions(xmlAccessDAO);
411
      		}
412
          }
413
      }
414
      
415
      //process guid
416
      logReplication.debug("Processing guid information from docinfoHash: " + docinfoHash.toString());
417
      String guid = docinfoHash.get("guid");
418
      String docName = docinfoHash.get("docName");
419
      System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%guid passed from docinfo hash: " + guid);
420
      IdentifierManager idman = IdentifierManager.getInstance();
421
      if(guid != null && !idman.identifierExists(guid))
422
      { //if the guid was passed in, put it in the identifiers table
423
        logReplication.debug("Creating guid/docid mapping for docid " + 
424
          docinfoHash.get("docid") + " and guid: " + guid);
425
        System.out.println("docname: " + docName);
426
        if(docName.trim().equals("systemMetadata"))
427
        {
428
            System.out.println("creating mapping for systemMetadata: guid: " + guid + " localId: " + docinfoHash.get("docid"));
429
            idman.createSystemMetadataMapping(guid, docinfoHash.get("docid"));
430
            Long dateUploadedLong = new Long(docinfoHash.get("date_uploaded"));
431
            Long dateModifiedLong = new Long(docinfoHash.get("modified_date"));
432
            idman.insertAdditionalSystemMetadataFields(
433
                    dateUploadedLong.longValue(), 
434
                    docinfoHash.get("rights_holder"),
435
                    docinfoHash.get("checksum"), 
436
                    docinfoHash.get("checksum_algorithm"), 
437
                    docinfoHash.get("origin_member_node"),
438
                    docinfoHash.get("authoritative_member_node"), 
439
                    dateModifiedLong.longValue(),
440
                    docinfoHash.get("submitter"),
441
                    docinfoHash.get("guid"));
442
        }
443
        else
444
        {
445
            System.out.println("creating mapping: guid: " + guid + " localId: " + docinfoHash.get("docid"));
446
            idman.createMapping(guid, docinfoHash.get("docid"));
447
        }
448
      }
449
      else
450
      {
451
        logReplication.debug("No guid information was included with the replicated document");
452
      }
453
      
454
      if(guid != null)
455
      {
456
          if(!docName.trim().equals("systemMetadata"))
457
          {
458
              logReplication.info("replicate D1GUID:" + guid + ":D1SCIMETADATA:" + 
459
                      accNumber + ":");
460
          }
461
          else
462
          {
463
              logReplication.info("replicate D1GUID:" + guid + ":D1SYSMETADATA:" + 
464
                      accNumber + ":");
465
          }
466
      }
467
      
468
      logReplication.info("ReplicationHandler.handleSingleXMLDocument - Successfully replicated doc " + accNumber);
469
      if (tableName.equals(DocumentImpl.DOCUMENTTABLE))
470
      {
471
        logReplication.info("ReplicationHandler.handleSingleXMLDocument - " + DOCINSERTNUMBER + " Wrote xml doc " + accNumber +
472
                                     " into "+tableName + " from " +
473
                                         remoteserver);
474
        DOCINSERTNUMBER++;
475
      }
476
      else
477
      {
478
          logReplication.info("ReplicationHandler.handleSingleXMLDocument - " +REVINSERTNUMBER + " Wrote xml doc " + accNumber +
479
                  " into "+tableName + " from " +
480
                      remoteserver);
481
          REVINSERTNUMBER++;
482
      }
483
      String ip = getIpFromURL(u);
484
      EventLog.getInstance().log(ip, ReplicationService.REPLICATIONUSER, accNumber, actions);
485
      
486

    
487
    }//try
488
    catch(Exception e)
489
    {
490
        
491
        if (tableName.equals(DocumentImpl.DOCUMENTTABLE))
492
        {
493
        	logMetacat.error("ReplicationHandler.handleSingleXMLDocument - " + ReplicationService.METACAT_REPL_ERROR_MSG); 
494
        	logReplication.error("ReplicationHandler.handleSingleXMLDocument - " +DOCERRORNUMBER + " Failed to write xml doc " + accNumber +
495
                                       " into "+tableName + " from " +
496
                                           remoteserver + " because "+e.getMessage());
497
          DOCERRORNUMBER++;
498
        }
499
        else
500
        {
501
        	logMetacat.error("ReplicationHandler.handleSingleXMLDocument - " + ReplicationService.METACAT_REPL_ERROR_MSG); 
502
        	logReplication.error("ReplicationHandler.handleSingleXMLDocument - " +REVERRORNUMBER + " Failed to write xml doc " + accNumber +
503
                    " into "+tableName + " from " +
504
                        remoteserver +" because "+e.getMessage());
505
            REVERRORNUMBER++;
506
        }
507
        logMetacat.error("ReplicationHandler.handleSingleXMLDocument - " + ReplicationService.METACAT_REPL_ERROR_MSG); 
508
        logReplication.error("ReplicationHandler.handleSingleXMLDocument - Failed to write doc " + accNumber +
509
                                      " into db because " +e.getMessage());
510
      throw new HandlerException("ReplicationHandler.handleSingleXMLDocument - generic exception " 
511
    		  + "writing Replication: " +e.getMessage());
512
    }
513
    finally
514
    {
515
       //return DBConnection
516
       DBConnectionPool.returnDBConnection(dbConn, serialNumber);
517
    }//finally
518
    logD1.info("replication.create localId:" + accNumber);
519
  }
520

    
521

    
522

    
523
  /* Handle replicate single xml document*/
524
  private void handleSingleDataFile(String remoteserver, String actions,
525
                                    String accNumber, String tableName)
526
               throws HandlerException
527
  {
528
    logReplication.info("ReplicationHandler.handleSingleDataFile - Try to replicate data file: " + accNumber);
529
    DBConnection dbConn = null;
530
    int serialNumber = -1;
531
    try
532
    {
533
      // Get DBConnection from pool
534
      dbConn=DBConnectionPool.
535
                  getDBConnection("ReplicationHandler.handleSinlgeDataFile");
536
      serialNumber=dbConn.getCheckOutSerialNumber();
537
      // Try get docid info from remote server
538
      DocInfoHandler dih = new DocInfoHandler();
539
      XMLReader docinfoParser = initParser(dih);
540
      String docInfoURLString = "https://" + remoteserver +
541
                  "?server="+MetacatUtil.getLocalReplicationServerName()+
542
                  "&action=getdocumentinfo&docid="+accNumber;
543
      docInfoURLString = MetacatUtil.replaceWhiteSpaceForURL(docInfoURLString);
544
      URL docinfoUrl = new URL(docInfoURLString);
545

    
546
      String docInfoStr = ReplicationService.getURLContent(docinfoUrl);
547
      docinfoParser.parse(new InputSource(new StringReader(docInfoStr)));
548
      Hashtable<String, String> docinfoHash = dih.getDocInfo();
549
      // Get docid owner
550
      String user = docinfoHash.get("user_owner");
551
      // Get docid name (such as acl or dataset)
552
      String docName = docinfoHash.get("docname");
553
      // Get doc type (eml public id)
554
      String docType = docinfoHash.get("doctype");
555
      // Get docid home sever. it might be different to remoteserver
556
      // because of hub feature
557
      String docHomeServer = docinfoHash.get("home_server");
558
      String createdDate = docinfoHash.get("date_created");
559
      String updatedDate = docinfoHash.get("date_updated");
560
      //docid should include rev number too
561
      /*String accnum=docId+util.getProperty("document.accNumSeparator")+
562
                                              (String)docinfoHash.get("rev");*/
563

    
564

    
565
      String datafilePath = PropertyService.getProperty("application.datafilepath");
566
      // Get data file content
567
      String readDataURLString = "https://" + remoteserver + "?server="+
568
                                        MetacatUtil.getLocalReplicationServerName()+
569
                                            "&action=readdata&docid="+accNumber;
570
      readDataURLString = MetacatUtil.replaceWhiteSpaceForURL(readDataURLString);
571
      URL u = new URL(readDataURLString);
572
      InputStream input = u.openStream();
573
      //register data file into xml_documents table and wite data file
574
      //into file system
575
      if ( input != null)
576
      {
577
        DocumentImpl.writeDataFileInReplication(input,
578
                                                datafilePath,
579
                                                docName,docType,
580
                                                accNumber, user,
581
                                                docHomeServer,
582
                                                remoteserver,
583
                                                tableName,
584
                                                true, //true means timed replication
585
                                                createdDate,
586
                                                updatedDate);
587
                                         
588
        //process extra access rules
589
        Vector<XMLAccessDAO> xmlAccessDAOList = dih.getAccessControlList();
590
        if (xmlAccessDAOList != null) {
591
        	AccessControlForSingleFile acfsf = new AccessControlForSingleFile(accNumber);
592
        	for (XMLAccessDAO xmlAccessDAO : xmlAccessDAOList) {
593
        		if (!acfsf.accessControlExists(xmlAccessDAO)) {
594
        			acfsf.insertPermissions(xmlAccessDAO);
595
        		}
596
            }
597
        }
598
        
599
        logReplication.info("ReplicationHandler.handleSingleDataFile - Successfully to write datafile " + accNumber);
600
        /*MetacatReplication.replLog("wrote datafile " + accNumber + " from " +
601
                                    remote server);*/
602
        if (tableName.equals(DocumentImpl.DOCUMENTTABLE))
603
        {
604
          logReplication.info("ReplicationHandler.handleSingleDataFile - " + DOCINSERTNUMBER + " Wrote data file" + accNumber +
605
                                       " into "+tableName + " from " +
606
                                           remoteserver);
607
          DOCINSERTNUMBER++;
608
        }
609
        else
610
        {
611
            logReplication.info("ReplicationHandler.handleSingleDataFile - " + REVINSERTNUMBER + " Wrote data file" + accNumber +
612
                    " into "+tableName + " from " +
613
                        remoteserver);
614
            REVINSERTNUMBER++;
615
        }
616
        String ip = getIpFromURL(u);
617
        EventLog.getInstance().log(ip, ReplicationService.REPLICATIONUSER, accNumber, actions);
618
        
619
      }//if
620
      else
621
      {
622
         logReplication.info("ReplicationHandler.handleSingleDataFile - Couldn't open the data file: " + accNumber);
623
         throw new HandlerException("ReplicationHandler.handleSingleDataFile - Couldn't open the data file: " + accNumber);
624
      }//else
625

    
626
    }//try
627
    catch(Exception e)
628
    {
629
      /*MetacatReplication.replErrorLog("Failed to try wrote data file " + accNumber +
630
                                      " because " +e.getMessage());*/
631
      if (tableName.equals(DocumentImpl.DOCUMENTTABLE))
632
      {
633
    	logMetacat.error("ReplicationHandler.handleSingleDataFile - " + ReplicationService.METACAT_REPL_ERROR_MSG); 
634
    	logReplication.error("ReplicationHandler.handleSingleDataFile - " + DOCERRORNUMBER + " Failed to write data file " + accNumber +
635
                                     " into " + tableName + " from " +
636
                                         remoteserver + " because " + e.getMessage());
637
        DOCERRORNUMBER++;
638
      }
639
      else
640
      {
641
    	  logMetacat.error("ReplicationHandler.handleSingleDataFile - " + ReplicationService.METACAT_REPL_ERROR_MSG); 
642
    	  logReplication.error("ReplicationHandler.handleSingleDataFile - " + REVERRORNUMBER + " Failed to write data file" + accNumber +
643
                  " into " + tableName + " from " +
644
                      remoteserver +" because "+ e.getMessage());
645
          REVERRORNUMBER++;
646
      }
647
      logMetacat.error("ReplicationHandler.handleSingleDataFile - " + ReplicationService.METACAT_REPL_ERROR_MSG); 
648
      logReplication.error("ReplicationHandler.handleSingleDataFile - Failed to try wrote datafile " + accNumber +
649
                                      " because " + e.getMessage());
650
      throw new HandlerException("ReplicationHandler.handleSingleDataFile - generic exception " 
651
    		  + "writing Replication: " + e.getMessage());
652
    }
653
    finally
654
    {
655
       //return DBConnection
656
       DBConnectionPool.returnDBConnection(dbConn, serialNumber);
657
    }//finally
658
    logD1.info("replication.create localId:" + accNumber);
659
  }
660

    
661

    
662

    
663
  /* Handle delete single document*/
664
  private void handleDeleteSingleDocument(String docId, String notifyServer)
665
               throws HandlerException
666
  {
667
    logReplication.info("ReplicationHandler.handleDeleteSingleDocument - Try delete doc: "+docId);
668
    DBConnection dbConn = null;
669
    int serialNumber = -1;
670
    try
671
    {
672
      // Get DBConnection from pool
673
      dbConn=DBConnectionPool.
674
                  getDBConnection("ReplicationHandler.handleDeleteSingleDoc");
675
      serialNumber=dbConn.getCheckOutSerialNumber();
676
      if(!alreadyDeleted(docId))
677
      {
678

    
679
         //because delete method docid should have rev number
680
         //so we just add one for it. This rev number is no sence.
681
         String accnum=docId+PropertyService.getProperty("document.accNumSeparator")+"1";
682
         //System.out.println("accnum: "+accnum);
683
         DocumentImpl.delete(accnum, null, null, notifyServer);
684
         logReplication.info("ReplicationHandler.handleDeleteSingleDocument - Successfully deleted doc " + docId);
685
         logReplication.info("ReplicationHandler.handleDeleteSingleDocument - Doc " + docId + " deleted");
686
         URL u = new URL("https://"+notifyServer);
687
         String ip = getIpFromURL(u);
688
         EventLog.getInstance().log(ip, ReplicationService.REPLICATIONUSER, docId, "delete");
689
      }
690

    
691
    }//try
692
    catch(Exception e)
693
    {
694
      logMetacat.error("ReplicationHandler.handleDeleteSingleDocument - " + ReplicationService.METACAT_REPL_ERROR_MSG); 
695
      logReplication.error("ReplicationHandler.handleDeleteSingleDocument - Failed to delete doc " + docId +
696
                                 " in db because because " + e.getMessage());
697
      throw new HandlerException("ReplicationHandler.handleDeleteSingleDocument - generic exception " 
698
    		  + "when handling document: " + e.getMessage());
699
    }
700
    finally
701
    {
702
       //return DBConnection
703
       DBConnectionPool.returnDBConnection(dbConn, serialNumber);
704
    }//finally
705
    logD1.info("replication.handleDeleteSingleDocument localId:" + docId);
706
  }
707

    
708
  /* Handle updateLastCheckTimForSingleServer*/
709
  private void updateLastCheckTimeForSingleServer(ReplicationServer repServer)
710
                                                  throws HandlerException
711
  {
712
    String server = repServer.getServerName();
713
    DBConnection dbConn = null;
714
    int serialNumber = -1;
715
    PreparedStatement pstmt = null;
716
    try
717
    {
718
      // Get DBConnection from pool
719
      dbConn=DBConnectionPool.
720
             getDBConnection("ReplicationHandler.updateLastCheckTimeForServer");
721
      serialNumber=dbConn.getCheckOutSerialNumber();
722

    
723
      logReplication.info("ReplicationHandler.updateLastCheckTimeForSingleServer - Try to update last_check for server: "+server);
724
      // Get time from remote server
725
      URL dateurl = new URL("https://" + server + "?server="+
726
      MetacatUtil.getLocalReplicationServerName()+"&action=gettime");
727
      String datexml = ReplicationService.getURLContent(dateurl);
728
      logReplication.info("ReplicationHandler.updateLastCheckTimeForSingleServer - datexml: "+datexml);
729
      if (datexml!=null && !datexml.equals(""))
730
      {
731
         String datestr = datexml.substring(11, datexml.indexOf('<', 11));
732
         StringBuffer sql = new StringBuffer();
733
         /*sql.append("update xml_replication set last_checked = to_date('");
734
         sql.append(datestr).append("', 'YY-MM-DD HH24:MI:SS') where ");
735
         sql.append("server like '").append(server).append("'");*/
736
         sql.append("update xml_replication set last_checked = ");
737
         sql.append(DatabaseService.getInstance().getDBAdapter().toDate(datestr, "MM/DD/YY HH24:MI:SS"));
738
         sql.append(" where server like '").append(server).append("'");
739
         pstmt = dbConn.prepareStatement(sql.toString());
740

    
741
         pstmt.executeUpdate();
742
         dbConn.commit();
743
         pstmt.close();
744
         logReplication.info("ReplicationHandler.updateLastCheckTimeForSingleServer - last_checked updated to "+datestr+" on "
745
                                      + server);
746
      }//if
747
      else
748
      {
749

    
750
         logReplication.info("ReplicationHandler.updateLastCheckTimeForSingleServer - Failed to update last_checked for server "  +
751
                                  server + " in db because couldn't get time "
752
                                  );
753
         throw new Exception("Couldn't get time for server "+ server);
754
      }
755

    
756
    }//try
757
    catch(Exception e)
758
    {
759
      logMetacat.error("ReplicationHandler.updateLastCheckTimeForSingleServer - " + ReplicationService.METACAT_REPL_ERROR_MSG); 
760
      logReplication.error("ReplicationHandler.updateLastCheckTimeForSingleServer - Failed to update last_checked for server " +
761
                                server + " in db because because " + e.getMessage());
762
      throw new HandlerException("ReplicationHandler.updateLastCheckTimeForSingleServer - " 
763
    		  + "Error updating last checked time: " + e.getMessage());
764
    }
765
    finally
766
    {
767
       //return DBConnection
768
       DBConnectionPool.returnDBConnection(dbConn, serialNumber);
769
    }//finally
770
  }
771

    
772

    
773

    
774
  /**
775
   * updates xml_catalog with entries from other servers.
776
   */
777
  private void updateCatalog()
778
  {
779
    logReplication.info("ReplicationHandler.updateCatalog - Start of updateCatalog");
780
    // ReplicationServer object in server list
781
    ReplicationServer replServer = null;
782
    PreparedStatement pstmt = null;
783
    String server = null;
784

    
785

    
786
    // Go through each ReplicationServer object in sererlist
787
    for (int j=0; j<serverList.size(); j++)
788
    {
789
      Vector<Vector<String>> remoteCatalog = new Vector<Vector<String>>();
790
      Vector<String> publicId = new Vector<String>();
791
      try
792
      {
793
        // Get ReplicationServer object from server list
794
        replServer = serverList.serverAt(j);
795
        // Get server name from the ReplicationServer object
796
        server = replServer.getServerName();
797
        // Try to get catalog
798
        URL u = new URL("https://" + server + "?server="+
799
        MetacatUtil.getLocalReplicationServerName()+"&action=getcatalog");
800
        logReplication.info("ReplicationHandler.updateCatalog - sending message " + u.toString());
801
        String catxml = ReplicationService.getURLContent(u);
802

    
803
        // Make sure there are not error, no empty string
804
        if (catxml.indexOf("error")!=-1 || catxml==null||catxml.equals(""))
805
        {
806
          throw new Exception("Couldn't get catalog list form server " +server);
807
        }
808
        logReplication.debug("ReplicationHandler.updateCatalog - catxml: " + catxml);
809
        CatalogMessageHandler cmh = new CatalogMessageHandler();
810
        XMLReader catparser = initParser(cmh);
811
        catparser.parse(new InputSource(new StringReader(catxml)));
812
        //parse the returned catalog xml and put it into a vector
813
        remoteCatalog = cmh.getCatalogVect();
814

    
815
        // Make sure remoteCatalog is not empty
816
        if (remoteCatalog.isEmpty())
817
        {
818
          throw new Exception("Couldn't get catalog list form server " +server);
819
        }
820

    
821
        String localcatxml = ReplicationService.getCatalogXML();
822

    
823
        // Make sure local catalog is no empty
824
        if (localcatxml==null||localcatxml.equals(""))
825
        {
826
          throw new Exception("Couldn't get catalog list form server " +server);
827
        }
828

    
829
        cmh = new CatalogMessageHandler();
830
        catparser = initParser(cmh);
831
        catparser.parse(new InputSource(new StringReader(localcatxml)));
832
        Vector<Vector<String>> localCatalog = cmh.getCatalogVect();
833

    
834
        //now we have the catalog from the remote server and this local server
835
        //we now need to compare the two and merge the differences.
836
        //the comparison is base on the public_id fields which is the 4th
837
        //entry in each row vector.
838
        publicId = new Vector<String>();
839
        for(int i=0; i<localCatalog.size(); i++)
840
        {
841
          Vector<String> v = new Vector<String>(localCatalog.elementAt(i));
842
          logReplication.info("ReplicationHandler.updateCatalog - v1: " + v.toString());
843
          publicId.add(new String((String)v.elementAt(3)));
844
          //System.out.println("adding " + (String)v.elementAt(3));
845
        }
846
      }//try
847
      catch (Exception e)
848
      {
849
        logMetacat.error("ReplicationHandler.updateCatalog - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
850
        logReplication.error("ReplicationHandler.updateCatalog - Failed to update catalog for server "+
851
                                    server + " because " +e.getMessage());
852
      }//catch
853

    
854
      for(int i=0; i<remoteCatalog.size(); i++)
855
      {
856
         // DConnection
857
        DBConnection dbConn = null;
858
        // DBConnection checkout serial number
859
        int serialNumber = -1;
860
        try
861
        {
862
            dbConn=DBConnectionPool.
863
                  getDBConnection("ReplicationHandler.updateCatalog");
864
            serialNumber=dbConn.getCheckOutSerialNumber();
865
            Vector<String> v = remoteCatalog.elementAt(i);
866
            //System.out.println("v2: " + v.toString());
867
            //System.out.println("i: " + i);
868
            //System.out.println("remoteCatalog.size(): " + remoteCatalog.size());
869
            //System.out.println("publicID: " + publicId.toString());
870
            logReplication.info
871
                              ("ReplicationHandler.updateCatalog - v.elementAt(3): " + (String)v.elementAt(3));
872
           if(!publicId.contains(v.elementAt(3)))
873
           { //so we don't have this public id in our local table so we need to
874
             //add it.
875
             //System.out.println("in if");
876
             StringBuffer sql = new StringBuffer();
877
             sql.append("insert into xml_catalog (entry_type, source_doctype, ");
878
             sql.append("target_doctype, public_id, system_id) values (?,?,?,");
879
             sql.append("?,?)");
880
             //System.out.println("sql: " + sql.toString());
881
             pstmt = dbConn.prepareStatement(sql.toString());
882
             pstmt.setString(1, (String)v.elementAt(0));
883
             pstmt.setString(2, (String)v.elementAt(1));
884
             pstmt.setString(3, (String)v.elementAt(2));
885
             pstmt.setString(4, (String)v.elementAt(3));
886
             pstmt.setString(5, (String)v.elementAt(4));
887
             pstmt.execute();
888
             pstmt.close();
889
             logReplication.info("ReplicationHandler.updateCatalog - Success fully to insert new publicid "+
890
                               (String)v.elementAt(3) + " from server"+server);
891
           }
892
        }
893
        catch(Exception e)
894
        {
895
           logMetacat.error("ReplicationHandler.updateCatalog - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
896
           logReplication.error("ReplicationHandler.updateCatalog - Failed to update catalog for server "+
897
                                    server + " because " +e.getMessage());
898
        }//catch
899
        finally
900
        {
901
           DBConnectionPool.returnDBConnection(dbConn, serialNumber);
902
        }//finally
903
      }//for remote catalog
904
    }//for server list
905
    logReplication.info("End of updateCatalog");
906
  }
907

    
908
  /**
909
   * Method that returns true if docid has already been "deleted" from metacat.
910
   * This method really implements a truth table for deleted documents
911
   * The table is (a docid in one of the tables is represented by the X):
912
   * xml_docs      xml_revs      deleted?
913
   * ------------------------------------
914
   *   X             X             FALSE
915
   *   X             _             FALSE
916
   *   _             X             TRUE
917
   *   _             _             TRUE
918
   */
919
  private static boolean alreadyDeleted(String docid) throws HandlerException
920
  {
921
    DBConnection dbConn = null;
922
    int serialNumber = -1;
923
    PreparedStatement pstmt = null;
924
    try
925
    {
926
      dbConn=DBConnectionPool.
927
                  getDBConnection("ReplicationHandler.alreadyDeleted");
928
      serialNumber=dbConn.getCheckOutSerialNumber();
929
      boolean xml_docs = false;
930
      boolean xml_revs = false;
931

    
932
      StringBuffer sb = new StringBuffer();
933
      sb.append("select docid from xml_revisions where docid like '");
934
      sb.append(docid).append("'");
935
      pstmt = dbConn.prepareStatement(sb.toString());
936
      pstmt.execute();
937
      ResultSet rs = pstmt.getResultSet();
938
      boolean tablehasrows = rs.next();
939
      if(tablehasrows)
940
      {
941
        xml_revs = true;
942
      }
943

    
944
      sb = new StringBuffer();
945
      sb.append("select docid from xml_documents where docid like '");
946
      sb.append(docid).append("'");
947
      pstmt.close();
948
      pstmt = dbConn.prepareStatement(sb.toString());
949
      //increase usage count
950
      dbConn.increaseUsageCount(1);
951
      pstmt.execute();
952
      rs = pstmt.getResultSet();
953
      tablehasrows = rs.next();
954
      pstmt.close();
955
      if(tablehasrows)
956
      {
957
        xml_docs = true;
958
      }
959

    
960
      if(xml_docs && xml_revs)
961
      {
962
        return false;
963
      }
964
      else if(xml_docs && !xml_revs)
965
      {
966
        return false;
967
      }
968
      else if(!xml_docs && xml_revs)
969
      {
970
        return true;
971
      }
972
      else if(!xml_docs && !xml_revs)
973
      {
974
        return true;
975
      }
976
    }
977
    catch(Exception e)
978
    {
979
      logMetacat.error("ReplicationHandler.alreadyDeleted - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
980
      logReplication.error("ReplicationHandler.alreadyDeleted - general error in alreadyDeleted: " +
981
                          e.getMessage());
982
      throw new HandlerException("ReplicationHandler.alreadyDeleted - general error: " 
983
    		  + e.getMessage());
984
    }
985
    finally
986
    {
987
      try
988
      {
989
        pstmt.close();
990
      }//try
991
      catch (SQLException ee)
992
      {
993
    	logMetacat.error("ReplicationHandler.alreadyDeleted - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
994
        logReplication.error("ReplicationHandler.alreadyDeleted - Error in replicationHandler.alreadyDeleted "+
995
                          "to close pstmt: "+ee.getMessage());
996
        throw new HandlerException("ReplicationHandler.alreadyDeleted - SQL error when closing prepared statement: " 
997
      		  + ee.getMessage());
998
      }//catch
999
      finally
1000
      {
1001
        DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1002
      }//finally
1003
    }//finally
1004
    return false;
1005
  }
1006

    
1007

    
1008
  /**
1009
   * Method to initialize the message parser
1010
   */
1011
  public static XMLReader initParser(DefaultHandler dh)
1012
          throws HandlerException
1013
  {
1014
    XMLReader parser = null;
1015

    
1016
    try {
1017
      ContentHandler chandler = dh;
1018

    
1019
      // Get an instance of the parser
1020
      String parserName = PropertyService.getProperty("xml.saxparser");
1021
      parser = XMLReaderFactory.createXMLReader(parserName);
1022

    
1023
      // Turn off validation
1024
      parser.setFeature("http://xml.org/sax/features/validation", false);
1025

    
1026
      parser.setContentHandler((ContentHandler)chandler);
1027
      parser.setErrorHandler((ErrorHandler)chandler);
1028

    
1029
    } catch (SAXException se) {
1030
      throw new HandlerException("ReplicationHandler.initParser - Sax error when " 
1031
    		  + " initializing parser: " + se.getMessage());
1032
    } catch (PropertyNotFoundException pnfe) {
1033
        throw new HandlerException("ReplicationHandler.initParser - Property error when " 
1034
      		  + " getting parser name: " + pnfe.getMessage());
1035
    } 
1036

    
1037
    return parser;
1038
  }
1039

    
1040
  /**
1041
	 * This method will combine given time string(in short format) to current
1042
	 * date. If the given time (e.g 10:00 AM) passed the current time (e.g 2:00
1043
	 * PM Aug 21, 2005), then the time will set to second day, 10:00 AM Aug 22,
1044
	 * 2005. If the given time (e.g 10:00 AM) haven't passed the current time
1045
	 * (e.g 8:00 AM Aug 21, 2005) The time will set to be 10:00 AM Aug 21, 2005.
1046
	 * 
1047
	 * @param givenTime
1048
	 *            the format should be "10:00 AM " or "2:00 PM"
1049
	 * @return
1050
	 * @throws Exception
1051
	 */
1052
	public static Date combinateCurrentDateAndGivenTime(String givenTime) throws HandlerException
1053
  {
1054
	  try {
1055
     Date givenDate = parseTime(givenTime);
1056
     Date newDate = null;
1057
     Date now = new Date();
1058
     String currentTimeString = getTimeString(now);
1059
     Date currentTime = parseTime(currentTimeString); 
1060
     if ( currentTime.getTime() >= givenDate.getTime())
1061
     {
1062
        logReplication.info("ReplicationHandler.combinateCurrentDateAndGivenTime - Today already pass the given time, we should set it as tomorrow");
1063
        String dateAndTime = getDateString(now) + " " + givenTime;
1064
        Date combinationDate = parseDateTime(dateAndTime);
1065
        // new date should plus 24 hours to make is the second day
1066
        newDate = new Date(combinationDate.getTime()+24*3600*1000);
1067
     }
1068
     else
1069
     {
1070
         logReplication.info("ReplicationHandler.combinateCurrentDateAndGivenTime - Today haven't pass the given time, we should it as today");
1071
         String dateAndTime = getDateString(now) + " " + givenTime;
1072
         newDate = parseDateTime(dateAndTime);
1073
     }
1074
     logReplication.warn("ReplicationHandler.combinateCurrentDateAndGivenTime - final setting time is "+ newDate.toString());
1075
     return newDate;
1076
	  } catch (ParseException pe) {
1077
		  throw new HandlerException("ReplicationHandler.combinateCurrentDateAndGivenTime - "
1078
				  + "parsing error: "  + pe.getMessage());
1079
	  }
1080
  }
1081

    
1082
  /*
1083
	 * parse a given string to Time in short format. For example, given time is
1084
	 * 10:00 AM, the date will be return as Jan 1 1970, 10:00 AM
1085
	 */
1086
  private static Date parseTime(String timeString) throws ParseException
1087
  {
1088
    DateFormat format = DateFormat.getTimeInstance(DateFormat.SHORT);
1089
    Date time = format.parse(timeString); 
1090
    logReplication.info("ReplicationHandler.parseTime - Date string is after parse a time string "
1091
                              +time.toString());
1092
    return time;
1093

    
1094
  }
1095
  
1096
  /*
1097
   * Parse a given string to date and time. Date format is long and time
1098
   * format is short.
1099
   */
1100
  private static Date parseDateTime(String timeString) throws ParseException
1101
  {
1102
    DateFormat format = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.SHORT);
1103
    Date time = format.parse(timeString);
1104
    logReplication.info("ReplicationHandler.parseDateTime - Date string is after parse a time string "+
1105
                             time.toString());
1106
    return time;
1107
  }
1108
  
1109
  /*
1110
   * Get a date string from a Date object. The date format will be long
1111
   */
1112
  private static String getDateString(Date now)
1113
  {
1114
     DateFormat df = DateFormat.getDateInstance(DateFormat.LONG);
1115
     String s = df.format(now);
1116
     logReplication.info("ReplicationHandler.getDateString - Today is " + s);
1117
     return s;
1118
  }
1119
  
1120
  /*
1121
   * Get a time string from a Date object, the time format will be short
1122
   */
1123
  private static String getTimeString(Date now)
1124
  {
1125
     DateFormat df = DateFormat.getTimeInstance(DateFormat.SHORT);
1126
     String s = df.format(now);
1127
     logReplication.info("ReplicationHandler.getTimeString - Time is " + s);
1128
     return s;
1129
  }
1130
  
1131
  
1132
  /*
1133
	 * This method will go through the docid list both in xml_Documents table
1134
	 * and in xml_revisions table @author tao
1135
	 */
1136
	private void handleDocList(Vector<Vector<String>> docList, String tableName) {
1137
		boolean dataFile = false;
1138
		for (int j = 0; j < docList.size(); j++) {
1139
			// initial dataFile is false
1140
			dataFile = false;
1141
			// w is information for one document, information contain
1142
			// docid, rev, server or datafile.
1143
			Vector<String> w = new Vector<String>(docList.elementAt(j));
1144
			// Check if the vector w contain "datafile"
1145
			// If it has, this document is data file
1146
			try {
1147
				if (w.contains((String) PropertyService.getProperty("replication.datafileflag"))) {
1148
					dataFile = true;
1149
				}
1150
			} catch (PropertyNotFoundException pnfe) {
1151
				logMetacat.error("ReplicationHandler.handleDocList - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
1152
				logReplication.error("ReplicationHandler.handleDocList - Could not retrieve data file flag property.  "
1153
						+ "Leaving as false: " + pnfe.getMessage());
1154
			}
1155
			// System.out.println("w: " + w.toString());
1156
			// Get docid
1157
			String docid = (String) w.elementAt(0);
1158
			logReplication.info("docid: " + docid);
1159
			// Get revision number
1160
			int rev = Integer.parseInt((String) w.elementAt(1));
1161
			logReplication.info("rev: " + rev);
1162
			// Get remote server name (it is may not be doc home server because
1163
			// the new hub feature
1164
			String remoteServer = (String) w.elementAt(2);
1165
			remoteServer = remoteServer.trim();
1166

    
1167
			try {
1168
				if (tableName.equals(DocumentImpl.DOCUMENTTABLE)) {
1169
					handleDocInXMLDocuments(docid, rev, remoteServer, dataFile);
1170
				} else if (tableName.equals(DocumentImpl.REVISIONTABLE)) {
1171
					handleDocInXMLRevisions(docid, rev, remoteServer, dataFile);
1172
				} else {
1173
					continue;
1174
				}
1175

    
1176
			} catch (Exception e) {
1177
				logMetacat.error("ReplicationHandler.handleDocList - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
1178
				logReplication.error("ReplicationHandler.handleDocList - error to handle update doc in " + tableName
1179
						+ " in time replication" + e.getMessage());
1180
				continue;
1181
			}
1182
			
1183
	        if (_xmlDocQueryCount > 0 && (_xmlDocQueryCount % 100) == 0) {
1184
	        	logMetacat.debug("ReplicationHandler.update - xml_doc query count: " + _xmlDocQueryCount + 
1185
	        			", xml_doc avg query time: " + (_xmlDocQueryTime / _xmlDocQueryCount));
1186
	        }
1187
	        
1188
	        if (_xmlRevQueryCount > 0 && (_xmlRevQueryCount % 100) == 0) {
1189
	        	logMetacat.debug("ReplicationHandler.update - xml_rev query count: " + _xmlRevQueryCount + 
1190
	        			", xml_rev avg query time: " + (_xmlRevQueryTime / _xmlRevQueryCount));
1191
	        }
1192

    
1193
		}// for update docs
1194

    
1195
	}
1196
   
1197
   /*
1198
	 * This method will handle doc in xml_documents table.
1199
	 */
1200
   private void handleDocInXMLDocuments(String docid, int rev, String remoteServer, boolean dataFile) 
1201
                                        throws HandlerException
1202
   {
1203
       // compare the update rev and local rev to see what need happen
1204
       int localrev = -1;
1205
       String action = null;
1206
       boolean flag = false;
1207
       try
1208
       {
1209
    	 long docQueryStartTime = System.currentTimeMillis();
1210
         localrev = DBUtil.getLatestRevisionInDocumentTable(docid);
1211
         long docQueryEndTime = System.currentTimeMillis();
1212
         _xmlDocQueryTime += (docQueryEndTime - docQueryStartTime);
1213
         _xmlDocQueryCount++;
1214
       }
1215
       catch (SQLException e)
1216
       {
1217
    	 logMetacat.error("ReplicationHandler.handleDocInXMLDocuments - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
1218
         logReplication.error("ReplicationHandler.handleDocInXMLDocuments - Local rev for docid "+ docid + " could not "+
1219
                                " be found because " + e.getMessage());
1220
         logReplication.error("ReplicationHandler.handleDocInXMLDocuments - " + DOCERRORNUMBER+"Docid "+ docid + " could not be "+
1221
                 "written because error happend to find it's local revision");
1222
         DOCERRORNUMBER++;
1223
         throw new HandlerException ("ReplicationHandler.handleDocInXMLDocuments - Local rev for docid "+ docid + " could not "+
1224
                 " be found: " + e.getMessage());
1225
       }
1226
       logReplication.info("ReplicationHandler.handleDocInXMLDocuments - Local rev for docid "+ docid + " is "+
1227
                               localrev);
1228

    
1229
       //check the revs for an update because this document is in the
1230
       //local DB, it might be out of date.
1231
       if (localrev == -1)
1232
       {
1233
          // check if the revision is in the revision table
1234
    	   Vector<Integer> localRevVector = null;
1235
    	 try {
1236
        	 long revQueryStartTime = System.currentTimeMillis();
1237
    		 localRevVector = DBUtil.getRevListFromRevisionTable(docid);
1238
             long revQueryEndTime = System.currentTimeMillis();
1239
             _xmlRevQueryTime += (revQueryEndTime - revQueryStartTime);
1240
             _xmlRevQueryCount++;
1241
    	 } catch (SQLException sqle) {
1242
    		 throw new HandlerException("ReplicationHandler.handleDocInXMLDocuments - SQL error " 
1243
    				 + " when getting rev list for docid: " + docid + " : " + sqle.getMessage());
1244
    	 }
1245
         if (localRevVector != null && localRevVector.contains(new Integer(rev)))
1246
         {
1247
             // this version was deleted, so don't need replicate
1248
             flag = false;
1249
         }
1250
         else
1251
         {
1252
           //insert this document as new because it is not in the local DB
1253
           action = "INSERT";
1254
           flag = true;
1255
         }
1256
       }
1257
       else
1258
       {
1259
         if(localrev == rev)
1260
         {
1261
           // Local meatacat has the same rev to remote host, don't need
1262
           // update and flag set false
1263
           flag = false;
1264
         }
1265
         else if(localrev < rev)
1266
         {
1267
           //this document needs to be updated so send an read request
1268
           action = "UPDATE";
1269
           flag = true;
1270
         }
1271
       }
1272
       
1273
       String accNumber = null;
1274
       try {
1275
    	   accNumber = docid + PropertyService.getProperty("document.accNumSeparator") + rev;
1276
       } catch (PropertyNotFoundException pnfe) {
1277
    	   throw new HandlerException("ReplicationHandler.handleDocInXMLDocuments - error getting " 
1278
    			   + "account number separator : " + pnfe.getMessage());
1279
       }
1280
       // this is non-data file
1281
       if(flag && !dataFile)
1282
       {
1283
         try
1284
         {
1285
           handleSingleXMLDocument(remoteServer, action, accNumber, DocumentImpl.DOCUMENTTABLE);
1286
         }
1287
         catch(HandlerException he)
1288
         {
1289
           // skip this document
1290
           throw he;
1291
         }
1292
       }//if for non-data file
1293

    
1294
        // this is for data file
1295
       if(flag && dataFile)
1296
       {
1297
         try
1298
         {
1299
           handleSingleDataFile(remoteServer, action, accNumber, DocumentImpl.DOCUMENTTABLE);
1300
         }
1301
         catch(HandlerException he)
1302
         {
1303
           // skip this data file
1304
           throw he;
1305
         }
1306

    
1307
       }//for data file
1308
   }
1309
   
1310
   /*
1311
    * This method will handle doc in xml_documents table.
1312
    */
1313
   private void handleDocInXMLRevisions(String docid, int rev, String remoteServer, boolean dataFile) 
1314
                                        throws HandlerException
1315
   {
1316
       // compare the update rev and local rev to see what need happen
1317
       logReplication.info("ReplicationHandler.handleDocInXMLRevisions - In handle repliation revsion table");
1318
       logReplication.info("ReplicationHandler.handleDocInXMLRevisions - the docid is "+ docid);
1319
       logReplication.info("ReplicationHandler.handleDocInXMLRevisions - The rev is "+rev);
1320
       Vector<Integer> localrev = null;
1321
       String action = "INSERT";
1322
       boolean flag = false;
1323
       try
1324
       {
1325
      	 long revQueryStartTime = System.currentTimeMillis();
1326
         localrev = DBUtil.getRevListFromRevisionTable(docid);
1327
         long revQueryEndTime = System.currentTimeMillis();
1328
         _xmlRevQueryTime += (revQueryEndTime - revQueryStartTime);
1329
         _xmlRevQueryCount++;
1330
       }
1331
       catch (SQLException sqle)
1332
       {
1333
    	 logMetacat.error("ReplicationHandler.handleDocInXMLDocuments - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
1334
         logReplication.error("ReplicationHandler.handleDocInXMLRevisions - Local rev for docid "+ docid + " could not "+
1335
                                " be found because " + sqle.getMessage());
1336
         REVERRORNUMBER++;
1337
         throw new HandlerException ("ReplicationHandler.handleDocInXMLRevisions - SQL exception getting rev list: " 
1338
        		 + sqle.getMessage());
1339
       }
1340
       logReplication.info("ReplicationHandler.handleDocInXMLRevisions - rev list in xml_revision table for docid "+ docid + " is "+
1341
                               localrev.toString());
1342
       
1343
       // if the rev is not in the xml_revision, we need insert it
1344
       if (!localrev.contains(new Integer(rev)))
1345
       {
1346
           flag = true;    
1347
       }
1348
     
1349
       String accNumber = null;
1350
       try {
1351
    	   accNumber = docid + PropertyService.getProperty("document.accNumSeparator") + rev;
1352
       } catch (PropertyNotFoundException pnfe) {
1353
    	   throw new HandlerException("ReplicationHandler.handleDocInXMLRevisions - error getting " 
1354
    			   + "account number separator : " + pnfe.getMessage());
1355
       }
1356
       // this is non-data file
1357
       if(flag && !dataFile)
1358
       {
1359
         try
1360
         {
1361
           
1362
           handleSingleXMLDocument(remoteServer, action, accNumber, DocumentImpl.REVISIONTABLE);
1363
         }
1364
         catch(HandlerException he)
1365
         {
1366
           // skip this document
1367
           throw he;
1368
         }
1369
       }//if for non-data file
1370

    
1371
        // this is for data file
1372
       if(flag && dataFile)
1373
       {
1374
         try
1375
         {
1376
           handleSingleDataFile(remoteServer, action, accNumber, DocumentImpl.REVISIONTABLE);
1377
         }
1378
         catch(HandlerException he)
1379
         {
1380
           // skip this data file
1381
           throw he;
1382
         }
1383

    
1384
       }//for data file
1385
   }
1386
   
1387
   /*
1388
    * Return a ip address for given url
1389
    */
1390
   private String getIpFromURL(URL url)
1391
   {
1392
	   String ip = null;
1393
	   try
1394
	   {
1395
	      InetAddress address = InetAddress.getByName(url.getHost());
1396
	      ip = address.getHostAddress();
1397
	   }
1398
	   catch(UnknownHostException e)
1399
	   {
1400
		   logMetacat.error("ReplicationHandler.getIpFromURL - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
1401
		   logReplication.error("ReplicationHandler.getIpFromURL - Error in get ip address for host: "
1402
                   +e.getMessage());
1403
	   }
1404

    
1405
	   return ip;
1406
   }
1407
  
1408
}
1409

    
(3-3/7)