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
 *
8
 *   '$Author: leinfelder $'
9
 *     '$Date: 2008-10-13 11:34:13 -0700 (Mon, 13 Oct 2008) $'
10
 * '$Revision: 4450 $'
11
 *
12
 * This program is free software; you can redistribute it and/or modify
13
 * it under the terms of the GNU General Public License as published by
14
 * the Free Software Foundation; either version 2 of the License, or
15
 * (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU General Public License
23
 * along with this program; if not, write to the Free Software
24
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25
 */
26

    
27
package edu.ucsb.nceas.metacat;
28

    
29
import java.util.*;
30
import java.util.Date;
31
import java.io.*;
32
import java.sql.*;
33
import java.net.*;
34
import java.text.*;
35

    
36
import javax.servlet.*;
37
import javax.servlet.http.*;
38

    
39
import edu.ucsb.nceas.metacat.service.DatabaseService;
40
import edu.ucsb.nceas.metacat.service.PropertyService;
41
import edu.ucsb.nceas.metacat.service.SessionService;
42
import edu.ucsb.nceas.metacat.util.LDAPUtil;
43
import edu.ucsb.nceas.metacat.util.MetaCatUtil;
44
import edu.ucsb.nceas.metacat.util.SessionData;
45
import edu.ucsb.nceas.metacat.util.SystemUtil;
46
import edu.ucsb.nceas.utilities.GeneralPropertyException;
47
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
48

    
49
import org.apache.log4j.Logger;
50
import org.xml.sax.*;
51

    
52
public class MetacatReplication extends HttpServlet implements Runnable
53
{
54

    
55
  private static final long serialVersionUID = -2898600143193513155L;
56
  private long timeInterval;
57
  private Date firstTime;
58
  private boolean timedReplicationIsOn = false;
59
  Timer replicationDaemon;
60
  private Vector fileLocks = new Vector();
61
  private Thread lockThread = null;
62
  public static final String FORCEREPLICATEDELETE = "forcereplicatedelete";
63
  private static final String TIMEREPLICATION = "replication.timedreplication";
64
  private static final String TIMEREPLICATIONINTERVAl = "replication.timedreplicationinterval";
65
  private static final String FIRSTTIME  = "replication.firsttimedreplication";
66
  private static final int    TIMEINTERVALLIMIT = 7200000;
67
  private static Logger logMetacat = Logger.getLogger(MetacatReplication.class);
68
  public static final String REPLICATIONUSER = "replication";
69

    
70
  /**
71
   * Initialize the servlet by creating appropriate database connections
72
   */
73
  public void init(ServletConfig config) throws ServletException
74
  {
75
     //initialize db connections to handle any update requests
76
    //deltaT = util.getProperty("replication.deltaT");
77
    //the default deltaT can be set from metacat.properties
78
    //create a thread to do the delta-T check but don't execute it yet
79
    replicationDaemon = new Timer(true);
80
    try
81
    {
82
       timedReplicationIsOn = (new Boolean(PropertyService.getProperty(TIMEREPLICATION ).trim())).booleanValue();
83
       logMetacat.info("The timed replication on is"+timedReplicationIsOn);
84
       timeInterval = (new Long(PropertyService.getProperty(TIMEREPLICATIONINTERVAl).trim())).longValue();
85
       logMetacat.warn("The timed replication time Inerval is "+ timeInterval);
86
       String firstTimeStr = PropertyService.getProperty(FIRSTTIME);
87
       logMetacat.warn("first replication time form property is "+firstTimeStr);
88
       firstTime = ReplicationHandler.combinateCurrentDateAndGivenTime(firstTimeStr);
89
       logMetacat.warn("After combine current time, the real first time is "
90
                                +firstTime.toString()+" minisec");
91
       // set up time replication if it is on
92
       if (timedReplicationIsOn)
93
       {
94
           replicationDaemon.scheduleAtFixedRate(new ReplicationHandler(), firstTime, timeInterval);
95
           MetacatReplication.replLog("deltaT handler started with rate=" +
96
                   timeInterval + " mini seconds at " +firstTime.toString());
97
       }
98
    }
99
    catch (Exception e)
100
    {
101
        // the timed replication in Metacat.properties file has problem
102
        // so timed replication is setting to false;
103
        logMetacat.error("Couldn't set up timed replication "+
104
                     " in Metacat replication servlet init because " +
105
                 e.getMessage());
106
        MetacatReplication.replErrorLog("Couldn't set up timed replication "+
107
                " in Metacat replication servlet init because " +
108
                e.getMessage());
109
        timedReplicationIsOn = false;
110
    }
111
    
112
  }
113

    
114
  public void destroy()
115
  {
116
    replicationDaemon.cancel();
117
   
118
  }
119

    
120
  public void doGet (HttpServletRequest request, HttpServletResponse response)
121
                     throws ServletException, IOException
122
  {
123
    // Process the data and send back the response
124
    handleGetOrPost(request, response);
125
  }
126

    
127
  public void doPost(HttpServletRequest request, HttpServletResponse response)
128
                     throws ServletException, IOException
129
  {
130
    // Process the data and send back the response
131
    handleGetOrPost(request, response);
132
  }
133

    
134
  private void handleGetOrPost(HttpServletRequest request,
135
                               HttpServletResponse response)
136
                               throws ServletException, IOException
137
  {
138
    //PrintWriter out = response.getWriter();
139
    //ServletOutputStream outPut = response.getOutputStream();
140
    Hashtable params = new Hashtable();
141
    Enumeration paramlist = request.getParameterNames();
142

    
143

    
144

    
145
// NOT NEEDED - doesn't provide enough security because of possible IP spoofing
146
// REPLACED with running replication comminications over HTTPS
147
//    String requestingServerIP = request.getRemoteAddr();
148
//    InetAddress iaddr = InetAddress.getByName(requestingServerIP);
149
//    String requestingServer = iaddr.getHostName();
150

    
151
    while (paramlist.hasMoreElements()) {
152
      String name = (String)paramlist.nextElement();
153
      String[] value = request.getParameterValues(name);
154
      params.put(name, value);
155
    }
156

    
157
    String action = ((String[])params.get("action"))[0];
158
    String server = null;
159

    
160
    try {
161
      // check if the server is included in the list of replicated servers
162
      if ( !action.equals("servercontrol") &&
163
           !action.equals("stop") &&
164
           !action.equals("start") &&
165
           !action.equals("getall") ) {
166

    
167
        server = ((String[])params.get("server"))[0];
168
        if ( getServerCodeForServerName(server) == 0 ) {
169
          System.out.println("Action \"" + action +
170
                             "\" rejected for server: " + server);
171
          return;
172
        } else {
173
          System.out.println("Action \"" + action +
174
                             "\" accepted for server: " + server);
175
        }
176
      }
177
      else
178
      {
179
          // start, stop, getall and servercontrol need to check
180
          // if user is administor
181
          HttpSession sess = request.getSession(true);
182
          SessionData sessionData = null;
183
          String sess_id = "";
184
          String username = "";
185
          String[] groupnames = {""};
186

    
187
          if (params.containsKey("sessionid")) 
188
          {
189
             sess_id = ((String[]) params.get("sessionid"))[0];
190
             logMetacat.info("in has sessionid "+ sess_id);
191
             if (SessionService.isSessionRegistered(sess_id)) 
192
             {
193
                  logMetacat.info("find the id " + sess_id + " in hash table");
194
                  sessionData = SessionService.getRegisteredSession(sess_id);
195
             }
196
           } 
197
          if (sessionData == null) {
198
        	  sessionData = new SessionData(sess.getId(), 
199
					(String) sess.getAttribute("username"), 
200
					(String[]) sess.getAttribute("groups"),
201
					(String) sess.getAttribute("password"));
202
          }
203
          
204
           username = sessionData.getUserName();
205
           logMetacat.warn("The user name from session is: "+ username);
206
           groupnames = sessionData.getGroupNames();
207
           if (!LDAPUtil.isAdministrator(username, groupnames)) 
208
           {
209
               PrintWriter out = response.getWriter();
210
               out.print("<error>");
211
               out.print("The user \"" + username +
212
                       "\" is not authorized for this action.");
213
               out.print("</error>");
214
               out.close();
215
               logMetacat.warn("The user \"" + username +
216
                       "\" is not authorized for this action: " +action);
217
               replErrorLog("The user \"" + username +
218
                       "\" is not authorized for this action: " +action);
219
               return;
220
           }
221
                        
222
      }// this is final else
223
    } catch (Exception e) {
224
      System.out.println("Error in MetacatReplication.handleGetOrPost: " +
225
                         e.getMessage() );
226
      return;
227
    }
228
    
229
    if ( action.equals("readdata") )
230
    {
231
      OutputStream outStream = response.getOutputStream();
232
      //to get the data file.
233
      handleGetDataFileRequest(outStream, params, response);
234
      outStream.close();
235
    }
236
    else if ( action.equals("forcereplicatedatafile") )
237
    {
238
      //read a specific docid from remote host, and store it into local host
239
      handleForceReplicateDataFileRequest(params, request);
240

    
241
    }
242
    else
243
    {
244
    PrintWriter out = response.getWriter();
245
    if ( action.equals("stop") ) {
246
      //stop the replication server
247
      replicationDaemon.cancel();
248
      replicationDaemon = new Timer(true);
249
      timedReplicationIsOn = false;
250
      try {
251
    	  PropertyService.setProperty(TIMEREPLICATION, (new Boolean(timedReplicationIsOn)).toString());
252
      } catch (GeneralPropertyException gpe) {
253
    	  logMetacat.warn("Could not set " + TIMEREPLICATION + " property: " + gpe.getMessage());
254
      }
255
      out.println("Replication Handler Stopped");
256
      MetacatReplication.replLog("deltaT handler stopped");
257

    
258

    
259
    } else if ( action.equals("start") ) {
260
       String firstTimeStr = "";
261
      //start the replication server
262
       if ( params.containsKey("rate") ) {
263
        timeInterval = new Long(
264
               new String(((String[])params.get("rate"))[0])).longValue();
265
        if(timeInterval < TIMEINTERVALLIMIT) {
266
            out.println("Replication deltaT rate cannot be less than "+
267
                    TIMEINTERVALLIMIT + " millisecs and system automatically setup the rate to "+TIMEINTERVALLIMIT);
268
            //deltaT<30 is a timing mess!
269
            timeInterval = TIMEINTERVALLIMIT;
270
        }
271
      } else {
272
        timeInterval = TIMEINTERVALLIMIT ;
273
      }
274
      logMetacat.info("New rate is: " + timeInterval + " mini seconds.");
275
      if ( params.containsKey("firsttime"))
276
      {
277
         firstTimeStr = ((String[])params.get("firsttime"))[0];
278
         try
279
         {
280
           firstTime = ReplicationHandler.combinateCurrentDateAndGivenTime(firstTimeStr);
281
           logMetacat.info("The first time setting is "+firstTime.toString());
282
         }
283
         catch (Exception e)
284
         {
285
            throw new ServletException(e.getMessage());
286
         }
287
         logMetacat.warn("After combine current time, the real first time is "
288
                                  +firstTime.toString()+" minisec");
289
      }
290
      else
291
      {
292
          MetacatReplication.replErrorLog("You should specify the first time " +
293
                                          "to start a time replication");
294
          logMetacat.warn("You should specify the first time " +
295
                                  "to start a time replication");
296
          return;
297
      }
298
      
299
      timedReplicationIsOn = true;
300
      try {
301
      // save settings to property file
302
      PropertyService.setProperty(TIMEREPLICATION, (new Boolean(timedReplicationIsOn)).toString());
303
      // note we couldn't use firstTime object because it has date info
304
      // we only need time info such as 10:00 PM
305
      PropertyService.setProperty(FIRSTTIME, firstTimeStr);
306
      PropertyService.setProperty(TIMEREPLICATIONINTERVAl, (new Long(timeInterval)).toString());
307
      } catch (GeneralPropertyException gpe) {
308
    	  logMetacat.warn("Could not set property: " + gpe.getMessage());
309
      }
310
      replicationDaemon.cancel();
311
      replicationDaemon = new Timer(true);
312
      replicationDaemon.scheduleAtFixedRate(new ReplicationHandler(), firstTime,
313
                                            timeInterval);
314
      out.println("Replication Handler Started");
315
      MetacatReplication.replLog("deltaT handler started with rate=" +
316
                                    timeInterval + " milliseconds at " +firstTime.toString());
317

    
318

    
319
    } else if ( action.equals("getall") ) {
320
      //updates this server exactly once
321
      replicationDaemon.schedule(new ReplicationHandler(), 0);
322
      response.setContentType("text/html");
323
      out.println("<html><body>\"Get All\" Done</body></html>");
324

    
325
    } else if ( action.equals("forcereplicate") ) {
326
      //read a specific docid from remote host, and store it into local host
327
      handleForceReplicateRequest(out, params, response, request);
328

    
329
    } else if ( action.equals(FORCEREPLICATEDELETE) ) {
330
      //read a specific docid from remote host, and store it into local host
331
      handleForceReplicateDeleteRequest(out, params, response, request);
332

    
333
    } else if ( action.equals("update") ) {
334
      //request an update list from the server
335
      handleUpdateRequest(out, params, response);
336

    
337
    } else if ( action.equals("read") ) {
338
      //request a specific document from the server
339
      //note that this could be replaced by a call to metacatServlet
340
      //handleGetDocumentAction().
341
      handleGetDocumentRequest(out, params, response);
342
    } else if ( action.equals("getlock") ) {
343
      handleGetLockRequest(out, params, response);
344

    
345
    } else if ( action.equals("getdocumentinfo") ) {
346
      handleGetDocumentInfoRequest(out, params, response);
347

    
348
    } else if ( action.equals("gettime") ) {
349
      handleGetTimeRequest(out, params, response);
350

    
351
    } else if ( action.equals("getcatalog") ) {
352
      handleGetCatalogRequest(out, params, response, true);
353

    
354
    } else if ( action.equals("servercontrol") ) {
355
      handleServerControlRequest(out, params, response);
356
    } else if ( action.equals("test") ) {
357
      response.setContentType("text/html");
358
      out.println("<html><body>Test successfully</body></html>");
359
    }
360

    
361
    out.close();
362
    }//else
363
  }
364

    
365
  /**
366
   * This method can add, delete and list the servers currently included in
367
   * xml_replication.
368
   * action           subaction            other needed params
369
   * ---------------------------------------------------------
370
   * servercontrol    add                  server
371
   * servercontrol    delete               server
372
   * servercontrol    list
373
   */
374
  private void handleServerControlRequest(PrintWriter out, Hashtable params,
375
                                          HttpServletResponse response)
376
  {
377
    String subaction = ((String[])params.get("subaction"))[0];
378
    DBConnection dbConn = null;
379
    int serialNumber = -1;
380
    PreparedStatement pstmt = null;
381
    String replicate =null;
382
    String server = null;
383
    String dataReplicate = null;
384
    String hub = null;
385
    try {
386
      //conn = util.openDBConnection();
387
      dbConn=DBConnectionPool.
388
               getDBConnection("MetacatReplication.handleServerControlRequest");
389
      serialNumber=dbConn.getCheckOutSerialNumber();
390

    
391
      // add server to server list
392
      if ( subaction.equals("add") ) {
393
        replicate = ((String[])params.get("replicate"))[0];
394
        server = ((String[])params.get("server"))[0];
395

    
396
        //Get data replication value
397
        dataReplicate = ((String[])params.get("datareplicate"))[0];
398
        //Get hub value
399
        hub = ((String[])params.get("hub"))[0];
400

    
401
        /*pstmt = dbConn.prepareStatement("INSERT INTO xml_replication " +
402
                  "(server, last_checked, replicate, datareplicate, hub) " +
403
                                      "VALUES ('" + server + "', to_date(" +
404
                                      "'01/01/00', 'MM/DD/YY'), '" +
405
                                      replicate +"', '" +dataReplicate+"', '"
406
                                      + hub +"')");*/
407
        pstmt = dbConn.prepareStatement("INSERT INTO xml_replication " +
408
                  "(server, last_checked, replicate, datareplicate, hub) " +
409
                                      "VALUES ('" + server + "', "+
410
                                      DatabaseService.getDBAdapter().toDate("01/01/1980", "MM/DD/YYYY")
411
                                      + ", '" +
412
                                      replicate +"', '" +dataReplicate+"', '"
413
                                      + hub +"')");
414

    
415
        pstmt.execute();
416
        pstmt.close();
417
        dbConn.commit();
418
        out.println("Server " + server + " added");
419
        response.setContentType("text/html");
420
        out.println("<html><body><table border=\"1\">");
421
        out.println("<tr><td><b>server</b></td><td><b>last_checked</b></td><td>");
422
        out.println("<b>replicate</b></td>");
423
        out.println("<td><b>datareplicate</b></td>");
424
        out.println("<td><b>hub</b></td></tr>");
425
        pstmt = dbConn.prepareStatement("SELECT * FROM xml_replication");
426
        //increase dbconnection usage
427
        dbConn.increaseUsageCount(1);
428

    
429
        pstmt.execute();
430
        ResultSet rs = pstmt.getResultSet();
431
        boolean tablehasrows = rs.next();
432
        while(tablehasrows) {
433
          out.println("<tr><td>" + rs.getString(2) + "</td><td>");
434
          out.println(rs.getString(3) + "</td><td>");
435
          out.println(rs.getString(4) + "</td><td>");
436
          out.println(rs.getString(5) + "</td><td>");
437
          out.println(rs.getString(6) + "</td></tr>");
438

    
439
          tablehasrows = rs.next();
440
        }
441
        out.println("</table></body></html>");
442

    
443
        // download certificate with the public key on this server
444
        // and import it as a trusted certificate
445
        String certURL = ((String[])params.get("certificate"))[0];
446
        downloadCertificate(certURL);
447

    
448
      // delete server from server list
449
      } else if ( subaction.equals("delete") ) {
450
        server = ((String[])params.get("server"))[0];
451
        pstmt = dbConn.prepareStatement("DELETE FROM xml_replication " +
452
                                      "WHERE server LIKE '" + server + "'");
453
        pstmt.execute();
454
        pstmt.close();
455
        dbConn.commit();
456
        out.println("Server " + server + " deleted");
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

    
464
        pstmt = dbConn.prepareStatement("SELECT * FROM xml_replication");
465
        //increase dbconnection usage
466
        dbConn.increaseUsageCount(1);
467
        pstmt.execute();
468
        ResultSet rs = pstmt.getResultSet();
469
        boolean tablehasrows = rs.next();
470
        while(tablehasrows)
471
        {
472
          out.println("<tr><td>" + rs.getString(2) + "</td><td>");
473
          out.println(rs.getString(3) + "</td><td>");
474
          out.println(rs.getString(4) + "</td><td>");
475
          out.println(rs.getString(5) + "</td><td>");
476
          out.println(rs.getString(6) + "</td></tr>");
477
          tablehasrows = rs.next();
478
        }
479
        out.println("</table></body></html>");
480

    
481
      // list servers in server list
482
      } else if ( subaction.equals("list") ) {
483
        response.setContentType("text/html");
484
        out.println("<html><body><table border=\"1\">");
485
        out.println("<tr><td><b>server</b></td><td><b>last_checked</b></td><td>");
486
        out.println("<b>replicate</b></td>");
487
        out.println("<td><b>datareplicate</b></td>");
488
        out.println("<td><b>hub</b></td></tr>");
489
        pstmt = dbConn.prepareStatement("SELECT * FROM xml_replication");
490
        pstmt.execute();
491
        ResultSet rs = pstmt.getResultSet();
492
        boolean tablehasrows = rs.next();
493
        while(tablehasrows) {
494
          out.println("<tr><td>" + rs.getString(2) + "</td><td>");
495
          out.println(rs.getString(3) + "</td><td>");
496
          out.println(rs.getString(4) + "</td><td>");
497
          out.println(rs.getString(5) + "</td><td>");
498
          out.println(rs.getString(6) + "</td></tr>");
499
          tablehasrows = rs.next();
500
        }
501
        out.println("</table></body></html>");
502
      }
503
      else
504
      {
505

    
506
        out.println("<error>Unkonwn subaction</error>");
507

    
508
      }
509
      pstmt.close();
510
      //conn.close();
511

    
512
    } catch(Exception e) {
513
      System.out.println("Error in " +
514
                         "MetacatReplication.handleServerControlRequest " +
515
                         e.getMessage());
516
      e.printStackTrace(System.out);
517
    }
518
    finally
519
    {
520
      try
521
      {
522
        pstmt.close();
523
      }//try
524
      catch (SQLException ee)
525
      {
526
        logMetacat.error("Error in " +
527
                "MetacatReplication.handleServerControlRequest to close pstmt "
528
                 + ee.getMessage());
529
      }//catch
530
      finally
531
      {
532
        DBConnectionPool.returnDBConnection(dbConn, serialNumber);
533
      }//finally
534
    }//finally
535

    
536
  }
537

    
538
   	// download certificate with the public key from certURL and
539
	// upload it onto this server; it then must be imported as a
540
	// trusted certificate
541
	private void downloadCertificate(String certURL) throws FileNotFoundException,
542
			IOException, MalformedURLException, PropertyNotFoundException {
543
		
544
		// the path to be uploaded to
545
		String certPath = SystemUtil.getContextDir(); 
546

    
547
		// get filename from the URL of the certificate
548
		String filename = certURL;
549
		int slash = Math.max(filename.lastIndexOf('/'), filename.lastIndexOf('\\'));
550
		if (slash > -1) {
551
			filename = filename.substring(slash + 1);
552
		}
553

    
554
		// open file output strem to write the input into it
555
		File f = new File(certPath, filename);
556
		synchronized (f) {
557
			try {
558
				if (f.exists()) {
559
					throw new IOException("File already exist: " + f.getCanonicalFile());
560
					// if ( f.exists() && !f.canWrite() ) {
561
					// throw new IOException("Not writable: " +
562
					// f.getCanonicalFile());
563
				}
564
			} catch (SecurityException se) {
565
				// if a security manager exists,
566
				// its checkRead method is called for f.exist()
567
				// or checkWrite method is called for f.canWrite()
568
				throw se;
569
			}
570

    
571
			// create a buffered byte output stream
572
			// that uses a default-sized output buffer
573
			FileOutputStream fos = new FileOutputStream(f);
574
			BufferedOutputStream out = new BufferedOutputStream(fos);
575

    
576
			// this should be http url
577
			URL url = new URL(certURL);
578
			BufferedInputStream bis = null;
579
			try {
580
				bis = new BufferedInputStream(url.openStream());
581
				byte[] buf = new byte[4 * 1024]; // 4K buffer
582
				int b = bis.read(buf);
583
				while (b != -1) {
584
					out.write(buf, 0, b);
585
					b = bis.read(buf);
586
				}
587
			} finally {
588
				if (bis != null)
589
					bis.close();
590
			}
591
			// the input and the output streams must be closed
592
			bis.close();
593
			out.flush();
594
			out.close();
595
			fos.close();
596
		} // end of synchronized(f)
597
	}
598

    
599
  /**
600
	 * when a forcereplication request comes in, local host sends a read request
601
	 * to the requesting server (remote server) for the specified docid. Then
602
	 * store it in local database.
603
	 */
604
  private void handleForceReplicateRequest(PrintWriter out, Hashtable params,
605
                                           HttpServletResponse response, HttpServletRequest request)
606
  {
607
    String server = ((String[])params.get("server"))[0]; // the server that
608
    String docid = ((String[])params.get("docid"))[0]; // sent the document
609
    String dbaction = "UPDATE"; // the default action is UPDATE
610
    boolean override = false;
611
    int serverCode = 1;
612
    DBConnection dbConn = null;
613
    int serialNumber = -1;
614

    
615
    try {
616
      //if the url contains a dbaction then the default action is overridden
617
      if(params.containsKey("dbaction")) {
618
        dbaction = ((String[])params.get("dbaction"))[0];
619
        //serverCode = MetacatReplication.getServerCode(server);
620
        //override = true; //we are now overriding the default action
621
      }
622
      MetacatReplication.replLog("force replication request from " + server);
623
      logMetacat.info("Force replication request from: "+ server);
624
      logMetacat.info("Force replication docid: "+docid);
625
      logMetacat.info("Force replication action: "+dbaction);
626
      // sending back read request to remote server
627
      URL u = new URL("https://" + server + "?server="
628
                +MetaCatUtil.getLocalReplicationServerName()
629
                +"&action=read&docid=" + docid);
630
      String xmldoc = MetacatReplication.getURLContent(u);
631

    
632
      // get the document info from server
633
      URL docinfourl = new URL("https://" + server +
634
                               "?server="+MetaCatUtil.getLocalReplicationServerName()
635
                               +"&action=getdocumentinfo&docid=" + docid);
636

    
637
      String docInfoStr = MetacatReplication.getURLContent(docinfourl);
638

    
639
      //dih is the parser for the docinfo xml format
640
      DocInfoHandler dih = new DocInfoHandler();
641
      XMLReader docinfoParser = ReplicationHandler.initParser(dih);
642
      docinfoParser.parse(new InputSource(new StringReader(docInfoStr)));
643
      Hashtable docinfoHash = dih.getDocInfo();
644

    
645
      // Get user owner of this docid
646
      String user = (String)docinfoHash.get("user_owner");
647
      // Get home server of this docid
648
      String homeServer=(String)docinfoHash.get("home_server");
649
      String createdDate = (String)docinfoHash.get("date_created");
650
      String updatedDate = (String)docinfoHash.get("date_updated");
651
      logMetacat.info("homeServer: "+homeServer);
652
      // Get Document type
653
      String docType = (String)docinfoHash.get("doctype");
654
      logMetacat.info("docType: "+docType);
655
      String parserBase = null;
656
      // this for eml2 and we need user eml2 parser
657
      if (docType != null &&
658
          (docType.trim()).equals(DocumentImpl.EML2_0_0NAMESPACE))
659
      {
660
         logMetacat.warn("This is an eml200 document!");
661
         parserBase = DocumentImpl.EML200;
662
      }
663
      else if (docType != null && (docType.trim()).equals(DocumentImpl.EML2_0_1NAMESPACE))
664
      {
665
         logMetacat.warn("This is an eml2.0.1 document!");
666
         parserBase = DocumentImpl.EML200;
667
      }
668
      else if (docType != null && (docType.trim()).equals(DocumentImpl.EML2_1_0NAMESPACE))
669
      {
670
         logMetacat.warn("This is an eml2.1.0 document!");
671
         parserBase = DocumentImpl.EML210;
672
      }
673
      logMetacat.warn("The parserBase is: "+parserBase);
674

    
675
      // Get DBConnection from pool
676
      dbConn=DBConnectionPool.
677
              getDBConnection("MetacatReplication.handleForceReplicateRequest");
678
      serialNumber=dbConn.getCheckOutSerialNumber();
679
      // write the document to local database
680
      DocumentImplWrapper wrapper = new DocumentImplWrapper(parserBase, false);
681
      //try this independently so we can set
682
      Exception writeException = null; 
683
      try {
684
	      wrapper.writeReplication(dbConn, new StringReader(xmldoc), null, null,
685
	                               dbaction, docid, user, null, homeServer, 
686
	                               server, createdDate, updatedDate);
687
      }
688
      catch (Exception e) {
689
    	  writeException = e;
690
      }
691

    
692
      //process extra access rules before dealing with the write exception (doc exist already)
693
      Vector accessControlList = (Vector) docinfoHash.get("accessControl");
694
      if (accessControlList != null) {
695
    	  for (int i = 0; i < accessControlList.size(); i++) {
696
        	  AccessControlForSingleFile acfsf = (AccessControlForSingleFile) accessControlList.get(i);
697
        	  if (!acfsf.accessControlExists()) {
698
	        	  acfsf.insertPermissions();
699
	        	  MetacatReplication.replLog("document " + docid + " permissions added to DB");
700
        	  }
701
          }
702
      }
703
      
704
      if (writeException != null) {
705
    	  throw writeException;
706
      }
707
      
708
      MetacatReplication.replLog("document " + docid + " added to DB with " +
709
                                 "action " + dbaction);
710
      EventLog.getInstance().log(request.getRemoteAddr(), REPLICATIONUSER, docid, dbaction);
711
    }//try
712
    catch(Exception e)
713
    {
714
      MetacatReplication.replErrorLog("document " + docid +
715
                                      " failed to added to DB with " +
716
                                      "action " + dbaction + " because "+
717
                                       e.getMessage());
718
      logMetacat.error("ERROR in MetacatReplication.handleForceReplicate" +
719
                         "Request(): " + e.getMessage());
720

    
721
    }//catch
722
    finally
723
    {
724
      // Return the checked out DBConnection
725
      DBConnectionPool.returnDBConnection(dbConn, serialNumber);
726
    }//finally
727
  }
728

    
729
/*
730
 * when a forcereplication delete request comes in, local host will delete this
731
 * document
732
 */
733
private void handleForceReplicateDeleteRequest(PrintWriter out, Hashtable params,
734
                                         HttpServletResponse response, HttpServletRequest request)
735
{
736
  String server = ((String[])params.get("server"))[0]; // the server that
737
  String docid = ((String[])params.get("docid"))[0]; // sent the document
738
  try
739
  {
740
    MetacatReplication.replLog("force replication delete request from " + server);
741
    MetacatReplication.replLog("force replication delete docid " + docid);
742
    logMetacat.info("Force replication delete request from: "+ server);
743
    logMetacat.info("Force replication delete docid: "+docid);
744
    DocumentImpl.delete(docid, null, null, server);
745
    MetacatReplication.replLog("document " + docid + " was successfully deleted ");
746
    EventLog.getInstance().log(request.getRemoteAddr(), REPLICATIONUSER, docid, "delete");
747
    logMetacat.info("document " + docid + " was successfully deleted ");
748
  }
749
  catch(Exception e)
750
  {
751
    MetacatReplication.replErrorLog("document " + docid +
752
                                    " failed to delete because "+
753
                                     e.getMessage());
754
    logMetacat.error("ERROR in MetacatReplication.handleForceDeleteReplicate" +
755
                       "Request(): " + e.getMessage());
756

    
757
  }//catch
758

    
759
}
760

    
761

    
762
  /**
763
   * when a forcereplication data file request comes in, local host sends a
764
   * readdata request to the requesting server (remote server) for the specified
765
   * docid. Then store it in local database and file system
766
   */
767
  private void handleForceReplicateDataFileRequest(Hashtable params, HttpServletRequest request)
768
  {
769

    
770
    //make sure there is some parameters
771
    if(params.isEmpty())
772
    {
773
      return;
774
    }
775
    // Get remote server
776
    String server = ((String[])params.get("server"))[0];
777
    // the docid should include rev number
778
    String docid = ((String[])params.get("docid"))[0];
779
    // Make sure there is a docid and server
780
    if (docid==null || server==null || server.equals(""))
781
    {
782
      logMetacat.error("Didn't specify docid or server for replication");
783
      return;
784
    }
785

    
786
    // Overide or not
787
    boolean override = false;
788
    // dbaction - update or insert
789
    String dbaction=null;
790

    
791
    try
792
    {
793
      //docid was switch to two parts uinque code and rev
794
      //String uniqueCode=MetaCatUtil.getDocIdFromString(docid);
795
      //int rev=MetaCatUtil.getVersionFromString(docid);
796
      if(params.containsKey("dbaction"))
797
      {
798
        dbaction = ((String[])params.get("dbaction"))[0];
799
      }
800
      else//default value is update
801
      {
802
        dbaction = "update";
803
      }
804

    
805
      MetacatReplication.replLog("force replication request from " + server);
806
      logMetacat.info("Force replication request from: "+ server);
807
      logMetacat.info("Force replication docid: "+docid);
808
      logMetacat.info("Force replication action: "+dbaction);
809
      // get the document info from server
810
      URL docinfourl = new URL("https://" + server +
811
                               "?server="+MetaCatUtil.getLocalReplicationServerName()
812
                               +"&action=getdocumentinfo&docid=" + docid);
813

    
814
      String docInfoStr = MetacatReplication.getURLContent(docinfourl);
815

    
816
      //dih is the parser for the docinfo xml format
817
      DocInfoHandler dih = new DocInfoHandler();
818
      XMLReader docinfoParser = ReplicationHandler.initParser(dih);
819
      docinfoParser.parse(new InputSource(new StringReader(docInfoStr)));
820
      Hashtable docinfoHash = dih.getDocInfo();
821
      String user = (String)docinfoHash.get("user_owner");
822

    
823
      String docName = (String)docinfoHash.get("docname");
824

    
825
      String docType = (String)docinfoHash.get("doctype");
826

    
827
      String docHomeServer= (String)docinfoHash.get("home_server");
828
      
829
      String createdDate = (String)docinfoHash.get("date_created");
830
      
831
      String updatedDate = (String)docinfoHash.get("date_updated");
832
      logMetacat.info("docHomeServer of datafile: "+docHomeServer);
833

    
834

    
835

    
836
      //if action is delete, we don't delete the data file. Just archieve
837
      //the xml_documents
838
      /*if (dbaction.equals("delete"))
839
      {
840
        //conn = util.getConnection();
841
        DocumentImpl.delete(docid,user,null);
842
        //util.returnConnection(conn);
843
      }*/
844
      //To data file insert or update is same
845
      if (dbaction.equals("insert")||dbaction.equals("update"))
846
      {
847
        //Get data file and store it into local file system.
848
        // sending back readdata request to server
849
        URL url = new URL("https://" + server + "?server="
850
                +MetaCatUtil.getLocalReplicationServerName()
851
                +"&action=readdata&docid=" + docid);
852
        String datafilePath = PropertyService.getProperty("application.datafilepath");
853
        
854
        Exception writeException = null;
855
        //register data file into xml_documents table and wite data file
856
        //into file system
857
        try {
858
        	DocumentImpl.writeDataFileInReplication(url.openStream(), datafilePath,
859
                            docName, docType, docid, user,docHomeServer,server, 
860
                            DocumentImpl.DOCUMENTTABLE, false, createdDate, updatedDate);
861
        }
862
        catch (Exception e) {
863
        	writeException = e;
864
		}
865
        //process extra access rules
866
        Vector accessControlList = (Vector) docinfoHash.get("accessControl");
867
        if (accessControlList != null) {
868
      	  for (int i = 0; i < accessControlList.size(); i++) {
869
          	  AccessControlForSingleFile acfsf = (AccessControlForSingleFile) accessControlList.get(i);
870
          	  if (!acfsf.accessControlExists()) {
871
          		  acfsf.insertPermissions();
872
          		  MetacatReplication.replLog("datafile " + docid + " permissions added to DB");
873
          	  }
874
            }
875
        }
876
        
877
        if (writeException != null) {
878
        	throw writeException;
879
        }
880
        
881
        //false means non-timed replication
882
        MetacatReplication.replLog("datafile " + docid + " added to DB with " +
883
                "action " + dbaction);
884
        EventLog.getInstance().log(request.getRemoteAddr(), REPLICATIONUSER, docid, dbaction);
885
     }
886
    
887
    }
888
    catch(Exception e)
889
    {
890

    
891
      MetacatReplication.replErrorLog("Datafile " + docid +
892
                                      " failed to added to DB with " +
893
                                      "action " + dbaction + " because "+
894
                                       e.getMessage());
895
      logMetacat.error
896
              ("ERROR in MetacatReplication.handleForceDataFileReplicate" +
897
                         "Request(): " + e.getMessage());
898
    }
899
  }
900
  /**
901
   * Grants or denies a lock to a requesting host.
902
   * The servlet parameters of interrest are:
903
   * docid: the docid of the file the lock is being requested for
904
   * currentdate: the timestamp of the document on the remote server
905
   *
906
   */
907
  private void handleGetLockRequest(PrintWriter out, Hashtable params,
908
                                    HttpServletResponse response)
909
  {
910

    
911
    try
912
    {
913

    
914
      String docid = ((String[])params.get("docid"))[0];
915
      String remoteRev = ((String[])params.get("updaterev"))[0];
916
      DocumentImpl requestDoc = new DocumentImpl(docid);
917
      MetacatReplication.replLog("lock request for " + docid);
918
      int localRevInt = requestDoc.getRev();
919
      int remoteRevInt = Integer.parseInt(remoteRev);
920

    
921
      if(remoteRevInt >= localRevInt)
922
      {
923
        if(!fileLocks.contains(docid))
924
        { //grant the lock if it is not already locked
925
          fileLocks.add(0, docid); //insert at the beginning of the queue Vector
926
          //send a message back to the the remote host authorizing the insert
927
          out.println("<lockgranted><docid>" +docid+ "</docid></lockgranted>");
928
          lockThread = new Thread(this);
929
          lockThread.setPriority(Thread.MIN_PRIORITY);
930
          lockThread.start();
931
          MetacatReplication.replLog("lock granted for " + docid);
932
        }
933
        else
934
        { //deny the lock
935
          out.println("<filelocked><docid>" + docid + "</docid></filelocked>");
936
          MetacatReplication.replLog("lock denied for " + docid +
937
                                     "reason: file already locked");
938
        }
939
      }
940
      else
941
      {//deny the lock.
942
        out.println("<outdatedfile><docid>" + docid + "</docid></filelocked>");
943
        MetacatReplication.replLog("lock denied for " + docid +
944
                                   "reason: client has outdated file");
945
      }
946
      //conn.close();
947
    }
948
    catch(Exception e)
949
    {
950
      System.out.println("error requesting file lock from MetacatReplication." +
951
                         "handleGetLockRequest: " + e.getMessage());
952
      e.printStackTrace(System.out);
953
    }
954
  }
955

    
956
  /**
957
   * Sends all of the xml_documents information encoded in xml to a requestor
958
   * the format is:
959
   * <!ELEMENT documentinfo (docid, docname, doctype, doctitle, user_owner,
960
   *                  user_updated, home_server, public_access, rev)/>
961
   * all of the subelements of document info are #PCDATA
962
   */
963
  private void handleGetDocumentInfoRequest(PrintWriter out, Hashtable params,
964
                                        HttpServletResponse response)
965
  {
966
    String docid = ((String[])(params.get("docid")))[0];
967
    StringBuffer sb = new StringBuffer();
968

    
969
    try
970
    {
971

    
972
      DocumentImpl doc = new DocumentImpl(docid);
973
      sb.append("<documentinfo><docid>").append(docid);
974
      sb.append("</docid><docname>").append(doc.getDocname());
975
      sb.append("</docname><doctype>").append(doc.getDoctype());
976
      sb.append("</doctype>");
977
      sb.append("<user_owner>").append(doc.getUserowner());
978
      sb.append("</user_owner><user_updated>").append(doc.getUserupdated());
979
      sb.append("</user_updated>");
980
      sb.append("<date_created>");
981
      sb.append(doc.getCreateDate());
982
      sb.append("</date_created>");
983
      sb.append("<date_updated>");
984
      sb.append(doc.getUpdateDate());
985
      sb.append("</date_updated>");
986
      sb.append("<home_server>");
987
      sb.append(doc.getDocHomeServer());
988
      sb.append("</home_server>");
989
      sb.append("<public_access>").append(doc.getPublicaccess());
990
      sb.append("</public_access><rev>").append(doc.getRev());
991
      sb.append("</rev>");
992
      
993
      //permissions on the document
994
      PermissionController permController = new PermissionController(docid);
995
      Vector accessControlList = permController.getAccessControl();
996
      sb.append("<accessControl>");
997
      for (int i = 0; i < accessControlList.size(); i++) {
998
    	  AccessControlForSingleFile acfsf = (AccessControlForSingleFile) accessControlList.get(i);
999
    	  sb.append(acfsf.getAccessString());
1000
      }
1001
      sb.append("</accessControl>");
1002
      
1003
      sb.append("</documentinfo>");
1004
      response.setContentType("text/xml");
1005
      out.println(sb.toString());
1006

    
1007
    }
1008
    catch (Exception e)
1009
    {
1010
      System.out.println("error in " +
1011
                         "metacatReplication.handlegetdocumentinforequest: " +
1012
                          e.getMessage());
1013
    }
1014

    
1015
  }
1016

    
1017
  /**
1018
   * Sends a datafile to a remote host
1019
   */
1020
  private void handleGetDataFileRequest(OutputStream outPut,
1021
                            Hashtable params, HttpServletResponse response)
1022

    
1023
  {
1024
    // File path for data file
1025
    String filepath;
1026
    // Request docid
1027
    String docId = ((String[])(params.get("docid")))[0];
1028
    //check if the doicd is null
1029
    if (docId==null)
1030
    {
1031
      logMetacat.error("Didn't specify docid for replication");
1032
      return;
1033
    }
1034

    
1035
    //try to open a https stream to test if the request server's public key
1036
    //in the key store, this is security issue
1037
    try
1038
    {
1039
      filepath = PropertyService.getProperty("application.datafilepath");
1040
      String server = ((String[])params.get("server"))[0];
1041
      URL u = new URL("https://" + server + "?server="
1042
                +MetaCatUtil.getLocalReplicationServerName()
1043
                +"&action=test");
1044
      String test = MetacatReplication.getURLContent(u);
1045
      //couldn't pass the test
1046
      if (test.indexOf("successfully")==-1)
1047
      {
1048
        //response.setContentType("text/xml");
1049
        //outPut.println("<error>Couldn't pass the trust test</error>");
1050
        logMetacat.error("Couldn't pass the trust test");
1051
        return;
1052
      }
1053
    }//try
1054
    catch (Exception ee)
1055
    {
1056
      return;
1057
    }//catch
1058

    
1059
    if(!filepath.endsWith("/"))
1060
    {
1061
          filepath += "/";
1062
    }
1063
    // Get file aboslute file name
1064
    String filename = filepath + docId;
1065

    
1066
    //MIME type
1067
    String contentType = null;
1068
    if (filename.endsWith(".xml"))
1069
    {
1070
        contentType="text/xml";
1071
    }
1072
    else if (filename.endsWith(".css"))
1073
    {
1074
        contentType="text/css";
1075
    }
1076
    else if (filename.endsWith(".dtd"))
1077
    {
1078
        contentType="text/plain";
1079
    }
1080
    else if (filename.endsWith(".xsd"))
1081
    {
1082
        contentType="text/xml";
1083
    }
1084
    else if (filename.endsWith("/"))
1085
    {
1086
        contentType="text/html";
1087
    }
1088
    else
1089
    {
1090
        File f = new File(filename);
1091
        if ( f.isDirectory() )
1092
        {
1093
           contentType="text/html";
1094
        }
1095
        else
1096
        {
1097
           contentType="application/octet-stream";
1098
        }
1099
     }
1100

    
1101
   // Set the mime type
1102
   response.setContentType(contentType);
1103

    
1104
   // Get the content of the file
1105
   FileInputStream fin = null;
1106
   try
1107
   {
1108
      // FileInputStream to metacat
1109
      fin = new FileInputStream(filename);
1110
      // 4K buffer
1111
      byte[] buf = new byte[4 * 1024];
1112
      // Read data from file input stream to byte array
1113
      int b = fin.read(buf);
1114
      // Write to outStream from byte array
1115
      while (b != -1)
1116
      {
1117
        outPut.write(buf, 0, b);
1118
        b = fin.read(buf);
1119
      }
1120
      // close file input stream
1121
      fin.close();
1122

    
1123
   }//try
1124
   catch(Exception e)
1125
   {
1126
      System.out.println("error getting data file from MetacatReplication." +
1127
                         "handlGetDataFileRequest " + e.getMessage());
1128
      e.printStackTrace(System.out);
1129
   }//catch
1130

    
1131
}
1132

    
1133

    
1134
  /**
1135
   * Sends a document to a remote host
1136
   */
1137
  private void handleGetDocumentRequest(PrintWriter out, Hashtable params,
1138
                                        HttpServletResponse response)
1139
  {
1140

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

    
1159
      String docid = ((String[])(params.get("docid")))[0];
1160

    
1161
      DocumentImpl di = new DocumentImpl(docid);
1162
      response.setContentType("text/xml");
1163
      out.print(di.toString(null, null, true));
1164

    
1165
      MetacatReplication.replLog("document " + docid + " sent");
1166

    
1167
    }
1168
    catch(Exception e)
1169
    {
1170
      logMetacat.error("error getting document from MetacatReplication."
1171
                          +"handlGetDocumentRequest " + e.getMessage());
1172
      //e.printStackTrace(System.out);
1173
      response.setContentType("text/xml");
1174
      out.println("<error>"+e.getMessage()+"</error>");
1175
    }
1176

    
1177
  }
1178

    
1179
  /**
1180
   * Sends a list of all of the documents on this sever along with their
1181
   * revision numbers.
1182
   * The format is:
1183
   * <!ELEMENT replication (server, updates)>
1184
   * <!ELEMENT server (#PCDATA)>
1185
   * <!ELEMENT updates ((updatedDocument | deleteDocument | revisionDocument)*)>
1186
   * <!ELEMENT updatedDocument (docid, rev, datafile*)>
1187
   * <!ELEMENT deletedDocument (docid, rev)>
1188
   * <!ELEMENT revisionDocument (docid, rev, datafile*)>
1189
   * <!ELEMENT docid (#PCDATA)>
1190
   * <!ELEMENT rev (#PCDATA)>
1191
   * <!ELEMENT datafile (#PCDATA)>
1192
   * note that the rev in deletedDocument is always empty.  I just left
1193
   * it in there to make the parser implementation easier.
1194
   */
1195
  private void handleUpdateRequest(PrintWriter out, Hashtable params,
1196
                                    HttpServletResponse response)
1197
  {
1198
    // Checked out DBConnection
1199
    DBConnection dbConn = null;
1200
    // DBConenction serial number when checked it out
1201
    int serialNumber = -1;
1202
    PreparedStatement pstmt = null;
1203
    // Server list to store server info of xml_replication table
1204
    ReplicationServerList serverList = null;
1205

    
1206
    try
1207
    {
1208
      // Check out a DBConnection from pool
1209
      dbConn=DBConnectionPool.
1210
                  getDBConnection("MetacatReplication.handleUpdateRequest");
1211
      serialNumber=dbConn.getCheckOutSerialNumber();
1212
      // Create a server list from xml_replication table
1213
      serverList = new ReplicationServerList();
1214

    
1215
      // Get remote server name from param
1216
      String server = ((String[])params.get("server"))[0];
1217
      // If no servr name in param, return a error
1218
      if ( server == null || server.equals(""))
1219
      {
1220
        response.setContentType("text/xml");
1221
        out.println("<error>Request didn't specify server name</error>");
1222
        out.close();
1223
        return;
1224
      }//if
1225

    
1226
      //try to open a https stream to test if the request server's public key
1227
      //in the key store, this is security issue
1228
      URL u = new URL("https://" + server + "?server="
1229
                +MetaCatUtil.getLocalReplicationServerName()
1230
                +"&action=test");
1231
      String test = MetacatReplication.getURLContent(u);
1232
      //couldn't pass the test
1233
      if (test.indexOf("successfully")==-1)
1234
      {
1235
        response.setContentType("text/xml");
1236
        out.println("<error>Couldn't pass the trust test</error>");
1237
        out.close();
1238
        return;
1239
      }
1240

    
1241

    
1242
      // Check if local host configure to replicate xml documents to remote
1243
      // server. If not send back a error message
1244
      if (!serverList.getReplicationValue(server))
1245
      {
1246
        response.setContentType("text/xml");
1247
        out.println
1248
        ("<error>Configuration not allow to replicate document to you</error>");
1249
        out.close();
1250
        return;
1251
      }//if
1252

    
1253
      // Store the sql command
1254
      StringBuffer docsql = new StringBuffer();
1255
      StringBuffer revisionSql = new StringBuffer();
1256
      // Stroe the docid list
1257
      StringBuffer doclist = new StringBuffer();
1258
      // Store the deleted docid list
1259
      StringBuffer delsql = new StringBuffer();
1260
      // Store the data set file
1261
      Vector packageFiles = new Vector();
1262

    
1263
      // Append local server's name and replication servlet to doclist
1264
      doclist.append("<?xml version=\"1.0\"?><replication>");
1265
      doclist.append("<server>").append(MetaCatUtil.getLocalReplicationServerName());
1266
      //doclist.append(util.getProperty("replicationpath"));
1267
      doclist.append("</server><updates>");
1268

    
1269
      // Get correct docid that reside on this server according the requesting
1270
      // server's replicate and data replicate value in xml_replication table
1271
      docsql.append(DatabaseService.getDBAdapter().getReplicationDocumentListSQL());
1272
      //docsql.append("select docid, rev, doctype from xml_documents where (docid not in (select a.docid from xml_documents a, xml_revisions b where a.docid=b.docid and a.rev<=b.rev)) ");
1273
      revisionSql.append("select docid, rev, doctype from xml_revisions ");
1274
      // If the localhost is not a hub to the remote server, only replicate
1275
      // the docid' which home server is local host (server_location =1)
1276
      if (!serverList.getHubValue(server))
1277
      {
1278
    	String serverLocationDoc = " and a.server_location = 1";
1279
        String serverLocationRev = "where server_location = 1";
1280
        docsql.append(serverLocationDoc);
1281
        revisionSql.append(serverLocationRev);
1282
      }
1283
      logMetacat.info("Doc sql: "+docsql.toString());
1284

    
1285
      // Get any deleted documents
1286
      delsql.append("select distinct docid from ");
1287
      delsql.append("xml_revisions where docid not in (select docid from ");
1288
      delsql.append("xml_documents) ");
1289
      // If the localhost is not a hub to the remote server, only replicate
1290
      // the docid' which home server is local host (server_location =1)
1291
      if (!serverList.getHubValue(server))
1292
      {
1293
        delsql.append("and server_location = 1");
1294
      }
1295
      logMetacat.info("Deleted sql: "+delsql.toString());
1296

    
1297

    
1298

    
1299
      // Get docid list of local host
1300
      pstmt = dbConn.prepareStatement(docsql.toString());
1301
      pstmt.execute();
1302
      ResultSet rs = pstmt.getResultSet();
1303
      boolean tablehasrows = rs.next();
1304
      //If metacat configed to replicate data file
1305
      //if ((util.getProperty("replicationsenddata")).equals("on"))
1306
      boolean replicateData = serverList.getDataReplicationValue(server);
1307
      if (replicateData)
1308
      {
1309
        while(tablehasrows)
1310
        {
1311
          String recordDoctype = rs.getString(3);
1312
          Vector packagedoctypes = MetaCatUtil.getOptionList(
1313
                                     PropertyService.getProperty("xml.packagedoctype"));
1314
          //if this is a package file, put it at the end
1315
          //because if a package file is read before all of the files it
1316
          //refers to are loaded then there is an error
1317
          if(recordDoctype != null && !packagedoctypes.contains(recordDoctype))
1318
          {
1319
              //If this is not data file
1320
              if (!recordDoctype.equals("BIN"))
1321
              {
1322
                //for non-data file document
1323
                doclist.append("<updatedDocument>");
1324
                doclist.append("<docid>").append(rs.getString(1));
1325
                doclist.append("</docid><rev>").append(rs.getInt(2));
1326
                doclist.append("</rev>");
1327
                doclist.append("</updatedDocument>");
1328
              }//if
1329
              else
1330
              {
1331
                //for data file document, in datafile attributes
1332
                //we put "datafile" value there
1333
                doclist.append("<updatedDocument>");
1334
                doclist.append("<docid>").append(rs.getString(1));
1335
                doclist.append("</docid><rev>").append(rs.getInt(2));
1336
                doclist.append("</rev>");
1337
                doclist.append("<datafile>");
1338
                doclist.append(PropertyService.getProperty("replication.datafileflag"));
1339
                doclist.append("</datafile>");
1340
                doclist.append("</updatedDocument>");
1341
              }//else
1342
          }//if packagedoctpes
1343
          else
1344
          { //the package files are saved to be put into the xml later.
1345
              Vector v = new Vector();
1346
              v.add(new String(rs.getString(1)));
1347
              v.add(new Integer(rs.getInt(2)));
1348
              packageFiles.add(new Vector(v));
1349
          }//esle
1350
          tablehasrows = rs.next();
1351
        }//while
1352
      }//if
1353
      else //metacat was configured not to send data file
1354
      {
1355
        while(tablehasrows)
1356
        {
1357
          String recordDoctype = rs.getString(3);
1358
          if(!recordDoctype.equals("BIN"))
1359
          { //don't replicate data files
1360
            Vector packagedoctypes = MetaCatUtil.getOptionList(
1361
                                     PropertyService.getProperty("xml.packagedoctype"));
1362
            if(recordDoctype != null && !packagedoctypes.contains(recordDoctype))
1363
            {   //if this is a package file, put it at the end
1364
              //because if a package file is read before all of the files it
1365
              //refers to are loaded then there is an error
1366
              doclist.append("<updatedDocument>");
1367
              doclist.append("<docid>").append(rs.getString(1));
1368
              doclist.append("</docid><rev>").append(rs.getInt(2));
1369
              doclist.append("</rev>");
1370
              doclist.append("</updatedDocument>");
1371
            }
1372
            else
1373
            { //the package files are saved to be put into the xml later.
1374
              Vector v = new Vector();
1375
              v.add(new String(rs.getString(1)));
1376
              v.add(new Integer(rs.getInt(2)));
1377
              packageFiles.add(new Vector(v));
1378
            }
1379
         }//if
1380
         tablehasrows = rs.next();
1381
        }//while
1382
      }//else
1383

    
1384
      pstmt = dbConn.prepareStatement(delsql.toString());
1385
      //usage count should increas 1
1386
      dbConn.increaseUsageCount(1);
1387

    
1388
      pstmt.execute();
1389
      rs = pstmt.getResultSet();
1390
      tablehasrows = rs.next();
1391
      while(tablehasrows)
1392
      { //handle the deleted documents
1393
        doclist.append("<deletedDocument><docid>").append(rs.getString(1));
1394
        doclist.append("</docid><rev></rev></deletedDocument>");
1395
        //note that rev is always empty for deleted docs
1396
        tablehasrows = rs.next();
1397
      }
1398

    
1399
      //now we can put the package files into the xml results
1400
      for(int i=0; i<packageFiles.size(); i++)
1401
      {
1402
        Vector v = (Vector)packageFiles.elementAt(i);
1403
        doclist.append("<updatedDocument>");
1404
        doclist.append("<docid>").append((String)v.elementAt(0));
1405
        doclist.append("</docid><rev>");
1406
        doclist.append(((Integer)v.elementAt(1)).intValue());
1407
        doclist.append("</rev>");
1408
        doclist.append("</updatedDocument>");
1409
      }
1410
      // add revision doc list  
1411
      doclist.append(prepareRevisionDoc(dbConn,revisionSql.toString(),replicateData));
1412
        
1413
      doclist.append("</updates></replication>");
1414
      logMetacat.info("doclist: " + doclist.toString());
1415
      pstmt.close();
1416
      //conn.close();
1417
      response.setContentType("text/xml");
1418
      out.println(doclist.toString());
1419

    
1420
    }
1421
    catch(Exception e)
1422
    {
1423
      logMetacat.error("error in MetacatReplication." +
1424
                         "handleupdaterequest: " + e.getMessage());
1425
      //e.printStackTrace(System.out);
1426
      response.setContentType("text/xml");
1427
      out.println("<error>"+e.getMessage()+"</error>");
1428
    }
1429
    finally
1430
    {
1431
      try
1432
      {
1433
        pstmt.close();
1434
      }//try
1435
      catch (SQLException ee)
1436
      {
1437
        logMetacat.error("Error in MetacatReplication." +
1438
                "handleUpdaterequest to close pstmt: "+ee.getMessage());
1439
      }//catch
1440
      finally
1441
      {
1442
        DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1443
      }//finally
1444
    }//finally
1445

    
1446
  }//handlUpdateRequest
1447
  
1448
  /*
1449
   * This method will get the xml string for document in xml_revision
1450
   * The schema look like <!ELEMENT revisionDocument (docid, rev, datafile*)>
1451
   */
1452
  private String prepareRevisionDoc(DBConnection dbConn, String revSql, 
1453
                            boolean replicateData) throws Exception
1454
  {
1455
      logMetacat.warn("The revision document sql is "+ revSql);
1456
      StringBuffer revDocList = new StringBuffer();
1457
      PreparedStatement pstmt = dbConn.prepareStatement(revSql);
1458
      //usage count should increas 1
1459
      dbConn.increaseUsageCount(1);
1460

    
1461
      pstmt.execute();
1462
      ResultSet rs = pstmt.getResultSet();
1463
      boolean tablehasrows = rs.next();
1464
      while(tablehasrows)
1465
      {
1466
        String recordDoctype = rs.getString(3);
1467
        
1468
        //If this is data file and it isn't configured to replicate data
1469
        if (recordDoctype.equals("BIN") && !replicateData)
1470
        {  
1471
            // do nothing
1472
            continue;
1473
        }
1474
        else
1475
        {  
1476
            
1477
            revDocList.append("<revisionDocument>");
1478
            revDocList.append("<docid>").append(rs.getString(1));
1479
            revDocList.append("</docid><rev>").append(rs.getInt(2));
1480
            revDocList.append("</rev>");
1481
            // data file
1482
            if (recordDoctype.equals("BIN"))
1483
            {
1484
                revDocList.append("<datafile>");
1485
                revDocList.append(PropertyService.getProperty("replication.datafileflag"));
1486
                revDocList.append("</datafile>");
1487
            }
1488
            revDocList.append("</revisionDocument>");
1489
        
1490
         }//else
1491
         tablehasrows = rs.next();
1492
      }
1493
      //System.out.println("The revision list is"+ revDocList.toString());
1494
      return revDocList.toString();
1495
  }
1496

    
1497
  /**
1498
   * Returns the xml_catalog table encoded in xml
1499
   */
1500
  public static String getCatalogXML()
1501
  {
1502
    return handleGetCatalogRequest(null, null, null, false);
1503
  }
1504

    
1505
  /**
1506
   * Sends the contents of the xml_catalog table encoded in xml
1507
   * The xml format is:
1508
   * <!ELEMENT xml_catalog (row*)>
1509
   * <!ELEMENT row (entry_type, source_doctype, target_doctype, public_id,
1510
   *                system_id)>
1511
   * All of the sub elements of row are #PCDATA
1512

    
1513
   * If printFlag == false then do not print to out.
1514
   */
1515
  private static String handleGetCatalogRequest(PrintWriter out,
1516
                                                Hashtable params,
1517
                                                HttpServletResponse response,
1518
                                                boolean printFlag)
1519
  {
1520
    DBConnection dbConn = null;
1521
    int serialNumber = -1;
1522
    PreparedStatement pstmt = null;
1523
    try
1524
    {
1525
      /*conn = MetacatReplication.getDBConnection("MetacatReplication." +
1526
                                                "handleGetCatalogRequest");*/
1527
      dbConn=DBConnectionPool.
1528
                 getDBConnection("MetacatReplication.handleGetCatalogRequest");
1529
      serialNumber=dbConn.getCheckOutSerialNumber();
1530
      pstmt = dbConn.prepareStatement("select entry_type, " +
1531
                              "source_doctype, target_doctype, public_id, " +
1532
                              "system_id from xml_catalog");
1533
      pstmt.execute();
1534
      ResultSet rs = pstmt.getResultSet();
1535
      boolean tablehasrows = rs.next();
1536
      StringBuffer sb = new StringBuffer();
1537
      sb.append("<?xml version=\"1.0\"?><xml_catalog>");
1538
      while(tablehasrows)
1539
      {
1540
        sb.append("<row><entry_type>").append(rs.getString(1));
1541
        sb.append("</entry_type><source_doctype>").append(rs.getString(2));
1542
        sb.append("</source_doctype><target_doctype>").append(rs.getString(3));
1543
        sb.append("</target_doctype><public_id>").append(rs.getString(4));
1544
        // system id may not have server url on front.  Add it if not.
1545
        String systemID = rs.getString(5);
1546
        if (!systemID.startsWith("http://")) {
1547
        	systemID = SystemUtil.getContextURL() + systemID;
1548
        }
1549
        sb.append("</public_id><system_id>").append(systemID);
1550
        sb.append("</system_id></row>");
1551

    
1552
        tablehasrows = rs.next();
1553
      }
1554
      sb.append("</xml_catalog>");
1555
      //conn.close();
1556
      if(printFlag)
1557
      {
1558
        response.setContentType("text/xml");
1559
        out.println(sb.toString());
1560
      }
1561
      pstmt.close();
1562
      return sb.toString();
1563
    }
1564
    catch(Exception e)
1565
    {
1566

    
1567
      logMetacat.error("error in MetacatReplication.handleGetCatalogRequest:"+
1568
                          e.getMessage());
1569
      e.printStackTrace(System.out);
1570
      if(printFlag)
1571
      {
1572
        out.println("<error>"+e.getMessage()+"</error>");
1573
      }
1574
    }
1575
    finally
1576
    {
1577
      try
1578
      {
1579
        pstmt.close();
1580
      }//try
1581
      catch (SQLException ee)
1582
      {
1583
        logMetacat.error("Error in MetacatReplication.handleGetCatalogRequest: "
1584
           +ee.getMessage());
1585
      }//catch
1586
      finally
1587
      {
1588
        DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1589
      }//finally
1590
    }//finally
1591

    
1592
    return null;
1593
  }
1594

    
1595
  /**
1596
   * Sends the current system date to the remote server.  Using this action
1597
   * for replication gets rid of any problems with syncronizing clocks
1598
   * because a time specific to a document is always kept on its home server.
1599
   */
1600
  private void handleGetTimeRequest(PrintWriter out, Hashtable params,
1601
                                    HttpServletResponse response)
1602
  {
1603
    SimpleDateFormat formatter = new SimpleDateFormat ("MM/dd/yy HH:mm:ss");
1604
    java.util.Date localtime = new java.util.Date();
1605
    String dateString = formatter.format(localtime);
1606
    response.setContentType("text/xml");
1607

    
1608
    out.println("<timestamp>" + dateString + "</timestamp>");
1609
  }
1610

    
1611
  /**
1612
   * this method handles the timeout for a file lock.  when a lock is
1613
   * granted it is granted for 30 seconds.  When this thread runs out
1614
   * it deletes the docid from the queue, thus eliminating the lock.
1615
   */
1616
  public void run()
1617
  {
1618
    try
1619
    {
1620
      logMetacat.info("thread started for docid: " +
1621
                               (String)fileLocks.elementAt(0));
1622

    
1623
      Thread.sleep(30000); //the lock will expire in 30 seconds
1624
      logMetacat.info("thread for docid: " +
1625
                             (String)fileLocks.elementAt(fileLocks.size() - 1) +
1626
                              " exiting.");
1627

    
1628
      fileLocks.remove(fileLocks.size() - 1);
1629
      //fileLocks is treated as a FIFO queue.  If there are more than one lock
1630
      //in the vector, the first one inserted will be removed.
1631
    }
1632
    catch(Exception e)
1633
    {
1634
      logMetacat.error("error in file lock thread from " +
1635
                                "MetacatReplication.run: " + e.getMessage());
1636
    }
1637
  }
1638

    
1639
  /**
1640
   * Returns the name of a server given a serverCode
1641
   * @param serverCode the serverid of the server
1642
   * @return the servername or null if the specified serverCode does not
1643
   *         exist.
1644
   */
1645
  public static String getServerNameForServerCode(int serverCode)
1646
  {
1647
    //System.out.println("serverid: " + serverCode);
1648
    DBConnection dbConn = null;
1649
    int serialNumber = -1;
1650
    PreparedStatement pstmt = null;
1651
    try
1652
    {
1653
      dbConn=DBConnectionPool.
1654
                  getDBConnection("MetacatReplication.getServer");
1655
      serialNumber=dbConn.getCheckOutSerialNumber();
1656
      String sql = new String("select server from " +
1657
                              "xml_replication where serverid = " +
1658
                              serverCode);
1659
      pstmt = dbConn.prepareStatement(sql);
1660
      //System.out.println("getserver sql: " + sql);
1661
      pstmt.execute();
1662
      ResultSet rs = pstmt.getResultSet();
1663
      boolean tablehasrows = rs.next();
1664
      if(tablehasrows)
1665
      {
1666
        //System.out.println("server: " + rs.getString(1));
1667
        return rs.getString(1);
1668
      }
1669

    
1670
      //conn.close();
1671
    }
1672
    catch(Exception e)
1673
    {
1674
      System.out.println("Error in MetacatReplication.getServer: " +
1675
                          e.getMessage());
1676
    }
1677
    finally
1678
    {
1679
      try
1680
      {
1681
        pstmt.close();
1682
      }//try
1683
      catch (SQLException ee)
1684
      {
1685
        logMetacat.error("Error in MetacactReplication.getserver: "+
1686
                                    ee.getMessage());
1687
      }//catch
1688
      finally
1689
      {
1690
        DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1691
      }//fianlly
1692
    }//finally
1693

    
1694

    
1695

    
1696
    return null;
1697
      //return null if the server does not exist
1698
  }
1699

    
1700
  /**
1701
   * Returns a server code given a server name
1702
   * @param server the name of the server
1703
   * @return integer > 0 representing the code of the server, 0 if the server
1704
   *  does not exist.
1705
   */
1706
  public static int getServerCodeForServerName(String server) throws Exception
1707
  {
1708
    DBConnection dbConn = null;
1709
    int serialNumber = -1;
1710
    PreparedStatement pstmt = null;
1711
    int serverCode = 0;
1712

    
1713
    try {
1714

    
1715
      //conn = util.openDBConnection();
1716
      dbConn=DBConnectionPool.
1717
                  getDBConnection("MetacatReplication.getServerCode");
1718
      serialNumber=dbConn.getCheckOutSerialNumber();
1719
      pstmt = dbConn.prepareStatement("SELECT serverid FROM xml_replication " +
1720
                                    "WHERE server LIKE '" + server + "'");
1721
      pstmt.execute();
1722
      ResultSet rs = pstmt.getResultSet();
1723
      boolean tablehasrows = rs.next();
1724
      if ( tablehasrows ) {
1725
        serverCode = rs.getInt(1);
1726
        pstmt.close();
1727
        //conn.close();
1728
        return serverCode;
1729
      }
1730

    
1731
    } catch(Exception e) {
1732
      throw e;
1733

    
1734
    } finally {
1735
      try
1736
      {
1737
        pstmt.close();
1738
        //conn.close();
1739
       }//try
1740
       catch(Exception ee)
1741
       {
1742
         logMetacat.error("Error in MetacatReplicatio.getServerCode: "
1743
                                  +ee.getMessage());
1744

    
1745
       }//catch
1746
       finally
1747
       {
1748
         DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1749
       }//finally
1750
    }//finally
1751

    
1752
    return serverCode;
1753
  }
1754

    
1755
  /**
1756
   * Method to get a host server information for given docid
1757
   * @param conn a connection to the database
1758
   */
1759
  public static Hashtable getHomeServerInfoForDocId(String docId)
1760
  {
1761
    Hashtable sl = new Hashtable();
1762
    DBConnection dbConn = null;
1763
    int serialNumber = -1;
1764
    docId=MetaCatUtil.getDocIdFromString(docId);
1765
    PreparedStatement pstmt=null;
1766
    int serverLocation;
1767
    try
1768
    {
1769
      //get conection
1770
      dbConn=DBConnectionPool.
1771
                  getDBConnection("ReplicationHandler.getHomeServer");
1772
      serialNumber=dbConn.getCheckOutSerialNumber();
1773
      //get a server location from xml_document table
1774
      pstmt=dbConn.prepareStatement("select server_location from xml_documents "
1775
                                            +"where docid = ?");
1776
      pstmt.setString(1, docId);
1777
      pstmt.execute();
1778
      ResultSet serverName = pstmt.getResultSet();
1779
      //get a server location
1780
      if(serverName.next())
1781
      {
1782
        serverLocation=serverName.getInt(1);
1783
        pstmt.close();
1784
      }
1785
      else
1786
      {
1787
        pstmt.close();
1788
        //ut.returnConnection(conn);
1789
        return null;
1790
      }
1791
      pstmt=dbConn.prepareStatement("select server, last_checked, replicate " +
1792
                        "from xml_replication where serverid = ?");
1793
      //increase usage count
1794
      dbConn.increaseUsageCount(1);
1795
      pstmt.setInt(1, serverLocation);
1796
      pstmt.execute();
1797
      ResultSet rs = pstmt.getResultSet();
1798
      boolean tableHasRows = rs.next();
1799
      if (tableHasRows)
1800
      {
1801

    
1802
          String server = rs.getString(1);
1803
          String last_checked = rs.getString(2);
1804
          if(!server.equals("localhost"))
1805
          {
1806
            sl.put(server, last_checked);
1807
          }
1808

    
1809
      }
1810
      else
1811
      {
1812
        pstmt.close();
1813
        //ut.returnConnection(conn);
1814
        return null;
1815
      }
1816
      pstmt.close();
1817
    }
1818
    catch(Exception e)
1819
    {
1820
      System.out.println("error in replicationHandler.getHomeServer(): " +
1821
                         e.getMessage());
1822
    }
1823
    finally
1824
    {
1825
      try
1826
      {
1827
        pstmt.close();
1828
        //ut.returnConnection(conn);
1829
      }
1830
      catch (Exception ee)
1831
      {
1832
        logMetacat.error("Eror irn rplicationHandler.getHomeServer() "+
1833
                          "to close pstmt: "+ee.getMessage());
1834
      }
1835
      finally
1836
      {
1837
        DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1838
      }
1839

    
1840
    }//finally
1841
    return sl;
1842
  }
1843

    
1844
  /**
1845
   * Returns a home server location  given a accnum
1846
   * @param accNum , given accNum for a document
1847
   *
1848
   */
1849
  public static int getHomeServerCodeForDocId(String accNum) throws Exception
1850
  {
1851
    DBConnection dbConn = null;
1852
    int serialNumber = -1;
1853
    PreparedStatement pstmt = null;
1854
    int serverCode = 1;
1855
    String docId=MetaCatUtil.getDocIdFromString(accNum);
1856

    
1857
    try
1858
    {
1859

    
1860
      // Get DBConnection
1861
      dbConn=DBConnectionPool.
1862
                  getDBConnection("ReplicationHandler.getServerLocation");
1863
      serialNumber=dbConn.getCheckOutSerialNumber();
1864
      pstmt=dbConn.prepareStatement("SELECT server_location FROM xml_documents "
1865
                              + "WHERE docid LIKE '" + docId + "'");
1866
      pstmt.execute();
1867
      ResultSet rs = pstmt.getResultSet();
1868
      boolean tablehasrows = rs.next();
1869
      //If a document is find, return the server location for it
1870
      if ( tablehasrows )
1871
      {
1872
        serverCode = rs.getInt(1);
1873
        pstmt.close();
1874
        //conn.close();
1875
        return serverCode;
1876
      }
1877
      //if couldn't find in xml_documents table, we think server code is 1
1878
      //(this is new document)
1879
      else
1880
      {
1881
        pstmt.close();
1882
        //conn.close();
1883
        return serverCode;
1884
      }
1885

    
1886
    }
1887
    catch(Exception e)
1888
    {
1889

    
1890
      throw e;
1891

    
1892
    }
1893
    finally
1894
    {
1895
      try
1896
      {
1897
        pstmt.close();
1898
        //conn.close();
1899

    
1900
      }
1901
      catch(Exception ee)
1902
      {
1903
        logMetacat.error("Erorr in Replication.getServerLocation "+
1904
                     "to close pstmt"+ee.getMessage());
1905
      }
1906
      finally
1907
      {
1908
        DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1909
      }//finally
1910
    }//finally
1911
   //return serverCode;
1912
  }
1913

    
1914

    
1915

    
1916
  /**
1917
   * This method returns the content of a url
1918
   * @param u the url to return the content from
1919
   * @return a string representing the content of the url
1920
   * @throws java.io.IOException
1921
   */
1922
  public static String getURLContent(URL u) throws java.io.IOException
1923
  {
1924
    char istreamChar;
1925
    int istreamInt;
1926
    logMetacat.info("Before open the stream"+u.toString());
1927
    InputStream input = u.openStream();
1928
    logMetacat.info("Afetr open the stream"+u.toString());
1929
    InputStreamReader istream = new InputStreamReader(input);
1930
    StringBuffer serverResponse = new StringBuffer();
1931
    while((istreamInt = istream.read()) != -1)
1932
    {
1933
      istreamChar = (char)istreamInt;
1934
      serverResponse.append(istreamChar);
1935
    }
1936
    istream.close();
1937
    input.close();
1938

    
1939
    return serverResponse.toString();
1940
  }
1941

    
1942
  /**
1943
	 * Method for writing replication messages to a log file specified in
1944
	 * metacat.properties
1945
	 */
1946
	public static void replLog(String message) {
1947
		try {
1948
			FileOutputStream fos = 
1949
				new FileOutputStream(PropertyService.getProperty("replication.logdir")
1950
					+ "/metacatreplication.log", true);
1951
			PrintWriter pw = new PrintWriter(fos);
1952
			SimpleDateFormat formatter = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
1953
			java.util.Date localtime = new java.util.Date();
1954
			String dateString = formatter.format(localtime);
1955
			dateString += " :: " + message;
1956
			// time stamp each entry
1957
			pw.println(dateString);
1958
			pw.flush();
1959
		} catch (Exception e) {
1960
			System.out.println("error writing to replication log from "
1961
					+ "MetacatReplication.replLog: " + e.getMessage());
1962
			// e.printStackTrace(System.out);
1963
		}
1964
	}
1965

    
1966
  /**
1967
	 * Method for writing replication messages to a log file specified in
1968
	 * metacat.properties
1969
	 */
1970
  public static void replErrorLog(String message)
1971
  {
1972
    try
1973
    {
1974
    	FileOutputStream fos = 
1975
			new FileOutputStream(PropertyService.getProperty("replication.logdir")
1976
				+ "/metacatreplicationerror.log", true);
1977
      PrintWriter pw = new PrintWriter(fos);
1978
      SimpleDateFormat formatter = new SimpleDateFormat ("yy-MM-dd HH:mm:ss");
1979
      java.util.Date localtime = new java.util.Date();
1980
      String dateString = formatter.format(localtime);
1981
      dateString += " :: " + message;
1982
      //time stamp each entry
1983
      pw.println(dateString);
1984
      pw.flush();
1985
    }
1986
    catch(Exception e)
1987
    {
1988
      System.out.println("error writing to replication error log from " +
1989
                         "MetacatReplication.replErrorLog: " + e.getMessage());
1990
      //e.printStackTrace(System.out);
1991
    }
1992
  }
1993

    
1994
  /**
1995
   * Returns true if the replicate field for server in xml_replication is 1.
1996
   * Returns false otherwise
1997
   */
1998
  public static boolean replToServer(String server)
1999
  {
2000
    DBConnection dbConn = null;
2001
    int serialNumber = -1;
2002
    PreparedStatement pstmt = null;
2003
    try
2004
    {
2005
      dbConn=DBConnectionPool.
2006
                  getDBConnection("MetacatReplication.repltoServer");
2007
      serialNumber=dbConn.getCheckOutSerialNumber();
2008
      pstmt = dbConn.prepareStatement("select replicate from " +
2009
                                    "xml_replication where server like '" +
2010
                                     server + "'");
2011
      pstmt.execute();
2012
      ResultSet rs = pstmt.getResultSet();
2013
      boolean tablehasrows = rs.next();
2014
      if(tablehasrows)
2015
      {
2016
        int i = rs.getInt(1);
2017
        if(i == 1)
2018
        {
2019
          pstmt.close();
2020
          //conn.close();
2021
          return true;
2022
        }
2023
        else
2024
        {
2025
          pstmt.close();
2026
          //conn.close();
2027
          return false;
2028
        }
2029
      }
2030
    }
2031
    catch(Exception e)
2032
    {
2033
      System.out.println("error in MetacatReplication.replToServer: " +
2034
                         e.getMessage());
2035
    }
2036
    finally
2037
    {
2038
      try
2039
      {
2040
        pstmt.close();
2041
        //conn.close();
2042
      }//try
2043
      catch(Exception ee)
2044
      {
2045
        logMetacat.error("Error in MetacatReplication.replToServer: "
2046
                                  +ee.getMessage());
2047
      }//catch
2048
      finally
2049
      {
2050
        DBConnectionPool.returnDBConnection(dbConn, serialNumber);
2051
      }//finally
2052
    }//finally
2053
    return false;
2054
    //the default if this server does not exist is to not replicate to it.
2055
  }
2056

    
2057

    
2058
}
(46-46/67)