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

    
27
package edu.ucsb.nceas.metacat.replication;
28

    
29
import java.io.BufferedInputStream;
30
import java.io.BufferedOutputStream;
31
import java.io.File;
32
import java.io.FileInputStream;
33
import java.io.FileNotFoundException;
34
import java.io.FileOutputStream;
35
import java.io.IOException;
36
import java.io.InputStream;
37
import java.io.InputStreamReader;
38
import java.io.OutputStream;
39
import java.io.StringReader;
40
import java.io.Writer;
41
import java.net.MalformedURLException;
42
import java.net.URL;
43
import java.sql.PreparedStatement;
44
import java.sql.ResultSet;
45
import java.sql.SQLException;
46
import java.text.SimpleDateFormat;
47
import java.util.Date;
48
import java.util.Enumeration;
49
import java.util.Hashtable;
50
import java.util.Timer;
51
import java.util.Vector;
52

    
53
import javax.servlet.http.HttpServletRequest;
54
import javax.servlet.http.HttpServletResponse;
55

    
56
import org.apache.log4j.Logger;
57
import org.xml.sax.InputSource;
58
import org.xml.sax.SAXException;
59
import org.xml.sax.XMLReader;
60

    
61
import edu.ucsb.nceas.metacat.DocInfoHandler;
62
import edu.ucsb.nceas.metacat.DocumentImpl;
63
import edu.ucsb.nceas.metacat.DocumentImplWrapper;
64
import edu.ucsb.nceas.metacat.EventLog;
65
import edu.ucsb.nceas.metacat.IdentifierManager;
66
import edu.ucsb.nceas.metacat.McdbDocNotFoundException;
67
import edu.ucsb.nceas.metacat.McdbException;
68
import edu.ucsb.nceas.metacat.accesscontrol.AccessControlException;
69
import edu.ucsb.nceas.metacat.accesscontrol.AccessControlForSingleFile;
70
import edu.ucsb.nceas.metacat.accesscontrol.PermOrderException;
71
import edu.ucsb.nceas.metacat.accesscontrol.XMLAccessDAO;
72
import edu.ucsb.nceas.metacat.client.InsufficientKarmaException;
73
import edu.ucsb.nceas.metacat.database.DBConnection;
74
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
75
import edu.ucsb.nceas.metacat.database.DatabaseService;
76
import edu.ucsb.nceas.metacat.properties.PropertyService;
77
import edu.ucsb.nceas.metacat.shared.BaseService;
78
import edu.ucsb.nceas.metacat.shared.HandlerException;
79
import edu.ucsb.nceas.metacat.shared.ServiceException;
80
import edu.ucsb.nceas.metacat.util.DocumentUtil;
81
import edu.ucsb.nceas.metacat.util.MetacatUtil;
82
import edu.ucsb.nceas.metacat.util.SystemUtil;
83
import edu.ucsb.nceas.utilities.FileUtil;
84
import edu.ucsb.nceas.utilities.GeneralPropertyException;
85
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
86

    
87
public class ReplicationService extends BaseService {
88

    
89
	private static ReplicationService replicationService = null;
90

    
91
	private long timeInterval;
92
	private Date firstTime;
93
	private boolean timedReplicationIsOn = false;
94
	Timer replicationDaemon;
95
	private static Vector<String> fileLocks = new Vector<String>();
96
//	private Thread lockThread = null;
97
	public static final String FORCEREPLICATEDELETE = "forcereplicatedelete";
98
	private static String TIMEREPLICATION = "replication.timedreplication";
99
	private static String TIMEREPLICATIONINTERVAl ="replication.timedreplicationinterval";
100
	private static String FIRSTTIME = "replication.firsttimedreplication";
101
	private static final int TIMEINTERVALLIMIT = 7200000;
102
	public static final String REPLICATIONUSER = "replication";
103

    
104
	public static final String REPLICATION_LOG_FILE_NAME = "metacatreplication.log";
105
	public static String METACAT_REPL_ERROR_MSG = null;
106
	private static Logger logReplication = Logger.getLogger("ReplicationLogging");
107
	private static Logger logMetacat = Logger.getLogger(ReplicationService.class);
108

    
109
	private ReplicationService() throws ServiceException {
110
		_serviceName = "ReplicationService";
111
		
112
		initialize();
113
	}
114
	
115
	private void initialize() throws ServiceException {
116
				
117
		// initialize db connections to handle any update requests
118
		// deltaT = util.getProperty("replication.deltaT");
119
		// the default deltaT can be set from metacat.properties
120
		// create a thread to do the delta-T check but don't execute it yet
121
		replicationDaemon = new Timer(true);
122
		try {
123
			String replLogFile = PropertyService.getProperty("replication.logdir")
124
				+ FileUtil.getFS() + REPLICATION_LOG_FILE_NAME;
125
			METACAT_REPL_ERROR_MSG = "An error occurred in replication.  Please see the " +
126
				"replication log at: " + replLogFile;
127
			
128
			String timedRepIsOnStr = 
129
				PropertyService.getProperty("replication.timedreplication");
130
			timedReplicationIsOn = (new Boolean(timedRepIsOnStr)).booleanValue();
131
			logReplication.info("ReplicationService.initialize - The timed replication on is" + timedReplicationIsOn);
132

    
133
			String timeIntervalStr = 
134
				PropertyService.getProperty("replication.timedreplicationinterval");
135
			timeInterval = (new Long(timeIntervalStr)).longValue();
136
			logReplication.info("ReplicationService.initialize - The timed replication time Interval is " + timeInterval);
137

    
138
			String firstTimeStr = 
139
				PropertyService.getProperty("replication.firsttimedreplication");
140
			logReplication.info("ReplicationService.initialize - first replication time form property is " + firstTimeStr);
141
			firstTime = ReplicationHandler.combinateCurrentDateAndGivenTime(firstTimeStr);
142

    
143
			logReplication.info("ReplicationService.initialize - After combine current time, the real first time is "
144
					+ firstTime.toString() + " minisec");
145

    
146
			// set up time replication if it is on
147
			if (timedReplicationIsOn) {
148
				replicationDaemon.scheduleAtFixedRate(new ReplicationHandler(),
149
						firstTime, timeInterval);
150
				logReplication.info("ReplicationService.initialize - deltaT handler started with rate="
151
						+ timeInterval + " mini seconds at " + firstTime.toString());
152
			}
153

    
154
		} catch (PropertyNotFoundException pnfe) {
155
			throw new ServiceException(
156
					"ReplicationService.initialize - Property error while instantiating "
157
							+ "replication service: " + pnfe.getMessage());
158
		} catch (HandlerException he) {
159
			throw new ServiceException(
160
					"ReplicationService.initialize - Handler error while instantiating "
161
							+ "replication service" + he.getMessage());
162
		} 
163
	}
164

    
165
	/**
166
	 * Get the single instance of SessionService.
167
	 * 
168
	 * @return the single instance of SessionService
169
	 */
170
	public static ReplicationService getInstance() throws ServiceException {
171
		if (replicationService == null) {
172
			replicationService = new ReplicationService();
173
		}
174
		return replicationService;
175
	}
176

    
177
	public boolean refreshable() {
178
		return true;
179
	}
180

    
181
	protected void doRefresh() throws ServiceException {
182
		return;
183
	}
184
	
185
	public void stop() throws ServiceException{
186
		
187
	}
188

    
189
	public void stopReplication() throws ServiceException {
190
	      //stop the replication server
191
	      replicationDaemon.cancel();
192
	      replicationDaemon = new Timer(true);
193
	      timedReplicationIsOn = false;
194
	      try {
195
	    	  PropertyService.setProperty("replication.timedreplication", (new Boolean(timedReplicationIsOn)).toString());
196
	      } catch (GeneralPropertyException gpe) {
197
	    	  logReplication.warn("ReplicationService.stopReplication - Could not set replication.timedreplication property: " + gpe.getMessage());
198
	      }
199

    
200
	      logReplication.info("ReplicationService.stopReplication - deltaT handler stopped");
201
		return;
202
	}
203
	
204
	protected void startReplication(Hashtable<String, String[]> params) throws ServiceException {
205

    
206
	       String firstTimeStr = "";
207
	      //start the replication server
208
	       if ( params.containsKey("rate") ) {
209
	        timeInterval = new Long(
210
	               new String(((String[])params.get("rate"))[0])).longValue();
211
	        if(timeInterval < TIMEINTERVALLIMIT) {
212
	            //deltaT<30 is a timing mess!
213
	            timeInterval = TIMEINTERVALLIMIT;
214
	            throw new ServiceException("Replication deltaT rate cannot be less than "+
215
	                    TIMEINTERVALLIMIT + " millisecs and system automatically setup the rate to "+TIMEINTERVALLIMIT);
216
	        }
217
	      } else {
218
	        timeInterval = TIMEINTERVALLIMIT ;
219
	      }
220
	      logReplication.info("ReplicationService.startReplication - New rate is: " + timeInterval + " mini seconds.");
221
	      if ( params.containsKey("firsttime"))
222
	      {
223
	         firstTimeStr = ((String[])params.get("firsttime"))[0];
224
	         try
225
	         {
226
	           firstTime = ReplicationHandler.combinateCurrentDateAndGivenTime(firstTimeStr);
227
	           logReplication.info("ReplicationService.startReplication - The first time setting is "+firstTime.toString());
228
	         }
229
	         catch (HandlerException e)
230
	         {
231
	            throw new ServiceException(e.getMessage());
232
	         }
233
	         logReplication.warn("After combine current time, the real first time is "
234
	                                  +firstTime.toString()+" minisec");
235
	      }
236
	      else
237
	      {
238
	    	  logMetacat.error("ReplicationService.startReplication - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
239
	          logReplication.error("ReplicationService.startReplication - You should specify the first time " +
240
	                                  "to start a time replication");
241
	          return;
242
	      }
243
	      
244
	      timedReplicationIsOn = true;
245
	      try {
246
	      // save settings to property file
247
	      PropertyService.setProperty(TIMEREPLICATION, (new Boolean(timedReplicationIsOn)).toString());
248
	      // note we couldn't use firstTime object because it has date info
249
	      // we only need time info such as 10:00 PM
250
	      PropertyService.setProperty(FIRSTTIME, firstTimeStr);
251
	      PropertyService.setProperty(TIMEREPLICATIONINTERVAl, (new Long(timeInterval)).toString());
252
	      } catch (GeneralPropertyException gpe) {
253
	    	  logReplication.warn("ReplicationService.startReplication - Could not set property: " + gpe.getMessage());
254
	      }
255
	      replicationDaemon.cancel();
256
	      replicationDaemon = new Timer(true);
257
	      replicationDaemon.scheduleAtFixedRate(new ReplicationHandler(), firstTime,
258
	                                            timeInterval);
259
	      
260
	      logReplication.info("ReplicationService.startReplication - deltaT handler started with rate=" +
261
	                                    timeInterval + " milliseconds at " +firstTime.toString());
262

    
263
	}
264
	
265
	public void runOnce() throws ServiceException {
266
	      //updates this server exactly once
267
	      replicationDaemon.schedule(new ReplicationHandler(), 0);
268
	}
269

    
270
	/**
271
	 * This method can add, delete and list the servers currently included in
272
	 * xml_replication.
273
	 * action           subaction            other needed params
274
	 * ---------------------------------------------------------
275
	 * servercontrol    add                  server
276
	 * servercontrol    delete               server
277
	 * servercontrol    list
278
	 */
279
	protected static void handleServerControlRequest(
280
			Hashtable<String, String[]> params, HttpServletResponse response) {
281
		String subaction = ((String[]) params.get("subaction"))[0];
282
		DBConnection dbConn = null;
283
		int serialNumber = -1;
284
		PreparedStatement pstmt = null;
285
		String replicate = null;
286
		String server = null;
287
		String dataReplicate = null;
288
		String hub = null;
289
		Writer out = null;
290
		try {
291
			response.setContentType("text/xml");
292
			out = response.getWriter();
293
			
294
			//conn = util.openDBConnection();
295
			dbConn = DBConnectionPool
296
					.getDBConnection("MetacatReplication.handleServerControlRequest");
297
			serialNumber = dbConn.getCheckOutSerialNumber();
298

    
299
			// add server to server list
300
			if (subaction.equals("add")) {
301
				replicate = ((String[]) params.get("replicate"))[0];
302
				server = ((String[]) params.get("server"))[0];
303

    
304
				//Get data replication value
305
				dataReplicate = ((String[]) params.get("datareplicate"))[0];
306
				//Get hub value
307
				hub = ((String[]) params.get("hub"))[0];
308

    
309
				String toDateSql = DatabaseService.getInstance().getDBAdapter().toDate("01/01/1980","MM/DD/YYYY");
310
				String sql = "INSERT INTO xml_replication "
311
						+ "(server, last_checked, replicate, datareplicate, hub) "
312
						+ "VALUES (?," + toDateSql + ",?,?,?)";
313
				
314
				pstmt = dbConn.prepareStatement(sql);
315
						
316
				pstmt.setString(1, server);
317
				pstmt.setInt(2, Integer.parseInt(replicate));
318
				pstmt.setInt(3, Integer.parseInt(dataReplicate));
319
				pstmt.setInt(4, Integer.parseInt(hub));
320
				
321
				String sqlReport = "XMLAccessAccess.getXMLAccessForDoc - SQL: " + sql;
322
				sqlReport += " [" + server + "," + replicate + 
323
					"," + dataReplicate + "," + hub + "]";
324
				
325
				logMetacat.info(sqlReport);
326
				
327
				pstmt.execute();
328
				pstmt.close();
329
				dbConn.commit();
330
				out.write("Server " + server + " added");
331
				response.setContentType("text/html");
332
				out.write("<html><body><table border=\"1\">");
333
				out.write("<tr><td><b>server</b></td><td><b>last_checked</b></td><td>");
334
				out.write("<b>replicate</b></td>");
335
				out.write("<td><b>datareplicate</b></td>");
336
				out.write("<td><b>hub</b></td></tr>");
337
				pstmt = dbConn.prepareStatement("SELECT * FROM xml_replication");
338
				//increase dbconnection usage
339
				dbConn.increaseUsageCount(1);
340

    
341
				pstmt.execute();
342
				ResultSet rs = pstmt.getResultSet();
343
				boolean tablehasrows = rs.next();
344
				while (tablehasrows) {
345
					out.write("<tr><td>" + rs.getString(2) + "</td><td>");
346
					out.write(rs.getString(3) + "</td><td>");
347
					out.write(rs.getString(4) + "</td><td>");
348
					out.write(rs.getString(5) + "</td><td>");
349
					out.write(rs.getString(6) + "</td></tr>");
350

    
351
					tablehasrows = rs.next();
352
				}
353
				out.write("</table></body></html>");
354

    
355
				// download certificate with the public key on this server
356
				// and import it as a trusted certificate
357
				String certURL = ((String[]) params.get("certificate"))[0];
358
				if (certURL != null && !certURL.equals("")) {
359
					downloadCertificate(certURL);
360
				}
361

    
362
				// delete server from server list
363
			} else if (subaction.equals("delete")) {
364
				server = ((String[]) params.get("server"))[0];
365
				pstmt = dbConn.prepareStatement("DELETE FROM xml_replication "
366
						+ "WHERE server LIKE '" + server + "'");
367
				pstmt.execute();
368
				pstmt.close();
369
				dbConn.commit();
370
				out.write("Server " + server + " deleted");
371
				response.setContentType("text/html");
372
				out.write("<html><body><table border=\"1\">");
373
				out.write("<tr><td><b>server</b></td><td><b>last_checked</b></td><td>");
374
				out.write("<b>replicate</b></td>");
375
				out.write("<td><b>datareplicate</b></td>");
376
				out.write("<td><b>hub</b></td></tr>");
377

    
378
				pstmt = dbConn.prepareStatement("SELECT * FROM xml_replication");
379
				//increase dbconnection usage
380
				dbConn.increaseUsageCount(1);
381
				pstmt.execute();
382
				ResultSet rs = pstmt.getResultSet();
383
				boolean tablehasrows = rs.next();
384
				while (tablehasrows) {
385
					out.write("<tr><td>" + rs.getString(2) + "</td><td>");
386
					out.write(rs.getString(3) + "</td><td>");
387
					out.write(rs.getString(4) + "</td><td>");
388
					out.write(rs.getString(5) + "</td><td>");
389
					out.write(rs.getString(6) + "</td></tr>");
390
					tablehasrows = rs.next();
391
				}
392
				out.write("</table></body></html>");
393

    
394
				// list servers in server list
395
			} else if (subaction.equals("list")) {
396
				response.setContentType("text/html");
397
				out.write("<html><body><table border=\"1\">");
398
				out.write("<tr><td><b>server</b></td><td><b>last_checked</b></td><td>");
399
				out.write("<b>replicate</b></td>");
400
				out.write("<td><b>datareplicate</b></td>");
401
				out.write("<td><b>hub</b></td></tr>");
402
				pstmt = dbConn.prepareStatement("SELECT * FROM xml_replication");
403
				pstmt.execute();
404
				ResultSet rs = pstmt.getResultSet();
405
				boolean tablehasrows = rs.next();
406
				while (tablehasrows) {
407
					out.write("<tr><td>" + rs.getString(2) + "</td><td>");
408
					out.write(rs.getString(3) + "</td><td>");
409
					out.write(rs.getString(4) + "</td><td>");
410
					out.write(rs.getString(5) + "</td><td>");
411
					out.write(rs.getString(6) + "</td></tr>");
412
					tablehasrows = rs.next();
413
				}
414
				out.write("</table></body></html>");
415
			} else {
416

    
417
				out.write("<error>Unkonwn subaction</error>");
418

    
419
			}
420
			pstmt.close();
421
			//conn.close();
422

    
423
		} catch (Exception e) {
424
			logMetacat.error("ReplicationService.handleServerControlRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
425
			logReplication.error("ReplicationService.handleServerControlRequest - Error in "
426
					+ "MetacatReplication.handleServerControlRequest " + e.getMessage());
427
			e.printStackTrace(System.out);
428
		} finally {
429
			try {
430
				pstmt.close();
431
			}//try
432
			catch (SQLException ee) {
433
				logMetacat.error("ReplicationService.handleServerControlRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
434
				logReplication.error("ReplicationService.handleServerControlRequest - Error in MetacatReplication.handleServerControlRequest to close pstmt "
435
						+ ee.getMessage());
436
			}//catch
437
			finally {
438
				DBConnectionPool.returnDBConnection(dbConn, serialNumber);
439
			}//finally
440
			if (out != null) {
441
				try {
442
					out.close();
443
				} catch (IOException e) {
444
					logMetacat.error(e.getMessage(), e);
445
				}
446
			}
447
		}//finally
448

    
449
	}
450

    
451
	// download certificate with the public key from certURL and
452
	// upload it onto this server; it then must be imported as a
453
	// trusted certificate
454
	private static void downloadCertificate(String certURL) throws FileNotFoundException,
455
			IOException, MalformedURLException, PropertyNotFoundException {
456

    
457
		// the path to be uploaded to
458
		String certPath = SystemUtil.getContextDir();
459

    
460
		// get filename from the URL of the certificate
461
		String filename = certURL;
462
		int slash = Math.max(filename.lastIndexOf('/'), filename.lastIndexOf('\\'));
463
		if (slash > -1) {
464
			filename = filename.substring(slash + 1);
465
		}
466

    
467
		// open file output strem to write the input into it
468
		File f = new File(certPath, filename);
469
		synchronized (f) {
470
			try {
471
				if (f.exists()) {
472
					throw new IOException("File already exist: " + f.getCanonicalFile());
473
					// if ( f.exists() && !f.canWrite() ) {
474
					// throw new IOException("Not writable: " +
475
					// f.getCanonicalFile());
476
				}
477
			} catch (SecurityException se) {
478
				// if a security manager exists,
479
				// its checkRead method is called for f.exist()
480
				// or checkWrite method is called for f.canWrite()
481
				throw se;
482
			}
483

    
484
			// create a buffered byte output stream
485
			// that uses a default-sized output buffer
486
			FileOutputStream fos = new FileOutputStream(f);
487
			BufferedOutputStream out = new BufferedOutputStream(fos);
488

    
489
			// this should be http url
490
			URL url = new URL(certURL);
491
			BufferedInputStream bis = null;
492
			try {
493
				bis = new BufferedInputStream(url.openStream());
494
				byte[] buf = new byte[4 * 1024]; // 4K buffer
495
				int b = bis.read(buf);
496
				while (b != -1) {
497
					out.write(buf, 0, b);
498
					b = bis.read(buf);
499
				}
500
			} finally {
501
				if (bis != null)
502
					bis.close();
503
			}
504
			// the input and the output streams must be closed
505
			bis.close();
506
			out.flush();
507
			out.close();
508
			fos.close();
509
		} // end of synchronized(f)
510
	}
511

    
512
	/**
513
	 * when a forcereplication request comes in, local host sends a read request
514
	 * to the requesting server (remote server) for the specified docid. Then
515
	 * store it in local database.
516
	 */
517
	protected static void handleForceReplicateRequest(
518
			Hashtable<String, String[]> params, HttpServletResponse response,
519
			HttpServletRequest request) {
520
		String server = ((String[]) params.get("server"))[0]; // the server that
521
		String docid = ((String[]) params.get("docid"))[0]; // sent the document
522
		String dbaction = "UPDATE"; // the default action is UPDATE
523
		//    boolean override = false;
524
		//    int serverCode = 1;
525
		DBConnection dbConn = null;
526
		int serialNumber = -1;
527
		String docName = null;
528

    
529
		try {
530
			//if the url contains a dbaction then the default action is overridden
531
			if (params.containsKey("dbaction")) {
532
				dbaction = ((String[]) params.get("dbaction"))[0];
533
				//serverCode = MetacatReplication.getServerCode(server);
534
				//override = true; //we are now overriding the default action
535
			}
536
			logReplication.info("ReplicationService.handleForceReplicateRequest - Force replication request from: " + server);
537
			logReplication.info("ReplicationService.handleForceReplicateRequest - Force replication docid: " + docid);
538
			logReplication.info("ReplicationService.handleForceReplicateRequest - Force replication action: " + dbaction);
539
			// sending back read request to remote server
540
			URL u = new URL("https://" + server + "?server="
541
					+ MetacatUtil.getLocalReplicationServerName() + "&action=read&docid="
542
					+ docid);
543
			String xmldoc = ReplicationService.getURLContent(u);
544

    
545
			// get the document info from server
546
			URL docinfourl = new URL("https://" + server + "?server="
547
					+ MetacatUtil.getLocalReplicationServerName()
548
					+ "&action=getdocumentinfo&docid=" + docid);
549
			
550

    
551
			String docInfoStr = ReplicationService.getURLContent(docinfourl);
552

    
553
			//dih is the parser for the docinfo xml format
554
			DocInfoHandler dih = new DocInfoHandler();
555
			XMLReader docinfoParser = ReplicationHandler.initParser(dih);
556
			docinfoParser.parse(new InputSource(new StringReader(docInfoStr)));
557
			//      Hashtable<String,Vector<AccessControlForSingleFile>> docinfoHash = dih.getDocInfo();
558
			Hashtable<String, String> docinfoHash = dih.getDocInfo();
559

    
560
			
561
			// Get home server of this docid
562
			String homeServer = (String) docinfoHash.get("home_server");
563
			String guid = (String) docinfoHash.get("guid");
564
			logReplication.info("XXXXXXXXXXXXXXXX GUID found in dociinfoHash: " + guid);
565
			
566
			logReplication.info("Processing guid " + guid + 
567
			  " information from handleForceReplicationRequest: " + 
568
			  docinfoHash.toString());
569
      IdentifierManager idman = IdentifierManager.getInstance();
570
      if(guid != null)
571
      { //if the guid was passed in, put it in the identifiers table
572
        logReplication.info("YYYYYYYYYYYYYY Creating guid/docid mapping for docid " + 
573
          docinfoHash.get("docid") + " and guid: " + guid);
574
        
575
        docName = (String) docinfoHash.get("docname");
576
        logReplication.info("ZZZZZZZZZZZZ docName: " + docName);
577
        if(docName.trim().equals("systemMetadata"))
578
        {
579
            logReplication.info("creating mapping for systemMetadata: guid: " + guid + " localId: " + docinfoHash.get("docid"));
580
            idman.createSystemMetadataMapping(guid, docinfoHash.get("docid"));
581
            logReplication.info("handleForceReplicateRequest: updating SM with additional info");
582
            System.out.println("!!!!!!!!!!!!!handleForceReplicateRequest: updating SM with additional info");
583
            Long dateUploadedLong = new Long(docinfoHash.get("date_uploaded"));
584
            Long dateModifiedLong = new Long(docinfoHash.get("date_modified"));
585
            idman.insertAdditionalSystemMetadataFields(
586
                    dateUploadedLong.longValue(), 
587
                    docinfoHash.get("rights_holder"),
588
                    docinfoHash.get("checksum"), 
589
                    docinfoHash.get("checksum_algorithm"), 
590
                    docinfoHash.get("origin_member_node"),
591
                    docinfoHash.get("authoritive_member_node"), 
592
                    dateModifiedLong.longValue(),
593
                    docinfoHash.get("submitter"),
594
                    docinfoHash.get("guid"),
595
                    docinfoHash.get("objectFormat"),
596
                    new Long(docinfoHash.get("size")).longValue());
597
        }
598
        else
599
        {
600
            logReplication.info("creating mapping: guid: " + guid + " localId: " + docinfoHash.get("docid"));
601
            idman.createMapping(guid, docinfoHash.get("docid"));
602
        }
603
      }
604
      else
605
      {
606
        logReplication.debug("No guid information was included with the replicated document");
607
      }
608
      
609
			String createdDate = (String) docinfoHash.get("date_created");
610
			String updatedDate = (String) docinfoHash.get("date_updated");
611
			logReplication.info("ReplicationService.handleForceReplicateRequest - homeServer: " + homeServer);
612
			// Get Document type
613
			String docType = (String) docinfoHash.get("doctype");
614
			logReplication.info("ReplicationService.handleForceReplicateRequest - docType: " + docType);
615
			String parserBase = null;
616
			// this for eml2 and we need user eml2 parser
617
			if (docType != null
618
					&& (docType.trim()).equals(DocumentImpl.EML2_0_0NAMESPACE)) {
619
				logReplication.warn("ReplicationService.handleForceReplicateRequest - This is an eml200 document!");
620
				parserBase = DocumentImpl.EML200;
621
			} else if (docType != null
622
					&& (docType.trim()).equals(DocumentImpl.EML2_0_1NAMESPACE)) {
623
				logReplication.warn("ReplicationService.handleForceReplicateRequest - This is an eml2.0.1 document!");
624
				parserBase = DocumentImpl.EML200;
625
			} else if (docType != null
626
					&& (docType.trim()).equals(DocumentImpl.EML2_1_0NAMESPACE)) {
627
				logReplication.warn("ReplicationService.handleForceReplicateRequest - This is an eml2.1.0 document!");
628
				parserBase = DocumentImpl.EML210;
629
			} else if (docType != null
630
					&& (docType.trim()).equals(DocumentImpl.EML2_1_1NAMESPACE)) {
631
				logReplication.warn("ReplicationService.handleForceReplicateRequest - This is an eml2.1.1 document!");
632
				parserBase = DocumentImpl.EML210;
633
			}
634
			logReplication.warn("ReplicationService.handleForceReplicateRequest - The parserBase is: " + parserBase);
635

    
636
			// Get DBConnection from pool
637
			dbConn = DBConnectionPool
638
					.getDBConnection("MetacatReplication.handleForceReplicateRequest");
639
			serialNumber = dbConn.getCheckOutSerialNumber();
640
			// write the document to local database
641
			DocumentImplWrapper wrapper = new DocumentImplWrapper(parserBase, false);
642
			//try this independently so we can set
643
//			Exception writeException = null;
644
			try {
645
				wrapper.writeReplication(dbConn, xmldoc, null, null,
646
						dbaction, docid, null, null, homeServer, server, createdDate,
647
						updatedDate);
648
			} finally {
649
//				writeException = e;
650

    
651
				//process extra access rules before dealing with the write exception (doc exist already)			
652
		        Vector<XMLAccessDAO> accessControlList = dih.getAccessControlList();
653
		        if (accessControlList != null) {
654
		        	AccessControlForSingleFile acfsf = new AccessControlForSingleFile(docid);
655
		        	for (XMLAccessDAO xmlAccessDAO : accessControlList) {
656
		        		if (!acfsf.accessControlExists(xmlAccessDAO)) {
657
		        			acfsf.insertPermissions(xmlAccessDAO);
658
							logReplication.info("ReplicationService.handleForceReplicateRequest - document " + docid
659
									+ " permissions added to DB");
660
		        		}
661
		            }
662
		        }
663
		        
664
		        // process the real owner and updater
665
				String user = (String) docinfoHash.get("user_owner");
666
				String updated = (String) docinfoHash.get("user_updated");
667
		        updateUserOwner(dbConn, docid, user, updated);
668

    
669
				logReplication.info("ReplicationService.handleForceReplicateRequest - document " + docid + " added to DB with "
670
						+ "action " + dbaction);
671
				
672
				if(guid != null)
673
                {
674
                    if(!docName.trim().equals("systemMetadata"))
675
                    {
676
                        logReplication.info("replicate D1GUID:" + guid + ":D1SCIMETADATA:" + 
677
                                docid + ":");
678
                    }
679
                    else
680
                    {
681
                        logReplication.info("replicate D1GUID:" + guid + ":D1SYSMETADATA:" + 
682
                                docid + ":");
683
                    }
684
                }
685
				EventLog.getInstance().log(request.getRemoteAddr(), REPLICATIONUSER, docid,
686
						dbaction);
687
			}
688
		} catch (SQLException sqle) {
689
			logMetacat.error("ReplicationService.handleForceReplicateRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
690
			logReplication.error("ReplicationService.handleForceReplicateRequest - SQL error when adding doc " + docid + 
691
					" to DB with action " + dbaction + ": " + sqle.getMessage());
692
		} catch (MalformedURLException mue) {
693
			logMetacat.error("ReplicationService.handleForceReplicateRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
694
			logReplication.error("ReplicationService.handleForceReplicateRequest - URL error when adding doc " + docid + 
695
					" to DB with action " + dbaction + ": " + mue.getMessage());
696
		} catch (SAXException se) {
697
			logMetacat.error("ReplicationService.handleForceReplicateRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
698
			logReplication.error("ReplicationService.handleForceReplicateRequest - SAX parsing error when adding doc " + docid + 
699
					" to DB with action " + dbaction + ": " + se.getMessage());
700
		} catch (HandlerException he) {
701
			logMetacat.error("ReplicationService.handleForceReplicateRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
702
			logReplication.error("ReplicationService.handleForceReplicateRequest - Handler error when adding doc " + docid + 
703
					" to DB with action " + dbaction + ": " + he.getMessage());
704
		} catch (IOException ioe) {
705
			logMetacat.error("ReplicationService.handleForceReplicateRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
706
			logReplication.error("ReplicationService.handleForceReplicateRequest - I/O error when adding doc " + docid + 
707
					" to DB with action " + dbaction + ": " + ioe.getMessage());
708
		} catch (PermOrderException poe) {
709
			logMetacat.error("ReplicationService.handleForceReplicateRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
710
			logReplication.error("ReplicationService.handleForceReplicateRequest - Permissions order error when adding doc " + docid + 
711
					" to DB with action " + dbaction + ": " + poe.getMessage());
712
		} catch (AccessControlException ace) {
713
			logMetacat.error("ReplicationService.handleForceReplicateRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
714
			logReplication.error("ReplicationService.handleForceReplicateRequest - Permissions order error when adding doc " + docid + 
715
					" to DB with action " + dbaction + ": " + ace.getMessage());
716
		} catch (Exception e) {
717
			logMetacat.error("ReplicationService.handleForceReplicateRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
718
			logReplication.error("ReplicationService.handleForceReplicateRequest - General error when adding doc " + docid + 
719
					" to DB with action " + dbaction + ": " + e.getMessage());
720
		} finally {
721
			// Return the checked out DBConnection
722
			DBConnectionPool.returnDBConnection(dbConn, serialNumber);
723
		}//finally
724
	}
725

    
726
	/*
727
	 * when a forcereplication delete request comes in, local host will delete this
728
	 * document
729
	 */
730
	protected static void handleForceReplicateDeleteRequest(
731
			Hashtable<String, String[]> params, HttpServletResponse response,
732
			HttpServletRequest request) {
733
		String server = ((String[]) params.get("server"))[0]; // the server that
734
		String docid = ((String[]) params.get("docid"))[0]; // sent the document
735
		try {
736
			logReplication.info("ReplicationService.handleForceReplicateDeleteRequest - force replication delete request from " + server);
737
			logReplication.info("ReplicationService.handleForceReplicateDeleteRequest - force replication delete docid " + docid);
738
			logReplication.info("ReplicationService.handleForceReplicateDeleteRequest - Force replication delete request from: " + server);
739
			logReplication.info("ReplicationService.handleForceReplicateDeleteRequest - Force replication delete docid: " + docid);
740
			DocumentImpl.delete(docid, null, null, server);
741
			logReplication.info("ReplicationService.handleForceReplicateDeleteRequest - document " + docid + " was successfully deleted ");
742
			EventLog.getInstance().log(request.getRemoteAddr(), REPLICATIONUSER, docid,
743
					"delete");
744
			logReplication.info("ReplicationService.handleForceReplicateDeleteRequest - document " + docid + " was successfully deleted ");
745
		} catch (McdbDocNotFoundException e) {
746
			logMetacat.error("ReplicationService.handleForceReplicateDeleteRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
747
			logReplication.error("document " + docid
748
					+ " failed to delete because " + e.getMessage());
749
			logReplication.error("ReplicationService.handleForceReplicateDeleteRequest - error: " + e.getMessage());
750
		} catch (InsufficientKarmaException e) {
751
			logMetacat.error("ReplicationService.handleForceReplicateDeleteRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
752
			logReplication.error("document " + docid
753
					+ " failed to delete because " + e.getMessage());
754
			logReplication.error("ReplicationService.handleForceReplicateDeleteRequest - error: " + e.getMessage());
755
		} catch (SQLException e) {
756
			logMetacat.error("ReplicationService.handleForceReplicateDeleteRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
757
			logReplication.error("document " + docid
758
					+ " failed to delete because " + e.getMessage());
759
			logReplication.error("ReplicationService.handleForceReplicateDeleteRequest - error: " + e.getMessage());
760
		} catch (Exception e) {
761
			logMetacat.error("ReplicationService.handleForceReplicateDeleteRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
762
			logReplication.error("document " + docid
763
					+ " failed to delete because " + e.getMessage());
764
			logReplication.error("ReplicationService.handleForceReplicateDeleteRequest - error: " + e.getMessage());
765

    
766
		}//catch
767

    
768
	}
769

    
770
	/**
771
	 * when a forcereplication data file request comes in, local host sends a
772
	 * readdata request to the requesting server (remote server) for the specified
773
	 * docid. Then store it in local database and file system
774
	 */
775
	protected static void handleForceReplicateDataFileRequest(Hashtable<String, String[]> params,
776
			HttpServletRequest request) {
777

    
778
		//make sure there is some parameters
779
		if (params.isEmpty()) {
780
			return;
781
		}
782
		// Get remote server
783
		String server = ((String[]) params.get("server"))[0];
784
		// the docid should include rev number
785
		String docid = ((String[]) params.get("docid"))[0];
786
		// Make sure there is a docid and server
787
		if (docid == null || server == null || server.equals("")) {
788
			logMetacat.error("ReplicationService.handleForceReplicateDataFileRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
789
			logReplication.error("ReplicationService.handleForceReplicateDataFileRequest - Didn't specify docid or server for replication");
790
			return;
791
		}
792

    
793
		// Overide or not
794
		//    boolean override = false;
795
		// dbaction - update or insert
796
		String dbaction = null;
797

    
798
		try {
799
			//docid was switch to two parts uinque code and rev
800
			//String uniqueCode=MetacatUtil.getDocIdFromString(docid);
801
			//int rev=MetacatUtil.getVersionFromString(docid);
802
			if (params.containsKey("dbaction")) {
803
				dbaction = ((String[]) params.get("dbaction"))[0];
804
			} else//default value is update
805
			{
806
				dbaction = "update";
807
			}
808

    
809
			logReplication.info("ReplicationService.handleForceReplicateDataFileRequest - force replication request from " + server);
810
			logReplication.info("ReplicationService.handleForceReplicateDataFileRequest - Force replication request from: " + server);
811
			logReplication.info("ReplicationService.handleForceReplicateDataFileRequest - Force replication docid: " + docid);
812
			logReplication.info("ReplicationService.handleForceReplicateDataFileRequest - Force replication action: " + dbaction);
813
			// get the document info from server
814
			URL docinfourl = new URL("https://" + server + "?server="
815
					+ MetacatUtil.getLocalReplicationServerName()
816
					+ "&action=getdocumentinfo&docid=" + docid);
817

    
818
			String docInfoStr = ReplicationService.getURLContent(docinfourl);
819

    
820
			//dih is the parser for the docinfo xml format
821
			DocInfoHandler dih = new DocInfoHandler();
822
			XMLReader docinfoParser = ReplicationHandler.initParser(dih);
823
			docinfoParser.parse(new InputSource(new StringReader(docInfoStr)));
824
			Hashtable<String, String> docinfoHash = dih.getDocInfo();
825
			
826
			String docName = (String) docinfoHash.get("docname");
827

    
828
			String docType = (String) docinfoHash.get("doctype");
829

    
830
			String docHomeServer = (String) docinfoHash.get("home_server");
831

    
832
			String createdDate = (String) docinfoHash.get("date_created");
833

    
834
			String updatedDate = (String) docinfoHash.get("date_updated");
835
			logReplication.info("ReplicationService.handleForceReplicateDataFileRequest - docHomeServer of datafile: " + docHomeServer);
836

    
837
			//if action is delete, we don't delete the data file. Just archieve
838
			//the xml_documents
839
			/*if (dbaction.equals("delete"))
840
			{
841
			  //conn = util.getConnection();
842
			  DocumentImpl.delete(docid,user,null);
843
			  //util.returnConnection(conn);
844
			}*/
845
			//To data file insert or update is same
846
			if (dbaction.equals("insert") || dbaction.equals("update")) {
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
853
						.getProperty("application.datafilepath");
854

    
855
				Exception writeException = null;
856
				//register data file into xml_documents table and wite data file
857
				//into file system
858
				try {
859
					DocumentImpl.writeDataFileInReplication(url.openStream(),
860
							datafilePath, docName, docType, docid, null, docHomeServer,
861
							server, DocumentImpl.DOCUMENTTABLE, false, createdDate,
862
							updatedDate);
863
				} catch (Exception e) {
864
					writeException = e;
865
				}
866
				
867
				// process the real owner and updater
868
				DBConnection dbConn = DBConnectionPool.getDBConnection("ReplicationService.handleForceDataFileRequest");
869
		        int serialNumber = dbConn.getCheckOutSerialNumber();
870
		        dbConn.setAutoCommit(false);
871
				String user = (String) docinfoHash.get("user_owner");
872
				String updated = (String) docinfoHash.get("user_updated");
873
		        updateUserOwner(dbConn, docid, user, updated);
874
		        DBConnectionPool.returnDBConnection(dbConn, serialNumber);
875
		        
876
		        Vector<XMLAccessDAO> accessControlList = dih.getAccessControlList();
877
		        if (accessControlList != null) {
878
		        	AccessControlForSingleFile acfsf = new AccessControlForSingleFile(docid);
879
		        	for (XMLAccessDAO xmlAccessDAO : accessControlList) {
880
		        		if (!acfsf.accessControlExists(xmlAccessDAO)) {
881
		        			acfsf.insertPermissions(xmlAccessDAO);
882
							logReplication.info("ReplicationService.handleForceReplicateRequest - document " + docid
883
									+ " permissions added to DB");
884
		        		}
885
		            }
886
		        }
887

    
888
				if (writeException != null) {
889
					throw writeException;
890
				}
891

    
892
				//false means non-timed replication
893
				logReplication.info("ReplicationService.handleForceReplicateDataFileRequest - datafile " + docid + " added to DB with "
894
						+ "action " + dbaction);
895
				EventLog.getInstance().log(request.getRemoteAddr(), REPLICATIONUSER,
896
						docid, dbaction);
897
			}
898

    
899
		} catch (Exception e) {
900
			logMetacat.error("ReplicationService.handleForceReplicateDataFileRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
901
			logReplication.error("ReplicationService.handleForceReplicateDataFileRequest - Datafile " + docid
902
					+ " failed to added to DB with " + "action " + dbaction + " because "
903
					+ e.getMessage());
904
			logReplication.error("ReplicationService.handleForceReplicateDataFileRequest - ERROR in MetacatReplication.handleForceDataFileReplicate"
905
					+ "Request(): " + e.getMessage());
906
		}
907
	}
908

    
909
	/**
910
	 * Grants or denies a lock to a requesting host.
911
	 * The servlet parameters of interrest are:
912
	 * docid: the docid of the file the lock is being requested for
913
	 * currentdate: the timestamp of the document on the remote server
914
	 *
915
	 */
916
	protected static void handleGetLockRequest(
917
			Hashtable<String, String[]> params, HttpServletResponse response) {
918

    
919
		try {
920

    
921
			String docid = ((String[]) params.get("docid"))[0];
922
			String remoteRev = ((String[]) params.get("updaterev"))[0];
923
			DocumentImpl requestDoc = new DocumentImpl(docid);
924
			logReplication.info("ReplicationService.handleGetLockRequest - lock request for " + docid);
925
			int localRevInt = requestDoc.getRev();
926
			int remoteRevInt = Integer.parseInt(remoteRev);
927

    
928
			// get a writer for sending back to response
929
			response.setContentType("text/xml");
930
			Writer out = response.getWriter();
931
			
932
			if (remoteRevInt >= localRevInt) {
933
				if (!fileLocks.contains(docid)) { //grant the lock if it is not already locked
934
					fileLocks.add(0, docid); //insert at the beginning of the queue Vector
935
					//send a message back to the the remote host authorizing the insert
936
					out.write("<lockgranted><docid>" + docid
937
									+ "</docid></lockgranted>");
938
					//          lockThread = new Thread(this);
939
					//          lockThread.setPriority(Thread.MIN_PRIORITY);
940
					//          lockThread.start();
941
					logReplication.info("ReplicationService.handleGetLockRequest - lock granted for " + docid);
942
				} else { //deny the lock
943
					out.write("<filelocked><docid>" + docid + "</docid></filelocked>");
944
					logReplication.info("ReplicationService.handleGetLockRequest - lock denied for " + docid
945
							+ "reason: file already locked");
946
				}
947
			} else {//deny the lock.
948
				out.write("<outdatedfile><docid>" + docid + "</docid></filelocked>");
949
				logReplication.info("ReplicationService.handleGetLockRequest - lock denied for " + docid
950
						+ "reason: client has outdated file");
951
			}
952
			out.close();
953
			//conn.close();
954
		} catch (Exception e) {
955
			logMetacat.error("ReplicationService.handleGetLockRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
956
			logReplication.error("ReplicationService.handleGetLockRequest - error requesting file lock from MetacatReplication."
957
					+ "handleGetLockRequest: " + e.getMessage());
958
			e.printStackTrace(System.out);
959
		}
960
	}
961

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

    
974
		try {
975
		  IdentifierManager idman = IdentifierManager.getInstance();
976

    
977
			DocumentImpl doc = new DocumentImpl(docid);
978
			sb.append("<documentinfo><docid>").append(docid);
979
			sb.append("</docid>");
980
			try
981
			{
982
			  String guid = idman.getGUID(doc.getDocID(), doc.getRev());
983
			  sb.append("<guid>").append(guid).append("</guid>");
984
			  String smLocalId = idman.getSystemMetadataLocalId(guid);
985
			  Hashtable<String, String> sysmetaInfo = idman.getSystemMetadataInfo(smLocalId);
986
			  if(smLocalId != null && !smLocalId.trim().equals(""))
987
			  {
988
			      sb.append("<systemmetadatalocalid>").append(smLocalId).append("</systemmetadatalocalid>");
989
			  }
990
			  
991
			  Enumeration<String> sysmetaKeys = sysmetaInfo.keys();
992
			  while(sysmetaKeys.hasMoreElements())
993
			  {
994
			      String key = sysmetaKeys.nextElement();
995
			      sb.append("<" + key + ">" + sysmetaInfo.get(key).toString() + "</" + key + ">");
996
			  }
997
			  
998
			}
999
			catch(McdbDocNotFoundException e)
1000
			{
1001
			  //do nothing, there was no guid for this document
1002
			}
1003
			sb.append("<docname>").append(doc.getDocname());
1004
			sb.append("</docname><doctype>").append(doc.getDoctype());
1005
			sb.append("</doctype>");
1006
			sb.append("<user_owner>").append(doc.getUserowner());
1007
			sb.append("</user_owner><user_updated>").append(doc.getUserupdated());
1008
			sb.append("</user_updated>");
1009
			sb.append("<date_created>");
1010
			sb.append(doc.getCreateDate());
1011
			sb.append("</date_created>");
1012
			sb.append("<date_updated>");
1013
			sb.append(doc.getUpdateDate());
1014
			sb.append("</date_updated>");
1015
			sb.append("<home_server>");
1016
			sb.append(doc.getDocHomeServer());
1017
			sb.append("</home_server>");
1018
			sb.append("<public_access>").append(doc.getPublicaccess());
1019
			sb.append("</public_access><rev>").append(doc.getRev());
1020
			sb.append("</rev>");
1021

    
1022
			sb.append("<accessControl>");
1023

    
1024
			AccessControlForSingleFile acfsf = new AccessControlForSingleFile(docid); 
1025
			sb.append(acfsf.getAccessString());
1026
			
1027
			sb.append("</accessControl>");
1028

    
1029
			sb.append("</documentinfo>");
1030
			// get a writer for sending back to response
1031
			response.setContentType("text/xml");
1032
			Writer out = response.getWriter();
1033
			out.write(sb.toString());
1034
			out.close();
1035

    
1036
		} catch (Exception e) {
1037
			logMetacat.error("ReplicationService.handleGetDocumentInfoRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
1038
			logReplication.error("ReplicationService.handleGetDocumentInfoRequest - error in metacatReplication.handlegetdocumentinforequest "
1039
					+ "for doc: " + docid + " : " + e.getMessage());
1040
		}
1041

    
1042
	}
1043

    
1044
	/**
1045
	 * Sends a datafile to a remote host
1046
	 */
1047
	protected static void handleGetDataFileRequest(OutputStream outPut,
1048
			Hashtable<String, String[]> params, HttpServletResponse response)
1049

    
1050
	{
1051
		// File path for data file
1052
		String filepath;
1053
		// Request docid
1054
		String docId = ((String[]) (params.get("docid")))[0];
1055
		//check if the doicd is null
1056
		if (docId == null) {
1057
			logMetacat.error("ReplicationService.handleGetDataFileRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
1058
			logReplication.error("ReplicationService.handleGetDataFileRequest - Didn't specify docid for replication");
1059
			return;
1060
		}
1061

    
1062
		//try to open a https stream to test if the request server's public key
1063
		//in the key store, this is security issue
1064
		try {
1065
			filepath = PropertyService.getProperty("application.datafilepath");
1066
			String server = params.get("server")[0];
1067
			URL u = new URL("https://" + server + "?server="
1068
					+ MetacatUtil.getLocalReplicationServerName() + "&action=test");
1069
			String test = ReplicationService.getURLContent(u);
1070
			//couldn't pass the test
1071
			if (test.indexOf("successfully") == -1) {
1072
				//response.setContentType("text/xml");
1073
				//outPut.println("<error>Couldn't pass the trust test</error>");
1074
				logMetacat.error("ReplicationService.handleGetDataFileRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
1075
				logReplication.error("ReplicationService.handleGetDataFileRequest - Couldn't pass the trust test");
1076
				return;
1077
			}
1078
		}//try
1079
		catch (Exception ee) {
1080
			return;
1081
		}//catch
1082

    
1083
		if (!filepath.endsWith("/")) {
1084
			filepath += "/";
1085
		}
1086
		// Get file aboslute file name
1087
		String filename = filepath + docId;
1088

    
1089
		//MIME type
1090
		String contentType = null;
1091
		if (filename.endsWith(".xml")) {
1092
			contentType = "text/xml";
1093
		} else if (filename.endsWith(".css")) {
1094
			contentType = "text/css";
1095
		} else if (filename.endsWith(".dtd")) {
1096
			contentType = "text/plain";
1097
		} else if (filename.endsWith(".xsd")) {
1098
			contentType = "text/xml";
1099
		} else if (filename.endsWith("/")) {
1100
			contentType = "text/html";
1101
		} else {
1102
			File f = new File(filename);
1103
			if (f.isDirectory()) {
1104
				contentType = "text/html";
1105
			} else {
1106
				contentType = "application/octet-stream";
1107
			}
1108
		}
1109

    
1110
		// Set the mime type
1111
		response.setContentType(contentType);
1112

    
1113
		// Get the content of the file
1114
		FileInputStream fin = null;
1115
		try {
1116
			// FileInputStream to metacat
1117
			fin = new FileInputStream(filename);
1118
			// 4K buffer
1119
			byte[] buf = new byte[4 * 1024];
1120
			// Read data from file input stream to byte array
1121
			int b = fin.read(buf);
1122
			// Write to outStream from byte array
1123
			while (b != -1) {
1124
				outPut.write(buf, 0, b);
1125
				b = fin.read(buf);
1126
			}
1127
			// close file input stream
1128
			fin.close();
1129

    
1130
		}//try
1131
		catch (Exception e) {
1132
			logMetacat.error("ReplicationService.handleGetDataFileRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
1133
			logReplication.error("ReplicationService.handleGetDataFileRequest - error getting data file from MetacatReplication."
1134
					+ "handlGetDataFileRequest " + e.getMessage());
1135
			e.printStackTrace(System.out);
1136
		}//catch
1137

    
1138
	}
1139

    
1140
	/**
1141
	 * Sends a document to a remote host
1142
	 */
1143
	protected static void handleGetDocumentRequest(
1144
			Hashtable<String, String[]> params, HttpServletResponse response) {
1145

    
1146
		String urlString = null;
1147
		String documentPath = null;
1148
		String errorMsg = null;
1149
		try {
1150
			// try to open a https stream to test if the request server's public
1151
			// key
1152
			// in the key store, this is security issue
1153
			String server = params.get("server")[0];
1154
			urlString = "https://" + server + "?server="
1155
					+ MetacatUtil.getLocalReplicationServerName() + "&action=test";
1156
			URL u = new URL(urlString);
1157
			String test = ReplicationService.getURLContent(u);
1158
			// couldn't pass the test
1159
			if (test.indexOf("successfully") == -1) {
1160
				response.setContentType("text/xml");
1161
				Writer out = response.getWriter();
1162
				out.write("<error>Couldn't pass the trust test " + test + " </error>");
1163
				out.close();
1164
				return;
1165
			}
1166

    
1167
			String docid = params.get("docid")[0];
1168
			logReplication.debug("ReplicationService.handleGetDocumentRequest - MetacatReplication.handleGetDocumentRequest for docid: "
1169
					+ docid);
1170
			DocumentImpl di = new DocumentImpl(docid);
1171

    
1172
			String documentDir = PropertyService
1173
					.getProperty("application.documentfilepath");
1174
			documentPath = documentDir + FileUtil.getFS() + docid;
1175

    
1176
			// if the document does not exist on disk, read it from db and write
1177
			// it to disk.
1178
			if (FileUtil.getFileStatus(documentPath) == FileUtil.DOES_NOT_EXIST
1179
					|| FileUtil.getFileSize(documentPath) == 0) {
1180
				FileOutputStream fos = new FileOutputStream(documentPath);
1181
				di.toXml(fos, null, null, true);
1182
			}
1183

    
1184
			// read the file from disk and send it to outputstream
1185
			OutputStream outputStream = response.getOutputStream();
1186
			di.readFromFileSystem(outputStream, null, null, documentPath);
1187

    
1188
			logReplication.info("ReplicationService.handleGetDocumentRequest - document " + docid + " sent");
1189

    
1190
			// return to avoid continuing to the error reporting section at the end
1191
			return;
1192
			
1193
		} catch (MalformedURLException mue) {
1194
			logMetacat.error("ReplicationService.handleGetDocumentRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
1195
			logReplication.error("ReplicationService.handleGetDocumentRequest - Url error when getting document from MetacatReplication."
1196
					+ "handlGetDocumentRequest for url: " + urlString + " : "
1197
					+ mue.getMessage());
1198
			// e.printStackTrace(System.out);
1199
			
1200
		} catch (IOException ioe) {
1201
			logMetacat.error("ReplicationService.handleGetDocumentRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
1202
			logReplication.error("ReplicationService.handleGetDocumentRequest - I/O error when getting document from MetacatReplication."
1203
					+ "handlGetDocumentRequest for file: " + documentPath + " : "
1204
					+ ioe.getMessage());
1205
			errorMsg = ioe.getMessage();
1206
		} catch (PropertyNotFoundException pnfe) {
1207
			logMetacat.error("ReplicationService.handleGetDocumentRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
1208
			logReplication
1209
					.error("ReplicationService.handleGetDocumentRequest - Error getting property when getting document from MetacatReplication."
1210
							+ "handlGetDocumentRequest for file: "
1211
							+ documentPath
1212
							+ " : "
1213
							+ pnfe.getMessage());
1214
			// e.printStackTrace(System.out);
1215
			errorMsg = pnfe.getMessage();
1216
		} catch (McdbException me) {
1217
			logReplication
1218
					.error("ReplicationService.handleGetDocumentRequest - Document implementation error  getting property when getting document from MetacatReplication."
1219
							+ "handlGetDocumentRequest for file: "
1220
							+ documentPath
1221
							+ " : "
1222
							+ me.getMessage());
1223
			// e.printStackTrace(System.out);
1224
			errorMsg = me.getMessage();
1225
		}
1226
		
1227
		// report any errors if we got here
1228
		response.setContentType("text/xml");
1229
		Writer out = null;
1230
		try {
1231
			response.getWriter();
1232
			out = response.getWriter();
1233
			out.write("<error>" + errorMsg + "</error>");
1234
		} catch (Exception e) {
1235
			logMetacat.error(e.getMessage(), e);
1236
		} finally {
1237
			try {
1238
				out.close();
1239
			} catch (IOException e) {
1240
				logMetacat.error(e.getMessage(), e);
1241
			}
1242
		}
1243
		
1244

    
1245
	}
1246

    
1247
	/**
1248
	 * Sends a list of all of the documents on this sever along with their
1249
	 * revision numbers. The format is: <!ELEMENT replication (server, updates)>
1250
	 * <!ELEMENT server (#PCDATA)> <!ELEMENT updates ((updatedDocument |
1251
	 * deleteDocument | revisionDocument)*)> <!ELEMENT updatedDocument (docid,
1252
	 * rev, datafile*)> <!ELEMENT deletedDocument (docid, rev)> <!ELEMENT
1253
	 * revisionDocument (docid, rev, datafile*)> <!ELEMENT docid (#PCDATA)>
1254
	 * <!ELEMENT rev (#PCDATA)> <!ELEMENT datafile (#PCDATA)> note that the rev
1255
	 * in deletedDocument is always empty. I just left it in there to make the
1256
	 * parser implementation easier.
1257
	 */
1258
	protected static void handleUpdateRequest(Hashtable<String, String[]> params,
1259
			HttpServletResponse response) {
1260
		// Checked out DBConnection
1261
		DBConnection dbConn = null;
1262
		// DBConenction serial number when checked it out
1263
		int serialNumber = -1;
1264
		PreparedStatement pstmt = null;
1265
		// Server list to store server info of xml_replication table
1266
		ReplicationServerList serverList = null;
1267
		
1268
		// a writer for response
1269
		Writer out = null;
1270

    
1271
		try {
1272
			// get writer, TODO: encoding?
1273
			response.setContentType("text/xml");
1274
			out = response.getWriter();
1275
			
1276
			// Check out a DBConnection from pool
1277
			dbConn = DBConnectionPool
1278
					.getDBConnection("MetacatReplication.handleUpdateRequest");
1279
			serialNumber = dbConn.getCheckOutSerialNumber();
1280
			// Create a server list from xml_replication table
1281
			serverList = new ReplicationServerList();
1282

    
1283
			// Get remote server name from param
1284
			String server = ((String[]) params.get("server"))[0];
1285
			// If no servr name in param, return a error
1286
			if (server == null || server.equals("")) {
1287
				out.write("<error>Request didn't specify server name</error>");
1288
				out.close();
1289
				return;
1290
			}//if
1291

    
1292
			//try to open a https stream to test if the request server's public key
1293
			//in the key store, this is security issue
1294
			String testUrl = "https://" + server + "?server="
1295
            + MetacatUtil.getLocalReplicationServerName() + "&action=test";
1296
			logReplication.info("Running trust test: " + testUrl);
1297
			URL u = new URL(testUrl);
1298
			String test = ReplicationService.getURLContent(u);
1299
			logReplication.info("Ouput from test is '" + test + "'");
1300
			//couldn't pass the test
1301
			if (test.indexOf("successfully") == -1) {
1302
			    logReplication.error("Trust test failed.");
1303
				out.write("<error>Couldn't pass the trust test</error>");
1304
				out.close();
1305
				return;
1306
			}
1307
			logReplication.info("Trust test succeeded.");
1308

    
1309
			// Check if local host configure to replicate xml documents to remote
1310
			// server. If not send back a error message
1311
			if (!serverList.getReplicationValue(server)) {
1312
				out.write("<error>Configuration not allow to replicate document to you</error>");
1313
				out.close();
1314
				return;
1315
			}//if
1316

    
1317
			// Store the sql command
1318
			StringBuffer docsql = new StringBuffer();
1319
			StringBuffer revisionSql = new StringBuffer();
1320
			// Stroe the docid list
1321
			StringBuffer doclist = new StringBuffer();
1322
			// Store the deleted docid list
1323
			StringBuffer delsql = new StringBuffer();
1324
			// Store the data set file
1325
			Vector<Vector<String>> packageFiles = new Vector<Vector<String>>();
1326

    
1327
			// Append local server's name and replication servlet to doclist
1328
			doclist.append("<?xml version=\"1.0\"?><replication>");
1329
			doclist.append("<server>")
1330
					.append(MetacatUtil.getLocalReplicationServerName());
1331
			//doclist.append(util.getProperty("replicationpath"));
1332
			doclist.append("</server><updates>");
1333

    
1334
			// Get correct docid that reside on this server according the requesting
1335
			// server's replicate and data replicate value in xml_replication table
1336
			docsql.append(DatabaseService.getInstance().getDBAdapter().getReplicationDocumentListSQL());
1337
			//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)) ");
1338
			revisionSql.append("select docid, rev, doctype from xml_revisions ");
1339
			// If the localhost is not a hub to the remote server, only replicate
1340
			// the docid' which home server is local host (server_location =1)
1341
			if (!serverList.getHubValue(server)) {
1342
				String serverLocationDoc = " and a.server_location = 1";
1343
				String serverLocationRev = "where server_location = 1";
1344
				docsql.append(serverLocationDoc);
1345
				revisionSql.append(serverLocationRev);
1346
			}
1347
			logReplication.info("ReplicationService.handleUpdateRequest - Doc sql: " + docsql.toString());
1348

    
1349
			// Get any deleted documents
1350
			delsql.append("select distinct docid from ");
1351
			delsql.append("xml_revisions where docid not in (select docid from ");
1352
			delsql.append("xml_documents) ");
1353
			// If the localhost is not a hub to the remote server, only replicate
1354
			// the docid' which home server is local host (server_location =1)
1355
			if (!serverList.getHubValue(server)) {
1356
				delsql.append("and server_location = 1");
1357
			}
1358
			logReplication.info("ReplicationService.handleUpdateRequest - Deleted sql: " + delsql.toString());
1359

    
1360
			// Get docid list of local host
1361
			pstmt = dbConn.prepareStatement(docsql.toString());
1362
			pstmt.execute();
1363
			ResultSet rs = pstmt.getResultSet();
1364
			boolean tablehasrows = rs.next();
1365
			//If metacat configed to replicate data file
1366
			//if ((util.getProperty("replicationsenddata")).equals("on"))
1367
			boolean replicateData = serverList.getDataReplicationValue(server);
1368
			if (replicateData) {
1369
				while (tablehasrows) {
1370
					String recordDoctype = rs.getString(3);
1371
					Vector<String> packagedoctypes = MetacatUtil
1372
							.getOptionList(PropertyService
1373
									.getProperty("xml.packagedoctype"));
1374
					//if this is a package file, put it at the end
1375
					//because if a package file is read before all of the files it
1376
					//refers to are loaded then there is an error
1377
					if (recordDoctype != null && !packagedoctypes.contains(recordDoctype)) {
1378
						//If this is not data file
1379
						if (!recordDoctype.equals("BIN")) {
1380
							//for non-data file document
1381
							doclist.append("<updatedDocument>");
1382
							doclist.append("<docid>").append(rs.getString(1));
1383
							doclist.append("</docid><rev>").append(rs.getInt(2));
1384
							doclist.append("</rev>");
1385
							doclist.append("</updatedDocument>");
1386
						}//if
1387
						else {
1388
							//for data file document, in datafile attributes
1389
							//we put "datafile" value there
1390
							doclist.append("<updatedDocument>");
1391
							doclist.append("<docid>").append(rs.getString(1));
1392
							doclist.append("</docid><rev>").append(rs.getInt(2));
1393
							doclist.append("</rev>");
1394
							doclist.append("<datafile>");
1395
							doclist.append(PropertyService
1396
									.getProperty("replication.datafileflag"));
1397
							doclist.append("</datafile>");
1398
							doclist.append("</updatedDocument>");
1399
						}//else
1400
					}//if packagedoctpes
1401
					else { //the package files are saved to be put into the xml later.
1402
						Vector<String> v = new Vector<String>();
1403
						v.add(rs.getString(1));
1404
						v.add(String.valueOf(rs.getInt(2)));
1405
						packageFiles.add(v);
1406
					}//esle
1407
					tablehasrows = rs.next();
1408
				}//while
1409
			}//if
1410
			else //metacat was configured not to send data file
1411
			{
1412
				while (tablehasrows) {
1413
					String recordDoctype = rs.getString(3);
1414
					if (!recordDoctype.equals("BIN")) { //don't replicate data files
1415
						Vector<String> packagedoctypes = MetacatUtil
1416
								.getOptionList(PropertyService
1417
										.getProperty("xml.packagedoctype"));
1418
						if (recordDoctype != null
1419
								&& !packagedoctypes.contains(recordDoctype)) { //if this is a package file, put it at the end
1420
							//because if a package file is read before all of the files it
1421
							//refers to are loaded then there is an error
1422
							doclist.append("<updatedDocument>");
1423
							doclist.append("<docid>").append(rs.getString(1));
1424
							doclist.append("</docid><rev>").append(rs.getInt(2));
1425
							doclist.append("</rev>");
1426
							doclist.append("</updatedDocument>");
1427
						} else { //the package files are saved to be put into the xml later.
1428
							Vector<String> v = new Vector<String>();
1429
							v.add(rs.getString(1));
1430
							v.add(String.valueOf(rs.getInt(2)));
1431
							packageFiles.add(v);
1432
						}
1433
					}//if
1434
					tablehasrows = rs.next();
1435
				}//while
1436
			}//else
1437

    
1438
			pstmt = dbConn.prepareStatement(delsql.toString());
1439
			//usage count should increas 1
1440
			dbConn.increaseUsageCount(1);
1441

    
1442
			pstmt.execute();
1443
			rs = pstmt.getResultSet();
1444
			tablehasrows = rs.next();
1445
			while (tablehasrows) { //handle the deleted documents
1446
				doclist.append("<deletedDocument><docid>").append(rs.getString(1));
1447
				doclist.append("</docid><rev></rev></deletedDocument>");
1448
				//note that rev is always empty for deleted docs
1449
				tablehasrows = rs.next();
1450
			}
1451

    
1452
			//now we can put the package files into the xml results
1453
			for (int i = 0; i < packageFiles.size(); i++) {
1454
				Vector<String> v = packageFiles.elementAt(i);
1455
				doclist.append("<updatedDocument>");
1456
				doclist.append("<docid>").append(v.elementAt(0));
1457
				doclist.append("</docid><rev>");
1458
				doclist.append(v.elementAt(1));
1459
				doclist.append("</rev>");
1460
				doclist.append("</updatedDocument>");
1461
			}
1462
			// add revision doc list  
1463
			doclist.append(prepareRevisionDoc(dbConn, revisionSql.toString(),
1464
					replicateData));
1465

    
1466
			doclist.append("</updates></replication>");
1467
			logReplication.info("ReplicationService.handleUpdateRequest - doclist: " + doclist.toString());
1468
			pstmt.close();
1469
			//conn.close();
1470
			out.write(doclist.toString());
1471

    
1472
		} catch (Exception e) {
1473
			logMetacat.error("ReplicationService.handleUpdateRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
1474
			logReplication.error("ReplicationService.handleUpdateRequest - error in MetacatReplication." + "handleupdaterequest: "
1475
					+ e.getMessage());
1476
			//e.printStackTrace(System.out);
1477
			try {
1478
				out.write("<error>" + e.getMessage() + "</error>");
1479
			} catch (IOException e1) {
1480
				logMetacat.error(e1.getMessage(), e1);
1481
			}
1482
		} finally {
1483
			try {
1484
				pstmt.close();
1485
			}//try
1486
			catch (SQLException ee) {
1487
				logMetacat.error("ReplicationService.handleUpdateRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
1488
				logReplication.error("ReplicationService.handleUpdateRequest - Error in MetacatReplication."
1489
						+ "handleUpdaterequest to close pstmt: " + ee.getMessage());
1490
			}//catch
1491
			finally {
1492
				DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1493
			}//finally
1494
			try {
1495
				out.close();
1496
			} catch (IOException e) {
1497
				logMetacat.error(e.getMessage(), e);
1498
			}
1499
		}//finally
1500

    
1501
	}//handlUpdateRequest
1502

    
1503
	/**
1504
	 * 
1505
	 * @param dbConn connection for doing the update
1506
	 * @param docid the document id to update
1507
	 * @param owner the user_owner
1508
	 * @param updater the user_updated
1509
	 * @throws SQLException
1510
	 */
1511
	public static void updateUserOwner(DBConnection dbConn, String docid, String owner, String updater) throws SQLException {
1512
	
1513
		String sql = 
1514
			"UPDATE xml_documents " +
1515
			"SET user_owner = ?, " +
1516
			"user_updated = ? " +
1517
			"WHERE docid = ?;";
1518
		PreparedStatement pstmt = dbConn.prepareStatement(sql);
1519
		//usage count should increas 1
1520
		dbConn.increaseUsageCount(1);
1521

    
1522
		docid = DocumentUtil.getSmartDocId(docid);
1523
		pstmt.setString(1, owner);
1524
		pstmt.setString(2, updater);
1525
		pstmt.setString(3, docid);
1526
		pstmt.execute();
1527
		pstmt.close();
1528
		
1529
		dbConn.commit();
1530
	}
1531
	
1532
	/*
1533
	 * This method will get the xml string for document in xml_revision
1534
	 * The schema look like <!ELEMENT revisionDocument (docid, rev, datafile*)>
1535
	 */
1536
	private static String prepareRevisionDoc(DBConnection dbConn, String revSql,
1537
			boolean replicateData) throws Exception {
1538
		logReplication.warn("ReplicationService.prepareRevisionDoc - The revision document sql is " + revSql);
1539
		StringBuffer revDocList = new StringBuffer();
1540
		PreparedStatement pstmt = dbConn.prepareStatement(revSql);
1541
		//usage count should increas 1
1542
		dbConn.increaseUsageCount(1);
1543

    
1544
		pstmt.execute();
1545
		ResultSet rs = pstmt.getResultSet();
1546
		boolean tablehasrows = rs.next();
1547
		while (tablehasrows) {
1548
			String recordDoctype = rs.getString(3);
1549

    
1550
			//If this is data file and it isn't configured to replicate data
1551
			if (recordDoctype.equals("BIN") && !replicateData) {
1552
				// do nothing
1553
				continue;
1554
			} else {
1555

    
1556
				revDocList.append("<revisionDocument>");
1557
				revDocList.append("<docid>").append(rs.getString(1));
1558
				revDocList.append("</docid><rev>").append(rs.getInt(2));
1559
				revDocList.append("</rev>");
1560
				// data file
1561
				if (recordDoctype.equals("BIN")) {
1562
					revDocList.append("<datafile>");
1563
					revDocList.append(PropertyService
1564
							.getProperty("replication.datafileflag"));
1565
					revDocList.append("</datafile>");
1566
				}
1567
				revDocList.append("</revisionDocument>");
1568

    
1569
			}//else
1570
			tablehasrows = rs.next();
1571
		}
1572
		//System.out.println("The revision list is"+ revDocList.toString());
1573
		return revDocList.toString();
1574
	}
1575

    
1576
	/**
1577
	 * Returns the xml_catalog table encoded in xml
1578
	 */
1579
	public static String getCatalogXML() {
1580
		return handleGetCatalogRequest(null, null, false);
1581
	}
1582

    
1583
	/**
1584
	 * Sends the contents of the xml_catalog table encoded in xml
1585
	 * The xml format is:
1586
	 * <!ELEMENT xml_catalog (row*)>
1587
	 * <!ELEMENT row (entry_type, source_doctype, target_doctype, public_id,
1588
	 *                system_id)>
1589
	 * All of the sub elements of row are #PCDATA
1590

    
1591
	 * If printFlag == false then do not print to out.
1592
	 */
1593
	protected static String handleGetCatalogRequest(
1594
			Hashtable<String, String[]> params, HttpServletResponse response,
1595
			boolean printFlag) {
1596
		DBConnection dbConn = null;
1597
		int serialNumber = -1;
1598
		PreparedStatement pstmt = null;
1599
		Writer out = null;
1600
		try {
1601
			// get writer, TODO: encoding?
1602
		    if(printFlag)
1603
		    {
1604
		        response.setContentType("text/xml");
1605
		        out = response.getWriter();
1606
		    }
1607
			/*conn = MetacatReplication.getDBConnection("MetacatReplication." +
1608
			                                          "handleGetCatalogRequest");*/
1609
			dbConn = DBConnectionPool
1610
					.getDBConnection("MetacatReplication.handleGetCatalogRequest");
1611
			serialNumber = dbConn.getCheckOutSerialNumber();
1612
			pstmt = dbConn.prepareStatement("select entry_type, "
1613
					+ "source_doctype, target_doctype, public_id, "
1614
					+ "system_id from xml_catalog");
1615
			pstmt.execute();
1616
			ResultSet rs = pstmt.getResultSet();
1617
			boolean tablehasrows = rs.next();
1618
			StringBuffer sb = new StringBuffer();
1619
			sb.append("<?xml version=\"1.0\"?><xml_catalog>");
1620
			while (tablehasrows) {
1621
				sb.append("<row><entry_type>").append(rs.getString(1));
1622
				sb.append("</entry_type><source_doctype>").append(rs.getString(2));
1623
				sb.append("</source_doctype><target_doctype>").append(rs.getString(3));
1624
				sb.append("</target_doctype><public_id>").append(rs.getString(4));
1625
				// system id may not have server url on front.  Add it if not.
1626
				String systemID = rs.getString(5);
1627
				if (!systemID.startsWith("http://")) {
1628
					systemID = SystemUtil.getContextURL() + systemID;
1629
				}
1630
				sb.append("</public_id><system_id>").append(systemID);
1631
				sb.append("</system_id></row>");
1632

    
1633
				tablehasrows = rs.next();
1634
			}
1635
			sb.append("</xml_catalog>");
1636
			//conn.close();
1637
			if (printFlag) {
1638
				response.setContentType("text/xml");
1639
				out.write(sb.toString());
1640
			}
1641
			pstmt.close();
1642
			return sb.toString();
1643
		} catch (Exception e) {
1644
			logMetacat.error("ReplicationService.handleGetCatalogRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
1645
			logReplication.error("ReplicationService.handleGetCatalogRequest - error in MetacatReplication.handleGetCatalogRequest:"
1646
					+ e.getMessage());
1647
			e.printStackTrace(System.out);
1648
			if (printFlag) {
1649
				try {
1650
					out.write("<error>" + e.getMessage() + "</error>");
1651
				} catch (IOException e1) {
1652
					logMetacat.error(e1.getMessage(), e1);
1653
				}
1654
			}
1655
		} finally {
1656
			try {
1657
				pstmt.close();
1658
			}//try
1659
			catch (SQLException ee) {
1660
				logMetacat.error("ReplicationService.handleGetCatalogRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
1661
				logReplication.error("ReplicationService.handleGetCatalogRequest - Error in MetacatReplication.handleGetCatalogRequest: "
1662
						+ ee.getMessage());
1663
			}//catch
1664
			finally {
1665
				DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1666
			}//finally
1667
			if (out != null) {
1668
				try {
1669
					out.close();
1670
				} catch (IOException e1) {
1671
					logMetacat.error(e1.getMessage(), e1);
1672
				}
1673
			}
1674
		}//finally
1675

    
1676
		return null;
1677
	}
1678

    
1679
	/**
1680
	 * Sends the current system date to the remote server.  Using this action
1681
	 * for replication gets rid of any problems with syncronizing clocks
1682
	 * because a time specific to a document is always kept on its home server.
1683
	 */
1684
	protected static void handleGetTimeRequest(
1685
			Hashtable<String, String[]> params, HttpServletResponse response) {
1686
		SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yy HH:mm:ss");
1687
		java.util.Date localtime = new java.util.Date();
1688
		String dateString = formatter.format(localtime);
1689
		
1690
		// get a writer for sending back to response
1691
		response.setContentType("text/xml");
1692
		Writer out = null;
1693
		try {
1694
			out = response.getWriter();
1695
			out.write("<timestamp>" + dateString + "</timestamp>");
1696
			out.close();
1697
		} catch (IOException e) {
1698
			logMetacat.error(e.getMessage(), e);
1699
		}
1700
		
1701
	}
1702

    
1703
	/**
1704
	 * this method handles the timeout for a file lock.  when a lock is
1705
	 * granted it is granted for 30 seconds.  When this thread runs out
1706
	 * it deletes the docid from the queue, thus eliminating the lock.
1707
	 */
1708
	public void run() {
1709
		try {
1710
			logReplication.info("ReplicationService.run - thread started for docid: "
1711
					+ (String) fileLocks.elementAt(0));
1712

    
1713
			Thread.sleep(30000); //the lock will expire in 30 seconds
1714
			logReplication.info("thread for docid: "
1715
					+ (String) fileLocks.elementAt(fileLocks.size() - 1) + " exiting.");
1716

    
1717
			fileLocks.remove(fileLocks.size() - 1);
1718
			//fileLocks is treated as a FIFO queue.  If there are more than one lock
1719
			//in the vector, the first one inserted will be removed.
1720
		} catch (Exception e) {
1721
			logMetacat.error("ReplicationService.run - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
1722
			logReplication.error("ReplicationService.run - error in file lock thread from "
1723
					+ "MetacatReplication.run: " + e.getMessage());
1724
		}
1725
	}
1726

    
1727
	/**
1728
	 * Returns the name of a server given a serverCode
1729
	 * @param serverCode the serverid of the server
1730
	 * @return the servername or null if the specified serverCode does not
1731
	 *         exist.
1732
	 */
1733
	public static String getServerNameForServerCode(int serverCode) {
1734
		//System.out.println("serverid: " + serverCode);
1735
		DBConnection dbConn = null;
1736
		int serialNumber = -1;
1737
		PreparedStatement pstmt = null;
1738
		try {
1739
			dbConn = DBConnectionPool.getDBConnection("MetacatReplication.getServer");
1740
			serialNumber = dbConn.getCheckOutSerialNumber();
1741
			String sql = new String("select server from "
1742
					+ "xml_replication where serverid = " + serverCode);
1743
			pstmt = dbConn.prepareStatement(sql);
1744
			//System.out.println("getserver sql: " + sql);
1745
			pstmt.execute();
1746
			ResultSet rs = pstmt.getResultSet();
1747
			boolean tablehasrows = rs.next();
1748
			if (tablehasrows) {
1749
				//System.out.println("server: " + rs.getString(1));
1750
				return rs.getString(1);
1751
			}
1752

    
1753
			//conn.close();
1754
		} catch (Exception e) {
1755
			logMetacat.error("ReplicationService.getServerNameForServerCode - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
1756
			logReplication.error("ReplicationService.getServerNameForServerCode - Error in MetacatReplication.getServer: " + e.getMessage());
1757
		} finally {
1758
			try {
1759
				pstmt.close();
1760
			}//try
1761
			catch (SQLException ee) {
1762
				logMetacat.error("ReplicationService.getServerNameForServerCode - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
1763
				logReplication.error("ReplicationService.getServerNameForServerCode - Error in MetacactReplication.getserver: "
1764
						+ ee.getMessage());
1765
			}//catch
1766
			finally {
1767
				DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1768
			}//fianlly
1769
		}//finally
1770

    
1771
		return null;
1772
		//return null if the server does not exist
1773
	}
1774

    
1775
	/**
1776
	 * Returns a server code given a server name
1777
	 * @param server the name of the server
1778
	 * @return integer > 0 representing the code of the server, 0 if the server
1779
	 *  does not exist.
1780
	 */
1781
	public static int getServerCodeForServerName(String server) throws ServiceException {
1782
		DBConnection dbConn = null;
1783
		int serialNumber = -1;
1784
		PreparedStatement pstmt = null;
1785
		int serverCode = 0;
1786

    
1787
		try {
1788

    
1789
			//conn = util.openDBConnection();
1790
			dbConn = DBConnectionPool.getDBConnection("MetacatReplication.getServerCode");
1791
			serialNumber = dbConn.getCheckOutSerialNumber();
1792
			pstmt = dbConn.prepareStatement("SELECT serverid FROM xml_replication "
1793
					+ "WHERE server LIKE '" + server + "'");
1794
			pstmt.execute();
1795
			ResultSet rs = pstmt.getResultSet();
1796
			boolean tablehasrows = rs.next();
1797
			if (tablehasrows) {
1798
				serverCode = rs.getInt(1);
1799
				pstmt.close();
1800
				//conn.close();
1801
				return serverCode;
1802
			}
1803

    
1804
		} catch (SQLException sqle) {
1805
			throw new ServiceException("ReplicationService.getServerCodeForServerName - " 
1806
					+ "SQL error when getting server code: " + sqle.getMessage());
1807

    
1808
		} finally {
1809
			try {
1810
				pstmt.close();
1811
				//conn.close();
1812
			}//try
1813
			catch (Exception ee) {
1814
				logMetacat.error("ReplicationService.getServerCodeForServerName - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
1815
				logReplication.error("ReplicationService.getServerNameForServerCode - Error in MetacatReplicatio.getServerCode: "
1816
						+ ee.getMessage());
1817

    
1818
			}//catch
1819
			finally {
1820
				DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1821
			}//finally
1822
		}//finally
1823

    
1824
		return serverCode;
1825
	}
1826

    
1827
	/**
1828
	 * Method to get a host server information for given docid
1829
	 * @param conn a connection to the database
1830
	 */
1831
	public static Hashtable<String, String> getHomeServerInfoForDocId(String docId) {
1832
		Hashtable<String, String> sl = new Hashtable<String, String>();
1833
		DBConnection dbConn = null;
1834
		int serialNumber = -1;
1835
		docId = DocumentUtil.getDocIdFromString(docId);
1836
		PreparedStatement pstmt = null;
1837
		int serverLocation;
1838
		try {
1839
			//get conection
1840
			dbConn = DBConnectionPool.getDBConnection("ReplicationHandler.getHomeServer");
1841
			serialNumber = dbConn.getCheckOutSerialNumber();
1842
			//get a server location from xml_document table
1843
			pstmt = dbConn.prepareStatement("select server_location from xml_documents "
1844
					+ "where docid = ?");
1845
			pstmt.setString(1, docId);
1846
			pstmt.execute();
1847
			ResultSet serverName = pstmt.getResultSet();
1848
			//get a server location
1849
			if (serverName.next()) {
1850
				serverLocation = serverName.getInt(1);
1851
				pstmt.close();
1852
			} else {
1853
				pstmt.close();
1854
				//ut.returnConnection(conn);
1855
				return null;
1856
			}
1857
			pstmt = dbConn.prepareStatement("select server, last_checked, replicate "
1858
					+ "from xml_replication where serverid = ?");
1859
			//increase usage count
1860
			dbConn.increaseUsageCount(1);
1861
			pstmt.setInt(1, serverLocation);
1862
			pstmt.execute();
1863
			ResultSet rs = pstmt.getResultSet();
1864
			boolean tableHasRows = rs.next();
1865
			if (tableHasRows) {
1866

    
1867
				String server = rs.getString(1);
1868
				String last_checked = rs.getString(2);
1869
				if (!server.equals("localhost")) {
1870
					sl.put(server, last_checked);
1871
				}
1872

    
1873
			} else {
1874
				pstmt.close();
1875
				//ut.returnConnection(conn);
1876
				return null;
1877
			}
1878
			pstmt.close();
1879
		} catch (Exception e) {
1880
			logMetacat.error("ReplicationService.getHomeServerInfoForDocId - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
1881
			logReplication.error("ReplicationService.getHomeServerInfoForDocId - error in replicationHandler.getHomeServer(): "
1882
					+ e.getMessage());
1883
		} finally {
1884
			try {
1885
				pstmt.close();
1886
				//ut.returnConnection(conn);
1887
			} catch (Exception ee) {
1888
				logMetacat.error("ReplicationService.getHomeServerInfoForDocId - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
1889
				logReplication.error("ReplicationService.getHomeServerInfoForDocId - Eror irn rplicationHandler.getHomeServer() "
1890
						+ "to close pstmt: " + ee.getMessage());
1891
			} finally {
1892
				DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1893
			}
1894

    
1895
		}//finally
1896
		return sl;
1897
	}
1898

    
1899
	/**
1900
	 * Returns a home server location  given a accnum
1901
	 * @param accNum , given accNum for a document
1902
	 *
1903
	 */
1904
	public static int getHomeServerCodeForDocId(String accNum) throws ServiceException {
1905
		DBConnection dbConn = null;
1906
		int serialNumber = -1;
1907
		PreparedStatement pstmt = null;
1908
		int serverCode = 1;
1909
		String docId = DocumentUtil.getDocIdFromString(accNum);
1910

    
1911
		try {
1912

    
1913
			// Get DBConnection
1914
			dbConn = DBConnectionPool
1915
					.getDBConnection("ReplicationHandler.getServerLocation");
1916
			serialNumber = dbConn.getCheckOutSerialNumber();
1917
			pstmt = dbConn.prepareStatement("SELECT server_location FROM xml_documents "
1918
					+ "WHERE docid LIKE '" + docId + "'");
1919
			pstmt.execute();
1920
			ResultSet rs = pstmt.getResultSet();
1921
			boolean tablehasrows = rs.next();
1922
			//If a document is find, return the server location for it
1923
			if (tablehasrows) {
1924
				serverCode = rs.getInt(1);
1925
				pstmt.close();
1926
				//conn.close();
1927
				return serverCode;
1928
			}
1929
			//if couldn't find in xml_documents table, we think server code is 1
1930
			//(this is new document)
1931
			else {
1932
				pstmt.close();
1933
				//conn.close();
1934
				return serverCode;
1935
			}
1936

    
1937
		} catch (SQLException sqle) {
1938
			throw new ServiceException("ReplicationService.getHomeServerCodeForDocId - " 
1939
					+ "SQL error when getting home server code for docid: " + docId + " : " 
1940
					+ sqle.getMessage());
1941

    
1942
		} finally {
1943
			try {
1944
				pstmt.close();
1945
				//conn.close();
1946

    
1947
			} catch (SQLException sqle) {
1948
				logMetacat.error("ReplicationService.getHomeServerCodeForDocId - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
1949
				logReplication.error("ReplicationService.getHomeServerCodeForDocId - ReplicationService.getHomeServerCodeForDocId - " 
1950
						+ "SQL error when getting home server code for docid: " + docId + " : " 
1951
						+ sqle.getMessage());
1952
			} finally {
1953
				DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1954
			}//finally
1955
		}//finally
1956
		//return serverCode;
1957
	}
1958

    
1959
	/**
1960
	 * This method returns the content of a url
1961
	 * @param u the url to return the content from
1962
	 * @return a string representing the content of the url
1963
	 * @throws java.io.IOException
1964
	 */
1965
	public static String getURLContent(URL u) throws java.io.IOException {
1966
	    logReplication.info("Getting url content from " + u.toString());
1967
		char istreamChar;
1968
		int istreamInt;
1969
		logReplication.info("ReplicationService.getURLContent - Before open the stream" + u.toString());
1970
		InputStream input = u.openStream();
1971
		logReplication.info("ReplicationService.getURLContent - After open the stream" + u.toString());
1972
		InputStreamReader istream = new InputStreamReader(input);
1973
		StringBuffer serverResponse = new StringBuffer();
1974
		while ((istreamInt = istream.read()) != -1) {
1975
			istreamChar = (char) istreamInt;
1976
			serverResponse.append(istreamChar);
1977
		}
1978
		istream.close();
1979
		input.close();
1980

    
1981
		return serverResponse.toString();
1982
	}
1983

    
1984
//	/**
1985
//	 * Method for writing replication messages to a log file specified in
1986
//	 * metacat.properties
1987
//	 */
1988
//	public static void replLog(String message) {
1989
//		try {
1990
//			FileOutputStream fos = new FileOutputStream(PropertyService
1991
//					.getProperty("replication.logdir")
1992
//					+ "/metacatreplication.log", true);
1993
//			PrintWriter pw = new PrintWriter(fos);
1994
//			SimpleDateFormat formatter = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
1995
//			java.util.Date localtime = new java.util.Date();
1996
//			String dateString = formatter.format(localtime);
1997
//			dateString += " :: " + message;
1998
//			// time stamp each entry
1999
//			pw.println(dateString);
2000
//			pw.flush();
2001
//		} catch (Exception e) {
2002
//			logReplication.error("error writing to replication log from "
2003
//					+ "MetacatReplication.replLog: " + e.getMessage());
2004
//			// e.printStackTrace(System.out);
2005
//		}
2006
//	}
2007

    
2008
//	/**
2009
//	 * Method for writing replication messages to a log file specified in
2010
//	 * metacat.properties
2011
//	 */
2012
//	public static void replErrorLog(String message) {
2013
//		try {
2014
//			FileOutputStream fos = new FileOutputStream(PropertyService
2015
//					.getProperty("replication.logdir")
2016
//					+ "/metacatreplicationerror.log", true);
2017
//			PrintWriter pw = new PrintWriter(fos);
2018
//			SimpleDateFormat formatter = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
2019
//			java.util.Date localtime = new java.util.Date();
2020
//			String dateString = formatter.format(localtime);
2021
//			dateString += " :: " + message;
2022
//			//time stamp each entry
2023
//			pw.println(dateString);
2024
//			pw.flush();
2025
//		} catch (Exception e) {
2026
//			logReplication.error("error writing to replication error log from "
2027
//					+ "MetacatReplication.replErrorLog: " + e.getMessage());
2028
//			//e.printStackTrace(System.out);
2029
//		}
2030
//	}
2031

    
2032
	/**
2033
	 * Returns true if the replicate field for server in xml_replication is 1.
2034
	 * Returns false otherwise
2035
	 */
2036
	public static boolean replToServer(String server) {
2037
		DBConnection dbConn = null;
2038
		int serialNumber = -1;
2039
		PreparedStatement pstmt = null;
2040
		try {
2041
			dbConn = DBConnectionPool.getDBConnection("MetacatReplication.repltoServer");
2042
			serialNumber = dbConn.getCheckOutSerialNumber();
2043
			pstmt = dbConn.prepareStatement("select replicate from "
2044
					+ "xml_replication where server like '" + server + "'");
2045
			pstmt.execute();
2046
			ResultSet rs = pstmt.getResultSet();
2047
			boolean tablehasrows = rs.next();
2048
			if (tablehasrows) {
2049
				int i = rs.getInt(1);
2050
				if (i == 1) {
2051
					pstmt.close();
2052
					//conn.close();
2053
					return true;
2054
				} else {
2055
					pstmt.close();
2056
					//conn.close();
2057
					return false;
2058
				}
2059
			}
2060
		} catch (SQLException sqle) {
2061
			logMetacat.error("ReplicationService.replToServer - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
2062
			logReplication.error("ReplicationService.replToServer - SQL error in MetacatReplication.replToServer: "
2063
					+ sqle.getMessage());
2064
		} finally {
2065
			try {
2066
				pstmt.close();
2067
				//conn.close();
2068
			}//try
2069
			catch (Exception ee) {
2070
				logMetacat.error("ReplicationService.replToServer - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
2071
				logReplication.error("ReplicationService.replToServer - Error in MetacatReplication.replToServer: "
2072
						+ ee.getMessage());
2073
			}//catch
2074
			finally {
2075
				DBConnectionPool.returnDBConnection(dbConn, serialNumber);
2076
			}//finally
2077
		}//finally
2078
		return false;
2079
		//the default if this server does not exist is to not replicate to it.
2080
	}
2081

    
2082
}
(6-6/7)