Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A Class that implements replication for metacat
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: tao $'
10
 *     '$Date: 2005-10-04 18:13:23 -0700 (Tue, 04 Oct 2005) $'
11
 * '$Revision: 2650 $'
12
 *
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
 */
27

    
28
package edu.ucsb.nceas.metacat;
29

    
30
import edu.ucsb.nceas.dbadapter.AbstractDatabase;
31
import java.util.*;
32
import java.util.Date;
33
import java.io.*;
34
import java.sql.*;
35
import java.net.*;
36
import java.lang.*;
37
import java.text.*;
38
import javax.servlet.*;
39
import javax.servlet.http.*;
40

    
41
import org.xml.sax.*;
42

    
43
public class MetacatReplication extends HttpServlet implements Runnable
44
{
45
  private long timeInterval;
46
  private Date firstTime;
47
  private boolean timedReplicationIsOn = false;
48
  Timer replicationDaemon;
49
  private static MetaCatUtil util = new MetaCatUtil();
50
  private Vector fileLocks = new Vector();
51
  private Thread lockThread = null;
52
  private static final AbstractDatabase dbAdapter = MetaCatUtil.dbAdapter;
53
  public static final String FORCEREPLICATEDELETE = "forcereplicatedelete";
54
  private static final String TIMEREPLICATION = "timedreplication";
55
  private static final String TIMEREPLICATIONINTERVAl = "timedreplicationinterval";
56
  private static final String FIRSTTIME  = "firsttimedreplication";
57
  private static final int    TIMEINTERVALLIMIT = 7200000;
58
  
59
  /**
60
   * Initialize the servlet by creating appropriate database connections
61
   */
62
  public void init(ServletConfig config) throws ServletException
63
  {
64
     //initialize db connections to handle any update requests
65
    MetaCatUtil util = new MetaCatUtil();
66
    //deltaT = util.getOption("deltaT");
67
    //the default deltaT can be set from metacat.properties
68
    //create a thread to do the delta-T check but don't execute it yet
69
    replicationDaemon = new Timer(true);
70
    try
71
    {
72
       timedReplicationIsOn = (new Boolean(util.getOption(TIMEREPLICATION ).trim())).booleanValue();
73
       MetaCatUtil.debugMessage("The timed replication on is"+timedReplicationIsOn, 30);
74
       timeInterval = (new Long(util.getOption(TIMEREPLICATIONINTERVAl).trim())).longValue();
75
       MetaCatUtil.debugMessage("The timed replication time Inerval is "+ timeInterval, 20);
76
       String firstTimeStr = util.getOption(FIRSTTIME);
77
       MetaCatUtil.debugMessage("first replication time form property is "+firstTimeStr, 20);
78
       firstTime = ReplicationHandler.combinateCurrentDateAndGivenTime(firstTimeStr);
79
       MetaCatUtil.debugMessage("After combine current time, the real first time is "
80
                                +firstTime.toString()+" minisec", 20);
81
       // set up time replication if it is on
82
       if (timedReplicationIsOn)
83
       {
84
           replicationDaemon.scheduleAtFixedRate(new ReplicationHandler(), firstTime, timeInterval);
85
           MetacatReplication.replLog("deltaT handler started with rate=" +
86
                   timeInterval + " mini seconds at " +firstTime.toString());
87
       }
88
    }
89
    catch (Exception e)
90
    {
91
        // the timed replication in Metacat.properties file has problem
92
        // so timed replication is setting to false;
93
        MetaCatUtil.debugMessage("Couldn't set up timed replication "+
94
                     " in Metacat replication servlet init because " +
95
                 e.getMessage(), 20);
96
        MetacatReplication.replErrorLog("Couldn't set up timed replication "+
97
                " in Metacat replication servlet init because " +
98
                e.getMessage());
99
        timedReplicationIsOn = false;
100
    }
101
    
102
  }
103

    
104
  public void destroy()
105
  {
106
    replicationDaemon.cancel();
107
   
108
  }
109

    
110
  public void doGet (HttpServletRequest request, HttpServletResponse response)
111
                     throws ServletException, IOException
112
  {
113
    // Process the data and send back the response
114
    handleGetOrPost(request, response);
115
  }
116

    
117
  public void doPost(HttpServletRequest request, HttpServletResponse response)
118
                     throws ServletException, IOException
119
  {
120
    // Process the data and send back the response
121
    handleGetOrPost(request, response);
122
  }
123

    
124
  private void handleGetOrPost(HttpServletRequest request,
125
                               HttpServletResponse response)
126
                               throws ServletException, IOException
127
  {
128
    //PrintWriter out = response.getWriter();
129
    //ServletOutputStream outPut = response.getOutputStream();
130
    Hashtable params = new Hashtable();
131
    Enumeration paramlist = request.getParameterNames();
132

    
133

    
134

    
135
// NOT NEEDED - doesn't provide enough security because of possible IP spoofing
136
// REPLACED with running replication comminications over HTTPS
137
//    String requestingServerIP = request.getRemoteAddr();
138
//    InetAddress iaddr = InetAddress.getByName(requestingServerIP);
139
//    String requestingServer = iaddr.getHostName();
140

    
141
    while (paramlist.hasMoreElements()) {
142
      String name = (String)paramlist.nextElement();
143
      String[] value = request.getParameterValues(name);
144
      params.put(name, value);
145
    }
146

    
147
    String action = ((String[])params.get("action"))[0];
148
    String server = null;
149

    
150
    try {
151
      // check if the server is included in the list of replicated servers
152
      if ( !action.equals("servercontrol") &&
153
           !action.equals("stop") &&
154
           !action.equals("start") &&
155
           !action.equals("getall") ) {
156

    
157
        server = ((String[])params.get("server"))[0];
158
        if ( getServerCodeForServerName(server) == 0 ) {
159
          System.out.println("Action \"" + action +
160
                             "\" rejected for server: " + server);
161
          return;
162
        } else {
163
          System.out.println("Action \"" + action +
164
                             "\" accepted for server: " + server);
165
        }
166
      }
167
      else
168
      {
169
          // start, stop, getall and servercontrol need to check
170
          // if user is administor
171
          HttpSession sess = request.getSession(true);
172
          String sess_id = "";
173
          String username = "";
174
          String[] groupnames = {""};
175
          Hashtable sessionHash = MetaCatServlet.getSessionHash();
176
          if (params.containsKey("sessionid")) 
177
          {
178
             sess_id = ((String[]) params.get("sessionid"))[0];
179
             MetaCatUtil.debugMessage("in has sessionid "+ sess_id, 40);
180
             if (sessionHash.containsKey(sess_id)) 
181
             {
182
                  MetaCatUtil.debugMessage("find the id " + sess_id + " in hash table", 40);
183
                  sess = (HttpSession) sessionHash.get(sess_id);
184
             }
185
           } 
186
           username = (String) sess.getAttribute("username");
187
           MetaCatUtil.debugMessage("The user name from session is: "+ username, 20);
188
           groupnames = (String[]) sess.getAttribute("groupnames");
189
           if (!MetaCatUtil.isAdministrator(username, groupnames)) 
190
           {
191
               PrintWriter out = response.getWriter();
192
               out.print("<error>");
193
               out.print("The user \"" + username +
194
                       "\" is not authorized for this action.");
195
               out.print("</error>");
196
               out.close();
197
               MetaCatUtil.debugMessage("The user \"" + username +
198
                       "\" is not authorized for this action: " +action, 30);
199
               replErrorLog("The user \"" + username +
200
                       "\" is not authorized for this action: " +action);
201
               return;
202
           }
203
                        
204
      }// this is final else
205
    } catch (Exception e) {
206
      System.out.println("Error in MetacatReplication.handleGetOrPost: " +
207
                         e.getMessage() );
208
      return;
209
    }
210
    
211
    if ( action.equals("readdata") )
212
    {
213
      OutputStream outStream = response.getOutputStream();
214
      //to get the data file.
215
      handleGetDataFileRequest(outStream, params, response);
216
      outStream.close();
217
    }
218
    else if ( action.equals("forcereplicatedatafile") )
219
    {
220
      //read a specific docid from remote host, and store it into local host
221
      handleForceReplicateDataFileRequest(params);
222

    
223
    }
224
    else
225
    {
226
    PrintWriter out = response.getWriter();
227
    if ( action.equals("stop") ) {
228
      //stop the replication server
229
      replicationDaemon.cancel();
230
      replicationDaemon = new Timer(true);
231
      timedReplicationIsOn = false;
232
      MetaCatUtil.setOption(TIMEREPLICATION, (new Boolean(timedReplicationIsOn)).toString());
233
      out.println("Replication Handler Stopped");
234
      MetacatReplication.replLog("deltaT handler stopped");
235

    
236

    
237
    } else if ( action.equals("start") ) {
238
       String firstTimeStr = "";
239
      //start the replication server
240
       if ( params.containsKey("rate") ) {
241
        timeInterval = new Long(
242
               new String(((String[])params.get("rate"))[0])).longValue();
243
        if(timeInterval < TIMEINTERVALLIMIT) {
244
            out.println("Replication deltaT rate cannot be less than "+
245
                    TIMEINTERVALLIMIT + " millisecs and system automatically setup the rate to "+TIMEINTERVALLIMIT);
246
            //deltaT<30 is a timing mess!
247
            timeInterval = TIMEINTERVALLIMIT;
248
        }
249
      } else {
250
        timeInterval = TIMEINTERVALLIMIT ;
251
      }
252
      MetaCatUtil.debugMessage("New rate is: " + timeInterval + " mini seconds.", 30);
253
      if ( params.containsKey("firsttime"))
254
      {
255
         firstTimeStr = ((String[])params.get("firsttime"))[0];
256
         try
257
         {
258
           firstTime = ReplicationHandler.combinateCurrentDateAndGivenTime(firstTimeStr);
259
           MetaCatUtil.debugMessage("The first time setting is "+firstTime.toString(), 30);
260
         }
261
         catch (Exception e)
262
         {
263
            throw new ServletException(e.getMessage());
264
         }
265
         MetaCatUtil.debugMessage("After combine current time, the real first time is "
266
                                  +firstTime.toString()+" minisec", 20);
267
      }
268
      else
269
      {
270
          MetacatReplication.replErrorLog("You should specify the first time " +
271
                                          "to start a time replication");
272
          MetaCatUtil.debugMessage("You should specify the first time " +
273
                                  "to start a time replication", 30);
274
          return;
275
      }
276
      
277
      timedReplicationIsOn = true;
278
      // save settings to property file
279
      MetaCatUtil.setOption(TIMEREPLICATION, (new Boolean(timedReplicationIsOn)).toString());
280
      // note we couldn't use firstTime object because it has date info
281
      // we only need time info such as 10:00 PM
282
      MetaCatUtil.setOption(FIRSTTIME, firstTimeStr);
283
      MetaCatUtil.setOption(TIMEREPLICATIONINTERVAl, (new Long(timeInterval)).toString());
284
      replicationDaemon.cancel();
285
      replicationDaemon = new Timer(true);
286
      replicationDaemon.scheduleAtFixedRate(new ReplicationHandler(), firstTime,
287
                                            timeInterval);
288
      out.println("Replication Handler Started");
289
      MetacatReplication.replLog("deltaT handler started with rate=" +
290
                                    timeInterval + " milliseconds at " +firstTime.toString());
291

    
292

    
293
    } else if ( action.equals("getall") ) {
294
      //updates this server exactly once
295
      replicationDaemon.schedule(new ReplicationHandler(), 0);
296
      response.setContentType("text/html");
297
      out.println("<html><body>\"Get All\" Done</body></html>");
298

    
299
    } else if ( action.equals("forcereplicate") ) {
300
      //read a specific docid from remote host, and store it into local host
301
      handleForceReplicateRequest(out, params, response);
302

    
303
    } else if ( action.equals(FORCEREPLICATEDELETE) ) {
304
      //read a specific docid from remote host, and store it into local host
305
      handleForceReplicateDeleteRequest(out, params, response);
306

    
307
    } else if ( action.equals("update") ) {
308
      //request an update list from the server
309
      handleUpdateRequest(out, params, response);
310

    
311
    } else if ( action.equals("read") ) {
312
      //request a specific document from the server
313
      //note that this could be replaced by a call to metacatServlet
314
      //handleGetDocumentAction().
315
      handleGetDocumentRequest(out, params, response);
316
    } else if ( action.equals("getlock") ) {
317
      handleGetLockRequest(out, params, response);
318

    
319
    } else if ( action.equals("getdocumentinfo") ) {
320
      handleGetDocumentInfoRequest(out, params, response);
321

    
322
    } else if ( action.equals("gettime") ) {
323
      handleGetTimeRequest(out, params, response);
324

    
325
    } else if ( action.equals("getcatalog") ) {
326
      handleGetCatalogRequest(out, params, response, true);
327

    
328
    } else if ( action.equals("servercontrol") ) {
329
      handleServerControlRequest(out, params, response);
330
    } else if ( action.equals("test") ) {
331
      response.setContentType("text/html");
332
      out.println("<html><body>Test successfully</body></html>");
333
    }
334

    
335
    out.close();
336
    }//else
337
  }
338

    
339
  /**
340
   * This method can add, delete and list the servers currently included in
341
   * xml_replication.
342
   * action           subaction            other needed params
343
   * ---------------------------------------------------------
344
   * servercontrol    add                  server
345
   * servercontrol    delete               server
346
   * servercontrol    list
347
   */
348
  private void handleServerControlRequest(PrintWriter out, Hashtable params,
349
                                          HttpServletResponse response)
350
  {
351
    String subaction = ((String[])params.get("subaction"))[0];
352
    DBConnection dbConn = null;
353
    int serialNumber = -1;
354
    PreparedStatement pstmt = null;
355
    String replicate =null;
356
    String server = null;
357
    String dataReplicate = null;
358
    String hub = null;
359
    try {
360
      //conn = util.openDBConnection();
361
      dbConn=DBConnectionPool.
362
               getDBConnection("MetacatReplication.handleServerControlRequest");
363
      serialNumber=dbConn.getCheckOutSerialNumber();
364

    
365
      // add server to server list
366
      if ( subaction.equals("add") ) {
367
        replicate = ((String[])params.get("replicate"))[0];
368
        server = ((String[])params.get("server"))[0];
369

    
370
        //Get data replication value
371
        dataReplicate = ((String[])params.get("datareplicate"))[0];
372
        //Get hub value
373
        hub = ((String[])params.get("hub"))[0];
374

    
375
        /*pstmt = dbConn.prepareStatement("INSERT INTO xml_replication " +
376
                  "(server, last_checked, replicate, datareplicate, hub) " +
377
                                      "VALUES ('" + server + "', to_date(" +
378
                                      "'01/01/00', 'MM/DD/YY'), '" +
379
                                      replicate +"', '" +dataReplicate+"', '"
380
                                      + hub +"')");*/
381
        pstmt = dbConn.prepareStatement("INSERT INTO xml_replication " +
382
                  "(server, last_checked, replicate, datareplicate, hub) " +
383
                                      "VALUES ('" + server + "', "+
384
                                      dbAdapter.toDate("01/01/1980", "MM/DD/YYYY")
385
                                      + ", '" +
386
                                      replicate +"', '" +dataReplicate+"', '"
387
                                      + hub +"')");
388

    
389
        pstmt.execute();
390
        pstmt.close();
391
        dbConn.commit();
392
        out.println("Server " + server + " added");
393
        response.setContentType("text/html");
394
        out.println("<html><body><table border=\"1\">");
395
        out.println("<tr><td><b>server</b></td><td><b>last_checked</b></td><td>");
396
        out.println("<b>replicate</b></td>");
397
        out.println("<td><b>datareplicate</b></td>");
398
        out.println("<td><b>hub</b></td></tr>");
399
        pstmt = dbConn.prepareStatement("SELECT * FROM xml_replication");
400
        //increase dbconnection usage
401
        dbConn.increaseUsageCount(1);
402

    
403
        pstmt.execute();
404
        ResultSet rs = pstmt.getResultSet();
405
        boolean tablehasrows = rs.next();
406
        while(tablehasrows) {
407
          out.println("<tr><td>" + rs.getString(2) + "</td><td>");
408
          out.println(rs.getString(3) + "</td><td>");
409
          out.println(rs.getString(4) + "</td><td>");
410
          out.println(rs.getString(5) + "</td><td>");
411
          out.println(rs.getString(6) + "</td></tr>");
412

    
413
          tablehasrows = rs.next();
414
        }
415
        out.println("</table></body></html>");
416

    
417
        // download certificate with the public key on this server
418
        // and import it as a trusted certificate
419
        String certURL = ((String[])params.get("certificate"))[0];
420
        downloadCertificate(certURL);
421

    
422
      // delete server from server list
423
      } else if ( subaction.equals("delete") ) {
424
        server = ((String[])params.get("server"))[0];
425
        pstmt = dbConn.prepareStatement("DELETE FROM xml_replication " +
426
                                      "WHERE server LIKE '" + server + "'");
427
        pstmt.execute();
428
        pstmt.close();
429
        dbConn.commit();
430
        out.println("Server " + server + " deleted");
431
        response.setContentType("text/html");
432
        out.println("<html><body><table border=\"1\">");
433
        out.println("<tr><td><b>server</b></td><td><b>last_checked</b></td><td>");
434
        out.println("<b>replicate</b></td>");
435
        out.println("<td><b>datareplicate</b></td>");
436
        out.println("<td><b>hub</b></td></tr>");
437

    
438
        pstmt = dbConn.prepareStatement("SELECT * FROM xml_replication");
439
        //increase dbconnection usage
440
        dbConn.increaseUsageCount(1);
441
        pstmt.execute();
442
        ResultSet rs = pstmt.getResultSet();
443
        boolean tablehasrows = rs.next();
444
        while(tablehasrows)
445
        {
446
          out.println("<tr><td>" + rs.getString(2) + "</td><td>");
447
          out.println(rs.getString(3) + "</td><td>");
448
          out.println(rs.getString(4) + "</td><td>");
449
          out.println(rs.getString(5) + "</td><td>");
450
          out.println(rs.getString(6) + "</td></tr>");
451
          tablehasrows = rs.next();
452
        }
453
        out.println("</table></body></html>");
454

    
455
      // list servers in server list
456
      } else if ( subaction.equals("list") ) {
457
        response.setContentType("text/html");
458
        out.println("<html><body><table border=\"1\">");
459
        out.println("<tr><td><b>server</b></td><td><b>last_checked</b></td><td>");
460
        out.println("<b>replicate</b></td>");
461
        out.println("<td><b>datareplicate</b></td>");
462
        out.println("<td><b>hub</b></td></tr>");
463
        pstmt = dbConn.prepareStatement("SELECT * FROM xml_replication");
464
        pstmt.execute();
465
        ResultSet rs = pstmt.getResultSet();
466
        boolean tablehasrows = rs.next();
467
        while(tablehasrows) {
468
          out.println("<tr><td>" + rs.getString(2) + "</td><td>");
469
          out.println(rs.getString(3) + "</td><td>");
470
          out.println(rs.getString(4) + "</td><td>");
471
          out.println(rs.getString(5) + "</td><td>");
472
          out.println(rs.getString(6) + "</td></tr>");
473
          tablehasrows = rs.next();
474
        }
475
        out.println("</table></body></html>");
476
      }
477
      else
478
      {
479

    
480
        out.println("<error>Unkonwn subaction</error>");
481

    
482
      }
483
      pstmt.close();
484
      //conn.close();
485

    
486
    } catch(Exception e) {
487
      System.out.println("Error in " +
488
                         "MetacatReplication.handleServerControlRequest " +
489
                         e.getMessage());
490
      e.printStackTrace(System.out);
491
    }
492
    finally
493
    {
494
      try
495
      {
496
        pstmt.close();
497
      }//try
498
      catch (SQLException ee)
499
      {
500
        MetaCatUtil.debugMessage("Error in " +
501
                "MetacatReplication.handleServerControlRequest to close pstmt "
502
                 + ee.getMessage(), 30);
503
      }//catch
504
      finally
505
      {
506
        DBConnectionPool.returnDBConnection(dbConn, serialNumber);
507
      }//finally
508
    }//finally
509

    
510
  }
511

    
512
  // download certificate with the public key from certURL and
513
  // upload it onto this server; it then must be imported as a
514
  // trusted certificate
515
  private void downloadCertificate (String certURL)
516
                throws FileNotFoundException, IOException, MalformedURLException
517
  {
518
    MetaCatUtil util = new MetaCatUtil();
519
    String certPath = util.getOption("certPath"); //the path to be uploaded to
520

    
521
    // get filename from the URL of the certificate
522
    String filename = certURL;
523
    int slash = Math.max(filename.lastIndexOf('/'), filename.lastIndexOf('\\'));
524
    if ( slash > -1 ) {
525
      filename = filename.substring(slash + 1);
526
    }
527

    
528
    // open file output strem to write the input into it
529
    File f = new File(certPath, filename);
530
    synchronized (f) {
531
      try {
532
        if ( f.exists() ) {
533
          throw new IOException("File already exist: " + f.getCanonicalFile());
534
          //if ( f.exists() && !f.canWrite() ) {
535
          //  throw new IOException("Not writable: " + f.getCanonicalFile());
536
        }
537
      } catch (SecurityException se) {
538
        // if a security manager exists,
539
        // its checkRead method is called for f.exist()
540
        // or checkWrite method is called for f.canWrite()
541
        throw se;
542
      }
543

    
544
      // create a buffered byte output stream
545
      // that uses a default-sized output buffer
546
      FileOutputStream fos = new FileOutputStream(f);
547
      BufferedOutputStream out = new BufferedOutputStream(fos);
548

    
549
      // this should be http url
550
      URL url = new URL(certURL);
551
      BufferedInputStream bis = null;
552
      try {
553
        bis = new BufferedInputStream(url.openStream());
554
        byte[] buf = new byte[4 * 1024]; // 4K buffer
555
        int b = bis.read(buf);
556
        while (b != -1) {
557
          out.write(buf, 0, b);
558
          b = bis.read(buf);
559
        }
560
      } finally {
561
        if (bis != null) bis.close();
562
      }
563
      // the input and the output streams must be closed
564
      bis.close();
565
            out.flush();
566
            out.close();
567
            fos.close();
568
    } // end of synchronized(f)
569
  }
570

    
571
  /**
572
   * when a forcereplication request comes in, local host sends a read request
573
   * to the requesting server (remote server) for the specified docid.
574
   * Then store it in local database.
575
   */
576
  private void handleForceReplicateRequest(PrintWriter out, Hashtable params,
577
                                           HttpServletResponse response)
578
  {
579
    String server = ((String[])params.get("server"))[0]; // the server that
580
    String docid = ((String[])params.get("docid"))[0]; // sent the document
581
    String dbaction = "UPDATE"; // the default action is UPDATE
582
    boolean override = false;
583
    int serverCode = 1;
584
    DBConnection dbConn = null;
585
    int serialNumber = -1;
586

    
587
    try {
588
      //if the url contains a dbaction then the default action is overridden
589
      if(params.containsKey("dbaction")) {
590
        dbaction = ((String[])params.get("dbaction"))[0];
591
        //serverCode = MetacatReplication.getServerCode(server);
592
        //override = true; //we are now overriding the default action
593
      }
594
      MetacatReplication.replLog("force replication request from " + server);
595
      MetaCatUtil.debugMessage("Force replication request from: "+ server, 34);
596
      MetaCatUtil.debugMessage("Force replication docid: "+docid, 34);
597
      MetaCatUtil.debugMessage("Force replication action: "+dbaction, 34);
598
      // sending back read request to remote server
599
      URL u = new URL("https://" + server + "?server="
600
                +util.getLocalReplicationServerName()
601
                +"&action=read&docid=" + docid);
602
      String xmldoc = MetacatReplication.getURLContent(u);
603

    
604
      // get the document info from server
605
      URL docinfourl = new URL("https://" + server +
606
                               "?server="+util.getLocalReplicationServerName()
607
                               +"&action=getdocumentinfo&docid=" + docid);
608

    
609
      String docInfoStr = MetacatReplication.getURLContent(docinfourl);
610

    
611
      //dih is the parser for the docinfo xml format
612
      DocInfoHandler dih = new DocInfoHandler();
613
      XMLReader docinfoParser = ReplicationHandler.initParser(dih);
614
      docinfoParser.parse(new InputSource(new StringReader(docInfoStr)));
615
      Hashtable docinfoHash = dih.getDocInfo();
616

    
617
      // Get user owner of this docid
618
      String user = (String)docinfoHash.get("user_owner");
619
      // Get home server of this docid
620
      String homeServer=(String)docinfoHash.get("home_server");
621
      String createdDate = (String)docinfoHash.get("date_created");
622
      String updatedDate = (String)docinfoHash.get("date_updated");
623
      MetaCatUtil.debugMessage("homeServer: "+homeServer, 34);
624
      // Get Document type
625
      String docType = (String)docinfoHash.get("doctype");
626
      MetaCatUtil.debugMessage("docType: "+docType, 34);
627
      String parserBase = null;
628
      // this for eml2 and we need user eml2 parser
629
      if (docType != null &&
630
          (docType.trim()).equals(DocumentImpl.EML2_0_0NAMESPACE))
631
      {
632
         MetaCatUtil.debugMessage("This is an eml200 document!", 30);
633
         parserBase = DocumentImpl.EML200;
634
      }
635
      else if (docType != null && (docType.trim()).equals(DocumentImpl.EML2_0_1NAMESPACE))
636
      {
637
         MetaCatUtil.debugMessage("This is an eml2.0.1 document!", 30);
638
         parserBase = DocumentImpl.EML200;
639
      }
640
      else if (docType != null && (docType.trim()).equals(DocumentImpl.EML2_1_0NAMESPACE))
641
      {
642
         MetaCatUtil.debugMessage("This is an eml2.1.0 document!", 30);
643
         parserBase = DocumentImpl.EML210;
644
      }
645
      MetaCatUtil.debugMessage("The parserBase is: "+parserBase, 30);
646

    
647
      // Get DBConnection from pool
648
      dbConn=DBConnectionPool.
649
              getDBConnection("MetacatReplication.handleForceReplicateRequest");
650
      serialNumber=dbConn.getCheckOutSerialNumber();
651
      // write the document to local database
652
      DocumentImplWrapper wrapper = new DocumentImplWrapper(parserBase, false);
653
      wrapper.writeReplication(dbConn, new StringReader(xmldoc), null, null,
654
                               dbaction, docid, user, null, homeServer, 
655
                               server, createdDate, updatedDate);
656

    
657
      MetacatReplication.replLog("document " + docid + " added to DB with " +
658
                                 "action " + dbaction);
659
    }//try
660
    catch(Exception e)
661
    {
662
      MetacatReplication.replErrorLog("document " + docid +
663
                                      " failed to added to DB with " +
664
                                      "action " + dbaction + " because "+
665
                                       e.getMessage());
666
      MetaCatUtil.debugMessage("ERROR in MetacatReplication.handleForceReplicate" +
667
                         "Request(): " + e.getMessage(), 30);
668

    
669
    }//catch
670
    finally
671
    {
672
      // Return the checked out DBConnection
673
      DBConnectionPool.returnDBConnection(dbConn, serialNumber);
674
    }//finally
675
  }
676

    
677
/*
678
 * when a forcereplication delete request comes in, local host will delete this
679
 * document
680
 */
681
private void handleForceReplicateDeleteRequest(PrintWriter out, Hashtable params,
682
                                         HttpServletResponse response)
683
{
684
  String server = ((String[])params.get("server"))[0]; // the server that
685
  String docid = ((String[])params.get("docid"))[0]; // sent the document
686
  try
687
  {
688
    MetacatReplication.replLog("force replication delete request from " + server);
689
    MetacatReplication.replLog("force replication delete docid " + docid);
690
    MetaCatUtil.debugMessage("Force replication delete request from: "+ server, 34);
691
    MetaCatUtil.debugMessage("Force replication delete docid: "+docid, 34);
692
    DocumentImpl.delete(docid, null, null, server);
693
    MetacatReplication.replLog("document " + docid + " was successfully deleted ");
694
    MetaCatUtil.debugMessage("document " + docid + " was successfully deleted ", 34);
695
  }
696
  catch(Exception e)
697
  {
698
    MetacatReplication.replErrorLog("document " + docid +
699
                                    " failed to delete because "+
700
                                     e.getMessage());
701
    MetaCatUtil.debugMessage("ERROR in MetacatReplication.handleForceDeleteReplicate" +
702
                       "Request(): " + e.getMessage(), 30);
703

    
704
  }//catch
705

    
706
}
707

    
708

    
709
  /**
710
   * when a forcereplication data file request comes in, local host sends a
711
   * readdata request to the requesting server (remote server) for the specified
712
   * docid. Then store it in local database and file system
713
   */
714
  private void handleForceReplicateDataFileRequest(Hashtable params)
715
  {
716

    
717
    //make sure there is some parameters
718
    if(params.isEmpty())
719
    {
720
      return;
721
    }
722
    // Get remote server
723
    String server = ((String[])params.get("server"))[0];
724
    // the docid should include rev number
725
    String docid = ((String[])params.get("docid"))[0];
726
    // Make sure there is a docid and server
727
    if (docid==null || server==null || server.equals(""))
728
    {
729
      MetaCatUtil.debugMessage("Didn't specify docid or server for replication"
730
                              , 20);
731
      return;
732
    }
733

    
734
    // Overide or not
735
    boolean override = false;
736
    // dbaction - update or insert
737
    String dbaction=null;
738

    
739
    try
740
    {
741
      //docid was switch to two parts uinque code and rev
742
      //String uniqueCode=MetaCatUtil.getDocIdFromString(docid);
743
      //int rev=MetaCatUtil.getVersionFromString(docid);
744
      if(params.containsKey("dbaction"))
745
      {
746
        dbaction = ((String[])params.get("dbaction"))[0];
747
      }
748
      else//default value is update
749
      {
750
        dbaction = "update";
751
      }
752

    
753
      MetacatReplication.replLog("force replication request from " + server);
754
      MetaCatUtil.debugMessage("Force replication request from: "+ server, 34);
755
      MetaCatUtil.debugMessage("Force replication docid: "+docid, 34);
756
      MetaCatUtil.debugMessage("Force replication action: "+dbaction, 34);
757
      // get the document info from server
758
      URL docinfourl = new URL("https://" + server +
759
                               "?server="+util.getLocalReplicationServerName()
760
                               +"&action=getdocumentinfo&docid=" + docid);
761

    
762
      String docInfoStr = MetacatReplication.getURLContent(docinfourl);
763

    
764
      //dih is the parser for the docinfo xml format
765
      DocInfoHandler dih = new DocInfoHandler();
766
      XMLReader docinfoParser = ReplicationHandler.initParser(dih);
767
      docinfoParser.parse(new InputSource(new StringReader(docInfoStr)));
768
      Hashtable docinfoHash = dih.getDocInfo();
769
      String user = (String)docinfoHash.get("user_owner");
770

    
771
      String docName = (String)docinfoHash.get("docname");
772

    
773
      String docType = (String)docinfoHash.get("doctype");
774

    
775
      String docHomeServer= (String)docinfoHash.get("home_server");
776
      
777
      String createdDate = (String)docinfoHash.get("date_created");
778
      
779
      String updatedDate = (String)docinfoHash.get("date_updated");
780
      MetaCatUtil.debugMessage("docHomeServer of datafile: "+docHomeServer, 34);
781

    
782

    
783

    
784
      //if action is delete, we don't delete the data file. Just archieve
785
      //the xml_documents
786
      /*if (dbaction.equals("delete"))
787
      {
788
        //conn = util.getConnection();
789
        DocumentImpl.delete(docid,user,null);
790
        //util.returnConnection(conn);
791
      }*/
792
      //To data file insert or update is same
793
      if (dbaction.equals("insert")||dbaction.equals("update"))
794
      {
795
        //Get data file and store it into local file system.
796
        // sending back readdata request to server
797
        URL url = new URL("https://" + server + "?server="
798
                +util.getLocalReplicationServerName()
799
                +"&action=readdata&docid=" + docid);
800
        String datafilePath = util.getOption("datafilepath");
801
        //register data file into xml_documents table and wite data file
802
        //into file system
803
        DocumentImpl.writeDataFileInReplication(url.openStream(), datafilePath,
804
                            docName, docType, docid, user,docHomeServer,server, 
805
                            DocumentImpl.DOCUMENTTABLE, false, createdDate, updatedDate);
806
                            //false means non-timed replication
807
     }
808

    
809

    
810

    
811
    MetacatReplication.replLog("datafile " + docid + " added to DB with " +
812
                                 "action " + dbaction);
813
    }
814
    catch(Exception e)
815
    {
816

    
817
      MetacatReplication.replErrorLog("Datafile " + docid +
818
                                      " failed to added to DB with " +
819
                                      "action " + dbaction + " because "+
820
                                       e.getMessage());
821
      MetaCatUtil.debugMessage
822
              ("ERROR in MetacatReplication.handleForceDataFileReplicate" +
823
                         "Request(): " + e.getMessage(), 30);
824
    }
825
  }
826
  /**
827
   * Grants or denies a lock to a requesting host.
828
   * The servlet parameters of interrest are:
829
   * docid: the docid of the file the lock is being requested for
830
   * currentdate: the timestamp of the document on the remote server
831
   *
832
   */
833
  private void handleGetLockRequest(PrintWriter out, Hashtable params,
834
                                    HttpServletResponse response)
835
  {
836

    
837
    try
838
    {
839

    
840
      String docid = ((String[])params.get("docid"))[0];
841
      String remoteRev = ((String[])params.get("updaterev"))[0];
842
      DocumentImpl requestDoc = new DocumentImpl(docid);
843
      MetacatReplication.replLog("lock request for " + docid);
844
      int localRevInt = requestDoc.getRev();
845
      int remoteRevInt = Integer.parseInt(remoteRev);
846

    
847
      if(remoteRevInt >= localRevInt)
848
      {
849
        if(!fileLocks.contains(docid))
850
        { //grant the lock if it is not already locked
851
          fileLocks.add(0, docid); //insert at the beginning of the queue Vector
852
          //send a message back to the the remote host authorizing the insert
853
          out.println("<lockgranted><docid>" +docid+ "</docid></lockgranted>");
854
          lockThread = new Thread(this);
855
          lockThread.setPriority(Thread.MIN_PRIORITY);
856
          lockThread.start();
857
          MetacatReplication.replLog("lock granted for " + docid);
858
        }
859
        else
860
        { //deny the lock
861
          out.println("<filelocked><docid>" + docid + "</docid></filelocked>");
862
          MetacatReplication.replLog("lock denied for " + docid +
863
                                     "reason: file already locked");
864
        }
865
      }
866
      else
867
      {//deny the lock.
868
        out.println("<outdatedfile><docid>" + docid + "</docid></filelocked>");
869
        MetacatReplication.replLog("lock denied for " + docid +
870
                                   "reason: client has outdated file");
871
      }
872
      //conn.close();
873
    }
874
    catch(Exception e)
875
    {
876
      System.out.println("error requesting file lock from MetacatReplication." +
877
                         "handleGetLockRequest: " + e.getMessage());
878
      e.printStackTrace(System.out);
879
    }
880
  }
881

    
882
  /**
883
   * Sends all of the xml_documents information encoded in xml to a requestor
884
   * the format is:
885
   * <!ELEMENT documentinfo (docid, docname, doctype, doctitle, user_owner,
886
   *                  user_updated, home_server, public_access, rev)/>
887
   * all of the subelements of document info are #PCDATA
888
   */
889
  private void handleGetDocumentInfoRequest(PrintWriter out, Hashtable params,
890
                                        HttpServletResponse response)
891
  {
892
    String docid = ((String[])(params.get("docid")))[0];
893
    StringBuffer sb = new StringBuffer();
894

    
895
    try
896
    {
897

    
898
      DocumentImpl doc = new DocumentImpl(docid);
899
      sb.append("<documentinfo><docid>").append(docid);
900
      sb.append("</docid><docname>").append(doc.getDocname());
901
      sb.append("</docname><doctype>").append(doc.getDoctype());
902
      sb.append("</doctype>");
903
      sb.append("<user_owner>").append(doc.getUserowner());
904
      sb.append("</user_owner><user_updated>").append(doc.getUserupdated());
905
      sb.append("</user_updated>");
906
      sb.append("<date_created>");
907
      sb.append(doc.getCreateDate());
908
      sb.append("</date_created>");
909
      sb.append("<date_updated>");
910
      sb.append(doc.getUpdateDate());
911
      sb.append("</date_updated>");
912
      sb.append("<home_server>");
913
      sb.append(doc.getDocHomeServer());
914
      sb.append("</home_server>");
915
      sb.append("<public_access>").append(doc.getPublicaccess());
916
      sb.append("</public_access><rev>").append(doc.getRev());
917
      sb.append("</rev></documentinfo>");
918
      response.setContentType("text/xml");
919
      out.println(sb.toString());
920

    
921
    }
922
    catch (Exception e)
923
    {
924
      System.out.println("error in " +
925
                         "metacatReplication.handlegetdocumentinforequest: " +
926
                          e.getMessage());
927
    }
928

    
929
  }
930

    
931
  /**
932
   * Sends a datafile to a remote host
933
   */
934
  private void handleGetDataFileRequest(OutputStream outPut,
935
                            Hashtable params, HttpServletResponse response)
936

    
937
  {
938
    // File path for data file
939
    String filepath = util.getOption("datafilepath");
940
    // Request docid
941
    String docId = ((String[])(params.get("docid")))[0];
942
    //check if the doicd is null
943
    if (docId==null)
944
    {
945
      util.debugMessage("Didn't specify docid for replication", 20);
946
      return;
947
    }
948

    
949
    //try to open a https stream to test if the request server's public key
950
    //in the key store, this is security issue
951
    try
952
    {
953
      String server = ((String[])params.get("server"))[0];
954
      URL u = new URL("https://" + server + "?server="
955
                +util.getLocalReplicationServerName()
956
                +"&action=test");
957
      String test = MetacatReplication.getURLContent(u);
958
      //couldn't pass the test
959
      if (test.indexOf("successfully")==-1)
960
      {
961
        //response.setContentType("text/xml");
962
        //outPut.println("<error>Couldn't pass the trust test</error>");
963
        MetaCatUtil.debugMessage("Couldn't pass the trust test", 20);
964
        return;
965
      }
966
    }//try
967
    catch (Exception ee)
968
    {
969
      return;
970
    }//catch
971

    
972
    if(!filepath.endsWith("/"))
973
    {
974
          filepath += "/";
975
    }
976
    // Get file aboslute file name
977
    String filename = filepath + docId;
978

    
979
    //MIME type
980
    String contentType = null;
981
    if (filename.endsWith(".xml"))
982
    {
983
        contentType="text/xml";
984
    }
985
    else if (filename.endsWith(".css"))
986
    {
987
        contentType="text/css";
988
    }
989
    else if (filename.endsWith(".dtd"))
990
    {
991
        contentType="text/plain";
992
    }
993
    else if (filename.endsWith(".xsd"))
994
    {
995
        contentType="text/xml";
996
    }
997
    else if (filename.endsWith("/"))
998
    {
999
        contentType="text/html";
1000
    }
1001
    else
1002
    {
1003
        File f = new File(filename);
1004
        if ( f.isDirectory() )
1005
        {
1006
           contentType="text/html";
1007
        }
1008
        else
1009
        {
1010
           contentType="application/octet-stream";
1011
        }
1012
     }
1013

    
1014
   // Set the mime type
1015
   response.setContentType(contentType);
1016

    
1017
   // Get the content of the file
1018
   FileInputStream fin = null;
1019
   try
1020
   {
1021
      // FileInputStream to metacat
1022
      fin = new FileInputStream(filename);
1023
      // 4K buffer
1024
      byte[] buf = new byte[4 * 1024];
1025
      // Read data from file input stream to byte array
1026
      int b = fin.read(buf);
1027
      // Write to outStream from byte array
1028
      while (b != -1)
1029
      {
1030
        outPut.write(buf, 0, b);
1031
        b = fin.read(buf);
1032
      }
1033
      // close file input stream
1034
      fin.close();
1035

    
1036
   }//try
1037
   catch(Exception e)
1038
   {
1039
      System.out.println("error getting data file from MetacatReplication." +
1040
                         "handlGetDataFileRequest " + e.getMessage());
1041
      e.printStackTrace(System.out);
1042
   }//catch
1043

    
1044
}
1045

    
1046

    
1047
  /**
1048
   * Sends a document to a remote host
1049
   */
1050
  private void handleGetDocumentRequest(PrintWriter out, Hashtable params,
1051
                                        HttpServletResponse response)
1052
  {
1053

    
1054
    try
1055
    {
1056
      //try to open a https stream to test if the request server's public key
1057
      //in the key store, this is security issue
1058
      String server = ((String[])params.get("server"))[0];
1059
      URL u = new URL("https://" + server + "?server="
1060
                +util.getLocalReplicationServerName()
1061
                +"&action=test");
1062
      String test = MetacatReplication.getURLContent(u);
1063
      //couldn't pass the test
1064
      if (test.indexOf("successfully")==-1)
1065
      {
1066
        response.setContentType("text/xml");
1067
        out.println("<error>Couldn't pass the trust test</error>");
1068
        out.close();
1069
        return;
1070
      }
1071

    
1072
      String docid = ((String[])(params.get("docid")))[0];
1073

    
1074
      DocumentImpl di = new DocumentImpl(docid);
1075
      response.setContentType("text/xml");
1076
      out.print(di.toString(null, null, true));
1077

    
1078
      MetacatReplication.replLog("document " + docid + " sent");
1079

    
1080
    }
1081
    catch(Exception e)
1082
    {
1083
      MetaCatUtil.debugMessage("error getting document from MetacatReplication."
1084
                          +"handlGetDocumentRequest " + e.getMessage(), 30);
1085
      //e.printStackTrace(System.out);
1086
      response.setContentType("text/xml");
1087
      out.println("<error>"+e.getMessage()+"</error>");
1088
    }
1089

    
1090
  }
1091

    
1092
  /**
1093
   * Sends a list of all of the documents on this sever along with their
1094
   * revision numbers.
1095
   * The format is:
1096
   * <!ELEMENT replication (server, updates)>
1097
   * <!ELEMENT server (#PCDATA)>
1098
   * <!ELEMENT updates ((updatedDocument | deleteDocument | revisionDocument)*)>
1099
   * <!ELEMENT updatedDocument (docid, rev, datafile*)>
1100
   * <!ELEMENT deletedDocument (docid, rev)>
1101
   * <!ELEMENT revisionDocument (docid, rev, datafile*)>
1102
   * <!ELEMENT docid (#PCDATA)>
1103
   * <!ELEMENT rev (#PCDATA)>
1104
   * <!ELEMENT datafile (#PCDATA)>
1105
   * note that the rev in deletedDocument is always empty.  I just left
1106
   * it in there to make the parser implementation easier.
1107
   */
1108
  private void handleUpdateRequest(PrintWriter out, Hashtable params,
1109
                                    HttpServletResponse response)
1110
  {
1111
    // Checked out DBConnection
1112
    DBConnection dbConn = null;
1113
    // DBConenction serial number when checked it out
1114
    int serialNumber = -1;
1115
    PreparedStatement pstmt = null;
1116
    // Server list to store server info of xml_replication table
1117
    ReplicationServerList serverList = null;
1118

    
1119
    try
1120
    {
1121
      // Check out a DBConnection from pool
1122
      dbConn=DBConnectionPool.
1123
                  getDBConnection("MetacatReplication.handleUpdateRequest");
1124
      serialNumber=dbConn.getCheckOutSerialNumber();
1125
      // Create a server list from xml_replication table
1126
      serverList = new ReplicationServerList();
1127

    
1128
      // Get remote server name from param
1129
      String server = ((String[])params.get("server"))[0];
1130
      // If no servr name in param, return a error
1131
      if ( server == null || server.equals(""))
1132
      {
1133
        response.setContentType("text/xml");
1134
        out.println("<error>Request didn't specify server name</error>");
1135
        out.close();
1136
        return;
1137
      }//if
1138

    
1139
      //try to open a https stream to test if the request server's public key
1140
      //in the key store, this is security issue
1141
      URL u = new URL("https://" + server + "?server="
1142
                +util.getLocalReplicationServerName()
1143
                +"&action=test");
1144
      String test = MetacatReplication.getURLContent(u);
1145
      //couldn't pass the test
1146
      if (test.indexOf("successfully")==-1)
1147
      {
1148
        response.setContentType("text/xml");
1149
        out.println("<error>Couldn't pass the trust test</error>");
1150
        out.close();
1151
        return;
1152
      }
1153

    
1154

    
1155
      // Check if local host configure to replicate xml documents to remote
1156
      // server. If not send back a error message
1157
      if (!serverList.getReplicationValue(server))
1158
      {
1159
        response.setContentType("text/xml");
1160
        out.println
1161
        ("<error>Configuration not allow to replicate document to you</error>");
1162
        out.close();
1163
        return;
1164
      }//if
1165

    
1166
      // Store the sql command
1167
      StringBuffer docsql = new StringBuffer();
1168
      StringBuffer revisionSql = new StringBuffer();
1169
      // Stroe the docid list
1170
      StringBuffer doclist = new StringBuffer();
1171
      // Store the deleted docid list
1172
      StringBuffer delsql = new StringBuffer();
1173
      // Store the data set file
1174
      Vector packageFiles = new Vector();
1175

    
1176
      // Append local server's name and replication servlet to doclist
1177
      doclist.append("<?xml version=\"1.0\"?><replication>");
1178
      doclist.append("<server>").append(util.getLocalReplicationServerName());
1179
      //doclist.append(util.getOption("replicationpath"));
1180
      doclist.append("</server><updates>");
1181

    
1182
      // Get correct docid that reside on this server according the requesting
1183
      // server's replicate and data replicate value in xml_replication table
1184
      docsql.append("select docid, rev, doctype from xml_documents ");
1185
      revisionSql.append("select docid, rev, doctype from xml_revisions ");
1186
      // If the localhost is not a hub to the remote server, only replicate
1187
      // the docid' which home server is local host (server_location =1)
1188
      if (!serverList.getHubValue(server))
1189
      {
1190
        String serverLocation = "where server_location = 1";
1191
        docsql.append(serverLocation);
1192
        revisionSql.append(serverLocation);
1193
      }
1194
      MetaCatUtil.debugMessage("Doc sql: "+docsql.toString(), 30);
1195

    
1196
      // Get any deleted documents
1197
      delsql.append("select distinct docid from ");
1198
      delsql.append("xml_revisions where docid not in (select docid from ");
1199
      delsql.append("xml_documents) ");
1200
      // If the localhost is not a hub to the remote server, only replicate
1201
      // the docid' which home server is local host (server_location =1)
1202
      if (!serverList.getHubValue(server))
1203
      {
1204
        delsql.append("and server_location = 1");
1205
      }
1206
      MetaCatUtil.debugMessage("Deleted sql: "+delsql.toString(), 30);
1207

    
1208

    
1209

    
1210
      // Get docid list of local host
1211
      pstmt = dbConn.prepareStatement(docsql.toString());
1212
      pstmt.execute();
1213
      ResultSet rs = pstmt.getResultSet();
1214
      boolean tablehasrows = rs.next();
1215
      //If metacat configed to replicate data file
1216
      //if ((util.getOption("replicationsenddata")).equals("on"))
1217
      boolean replicateData = serverList.getDataReplicationValue(server);
1218
      if (replicateData)
1219
      {
1220
        while(tablehasrows)
1221
        {
1222
          String recordDoctype = rs.getString(3);
1223
          Vector packagedoctypes = MetaCatUtil.getOptionList(
1224
                                     MetaCatUtil.getOption("packagedoctype"));
1225
          //if this is a package file, put it at the end
1226
          //because if a package file is read before all of the files it
1227
          //refers to are loaded then there is an error
1228
          if(recordDoctype != null && !packagedoctypes.contains(recordDoctype))
1229
          {
1230
              //If this is not data file
1231
              if (!recordDoctype.equals("BIN"))
1232
              {
1233
                //for non-data file document
1234
                doclist.append("<updatedDocument>");
1235
                doclist.append("<docid>").append(rs.getString(1));
1236
                doclist.append("</docid><rev>").append(rs.getInt(2));
1237
                doclist.append("</rev>");
1238
                doclist.append("</updatedDocument>");
1239
              }//if
1240
              else
1241
              {
1242
                //for data file document, in datafile attributes
1243
                //we put "datafile" value there
1244
                doclist.append("<updatedDocument>");
1245
                doclist.append("<docid>").append(rs.getString(1));
1246
                doclist.append("</docid><rev>").append(rs.getInt(2));
1247
                doclist.append("</rev>");
1248
                doclist.append("<datafile>");
1249
                doclist.append(MetaCatUtil.getOption("datafileflag"));
1250
                doclist.append("</datafile>");
1251
                doclist.append("</updatedDocument>");
1252
              }//else
1253
          }//if packagedoctpes
1254
          else
1255
          { //the package files are saved to be put into the xml later.
1256
              Vector v = new Vector();
1257
              v.add(new String(rs.getString(1)));
1258
              v.add(new Integer(rs.getInt(2)));
1259
              packageFiles.add(new Vector(v));
1260
          }//esle
1261
          tablehasrows = rs.next();
1262
        }//while
1263
      }//if
1264
      else //metacat was configured not to send data file
1265
      {
1266
        while(tablehasrows)
1267
        {
1268
          String recordDoctype = rs.getString(3);
1269
          if(!recordDoctype.equals("BIN"))
1270
          { //don't replicate data files
1271
            Vector packagedoctypes = MetaCatUtil.getOptionList(
1272
                                     MetaCatUtil.getOption("packagedoctype"));
1273
            if(recordDoctype != null && !packagedoctypes.contains(recordDoctype))
1274
            {   //if this is a package file, put it at the end
1275
              //because if a package file is read before all of the files it
1276
              //refers to are loaded then there is an error
1277
              doclist.append("<updatedDocument>");
1278
              doclist.append("<docid>").append(rs.getString(1));
1279
              doclist.append("</docid><rev>").append(rs.getInt(2));
1280
              doclist.append("</rev>");
1281
              doclist.append("</updatedDocument>");
1282
            }
1283
            else
1284
            { //the package files are saved to be put into the xml later.
1285
              Vector v = new Vector();
1286
              v.add(new String(rs.getString(1)));
1287
              v.add(new Integer(rs.getInt(2)));
1288
              packageFiles.add(new Vector(v));
1289
            }
1290
         }//if
1291
         tablehasrows = rs.next();
1292
        }//while
1293
      }//else
1294

    
1295
      pstmt = dbConn.prepareStatement(delsql.toString());
1296
      //usage count should increas 1
1297
      dbConn.increaseUsageCount(1);
1298

    
1299
      pstmt.execute();
1300
      rs = pstmt.getResultSet();
1301
      tablehasrows = rs.next();
1302
      while(tablehasrows)
1303
      { //handle the deleted documents
1304
        doclist.append("<deletedDocument><docid>").append(rs.getString(1));
1305
        doclist.append("</docid><rev></rev></deletedDocument>");
1306
        //note that rev is always empty for deleted docs
1307
        tablehasrows = rs.next();
1308
      }
1309

    
1310
      //now we can put the package files into the xml results
1311
      for(int i=0; i<packageFiles.size(); i++)
1312
      {
1313
        Vector v = (Vector)packageFiles.elementAt(i);
1314
        doclist.append("<updatedDocument>");
1315
        doclist.append("<docid>").append((String)v.elementAt(0));
1316
        doclist.append("</docid><rev>");
1317
        doclist.append(((Integer)v.elementAt(1)).intValue());
1318
        doclist.append("</rev>");
1319
        doclist.append("</updatedDocument>");
1320
      }
1321
      // add revision doc list  
1322
      doclist.append(prepareRevisionDoc(dbConn,revisionSql.toString(),replicateData));
1323
        
1324
      doclist.append("</updates></replication>");
1325
      MetaCatUtil.debugMessage("doclist: " + doclist.toString(), 40);
1326
      pstmt.close();
1327
      //conn.close();
1328
      response.setContentType("text/xml");
1329
      out.println(doclist.toString());
1330

    
1331
    }
1332
    catch(Exception e)
1333
    {
1334
      MetaCatUtil.debugMessage("error in MetacatReplication." +
1335
                         "handleupdaterequest: " + e.getMessage(), 30);
1336
      //e.printStackTrace(System.out);
1337
      response.setContentType("text/xml");
1338
      out.println("<error>"+e.getMessage()+"</error>");
1339
    }
1340
    finally
1341
    {
1342
      try
1343
      {
1344
        pstmt.close();
1345
      }//try
1346
      catch (SQLException ee)
1347
      {
1348
        MetaCatUtil.debugMessage("Error in MetacatReplication." +
1349
                "handleUpdaterequest to close pstmt: "+ee.getMessage(), 30);
1350
      }//catch
1351
      finally
1352
      {
1353
        DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1354
      }//finally
1355
    }//finally
1356

    
1357
  }//handlUpdateRequest
1358
  
1359
  /*
1360
   * This method will get the xml string for document in xml_revision
1361
   * The schema look like <!ELEMENT revisionDocument (docid, rev, datafile*)>
1362
   */
1363
  private String prepareRevisionDoc(DBConnection dbConn, String revSql, 
1364
                            boolean replicateData) throws Exception
1365
  {
1366
      MetaCatUtil.debugMessage("The revision document sql is "+ revSql, 20);
1367
      StringBuffer revDocList = new StringBuffer();
1368
      PreparedStatement pstmt = dbConn.prepareStatement(revSql);
1369
      //usage count should increas 1
1370
      dbConn.increaseUsageCount(1);
1371

    
1372
      pstmt.execute();
1373
      ResultSet rs = pstmt.getResultSet();
1374
      boolean tablehasrows = rs.next();
1375
      while(tablehasrows)
1376
      {
1377
        String recordDoctype = rs.getString(3);
1378
        
1379
        //If this is data file and it isn't configured to replicate data
1380
        if (recordDoctype.equals("BIN") && !replicateData)
1381
        {  
1382
            // do nothing
1383
            continue;
1384
        }
1385
        else
1386
        {  
1387
            
1388
            revDocList.append("<revisionDocument>");
1389
            revDocList.append("<docid>").append(rs.getString(1));
1390
            revDocList.append("</docid><rev>").append(rs.getInt(2));
1391
            revDocList.append("</rev>");
1392
            // data file
1393
            if (recordDoctype.equals("BIN"))
1394
            {
1395
                revDocList.append("<datafile>");
1396
                revDocList.append(MetaCatUtil.getOption("datafileflag"));
1397
                revDocList.append("</datafile>");
1398
            }
1399
            revDocList.append("</revisionDocument>");
1400
        
1401
         }//else
1402
         tablehasrows = rs.next();
1403
      }
1404
      //System.out.println("The revision list is"+ revDocList.toString());
1405
      return revDocList.toString();
1406
  }
1407

    
1408
  /**
1409
   * Returns the xml_catalog table encoded in xml
1410
   */
1411
  public static String getCatalogXML()
1412
  {
1413
    return handleGetCatalogRequest(null, null, null, false);
1414
  }
1415

    
1416
  /**
1417
   * Sends the contents of the xml_catalog table encoded in xml
1418
   * The xml format is:
1419
   * <!ELEMENT xml_catalog (row*)>
1420
   * <!ELEMENT row (entry_type, source_doctype, target_doctype, public_id,
1421
   *                system_id)>
1422
   * All of the sub elements of row are #PCDATA
1423

    
1424
   * If printFlag == false then do not print to out.
1425
   */
1426
  private static String handleGetCatalogRequest(PrintWriter out,
1427
                                                Hashtable params,
1428
                                                HttpServletResponse response,
1429
                                                boolean printFlag)
1430
  {
1431
    DBConnection dbConn = null;
1432
    int serialNumber = -1;
1433
    PreparedStatement pstmt = null;
1434
    try
1435
    {
1436
      /*conn = MetacatReplication.getDBConnection("MetacatReplication." +
1437
                                                "handleGetCatalogRequest");*/
1438
      dbConn=DBConnectionPool.
1439
                 getDBConnection("MetacatReplication.handleGetCatalogRequest");
1440
      serialNumber=dbConn.getCheckOutSerialNumber();
1441
      pstmt = dbConn.prepareStatement("select entry_type, " +
1442
                              "source_doctype, target_doctype, public_id, " +
1443
                              "system_id from xml_catalog");
1444
      pstmt.execute();
1445
      ResultSet rs = pstmt.getResultSet();
1446
      boolean tablehasrows = rs.next();
1447
      StringBuffer sb = new StringBuffer();
1448
      sb.append("<?xml version=\"1.0\"?><xml_catalog>");
1449
      while(tablehasrows)
1450
      {
1451
        sb.append("<row><entry_type>").append(rs.getString(1));
1452
        sb.append("</entry_type><source_doctype>").append(rs.getString(2));
1453
        sb.append("</source_doctype><target_doctype>").append(rs.getString(3));
1454
        sb.append("</target_doctype><public_id>").append(rs.getString(4));
1455
        sb.append("</public_id><system_id>").append(rs.getString(5));
1456
        sb.append("</system_id></row>");
1457

    
1458
        tablehasrows = rs.next();
1459
      }
1460
      sb.append("</xml_catalog>");
1461
      //conn.close();
1462
      if(printFlag)
1463
      {
1464
        response.setContentType("text/xml");
1465
        out.println(sb.toString());
1466
      }
1467
      pstmt.close();
1468
      return sb.toString();
1469
    }
1470
    catch(Exception e)
1471
    {
1472

    
1473
      MetaCatUtil.debugMessage("error in MetacatReplication.handleGetCatalogRequest:"+
1474
                          e.getMessage(), 30);
1475
      e.printStackTrace(System.out);
1476
      if(printFlag)
1477
      {
1478
        out.println("<error>"+e.getMessage()+"</error>");
1479
      }
1480
    }
1481
    finally
1482
    {
1483
      try
1484
      {
1485
        pstmt.close();
1486
      }//try
1487
      catch (SQLException ee)
1488
      {
1489
        MetaCatUtil.
1490
           debugMessage("Error in MetacatReplication.handleGetCatalogRequest: "
1491
           +ee.getMessage(), 30);
1492
      }//catch
1493
      finally
1494
      {
1495
        DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1496
      }//finally
1497
    }//finally
1498

    
1499
    return null;
1500
  }
1501

    
1502
  /**
1503
   * Sends the current system date to the remote server.  Using this action
1504
   * for replication gets rid of any problems with syncronizing clocks
1505
   * because a time specific to a document is always kept on its home server.
1506
   */
1507
  private void handleGetTimeRequest(PrintWriter out, Hashtable params,
1508
                                    HttpServletResponse response)
1509
  {
1510
    SimpleDateFormat formatter = new SimpleDateFormat ("MM/dd/yy HH:mm:ss");
1511
    java.util.Date localtime = new java.util.Date();
1512
    String dateString = formatter.format(localtime);
1513
    response.setContentType("text/xml");
1514

    
1515
    out.println("<timestamp>" + dateString + "</timestamp>");
1516
  }
1517

    
1518
  /**
1519
   * this method handles the timeout for a file lock.  when a lock is
1520
   * granted it is granted for 30 seconds.  When this thread runs out
1521
   * it deletes the docid from the queue, thus eliminating the lock.
1522
   */
1523
  public void run()
1524
  {
1525
    try
1526
    {
1527
      MetaCatUtil.debugMessage("thread started for docid: " +
1528
                               (String)fileLocks.elementAt(0), 45);
1529

    
1530
      Thread.sleep(30000); //the lock will expire in 30 seconds
1531
      MetaCatUtil.debugMessage("thread for docid: " +
1532
                             (String)fileLocks.elementAt(fileLocks.size() - 1) +
1533
                              " exiting.", 45);
1534

    
1535
      fileLocks.remove(fileLocks.size() - 1);
1536
      //fileLocks is treated as a FIFO queue.  If there are more than one lock
1537
      //in the vector, the first one inserted will be removed.
1538
    }
1539
    catch(Exception e)
1540
    {
1541
      MetaCatUtil.debugMessage("error in file lock thread from " +
1542
                                "MetacatReplication.run: " + e.getMessage(), 30);
1543
    }
1544
  }
1545

    
1546
  /**
1547
   * Returns the name of a server given a serverCode
1548
   * @param serverCode the serverid of the server
1549
   * @return the servername or null if the specified serverCode does not
1550
   *         exist.
1551
   */
1552
  public static String getServerNameForServerCode(int serverCode)
1553
  {
1554
    //System.out.println("serverid: " + serverCode);
1555
    DBConnection dbConn = null;
1556
    int serialNumber = -1;
1557
    PreparedStatement pstmt = null;
1558
    try
1559
    {
1560
      dbConn=DBConnectionPool.
1561
                  getDBConnection("MetacatReplication.getServer");
1562
      serialNumber=dbConn.getCheckOutSerialNumber();
1563
      String sql = new String("select server from " +
1564
                              "xml_replication where serverid = " +
1565
                              serverCode);
1566
      pstmt = dbConn.prepareStatement(sql);
1567
      //System.out.println("getserver sql: " + sql);
1568
      pstmt.execute();
1569
      ResultSet rs = pstmt.getResultSet();
1570
      boolean tablehasrows = rs.next();
1571
      if(tablehasrows)
1572
      {
1573
        //System.out.println("server: " + rs.getString(1));
1574
        return rs.getString(1);
1575
      }
1576

    
1577
      //conn.close();
1578
    }
1579
    catch(Exception e)
1580
    {
1581
      System.out.println("Error in MetacatReplication.getServer: " +
1582
                          e.getMessage());
1583
    }
1584
    finally
1585
    {
1586
      try
1587
      {
1588
        pstmt.close();
1589
      }//try
1590
      catch (SQLException ee)
1591
      {
1592
        MetaCatUtil.debugMessage("Error in MetacactReplication.getserver: "+
1593
                                    ee.getMessage(), 30);
1594
      }//catch
1595
      finally
1596
      {
1597
        DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1598
      }//fianlly
1599
    }//finally
1600

    
1601

    
1602

    
1603
    return null;
1604
      //return null if the server does not exist
1605
  }
1606

    
1607
  /**
1608
   * Returns a server code given a server name
1609
   * @param server the name of the server
1610
   * @return integer > 0 representing the code of the server, 0 if the server
1611
   *  does not exist.
1612
   */
1613
  public static int getServerCodeForServerName(String server) throws Exception
1614
  {
1615
    DBConnection dbConn = null;
1616
    int serialNumber = -1;
1617
    PreparedStatement pstmt = null;
1618
    int serverCode = 0;
1619

    
1620
    try {
1621

    
1622
      //conn = util.openDBConnection();
1623
      dbConn=DBConnectionPool.
1624
                  getDBConnection("MetacatReplication.getServerCode");
1625
      serialNumber=dbConn.getCheckOutSerialNumber();
1626
      pstmt = dbConn.prepareStatement("SELECT serverid FROM xml_replication " +
1627
                                    "WHERE server LIKE '" + server + "'");
1628
      pstmt.execute();
1629
      ResultSet rs = pstmt.getResultSet();
1630
      boolean tablehasrows = rs.next();
1631
      if ( tablehasrows ) {
1632
        serverCode = rs.getInt(1);
1633
        pstmt.close();
1634
        //conn.close();
1635
        return serverCode;
1636
      }
1637

    
1638
    } catch(Exception e) {
1639
      throw e;
1640

    
1641
    } finally {
1642
      try
1643
      {
1644
        pstmt.close();
1645
        //conn.close();
1646
       }//try
1647
       catch(Exception ee)
1648
       {
1649
         MetaCatUtil.debugMessage("Error in MetacatReplicatio.getServerCode: "
1650
                                  +ee.getMessage(), 30);
1651

    
1652
       }//catch
1653
       finally
1654
       {
1655
         DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1656
       }//finally
1657
    }//finally
1658

    
1659
    return serverCode;
1660
  }
1661

    
1662
  /**
1663
   * Method to get a host server information for given docid
1664
   * @param conn a connection to the database
1665
   */
1666
  public static Hashtable getHomeServerInfoForDocId(String docId)
1667
  {
1668
    Hashtable sl = new Hashtable();
1669
    DBConnection dbConn = null;
1670
    int serialNumber = -1;
1671
    //MetaCatUtil ut=new MetaCatUtil();
1672
    docId=MetaCatUtil.getDocIdFromString(docId);
1673
    PreparedStatement pstmt=null;
1674
    int serverLocation;
1675
    try
1676
    {
1677
      //get conection
1678
      dbConn=DBConnectionPool.
1679
                  getDBConnection("ReplicationHandler.getHomeServer");
1680
      serialNumber=dbConn.getCheckOutSerialNumber();
1681
      //get a server location from xml_document table
1682
      pstmt=dbConn.prepareStatement("select server_location from xml_documents "
1683
                                            +"where docid = ?");
1684
      pstmt.setString(1, docId);
1685
      pstmt.execute();
1686
      ResultSet serverName = pstmt.getResultSet();
1687
      //get a server location
1688
      if(serverName.next())
1689
      {
1690
        serverLocation=serverName.getInt(1);
1691
        pstmt.close();
1692
      }
1693
      else
1694
      {
1695
        pstmt.close();
1696
        //ut.returnConnection(conn);
1697
        return null;
1698
      }
1699
      pstmt=dbConn.prepareStatement("select server, last_checked, replicate " +
1700
                        "from xml_replication where serverid = ?");
1701
      //increase usage count
1702
      dbConn.increaseUsageCount(1);
1703
      pstmt.setInt(1, serverLocation);
1704
      pstmt.execute();
1705
      ResultSet rs = pstmt.getResultSet();
1706
      boolean tableHasRows = rs.next();
1707
      if (tableHasRows)
1708
      {
1709

    
1710
          String server = rs.getString(1);
1711
          String last_checked = rs.getString(2);
1712
          if(!server.equals("localhost"))
1713
          {
1714
            sl.put(server, last_checked);
1715
          }
1716

    
1717
      }
1718
      else
1719
      {
1720
        pstmt.close();
1721
        //ut.returnConnection(conn);
1722
        return null;
1723
      }
1724
      pstmt.close();
1725
    }
1726
    catch(Exception e)
1727
    {
1728
      System.out.println("error in replicationHandler.getHomeServer(): " +
1729
                         e.getMessage());
1730
    }
1731
    finally
1732
    {
1733
      try
1734
      {
1735
        pstmt.close();
1736
        //ut.returnConnection(conn);
1737
      }
1738
      catch (Exception ee)
1739
      {
1740
        MetaCatUtil.debugMessage("Eror irn rplicationHandler.getHomeServer() "+
1741
                          "to close pstmt: "+ee.getMessage(), 30);
1742
      }
1743
      finally
1744
      {
1745
        DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1746
      }
1747

    
1748
    }//finally
1749
    return sl;
1750
  }
1751

    
1752
  /**
1753
   * Returns a home server location  given a accnum
1754
   * @param accNum , given accNum for a document
1755
   *
1756
   */
1757
  public static int getHomeServerCodeForDocId(String accNum) throws Exception
1758
  {
1759
    DBConnection dbConn = null;
1760
    int serialNumber = -1;
1761
    PreparedStatement pstmt = null;
1762
    int serverCode = 1;
1763
    //MetaCatUtil ut = new MetaCatUtil();
1764
    String docId=MetaCatUtil.getDocIdFromString(accNum);
1765

    
1766
    try
1767
    {
1768

    
1769
      // Get DBConnection
1770
      dbConn=DBConnectionPool.
1771
                  getDBConnection("ReplicationHandler.getServerLocation");
1772
      serialNumber=dbConn.getCheckOutSerialNumber();
1773
      pstmt=dbConn.prepareStatement("SELECT server_location FROM xml_documents "
1774
                              + "WHERE docid LIKE '" + docId + "'");
1775
      pstmt.execute();
1776
      ResultSet rs = pstmt.getResultSet();
1777
      boolean tablehasrows = rs.next();
1778
      //If a document is find, return the server location for it
1779
      if ( tablehasrows )
1780
      {
1781
        serverCode = rs.getInt(1);
1782
        pstmt.close();
1783
        //conn.close();
1784
        return serverCode;
1785
      }
1786
      //if couldn't find in xml_documents table, we think server code is 1
1787
      //(this is new document)
1788
      else
1789
      {
1790
        pstmt.close();
1791
        //conn.close();
1792
        return serverCode;
1793
      }
1794

    
1795
    }
1796
    catch(Exception e)
1797
    {
1798

    
1799
      throw e;
1800

    
1801
    }
1802
    finally
1803
    {
1804
      try
1805
      {
1806
        pstmt.close();
1807
        //conn.close();
1808

    
1809
      }
1810
      catch(Exception ee)
1811
      {
1812
        MetaCatUtil.debugMessage("Erorr in Replication.getServerLocation "+
1813
                     "to close pstmt"+ee.getMessage(), 30);
1814
      }
1815
      finally
1816
      {
1817
        DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1818
      }//finally
1819
    }//finally
1820
   //return serverCode;
1821
  }
1822

    
1823

    
1824

    
1825
  /**
1826
   * This method returns the content of a url
1827
   * @param u the url to return the content from
1828
   * @return a string representing the content of the url
1829
   * @throws java.io.IOException
1830
   */
1831
  public static String getURLContent(URL u) throws java.io.IOException
1832
  {
1833
    char istreamChar;
1834
    int istreamInt;
1835
    MetaCatUtil.debugMessage("Before open the stream"+u.toString(), 50);
1836
    InputStream input = u.openStream();
1837
    MetaCatUtil.debugMessage("Afetr open the stream"+u.toString(), 50);
1838
    InputStreamReader istream = new InputStreamReader(input);
1839
    StringBuffer serverResponse = new StringBuffer();
1840
    while((istreamInt = istream.read()) != -1)
1841
    {
1842
      istreamChar = (char)istreamInt;
1843
      serverResponse.append(istreamChar);
1844
    }
1845
    istream.close();
1846
    input.close();
1847

    
1848
    return serverResponse.toString();
1849
  }
1850

    
1851
  /**
1852
   * Method for writing replication messages to a log file specified in
1853
   * metacat.properties
1854
   */
1855
  public static void replLog(String message)
1856
  {
1857
    try
1858
    {
1859
      FileOutputStream fos = new FileOutputStream(
1860
                                 util.getOption("replicationlog"), true);
1861
      PrintWriter pw = new PrintWriter(fos);
1862
      SimpleDateFormat formatter = new SimpleDateFormat ("yy-MM-dd HH:mm:ss");
1863
      java.util.Date localtime = new java.util.Date();
1864
      String dateString = formatter.format(localtime);
1865
      dateString += " :: " + message;
1866
      //time stamp each entry
1867
      pw.println(dateString);
1868
      pw.flush();
1869
    }
1870
    catch(Exception e)
1871
    {
1872
      System.out.println("error writing to replication log from " +
1873
                         "MetacatReplication.replLog: " + e.getMessage());
1874
      //e.printStackTrace(System.out);
1875
    }
1876
  }
1877

    
1878
  /**
1879
   * Method for writing replication messages to a log file specified in
1880
   * metacat.properties
1881
   */
1882
  public static void replErrorLog(String message)
1883
  {
1884
    try
1885
    {
1886
      FileOutputStream fos = new FileOutputStream(
1887
                                 util.getOption("replicationerrorlog"), true);
1888
      PrintWriter pw = new PrintWriter(fos);
1889
      SimpleDateFormat formatter = new SimpleDateFormat ("yy-MM-dd HH:mm:ss");
1890
      java.util.Date localtime = new java.util.Date();
1891
      String dateString = formatter.format(localtime);
1892
      dateString += " :: " + message;
1893
      //time stamp each entry
1894
      pw.println(dateString);
1895
      pw.flush();
1896
    }
1897
    catch(Exception e)
1898
    {
1899
      System.out.println("error writing to replication log from " +
1900
                         "MetacatReplication.replLog: " + e.getMessage());
1901
      //e.printStackTrace(System.out);
1902
    }
1903
  }
1904

    
1905
  /**
1906
   * Returns true if the replicate field for server in xml_replication is 1.
1907
   * Returns false otherwise
1908
   */
1909
  public static boolean replToServer(String server)
1910
  {
1911
    DBConnection dbConn = null;
1912
    int serialNumber = -1;
1913
    PreparedStatement pstmt = null;
1914
    try
1915
    {
1916
      dbConn=DBConnectionPool.
1917
                  getDBConnection("MetacatReplication.repltoServer");
1918
      serialNumber=dbConn.getCheckOutSerialNumber();
1919
      pstmt = dbConn.prepareStatement("select replicate from " +
1920
                                    "xml_replication where server like '" +
1921
                                     server + "'");
1922
      pstmt.execute();
1923
      ResultSet rs = pstmt.getResultSet();
1924
      boolean tablehasrows = rs.next();
1925
      if(tablehasrows)
1926
      {
1927
        int i = rs.getInt(1);
1928
        if(i == 1)
1929
        {
1930
          pstmt.close();
1931
          //conn.close();
1932
          return true;
1933
        }
1934
        else
1935
        {
1936
          pstmt.close();
1937
          //conn.close();
1938
          return false;
1939
        }
1940
      }
1941
    }
1942
    catch(Exception e)
1943
    {
1944
      System.out.println("error in MetacatReplication.replToServer: " +
1945
                         e.getMessage());
1946
    }
1947
    finally
1948
    {
1949
      try
1950
      {
1951
        pstmt.close();
1952
        //conn.close();
1953
      }//try
1954
      catch(Exception ee)
1955
      {
1956
        MetaCatUtil.debugMessage("Error in MetacatReplication.replToServer: "
1957
                                  +ee.getMessage(), 30);
1958
      }//catch
1959
      finally
1960
      {
1961
        DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1962
      }//finally
1963
    }//finally
1964
    return false;
1965
    //the default if this server does not exist is to not replicate to it.
1966
  }
1967

    
1968

    
1969
}
(44-44/63)