Project

General

Profile

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