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