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-04 12:39:21 -0800 (Fri, 04 Feb 2011) $'
10
 * '$Revision: 5917 $'
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
                    docinfoHash.get("objectFormat"),
443
                    new Long(docinfoHash.get("size")).longValue());
444
        }
445
        else
446
        {
447
            System.out.println("creating mapping: guid: " + guid + " localId: " + docinfoHash.get("docid"));
448
            idman.createMapping(guid, docinfoHash.get("docid"));
449
        }
450
      }
451
      else
452
      {
453
        logReplication.debug("No guid information was included with the replicated document");
454
      }
455
      
456
      if(guid != null)
457
      {
458
          if(!docName.trim().equals("systemMetadata"))
459
          {
460
              logReplication.info("replicate D1GUID:" + guid + ":D1SCIMETADATA:" + 
461
                      accNumber + ":");
462
          }
463
          else
464
          {
465
              logReplication.info("replicate D1GUID:" + guid + ":D1SYSMETADATA:" + 
466
                      accNumber + ":");
467
          }
468
      }
469
      
470
      logReplication.info("ReplicationHandler.handleSingleXMLDocument - Successfully replicated doc " + accNumber);
471
      if (tableName.equals(DocumentImpl.DOCUMENTTABLE))
472
      {
473
        logReplication.info("ReplicationHandler.handleSingleXMLDocument - " + DOCINSERTNUMBER + " Wrote xml doc " + accNumber +
474
                                     " into "+tableName + " from " +
475
                                         remoteserver);
476
        DOCINSERTNUMBER++;
477
      }
478
      else
479
      {
480
          logReplication.info("ReplicationHandler.handleSingleXMLDocument - " +REVINSERTNUMBER + " Wrote xml doc " + accNumber +
481
                  " into "+tableName + " from " +
482
                      remoteserver);
483
          REVINSERTNUMBER++;
484
      }
485
      String ip = getIpFromURL(u);
486
      EventLog.getInstance().log(ip, ReplicationService.REPLICATIONUSER, accNumber, actions);
487
      
488

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

    
523

    
524

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

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

    
566

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

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

    
663

    
664

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

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

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

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

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

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

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

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

    
774

    
775

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

    
787

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

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

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

    
823
        String localcatxml = ReplicationService.getCatalogXML();
824

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

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

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

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

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

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

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

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

    
1009

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

    
1018
    try {
1019
      ContentHandler chandler = dh;
1020

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

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

    
1028
      parser.setContentHandler((ContentHandler)chandler);
1029
      parser.setErrorHandler((ErrorHandler)chandler);
1030

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

    
1039
    return parser;
1040
  }
1041

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

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

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

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

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

    
1195
		}// for update docs
1196

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

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

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

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

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

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

    
1407
	   return ip;
1408
   }
1409
  
1410
}
1411

    
(3-3/7)