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: 2012-08-01 19:00:12 -0700 (Wed, 01 Aug 2012) $'
10
 * '$Revision: 7338 $'
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.ByteArrayInputStream;
30
import java.io.ByteArrayOutputStream;
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.security.PrivateKey;
44
import java.security.cert.X509Certificate;
45
import java.sql.PreparedStatement;
46
import java.sql.ResultSet;
47
import java.sql.SQLException;
48
import java.sql.Timestamp;
49
import java.util.Calendar;
50
import java.util.Date;
51
import java.util.Hashtable;
52
import java.util.Timer;
53
import java.util.Vector;
54

    
55
import javax.servlet.http.HttpServletRequest;
56
import javax.servlet.http.HttpServletResponse;
57

    
58
import org.apache.http.HttpResponse;
59
import org.apache.http.conn.scheme.Scheme;
60
import org.apache.http.conn.ssl.SSLSocketFactory;
61
import org.apache.log4j.Logger;
62
import org.dataone.client.RestClient;
63
import org.dataone.client.auth.CertificateManager;
64
import org.dataone.service.types.v1.SystemMetadata;
65
import org.dataone.service.util.DateTimeMarshaller;
66
import org.dataone.service.util.TypeMarshaller;
67
import org.jibx.runtime.JiBXException;
68
import org.xml.sax.InputSource;
69
import org.xml.sax.SAXException;
70
import org.xml.sax.XMLReader;
71

    
72
import edu.ucsb.nceas.metacat.DocInfoHandler;
73
import edu.ucsb.nceas.metacat.DocumentImpl;
74
import edu.ucsb.nceas.metacat.DocumentImplWrapper;
75
import edu.ucsb.nceas.metacat.EventLog;
76
import edu.ucsb.nceas.metacat.IdentifierManager;
77
import edu.ucsb.nceas.metacat.McdbDocNotFoundException;
78
import edu.ucsb.nceas.metacat.McdbException;
79
import edu.ucsb.nceas.metacat.accesscontrol.AccessControlException;
80
import edu.ucsb.nceas.metacat.accesscontrol.AccessControlForSingleFile;
81
import edu.ucsb.nceas.metacat.accesscontrol.PermOrderException;
82
import edu.ucsb.nceas.metacat.accesscontrol.XMLAccessDAO;
83
import edu.ucsb.nceas.metacat.admin.upgrade.dataone.GenerateORE;
84
import edu.ucsb.nceas.metacat.admin.upgrade.dataone.GenerateSystemMetadata;
85
import edu.ucsb.nceas.metacat.client.InsufficientKarmaException;
86
import edu.ucsb.nceas.metacat.database.DBConnection;
87
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
88
import edu.ucsb.nceas.metacat.database.DatabaseService;
89
import edu.ucsb.nceas.metacat.dataone.hazelcast.HazelcastService;
90
import edu.ucsb.nceas.metacat.properties.PropertyService;
91
import edu.ucsb.nceas.metacat.shared.BaseService;
92
import edu.ucsb.nceas.metacat.shared.HandlerException;
93
import edu.ucsb.nceas.metacat.shared.ServiceException;
94
import edu.ucsb.nceas.metacat.util.DocumentUtil;
95
import edu.ucsb.nceas.metacat.util.MetacatUtil;
96
import edu.ucsb.nceas.metacat.util.ReplicationUtil;
97
import edu.ucsb.nceas.metacat.util.SystemUtil;
98
import edu.ucsb.nceas.utilities.FileUtil;
99
import edu.ucsb.nceas.utilities.GeneralPropertyException;
100
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
101

    
102
public class ReplicationService extends BaseService {
103

    
104
	private static ReplicationService replicationService = null;
105

    
106
	private long timeInterval;
107
	private Date firstTime;
108
	private boolean timedReplicationIsOn = false;
109
	Timer replicationDaemon;
110
	private static Vector<String> fileLocks = new Vector<String>();
111
//	private Thread lockThread = null;
112
	public static final String FORCEREPLICATEDELETE = "forcereplicatedelete";
113
	private static String TIMEREPLICATION = "replication.timedreplication";
114
	private static String TIMEREPLICATIONINTERVAl ="replication.timedreplicationinterval";
115
	private static String FIRSTTIME = "replication.firsttimedreplication";
116
	private static final int TIMEINTERVALLIMIT = 7200000;
117
	public static final String REPLICATIONUSER = "replication";
118
	
119
	private static int CLIENTTIMEOUT = 30000;
120

    
121
	public static final String REPLICATION_LOG_FILE_NAME = "metacatreplication.log";
122

    
123
	private static Object DATA_FILE_FLAG = null;
124
	public static String METACAT_REPL_ERROR_MSG = null;
125
	private static Logger logReplication = Logger.getLogger("ReplicationLogging");
126
	private static Logger logMetacat = Logger.getLogger(ReplicationService.class);
127

    
128
	static {
129
		// lookup the client timeout
130
		String clientTimeout = null;
131
		try {
132
			clientTimeout = PropertyService.getProperty("replication.client.timeout");
133
			CLIENTTIMEOUT = Integer.parseInt(clientTimeout);
134
		} catch (Exception e) {
135
			// just use default
136
			logReplication.warn("No custom client timeout specified in configuration, using default." + e.getMessage());
137
		}
138
		try {
139
			DATA_FILE_FLAG = PropertyService.getProperty("replication.datafileflag");
140
		} catch (PropertyNotFoundException e) {
141
			logReplication.error("No 'replication.datafileflag' specified in configuration." + e.getMessage());
142
		}
143

    
144
	}
145
	
146
	private ReplicationService() throws ServiceException {
147
		_serviceName = "ReplicationService";
148
		
149
		initialize();
150
	}
151
	
152
	private void initialize() throws ServiceException {
153
				
154
		// initialize db connections to handle any update requests
155
		// deltaT = util.getProperty("replication.deltaT");
156
		// the default deltaT can be set from metacat.properties
157
		// create a thread to do the delta-T check but don't execute it yet
158
		replicationDaemon = new Timer(true);
159
		try {
160
			String replLogFile = PropertyService.getProperty("replication.logdir")
161
				+ FileUtil.getFS() + REPLICATION_LOG_FILE_NAME;
162
			METACAT_REPL_ERROR_MSG = "An error occurred in replication.  Please see the " +
163
				"replication log at: " + replLogFile;
164
			
165
			String timedRepIsOnStr = 
166
				PropertyService.getProperty("replication.timedreplication");
167
			timedReplicationIsOn = (new Boolean(timedRepIsOnStr)).booleanValue();
168
			logReplication.info("ReplicationService.initialize - The timed replication on is" + timedReplicationIsOn);
169

    
170
			String timeIntervalStr = 
171
				PropertyService.getProperty("replication.timedreplicationinterval");
172
			timeInterval = (new Long(timeIntervalStr)).longValue();
173
			logReplication.info("ReplicationService.initialize - The timed replication time Interval is " + timeInterval);
174

    
175
			String firstTimeStr = 
176
				PropertyService.getProperty("replication.firsttimedreplication");
177
			logReplication.info("ReplicationService.initialize - first replication time form property is " + firstTimeStr);
178
			firstTime = ReplicationHandler.combinateCurrentDateAndGivenTime(firstTimeStr);
179

    
180
			logReplication.info("ReplicationService.initialize - After combine current time, the real first time is "
181
					+ firstTime.toString() + " minisec");
182

    
183
			// set up time replication if it is on
184
			if (timedReplicationIsOn) {
185
				replicationDaemon.scheduleAtFixedRate(new ReplicationHandler(),
186
						firstTime, timeInterval);
187
				logReplication.info("ReplicationService.initialize - deltaT handler started with rate="
188
						+ timeInterval + " mini seconds at " + firstTime.toString());
189
			}
190

    
191
		} catch (PropertyNotFoundException pnfe) {
192
			throw new ServiceException(
193
					"ReplicationService.initialize - Property error while instantiating "
194
							+ "replication service: " + pnfe.getMessage());
195
		} catch (HandlerException he) {
196
			throw new ServiceException(
197
					"ReplicationService.initialize - Handler error while instantiating "
198
							+ "replication service" + he.getMessage());
199
		} 
200
	}
201

    
202
	/**
203
	 * Get the single instance of SessionService.
204
	 * 
205
	 * @return the single instance of SessionService
206
	 */
207
	public static ReplicationService getInstance() throws ServiceException {
208
		if (replicationService == null) {
209
			replicationService = new ReplicationService();
210
		}
211
		return replicationService;
212
	}
213

    
214
	public boolean refreshable() {
215
		return true;
216
	}
217

    
218
	protected void doRefresh() throws ServiceException {
219
		return;
220
	}
221
	
222
	public void stop() throws ServiceException{
223
		
224
	}
225

    
226
	public void stopReplication() throws ServiceException {
227
	      //stop the replication server
228
	      replicationDaemon.cancel();
229
	      replicationDaemon = new Timer(true);
230
	      timedReplicationIsOn = false;
231
	      try {
232
	    	  PropertyService.setProperty("replication.timedreplication", (new Boolean(timedReplicationIsOn)).toString());
233
	      } catch (GeneralPropertyException gpe) {
234
	    	  logReplication.warn("ReplicationService.stopReplication - Could not set replication.timedreplication property: " + gpe.getMessage());
235
	      }
236

    
237
	      logReplication.info("ReplicationService.stopReplication - deltaT handler stopped");
238
		return;
239
	}
240
	
241
	public void startReplication(Hashtable<String, String[]> params) throws ServiceException {
242

    
243
	       String firstTimeStr = "";
244
	      //start the replication server
245
	       if ( params.containsKey("rate") ) {
246
	        timeInterval = new Long(
247
	               new String(((String[])params.get("rate"))[0])).longValue();
248
	        if(timeInterval < TIMEINTERVALLIMIT) {
249
	            //deltaT<30 is a timing mess!
250
	            timeInterval = TIMEINTERVALLIMIT;
251
	            throw new ServiceException("Replication deltaT rate cannot be less than "+
252
	                    TIMEINTERVALLIMIT + " millisecs and system automatically setup the rate to "+TIMEINTERVALLIMIT);
253
	        }
254
	      } else {
255
	        timeInterval = TIMEINTERVALLIMIT ;
256
	      }
257
	      logReplication.info("ReplicationService.startReplication - New rate is: " + timeInterval + " mini seconds.");
258
	      if ( params.containsKey("firsttime"))
259
	      {
260
	         firstTimeStr = ((String[])params.get("firsttime"))[0];
261
	         try
262
	         {
263
	           firstTime = ReplicationHandler.combinateCurrentDateAndGivenTime(firstTimeStr);
264
	           logReplication.info("ReplicationService.startReplication - The first time setting is "+firstTime.toString());
265
	         }
266
	         catch (HandlerException e)
267
	         {
268
	            throw new ServiceException(e.getMessage());
269
	         }
270
	         logReplication.warn("After combine current time, the real first time is "
271
	                                  +firstTime.toString()+" minisec");
272
	      }
273
	      else
274
	      {
275
	    	  logMetacat.error("ReplicationService.startReplication - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
276
	          logReplication.error("ReplicationService.startReplication - You should specify the first time " +
277
	                                  "to start a time replication");
278
	          return;
279
	      }
280
	      
281
	      timedReplicationIsOn = true;
282
	      try {
283
	      // save settings to property file
284
	      PropertyService.setProperty(TIMEREPLICATION, (new Boolean(timedReplicationIsOn)).toString());
285
	      // note we couldn't use firstTime object because it has date info
286
	      // we only need time info such as 10:00 PM
287
	      PropertyService.setProperty(FIRSTTIME, firstTimeStr);
288
	      PropertyService.setProperty(TIMEREPLICATIONINTERVAl, (new Long(timeInterval)).toString());
289
	      } catch (GeneralPropertyException gpe) {
290
	    	  logReplication.warn("ReplicationService.startReplication - Could not set property: " + gpe.getMessage());
291
	      }
292
	      replicationDaemon.cancel();
293
	      replicationDaemon = new Timer(true);
294
	      replicationDaemon.scheduleAtFixedRate(new ReplicationHandler(), firstTime,
295
	                                            timeInterval);
296
	      
297
	      logReplication.info("ReplicationService.startReplication - deltaT handler started with rate=" +
298
	                                    timeInterval + " milliseconds at " +firstTime.toString());
299

    
300
	}
301
	
302
	public void runOnce() throws ServiceException {
303
	      //updates this server exactly once
304
	      replicationDaemon.schedule(new ReplicationHandler(), 0);
305
	}
306
	
307
	/**
308
	 * This method can add, delete and list the servers currently included in
309
	 * xml_replication.
310
	 * action           subaction            other needed params
311
	 * ---------------------------------------------------------
312
	 * servercontrol    add                  server
313
	 * servercontrol    delete               server
314
	 * servercontrol    list
315
	 */
316
	public static void handleServerControlRequest(
317
			Hashtable<String, String[]> params, HttpServletRequest request, HttpServletResponse response) {
318
		String subaction = ((String[]) params.get("subaction"))[0];
319
		DBConnection dbConn = null;
320
		int serialNumber = -1;
321
		PreparedStatement pstmt = null;
322
		String replicate = null;
323
		String server = null;
324
		String dataReplicate = null;
325
		String hub = null;
326
		Writer out = null;
327
		boolean showGenerateSystemMetadata = false;
328
		try {
329
			response.setContentType("text/xml");
330
			out = response.getWriter();
331
			
332
			//conn = util.openDBConnection();
333
			dbConn = DBConnectionPool
334
					.getDBConnection("MetacatReplication.handleServerControlRequest");
335
			serialNumber = dbConn.getCheckOutSerialNumber();
336

    
337
			// add server to server list
338
			if (subaction.equals("add")) {
339
				replicate = ((String[]) params.get("replicate"))[0];
340
				server = ((String[]) params.get("server"))[0];
341

    
342
				//Get data replication value
343
				dataReplicate = ((String[]) params.get("datareplicate"))[0];
344
				
345
				//Get hub value
346
				hub = ((String[]) params.get("hub"))[0];
347

    
348
				Calendar cal = Calendar.getInstance();
349
                cal.set(1980, 1, 1);
350
				String sql = "INSERT INTO xml_replication "
351
						+ "(server, last_checked, replicate, datareplicate, hub) "
352
						+ "VALUES (?,?,?,?,?)";
353
				
354
				pstmt = dbConn.prepareStatement(sql);
355
						
356
				pstmt.setString(1, server);
357
				pstmt.setTimestamp(2, new Timestamp(cal.getTimeInMillis()));
358
				pstmt.setInt(3, Integer.parseInt(replicate));
359
				pstmt.setInt(4, Integer.parseInt(dataReplicate));
360
				pstmt.setInt(5, Integer.parseInt(hub));
361
				
362
				String sqlReport = "XMLAccessAccess.getXMLAccessForDoc - SQL: " + sql;
363
				sqlReport += " [" + server + "," + replicate + 
364
					"," + dataReplicate + "," + hub + "]";
365
				
366
				logMetacat.info(sqlReport);
367
				
368
				pstmt.execute();
369
				pstmt.close();
370
				dbConn.commit();
371
				out.write("Server " + server + " added");
372
				
373
				
374
				// delete server from server list
375
			} else if (subaction.equals("delete")) {
376
				server = ((String[]) params.get("server"))[0];
377
				pstmt = dbConn.prepareStatement("DELETE FROM xml_replication "
378
						+ "WHERE server LIKE ?");
379
				pstmt.setString(1, server);
380
				pstmt.execute();
381
				pstmt.close();
382
				dbConn.commit();
383
				out.write("Server " + server + " deleted");
384
			} else if (subaction.equals("list")) {
385
				// nothing special - it's the default behavior
386

    
387
			} else if (subaction.equals("generatesystemmetadata")) {
388
				GenerateSystemMetadata gsm = new GenerateSystemMetadata();
389
				int serverLocation = -1;
390
				String serverid = ((String[]) params.get("serverid"))[0];
391
				serverLocation = Integer.parseInt(serverid);
392
				gsm.setServerLocation(serverLocation );
393
				gsm.multiThreadUpgrade();
394
				out.write("System Metadata generated for server " + serverid);
395
				
396
			} else if (subaction.equals("generateore")) {
397
				GenerateORE gore = new GenerateORE();
398
				int serverLocation = -1;
399
				String serverid = ((String[]) params.get("serverid"))[0];
400
				serverLocation = Integer.parseInt(serverid);
401
				gore.setServerLocation(serverLocation );
402
				gore.upgrade();
403
				out.write("Generated ORE maps for server " + serverid);
404
				
405
			} else {
406
			
407
				out.write("<error>Unkonwn subaction</error>");
408
				return;
409
			}
410
			
411
			// show SM generate button?
412
			String dataoneConfigured = PropertyService.getProperty("configutil.dataoneConfigured");
413
			if (dataoneConfigured != null) {
414
				showGenerateSystemMetadata = Boolean.parseBoolean(dataoneConfigured);
415
			}
416
			
417
			// always list them after processing
418
			response.setContentType("text/html");
419
			out.write("<html><body><table border=\"1\">");
420
			out.write("<tr><td><b>server</b></td>");
421
			out.write("<td><b>last_checked</b></td>");
422
			out.write("<td><b>replicate</b></td>");
423
			out.write("<td><b>datareplicate</b></td>");
424
			out.write("<td><b>hub</b></td>");
425
			if (showGenerateSystemMetadata) {
426
				out.write("<td><b>System Metadata</b></td>");
427
				out.write("<td><b>ORE Maps</b></td>");
428
			}
429
			out.write("</tr>");
430

    
431
			pstmt = dbConn.prepareStatement("SELECT serverid, server, last_checked, replicate, datareplicate, hub FROM xml_replication");
432
			pstmt.execute();
433
			ResultSet rs = pstmt.getResultSet();
434
			boolean tablehasrows = rs.next();
435
			while (tablehasrows) {
436
				out.write("<tr><td>" + rs.getString(2) + "</td><td>");
437
				out.write(rs.getString(3) + "</td><td>");
438
				out.write(rs.getString(4) + "</td><td>");
439
				out.write(rs.getString(5) + "</td><td>");
440
				out.write(rs.getString(6) + "</td>");
441
				if (showGenerateSystemMetadata) {
442
					// for SM
443
					out.write("<td><form action='" + request.getContextPath() + "/admin'>");
444
					out.write("<input name='serverid' type='hidden' value='" + rs.getString(1) + "'/>");
445
					out.write("<input name='configureType' type='hidden' value='replication'/>");
446
					out.write("<input name='action' type='hidden' value='servercontrol'/>");
447
					out.write("<input name='subaction' type='hidden' value='generatesystemmetadata'/>");
448
					out.write("<input type='submit' value='Generate System Metadata'/>");
449
					out.write("</form></td>");
450
					
451
					// for ORE maps
452
					out.write("<td><form action='" + request.getContextPath() + "/admin'>");
453
					out.write("<input name='serverid' type='hidden' value='" + rs.getString(1) + "'/>");
454
					out.write("<input name='configureType' type='hidden' value='replication'/>");
455
					out.write("<input name='action' type='hidden' value='servercontrol'/>");
456
					out.write("<input name='subaction' type='hidden' value='generateore'/>");
457
					out.write("<input type='submit' value='Generate ORE'/>");
458
					out.write("</form></td>");
459
				}
460
				out.write("</tr>");
461

    
462
				tablehasrows = rs.next();
463
			}
464
			out.write("</table></body></html>");
465
			
466
			
467
			pstmt.close();
468
			//conn.close();
469

    
470
		} catch (Exception e) {
471
			logMetacat.error("ReplicationService.handleServerControlRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
472
			logReplication.error("ReplicationService.handleServerControlRequest - Error in "
473
					+ "MetacatReplication.handleServerControlRequest " + e.getMessage());
474
			e.printStackTrace(System.out);
475
		} finally {
476
			try {
477
				pstmt.close();
478
			}//try
479
			catch (SQLException ee) {
480
				logMetacat.error("ReplicationService.handleServerControlRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
481
				logReplication.error("ReplicationService.handleServerControlRequest - Error in MetacatReplication.handleServerControlRequest to close pstmt "
482
						+ ee.getMessage());
483
			}//catch
484
			finally {
485
				DBConnectionPool.returnDBConnection(dbConn, serialNumber);
486
			}//finally
487
			if (out != null) {
488
				try {
489
					out.close();
490
				} catch (IOException e) {
491
					logMetacat.error(e.getMessage(), e);
492
				}
493
			}
494
		}//finally
495

    
496
	}
497

    
498
	/**
499
	 * when a forcereplication request comes in, local host sends a read request
500
	 * to the requesting server (remote server) for the specified docid. Then
501
	 * store it in local database.
502
	 */
503
	protected static void handleForceReplicateRequest(
504
			Hashtable<String, String[]> params, HttpServletResponse response,
505
			HttpServletRequest request) {
506
		String server = ((String[]) params.get("server"))[0]; // the server that
507
		String docid = ((String[]) params.get("docid"))[0]; // sent the document
508
		String dbaction = "UPDATE"; // the default action is UPDATE
509
		//    boolean override = false;
510
		//    int serverCode = 1;
511
		DBConnection dbConn = null;
512
		int serialNumber = -1;
513
		String docName = null;
514

    
515
		try {
516
			//if the url contains a dbaction then the default action is overridden
517
			if (params.containsKey("dbaction")) {
518
				dbaction = ((String[]) params.get("dbaction"))[0];
519
				//serverCode = MetacatReplication.getServerCode(server);
520
				//override = true; //we are now overriding the default action
521
			}
522
			logReplication.info("ReplicationService.handleForceReplicateRequest - Force replication request from: " + server);
523
			logReplication.info("ReplicationService.handleForceReplicateRequest - Force replication docid: " + docid);
524
			logReplication.info("ReplicationService.handleForceReplicateRequest - Force replication action: " + dbaction);
525
			// sending back read request to remote server
526
			URL u = new URL("https://" + server + "?server="
527
					+ MetacatUtil.getLocalReplicationServerName() + "&action=read&docid="
528
					+ docid);
529
			String xmldoc = ReplicationService.getURLContent(u);
530

    
531
			// get the document info from server
532
			URL docinfourl = new URL("https://" + server + "?server="
533
					+ MetacatUtil.getLocalReplicationServerName()
534
					+ "&action=getdocumentinfo&docid=" + docid);
535
			
536

    
537
			String docInfoStr = ReplicationService.getURLContent(docinfourl);
538
			// strip out the system metadata portion
539
			String systemMetadataXML = ReplicationUtil.getSystemMetadataContent(docInfoStr);
540
			docInfoStr = ReplicationUtil.getContentWithoutSystemMetadata(docInfoStr);
541
		   	  
542
			//dih is the parser for the docinfo xml format
543
			DocInfoHandler dih = new DocInfoHandler();
544
			XMLReader docinfoParser = ReplicationHandler.initParser(dih);
545
			docinfoParser.parse(new InputSource(new StringReader(docInfoStr)));
546
			//      Hashtable<String,Vector<AccessControlForSingleFile>> docinfoHash = dih.getDocInfo();
547
			Hashtable<String, String> docinfoHash = dih.getDocInfo();
548
			
549
			// Get home server of this docid
550
			String homeServer = (String) docinfoHash.get("home_server");
551
			
552
			// process system metadata
553
			if (systemMetadataXML != null) {
554
				SystemMetadata sysMeta = 
555
					TypeMarshaller.unmarshalTypeFromStream(
556
							SystemMetadata.class,
557
							new ByteArrayInputStream(systemMetadataXML.getBytes("UTF-8")));
558
				// need the guid-to-docid mapping
559
				boolean mappingExists = true;
560
		      	mappingExists = IdentifierManager.getInstance().mappingExists(sysMeta.getIdentifier().getValue());
561
		      	if (!mappingExists) {
562
		      		IdentifierManager.getInstance().createMapping(sysMeta.getIdentifier().getValue(), docid);
563
		      	}
564
				// save the system metadata
565
				HazelcastService.getInstance().getSystemMetadataMap().put(sysMeta.getIdentifier(), sysMeta);
566
			}
567
      
568
			// dates
569
			String createdDateString = docinfoHash.get("date_created");
570
			String updatedDateString = docinfoHash.get("date_updated");
571
			Date createdDate = DateTimeMarshaller.deserializeDateToUTC(createdDateString);
572
			Date updatedDate = DateTimeMarshaller.deserializeDateToUTC(updatedDateString);
573
		      
574
			logReplication.info("ReplicationService.handleForceReplicateRequest - homeServer: " + homeServer);
575
			// Get Document type
576
			String docType = (String) docinfoHash.get("doctype");
577
			logReplication.info("ReplicationService.handleForceReplicateRequest - docType: " + docType);
578
			String parserBase = null;
579
			// this for eml2 and we need user eml2 parser
580
			if (docType != null
581
					&& (docType.trim()).equals(DocumentImpl.EML2_0_0NAMESPACE)) {
582
				logReplication.warn("ReplicationService.handleForceReplicateRequest - This is an eml200 document!");
583
				parserBase = DocumentImpl.EML200;
584
			} else if (docType != null
585
					&& (docType.trim()).equals(DocumentImpl.EML2_0_1NAMESPACE)) {
586
				logReplication.warn("ReplicationService.handleForceReplicateRequest - This is an eml2.0.1 document!");
587
				parserBase = DocumentImpl.EML200;
588
			} else if (docType != null
589
					&& (docType.trim()).equals(DocumentImpl.EML2_1_0NAMESPACE)) {
590
				logReplication.warn("ReplicationService.handleForceReplicateRequest - This is an eml2.1.0 document!");
591
				parserBase = DocumentImpl.EML210;
592
			} else if (docType != null
593
					&& (docType.trim()).equals(DocumentImpl.EML2_1_1NAMESPACE)) {
594
				logReplication.warn("ReplicationService.handleForceReplicateRequest - This is an eml2.1.1 document!");
595
				parserBase = DocumentImpl.EML210;
596
			}
597
			logReplication.warn("ReplicationService.handleForceReplicateRequest - The parserBase is: " + parserBase);
598

    
599
			// Get DBConnection from pool
600
			dbConn = DBConnectionPool
601
					.getDBConnection("MetacatReplication.handleForceReplicateRequest");
602
			serialNumber = dbConn.getCheckOutSerialNumber();
603
			// write the document to local database
604
			DocumentImplWrapper wrapper = new DocumentImplWrapper(parserBase, false, false);
605
			//try this independently so we can set access even if the update action is invalid (i.e docid has not changed)
606
			try {
607
				wrapper.writeReplication(dbConn, xmldoc, null, null,
608
						dbaction, docid, null, null, homeServer, server, createdDate,
609
						updatedDate);
610
			} finally {
611

    
612
				//process extra access rules before dealing with the write exception (doc exist already)			
613
		        Vector<XMLAccessDAO> accessControlList = dih.getAccessControlList();
614
		        if (accessControlList != null) {
615
		        	AccessControlForSingleFile acfsf = new AccessControlForSingleFile(docid);
616
		        	for (XMLAccessDAO xmlAccessDAO : accessControlList) {
617
		        		try {
618
			        		if (!acfsf.accessControlExists(xmlAccessDAO)) {
619
			        			acfsf.insertPermissions(xmlAccessDAO);
620
								logReplication.info("ReplicationService.handleForceReplicateRequest - document " + docid
621
										+ " permissions added to DB");
622
			        		}
623
		        		} catch (PermOrderException poe) {
624
		        			// this is problematic, but should not prevent us from replicating
625
		        			// see https://redmine.dataone.org/issues/2583
626
		        			String msg = "Could not insert access control for: " + docid + " Message: " + poe.getMessage();
627
		        			logMetacat.error(msg, poe);
628
		        			logReplication.error(msg, poe);
629
		        		}
630
		            }
631
		        }
632
		        
633
		        // process the real owner and updater
634
				String user = (String) docinfoHash.get("user_owner");
635
				String updated = (String) docinfoHash.get("user_updated");
636
		        updateUserOwner(dbConn, docid, user, updated);
637

    
638
				logReplication.info("ReplicationService.handleForceReplicateRequest - document " + docid + " added to DB with "
639
						+ "action " + dbaction);
640
				
641
				EventLog.getInstance().log(request.getRemoteAddr(), request.getHeader("User-Agent"), REPLICATIONUSER, docid, dbaction);
642
			}
643
		} catch (SQLException sqle) {
644
			logMetacat.error("ReplicationService.handleForceReplicateRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
645
			logReplication.error("ReplicationService.handleForceReplicateRequest - SQL error when adding doc " + docid + 
646
					" to DB with action " + dbaction + ": " + sqle.getMessage());
647
		} catch (MalformedURLException mue) {
648
			logMetacat.error("ReplicationService.handleForceReplicateRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
649
			logReplication.error("ReplicationService.handleForceReplicateRequest - URL error when adding doc " + docid + 
650
					" to DB with action " + dbaction + ": " + mue.getMessage());
651
		} catch (SAXException se) {
652
			logMetacat.error("ReplicationService.handleForceReplicateRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
653
			logReplication.error("ReplicationService.handleForceReplicateRequest - SAX parsing error when adding doc " + docid + 
654
					" to DB with action " + dbaction + ": " + se.getMessage());
655
		} catch (HandlerException he) {
656
			logMetacat.error("ReplicationService.handleForceReplicateRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
657
			logReplication.error("ReplicationService.handleForceReplicateRequest - Handler error when adding doc " + docid + 
658
					" to DB with action " + dbaction + ": " + he.getMessage());
659
		} catch (IOException ioe) {
660
			logMetacat.error("ReplicationService.handleForceReplicateRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
661
			logReplication.error("ReplicationService.handleForceReplicateRequest - I/O error when adding doc " + docid + 
662
					" to DB with action " + dbaction + ": " + ioe.getMessage());
663
		} catch (PermOrderException poe) {
664
			logMetacat.error("ReplicationService.handleForceReplicateRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
665
			logReplication.error("ReplicationService.handleForceReplicateRequest - Permissions order error when adding doc " + docid + 
666
					" to DB with action " + dbaction + ": " + poe.getMessage());
667
		} catch (AccessControlException ace) {
668
			logMetacat.error("ReplicationService.handleForceReplicateRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
669
			logReplication.error("ReplicationService.handleForceReplicateRequest - Permissions order error when adding doc " + docid + 
670
					" to DB with action " + dbaction + ": " + ace.getMessage());
671
		} catch (Exception e) {
672
			logMetacat.error("ReplicationService.handleForceReplicateRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
673
			logReplication.error("ReplicationService.handleForceReplicateRequest - General error when adding doc " + docid + 
674
					" to DB with action " + dbaction + ": " + e.getMessage());
675
		} finally {
676
			// Return the checked out DBConnection
677
			DBConnectionPool.returnDBConnection(dbConn, serialNumber);
678
		}//finally
679
	}
680

    
681
	/*
682
	 * when a forcereplication delete request comes in, local host will delete this
683
	 * document
684
	 */
685
	protected static void handleForceReplicateDeleteRequest(
686
			Hashtable<String, String[]> params, HttpServletResponse response,
687
			HttpServletRequest request) {
688
		String server = ((String[]) params.get("server"))[0]; // the server that
689
		String docid = ((String[]) params.get("docid"))[0]; // sent the document
690
		try {
691
			logReplication.info("ReplicationService.handleForceReplicateDeleteRequest - force replication delete request from " + server);
692
			logReplication.info("ReplicationService.handleForceReplicateDeleteRequest - force replication delete docid " + docid);
693
			logReplication.info("ReplicationService.handleForceReplicateDeleteRequest - Force replication delete request from: " + server);
694
			logReplication.info("ReplicationService.handleForceReplicateDeleteRequest - Force replication delete docid: " + docid);
695
			DocumentImpl.delete(docid, null, null, server, false);
696
			logReplication.info("ReplicationService.handleForceReplicateDeleteRequest - document " + docid + " was successfully deleted ");
697
			EventLog.getInstance().log(request.getRemoteAddr(), request.getHeader("User-Agent"), REPLICATIONUSER, docid,
698
					"delete");
699
			logReplication.info("ReplicationService.handleForceReplicateDeleteRequest - document " + docid + " was successfully deleted ");
700
		} catch (McdbDocNotFoundException e) {
701
			logMetacat.error("ReplicationService.handleForceReplicateDeleteRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
702
			logReplication.error("document " + docid
703
					+ " failed to delete because " + e.getMessage());
704
			logReplication.error("ReplicationService.handleForceReplicateDeleteRequest - error: " + e.getMessage());
705
		} catch (InsufficientKarmaException e) {
706
			logMetacat.error("ReplicationService.handleForceReplicateDeleteRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
707
			logReplication.error("document " + docid
708
					+ " failed to delete because " + e.getMessage());
709
			logReplication.error("ReplicationService.handleForceReplicateDeleteRequest - error: " + e.getMessage());
710
		} catch (SQLException e) {
711
			logMetacat.error("ReplicationService.handleForceReplicateDeleteRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
712
			logReplication.error("document " + docid
713
					+ " failed to delete because " + e.getMessage());
714
			logReplication.error("ReplicationService.handleForceReplicateDeleteRequest - error: " + e.getMessage());
715
		} catch (Exception e) {
716
			logMetacat.error("ReplicationService.handleForceReplicateDeleteRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
717
			logReplication.error("document " + docid
718
					+ " failed to delete because " + e.getMessage());
719
			logReplication.error("ReplicationService.handleForceReplicateDeleteRequest - error: " + e.getMessage());
720

    
721
		}//catch
722

    
723
	}
724

    
725
	/**
726
	 * when a forcereplication data file request comes in, local host sends a
727
	 * readdata request to the requesting server (remote server) for the specified
728
	 * docid. Then store it in local database and file system
729
	 */
730
	protected static void handleForceReplicateDataFileRequest(Hashtable<String, String[]> params,
731
			HttpServletRequest request) {
732

    
733
		//make sure there is some parameters
734
		if (params.isEmpty()) {
735
			return;
736
		}
737
		// Get remote server
738
		String server = ((String[]) params.get("server"))[0];
739
		// the docid should include rev number
740
		String docid = ((String[]) params.get("docid"))[0];
741
		// Make sure there is a docid and server
742
		if (docid == null || server == null || server.equals("")) {
743
			logMetacat.error("ReplicationService.handleForceReplicateDataFileRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
744
			logReplication.error("ReplicationService.handleForceReplicateDataFileRequest - Didn't specify docid or server for replication");
745
			return;
746
		}
747

    
748
		// Overide or not
749
		//    boolean override = false;
750
		// dbaction - update or insert
751
		String dbaction = null;
752

    
753
		try {
754
			//docid was switch to two parts uinque code and rev
755
			//String uniqueCode=MetacatUtil.getDocIdFromString(docid);
756
			//int rev=MetacatUtil.getVersionFromString(docid);
757
			if (params.containsKey("dbaction")) {
758
				dbaction = ((String[]) params.get("dbaction"))[0];
759
			} else//default value is update
760
			{
761
//				dbaction = "update";
762
				dbaction = null;
763
			}
764

    
765
			logReplication.info("ReplicationService.handleForceReplicateDataFileRequest - Force replication request from: " + server);
766
			logReplication.info("ReplicationService.handleForceReplicateDataFileRequest - Force replication docid: " + docid);
767
			logReplication.info("ReplicationService.handleForceReplicateDataFileRequest - Force replication action: " + dbaction);
768
			// get the document info from server
769
			URL docinfourl = new URL("https://" + server + "?server="
770
					+ MetacatUtil.getLocalReplicationServerName()
771
					+ "&action=getdocumentinfo&docid=" + docid);
772

    
773
			String docInfoStr = ReplicationService.getURLContent(docinfourl);
774
			
775
			// strip out the system metadata portion
776
		    String systemMetadataXML = ReplicationUtil.getSystemMetadataContent(docInfoStr);
777
		   	docInfoStr = ReplicationUtil.getContentWithoutSystemMetadata(docInfoStr);
778

    
779
			//dih is the parser for the docinfo xml format
780
			DocInfoHandler dih = new DocInfoHandler();
781
			XMLReader docinfoParser = ReplicationHandler.initParser(dih);
782
			docinfoParser.parse(new InputSource(new StringReader(docInfoStr)));
783
			Hashtable<String, String> docinfoHash = dih.getDocInfo();
784
			
785
			String docName = (String) docinfoHash.get("docname");
786

    
787
			String docType = (String) docinfoHash.get("doctype");
788

    
789
			String docHomeServer = (String) docinfoHash.get("home_server");
790
			
791
			String createdDateString = docinfoHash.get("date_created");
792
			String updatedDateString = docinfoHash.get("date_updated");
793
			
794
			Date createdDate = DateTimeMarshaller.deserializeDateToUTC(createdDateString);
795
			Date updatedDate = DateTimeMarshaller.deserializeDateToUTC(updatedDateString);
796
			
797
			logReplication.info("ReplicationService.handleForceReplicateDataFileRequest - docHomeServer of datafile: " + docHomeServer);
798

    
799
			// in case we have a write exception, we still want to track access and sysmeta
800
			Exception writeException = null;
801

    
802
			// do we need the object content?
803
			if (dbaction != null && (dbaction.equals("insert") || dbaction.equals("update"))) {
804
				//Get data file and store it into local file system.
805
				// sending back readdata request to server
806
				URL url = new URL("https://" + server + "?server="
807
						+ MetacatUtil.getLocalReplicationServerName()
808
						+ "&action=readdata&docid=" + docid);
809
				String datafilePath = PropertyService
810
						.getProperty("application.datafilepath");
811

    
812
				InputStream inputStream = getURLStream(url);
813
				
814
				//register data file into xml_documents table and write data file
815
				//into file system
816
				try {
817
					DocumentImpl.writeDataFileInReplication(inputStream,
818
							datafilePath, docName, docType, docid, null, docHomeServer,
819
							server, DocumentImpl.DOCUMENTTABLE, false, createdDate,
820
							updatedDate);
821
				} catch (Exception e) {
822
					writeException = e;
823
				}
824

    
825
			}
826
			
827
			// process the real owner and updater
828
			DBConnection dbConn = DBConnectionPool.getDBConnection("ReplicationService.handleForceDataFileRequest");
829
	        int serialNumber = dbConn.getCheckOutSerialNumber();
830
	        dbConn.setAutoCommit(false);
831
			String user = (String) docinfoHash.get("user_owner");
832
			String updated = (String) docinfoHash.get("user_updated");
833
	        updateUserOwner(dbConn, docid, user, updated);
834
	        DBConnectionPool.returnDBConnection(dbConn, serialNumber);
835
	        
836
			// process system metadata
837
	        if (systemMetadataXML != null) {
838
	      	  SystemMetadata sysMeta = 
839
	      		TypeMarshaller.unmarshalTypeFromStream(
840
	      				  SystemMetadata.class, 
841
	      				  new ByteArrayInputStream(systemMetadataXML.getBytes("UTF-8")));
842
	      	  
843
	      	  // need the guid-to-docid mapping
844
	      	  boolean mappingExists = true;
845
	      	  mappingExists = IdentifierManager.getInstance().mappingExists(sysMeta.getIdentifier().getValue());
846
	      	  if (!mappingExists) {
847
	      		  IdentifierManager.getInstance().createMapping(sysMeta.getIdentifier().getValue(), docid);
848
	      	  }
849
	      	  // save the system metadata
850
	      	  HazelcastService.getInstance().getSystemMetadataMap().put(sysMeta.getIdentifier(), sysMeta);
851
	        }
852
	        
853
	        // process the access control
854
	        Vector<XMLAccessDAO> accessControlList = dih.getAccessControlList();
855
	        if (accessControlList != null) {
856
	        	AccessControlForSingleFile acfsf = new AccessControlForSingleFile(docid);
857
	        	for (XMLAccessDAO xmlAccessDAO : accessControlList) {
858
	        		if (!acfsf.accessControlExists(xmlAccessDAO)) {
859
	        			acfsf.insertPermissions(xmlAccessDAO);
860
						logReplication.info("ReplicationService.handleForceReplicateRequest - document " + docid
861
								+ " permissions added to DB");
862
	        		}
863
	            }
864
	        }
865
	        
866
	        // throw the write exception now -- this happens when access changes on an object
867
			if (writeException != null) {
868
				throw writeException;
869
			}
870

    
871
			logReplication.info("ReplicationService.handleForceReplicateDataFileRequest - datafile " + docid + " added to DB with "
872
					+ "action " + dbaction);
873
			EventLog.getInstance().log(request.getRemoteAddr(), request.getHeader("User-Agent"), REPLICATIONUSER,
874
					docid, dbaction);
875

    
876
		} catch (Exception e) {
877
			e.printStackTrace();
878
			logMetacat.error("ReplicationService.handleForceReplicateDataFileRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG, e);                         
879
			logReplication.error("ReplicationService.handleForceReplicateDataFileRequest - Datafile " + docid
880
					+ " failed to added to DB with " + "action " + dbaction + " because "
881
					+ e.getMessage());
882
			logReplication.error("ReplicationService.handleForceReplicateDataFileRequest - ERROR in MetacatReplication.handleForceDataFileReplicate"
883
					+ "Request(): " + e.getMessage());
884
		}
885
	}
886

    
887
	/**
888
	 * Grants or denies a lock to a requesting host.
889
	 * The servlet parameters of interrest are:
890
	 * docid: the docid of the file the lock is being requested for
891
	 * currentdate: the timestamp of the document on the remote server
892
	 *
893
	 */
894
	protected static void handleGetLockRequest(
895
			Hashtable<String, String[]> params, HttpServletResponse response) {
896

    
897
		try {
898

    
899
			String docid = ((String[]) params.get("docid"))[0];
900
			String remoteRev = ((String[]) params.get("updaterev"))[0];
901
			DocumentImpl requestDoc = new DocumentImpl(docid);
902
			logReplication.info("ReplicationService.handleGetLockRequest - lock request for " + docid);
903
			int localRevInt = requestDoc.getRev();
904
			int remoteRevInt = Integer.parseInt(remoteRev);
905

    
906
			// get a writer for sending back to response
907
			response.setContentType("text/xml");
908
			Writer out = response.getWriter();
909
			
910
			if (remoteRevInt >= localRevInt) {
911
				if (!fileLocks.contains(docid)) { //grant the lock if it is not already locked
912
					fileLocks.add(0, docid); //insert at the beginning of the queue Vector
913
					//send a message back to the the remote host authorizing the insert
914
					out.write("<lockgranted><docid>" + docid
915
									+ "</docid></lockgranted>");
916
					//          lockThread = new Thread(this);
917
					//          lockThread.setPriority(Thread.MIN_PRIORITY);
918
					//          lockThread.start();
919
					logReplication.info("ReplicationService.handleGetLockRequest - lock granted for " + docid);
920
				} else { //deny the lock
921
					out.write("<filelocked><docid>" + docid + "</docid></filelocked>");
922
					logReplication.info("ReplicationService.handleGetLockRequest - lock denied for " + docid
923
							+ "reason: file already locked");
924
				}
925
			} else {//deny the lock.
926
				out.write("<outdatedfile><docid>" + docid + "</docid></filelocked>");
927
				logReplication.info("ReplicationService.handleGetLockRequest - lock denied for " + docid
928
						+ "reason: client has outdated file");
929
			}
930
			out.close();
931
			//conn.close();
932
		} catch (Exception e) {
933
			logMetacat.error("ReplicationService.handleGetLockRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
934
			logReplication.error("ReplicationService.handleGetLockRequest - error requesting file lock from MetacatReplication."
935
					+ "handleGetLockRequest: " + e.getMessage());
936
			e.printStackTrace(System.out);
937
		}
938
	}
939

    
940
	/**
941
	 * Sends all of the xml_documents information encoded in xml to a requestor
942
	 * the format is:
943
	 * <!ELEMENT documentinfo (docid, docname, doctype, doctitle, user_owner,
944
	 *                  user_updated, home_server, public_access, rev)/>
945
	 * all of the subelements of document info are #PCDATA
946
	 */
947
	protected static void handleGetDocumentInfoRequest(
948
			Hashtable<String, String[]> params, HttpServletResponse response) {
949
		String docid = ((String[]) (params.get("docid")))[0];
950

    
951
		try {
952
			// get docinfo as XML string
953
			String docinfoXML = getDocumentInfo(docid);
954
			
955
			// get a writer for sending back to response
956
			response.setContentType("text/xml");
957
			Writer out = response.getWriter();
958
			out.write(docinfoXML);
959
			out.close();
960

    
961
		} catch (Exception e) {
962
			logMetacat.error("ReplicationService.handleGetDocumentInfoRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
963
			logReplication.error("ReplicationService.handleGetDocumentInfoRequest - error in metacatReplication.handlegetdocumentinforequest "
964
					+ "for doc: " + docid + " : " + e.getMessage());
965
		}
966

    
967
	}
968
	
969
	public static Hashtable<String, String> getDocumentInfoMap(String docid)
970
			throws HandlerException, AccessControlException, JiBXException,
971
			IOException, McdbException, SAXException {
972
		
973
		// Try get docid info from remote server
974
		DocInfoHandler dih = new DocInfoHandler();
975
		XMLReader docinfoParser = ReplicationHandler.initParser(dih);
976

    
977
		String docInfoStr = getDocumentInfo(docid);
978

    
979
		// strip out the system metadata portion
980
		String systemMetadataXML = ReplicationUtil.getSystemMetadataContent(docInfoStr);
981
		docInfoStr = ReplicationUtil.getContentWithoutSystemMetadata(docInfoStr);
982

    
983
		docinfoParser.parse(new InputSource(new StringReader(docInfoStr)));
984
		Hashtable<String, String> docinfoHash = dih.getDocInfo();
985

    
986
		return docinfoHash;
987
	}
988
	
989
	/**
990
	 * Gets a docInfo XML snippet for the replication API
991
	 * @param docid
992
	 * @return
993
	 * @throws AccessControlException
994
	 * @throws JiBXException
995
	 * @throws IOException
996
	 * @throws McdbException
997
	 */
998
	public static String getDocumentInfo(String docid) throws AccessControlException, JiBXException, IOException, McdbException {
999
		StringBuffer sb = new StringBuffer();
1000

    
1001
		DocumentImpl doc = new DocumentImpl(docid);
1002
		sb.append("<documentinfo><docid>").append(docid);
1003
		sb.append("</docid>");
1004
		
1005
		try {
1006
			// serialize the System Metadata as XML for docinfo
1007
			String guid = IdentifierManager.getInstance().getGUID(doc.getDocID(), doc.getRev());
1008
			SystemMetadata systemMetadata = IdentifierManager.getInstance().getSystemMetadata(guid);
1009
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
1010
			TypeMarshaller.marshalTypeToOutputStream(systemMetadata, baos);
1011
			String systemMetadataXML = baos.toString("UTF-8");
1012
			sb.append("<systemMetadata>");
1013
			sb.append(systemMetadataXML);
1014
			sb.append("</systemMetadata>");
1015
		} catch(McdbDocNotFoundException e) {
1016
		  logMetacat.warn("No SystemMetadata found for: " + docid);
1017
		}
1018
		
1019
		Calendar created = Calendar.getInstance();
1020
		created.setTime(doc.getCreateDate());
1021
		Calendar updated = Calendar.getInstance();
1022
		updated.setTime(doc.getUpdateDate());
1023
		
1024
		sb.append("<docname>").append(doc.getDocname());
1025
		sb.append("</docname><doctype>").append(doc.getDoctype());
1026
		sb.append("</doctype>");
1027
		sb.append("<user_owner>").append(doc.getUserowner());
1028
		sb.append("</user_owner><user_updated>").append(doc.getUserupdated());
1029
		sb.append("</user_updated>");
1030
		sb.append("<date_created>");
1031
		sb.append(DateTimeMarshaller.serializeDateToUTC(doc.getCreateDate()));
1032
		sb.append("</date_created>");
1033
		sb.append("<date_updated>");
1034
		sb.append(DateTimeMarshaller.serializeDateToUTC(doc.getUpdateDate()));
1035
		sb.append("</date_updated>");
1036
		sb.append("<home_server>");
1037
		sb.append(doc.getDocHomeServer());
1038
		sb.append("</home_server>");
1039
		sb.append("<public_access>").append(doc.getPublicaccess());
1040
		sb.append("</public_access><rev>").append(doc.getRev());
1041
		sb.append("</rev>");
1042

    
1043
		sb.append("<accessControl>");
1044

    
1045
		AccessControlForSingleFile acfsf = new AccessControlForSingleFile(docid); 
1046
		sb.append(acfsf.getAccessString());
1047
		
1048
		sb.append("</accessControl>");
1049

    
1050
		sb.append("</documentinfo>");
1051
			
1052
		return sb.toString();
1053
	}
1054
	
1055
	/**
1056
	 * Sends System Metadata as XML
1057
	 */
1058
	protected static void handleGetSystemMetadataRequest(
1059
			Hashtable<String, String[]> params, HttpServletResponse response) {
1060
		String guid = ((String[]) (params.get("guid")))[0];
1061
		String systemMetadataXML = null;
1062
		try {
1063
			
1064
			// serialize the System Metadata as XML 
1065
			SystemMetadata systemMetadata = IdentifierManager.getInstance().getSystemMetadata(guid);
1066
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
1067
			TypeMarshaller.marshalTypeToOutputStream(systemMetadata, baos);
1068
			systemMetadataXML = baos.toString("UTF-8");
1069
				
1070
			// get a writer for sending back to response
1071
			response.setContentType("text/xml");
1072
			Writer out = response.getWriter();
1073
			out.write(systemMetadataXML);
1074
			out.close();
1075

    
1076
		} catch (Exception e) {
1077
			String msg = "ReplicationService.handleGetSystemMetadataRequest for guid: " + guid + " : " + e.getMessage();
1078
			logMetacat.error(msg);                         
1079
			logReplication.error(msg);
1080
		}
1081

    
1082
	}
1083
	
1084
	/**
1085
	 * when a forcereplication request comes in, local host sends a read request
1086
	 * to the requesting server (remote server) for the specified docid. Then
1087
	 * store it in local database.
1088
	 */
1089
	protected static void handleForceReplicateSystemMetadataRequest(
1090
			Hashtable<String, String[]> params, HttpServletResponse response,
1091
			HttpServletRequest request) {
1092
		String server = ((String[]) params.get("server"))[0]; // the server that
1093
		String guid = ((String[]) params.get("guid"))[0]; // sent the document
1094
		
1095
		try {
1096
			logReplication.info("ReplicationService.handleForceReplicateSystemMetadataRequest - Force replication system metadata request from: " + server);
1097
			// get the system metadata from server
1098
			URL docinfourl = new URL("https://" + server + "?server="
1099
					+ MetacatUtil.getLocalReplicationServerName()
1100
					+ "&action=getsystemmetadata&guid=" + guid);
1101
			
1102
			String systemMetadataXML = ReplicationService.getURLContent(docinfourl);
1103
						
1104
			// process system metadata
1105
			if (systemMetadataXML != null) {
1106
				SystemMetadata sysMeta = 
1107
					TypeMarshaller.unmarshalTypeFromStream(
1108
							SystemMetadata.class,
1109
							new ByteArrayInputStream(systemMetadataXML.getBytes("UTF-8")));
1110
				HazelcastService.getInstance().getSystemMetadataMap().put(sysMeta.getIdentifier(), sysMeta);
1111
			}
1112
      
1113
			logReplication.info("ReplicationService.handleForceReplicateSystemMetadataRequest - processed guid: " + guid);
1114
			EventLog.getInstance().log(request.getRemoteAddr(), request.getHeader("User-Agent"), REPLICATIONUSER, guid, "systemMetadata");
1115

    
1116
		} catch (Exception e) {
1117
			logMetacat.error("ReplicationService.handleForceReplicateRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG, e);                         
1118
			logReplication.error("ReplicationService.handleForceReplicateRequest - General error when processing guid: " + guid, e);
1119
		}
1120
	}
1121

    
1122
	/**
1123
	 * Sends a datafile to a remote host
1124
	 */
1125
	protected static void handleGetDataFileRequest(OutputStream outPut,
1126
			Hashtable<String, String[]> params, HttpServletResponse response)
1127

    
1128
	{
1129
		// File path for data file
1130
		String filepath;
1131
		// Request docid
1132
		String docId = ((String[]) (params.get("docid")))[0];
1133
		//check if the doicd is null
1134
		if (docId == null) {
1135
			logMetacat.error("ReplicationService.handleGetDataFileRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
1136
			logReplication.error("ReplicationService.handleGetDataFileRequest - Didn't specify docid for replication");
1137
			return;
1138
		}
1139

    
1140
		//try to open a https stream to test if the request server's public key
1141
		//in the key store, this is security issue
1142
		try {
1143
			filepath = PropertyService.getProperty("application.datafilepath");
1144
			String server = params.get("server")[0];
1145
			URL u = new URL("https://" + server + "?server="
1146
					+ MetacatUtil.getLocalReplicationServerName() + "&action=test");
1147
			String test = ReplicationService.getURLContent(u);
1148
			//couldn't pass the test
1149
			if (test.indexOf("successfully") == -1) {
1150
				//response.setContentType("text/xml");
1151
				//outPut.println("<error>Couldn't pass the trust test</error>");
1152
				logMetacat.error("ReplicationService.handleGetDataFileRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
1153
				logReplication.error("ReplicationService.handleGetDataFileRequest - Couldn't pass the trust test");
1154
				return;
1155
			}
1156
		}//try
1157
		catch (Exception ee) {
1158
			return;
1159
		}//catch
1160

    
1161
		if (!filepath.endsWith("/")) {
1162
			filepath += "/";
1163
		}
1164
		// Get file aboslute file name
1165
		String filename = filepath + docId;
1166

    
1167
		//MIME type
1168
		String contentType = null;
1169
		if (filename.endsWith(".xml")) {
1170
			contentType = "text/xml";
1171
		} else if (filename.endsWith(".css")) {
1172
			contentType = "text/css";
1173
		} else if (filename.endsWith(".dtd")) {
1174
			contentType = "text/plain";
1175
		} else if (filename.endsWith(".xsd")) {
1176
			contentType = "text/xml";
1177
		} else if (filename.endsWith("/")) {
1178
			contentType = "text/html";
1179
		} else {
1180
			File f = new File(filename);
1181
			if (f.isDirectory()) {
1182
				contentType = "text/html";
1183
			} else {
1184
				contentType = "application/octet-stream";
1185
			}
1186
		}
1187

    
1188
		// Set the mime type
1189
		response.setContentType(contentType);
1190

    
1191
		// Get the content of the file
1192
		FileInputStream fin = null;
1193
		try {
1194
			// FileInputStream to metacat
1195
			fin = new FileInputStream(filename);
1196
			// 4K buffer
1197
			byte[] buf = new byte[4 * 1024];
1198
			// Read data from file input stream to byte array
1199
			int b = fin.read(buf);
1200
			// Write to outStream from byte array
1201
			while (b != -1) {
1202
				outPut.write(buf, 0, b);
1203
				b = fin.read(buf);
1204
			}
1205
			// close file input stream
1206
			fin.close();
1207

    
1208
		}//try
1209
		catch (Exception e) {
1210
			logMetacat.error("ReplicationService.handleGetDataFileRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
1211
			logReplication.error("ReplicationService.handleGetDataFileRequest - error getting data file from MetacatReplication."
1212
					+ "handlGetDataFileRequest " + e.getMessage());
1213
			e.printStackTrace(System.out);
1214
		}//catch
1215

    
1216
	}
1217

    
1218
	/**
1219
	 * Sends a document to a remote host
1220
	 */
1221
	protected static void handleGetDocumentRequest(
1222
			Hashtable<String, String[]> params, HttpServletResponse response) {
1223

    
1224
		String urlString = null;
1225
		String documentPath = null;
1226
		String errorMsg = null;
1227
		try {
1228
			// try to open a https stream to test if the request server's public
1229
			// key
1230
			// in the key store, this is security issue
1231
			String server = params.get("server")[0];
1232
			urlString = "https://" + server + "?server="
1233
					+ MetacatUtil.getLocalReplicationServerName() + "&action=test";
1234
			URL u = new URL(urlString);
1235
			String test = ReplicationService.getURLContent(u);
1236
			// couldn't pass the test
1237
			if (test.indexOf("successfully") == -1) {
1238
				response.setContentType("text/xml");
1239
				Writer out = response.getWriter();
1240
				out.write("<error>Couldn't pass the trust test " + test + " </error>");
1241
				out.close();
1242
				return;
1243
			}
1244

    
1245
			String docid = params.get("docid")[0];
1246
			logReplication.debug("ReplicationService.handleGetDocumentRequest - MetacatReplication.handleGetDocumentRequest for docid: "
1247
					+ docid);
1248
			DocumentImpl di = new DocumentImpl(docid);
1249

    
1250
			String documentDir = PropertyService
1251
					.getProperty("application.documentfilepath");
1252
			documentPath = documentDir + FileUtil.getFS() + docid;
1253

    
1254
			// if the document does not exist on disk, read it from db and write
1255
			// it to disk.
1256
			if (FileUtil.getFileStatus(documentPath) == FileUtil.DOES_NOT_EXIST
1257
					|| FileUtil.getFileSize(documentPath) == 0) {
1258
				FileOutputStream fos = new FileOutputStream(documentPath);
1259
				di.toXml(fos, null, null, true);
1260
			}
1261

    
1262
			// read the file from disk and send it to outputstream
1263
			OutputStream outputStream = response.getOutputStream();
1264
			di.readFromFileSystem(outputStream, null, null, documentPath);
1265

    
1266
			logReplication.info("ReplicationService.handleGetDocumentRequest - document " + docid + " sent");
1267

    
1268
			// return to avoid continuing to the error reporting section at the end
1269
			return;
1270
			
1271
		} catch (MalformedURLException mue) {
1272
			logMetacat.error("ReplicationService.handleGetDocumentRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
1273
			logReplication.error("ReplicationService.handleGetDocumentRequest - Url error when getting document from MetacatReplication."
1274
					+ "handlGetDocumentRequest for url: " + urlString + " : "
1275
					+ mue.getMessage());
1276
			// e.printStackTrace(System.out);
1277
			
1278
		} catch (IOException ioe) {
1279
			logMetacat.error("ReplicationService.handleGetDocumentRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
1280
			logReplication.error("ReplicationService.handleGetDocumentRequest - I/O error when getting document from MetacatReplication."
1281
					+ "handlGetDocumentRequest for file: " + documentPath + " : "
1282
					+ ioe.getMessage());
1283
			errorMsg = ioe.getMessage();
1284
		} catch (PropertyNotFoundException pnfe) {
1285
			logMetacat.error("ReplicationService.handleGetDocumentRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
1286
			logReplication
1287
					.error("ReplicationService.handleGetDocumentRequest - Error getting property when getting document from MetacatReplication."
1288
							+ "handlGetDocumentRequest for file: "
1289
							+ documentPath
1290
							+ " : "
1291
							+ pnfe.getMessage());
1292
			// e.printStackTrace(System.out);
1293
			errorMsg = pnfe.getMessage();
1294
		} catch (McdbException me) {
1295
			logReplication
1296
					.error("ReplicationService.handleGetDocumentRequest - Document implementation error  getting property when getting document from MetacatReplication."
1297
							+ "handlGetDocumentRequest for file: "
1298
							+ documentPath
1299
							+ " : "
1300
							+ me.getMessage());
1301
			// e.printStackTrace(System.out);
1302
			errorMsg = me.getMessage();
1303
		}
1304
		
1305
		// report any errors if we got here
1306
		response.setContentType("text/xml");
1307
		Writer out = null;
1308
		try {
1309
			response.getWriter();
1310
			out = response.getWriter();
1311
			out.write("<error>" + errorMsg + "</error>");
1312
		} catch (Exception e) {
1313
			logMetacat.error(e.getMessage(), e);
1314
		} finally {
1315
			try {
1316
				out.close();
1317
			} catch (IOException e) {
1318
				logMetacat.error(e.getMessage(), e);
1319
			}
1320
		}
1321
		
1322

    
1323
	}
1324

    
1325
	/**
1326
	 * Sends a list of all of the documents on this sever along with their
1327
	 * revision numbers. The format is: <!ELEMENT replication (server, updates)>
1328
	 * <!ELEMENT server (#PCDATA)> <!ELEMENT updates ((updatedDocument |
1329
	 * deleteDocument | revisionDocument)*)> <!ELEMENT updatedDocument (docid,
1330
	 * rev, datafile*)> <!ELEMENT deletedDocument (docid, rev)> <!ELEMENT
1331
	 * revisionDocument (docid, rev, datafile*)> <!ELEMENT docid (#PCDATA)>
1332
	 * <!ELEMENT rev (#PCDATA)> <!ELEMENT datafile (#PCDATA)> note that the rev
1333
	 * in deletedDocument is always empty. I just left it in there to make the
1334
	 * parser implementation easier.
1335
	 */
1336
	protected static void handleUpdateRequest(Hashtable<String, String[]> params,
1337
			HttpServletResponse response) {
1338
		// Checked out DBConnection
1339
		DBConnection dbConn = null;
1340
		// DBConenction serial number when checked it out
1341
		int serialNumber = -1;
1342
		PreparedStatement pstmt = null;
1343
		// Server list to store server info of xml_replication table
1344
		ReplicationServerList serverList = null;
1345
		
1346
		// a writer for response
1347
		Writer out = null;
1348

    
1349
		try {
1350
			// get writer, TODO: encoding?
1351
			response.setContentType("text/xml");
1352
			out = response.getWriter();
1353
			
1354
			// Check out a DBConnection from pool
1355
			dbConn = DBConnectionPool
1356
					.getDBConnection("MetacatReplication.handleUpdateRequest");
1357
			serialNumber = dbConn.getCheckOutSerialNumber();
1358
			// Create a server list from xml_replication table
1359
			serverList = new ReplicationServerList();
1360

    
1361
			// Get remote server name from param
1362
			String server = ((String[]) params.get("server"))[0];
1363
			// If no servr name in param, return a error
1364
			if (server == null || server.equals("")) {
1365
				out.write("<error>Request didn't specify server name</error>");
1366
				out.close();
1367
				return;
1368
			}//if
1369

    
1370
			//try to open a https stream to test if the request server's public key
1371
			//in the key store, this is security issue
1372
			String testUrl = "https://" + server + "?server="
1373
            + MetacatUtil.getLocalReplicationServerName() + "&action=test";
1374
			logReplication.info("Running trust test: " + testUrl);
1375
			URL u = new URL(testUrl);
1376
			String test = ReplicationService.getURLContent(u);
1377
			logReplication.info("Ouput from test is '" + test + "'");
1378
			//couldn't pass the test
1379
			if (test.indexOf("successfully") == -1) {
1380
			    logReplication.error("Trust test failed.");
1381
				out.write("<error>Couldn't pass the trust test</error>");
1382
				out.close();
1383
				return;
1384
			}
1385
			logReplication.info("Trust test succeeded.");
1386

    
1387
			// Check if local host configure to replicate xml documents to remote
1388
			// server. If not send back a error message
1389
			if (!serverList.getReplicationValue(server)) {
1390
				out.write("<error>Configuration not allow to replicate document to you</error>");
1391
				out.close();
1392
				return;
1393
			}//if
1394

    
1395
			// Store the sql command
1396
			StringBuffer docsql = new StringBuffer();
1397
			StringBuffer revisionSql = new StringBuffer();
1398
			// Stroe the docid list
1399
			StringBuffer doclist = new StringBuffer();
1400
			// Store the deleted docid list
1401
			StringBuffer delsql = new StringBuffer();
1402
			// Store the data set file
1403
			Vector<Vector<String>> packageFiles = new Vector<Vector<String>>();
1404

    
1405
			// Append local server's name and replication servlet to doclist
1406
			doclist.append("<?xml version=\"1.0\"?><replication>");
1407
			doclist.append("<server>")
1408
					.append(MetacatUtil.getLocalReplicationServerName());
1409
			//doclist.append(util.getProperty("replicationpath"));
1410
			doclist.append("</server><updates>");
1411

    
1412
			// Get correct docid that reside on this server according the requesting
1413
			// server's replicate and data replicate value in xml_replication table
1414
			docsql.append(DatabaseService.getInstance().getDBAdapter().getReplicationDocumentListSQL());
1415
			//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)) ");
1416
			revisionSql.append("select docid, rev, doctype from xml_revisions ");
1417
			// If the localhost is not a hub to the remote server, only replicate
1418
			// the docid' which home server is local host (server_location =1)
1419
			if (!serverList.getHubValue(server)) {
1420
				String serverLocationDoc = " and a.server_location = 1";
1421
				String serverLocationRev = "where server_location = 1";
1422
				docsql.append(serverLocationDoc);
1423
				revisionSql.append(serverLocationRev);
1424
			}
1425
			logReplication.info("ReplicationService.handleUpdateRequest - Doc sql: " + docsql.toString());
1426

    
1427
			// Get any deleted documents
1428
			delsql.append("select distinct docid from ");
1429
			delsql.append("xml_revisions where docid not in (select docid from ");
1430
			delsql.append("xml_documents) ");
1431
			// If the localhost is not a hub to the remote server, only replicate
1432
			// the docid' which home server is local host (server_location =1)
1433
			if (!serverList.getHubValue(server)) {
1434
				delsql.append("and server_location = 1");
1435
			}
1436
			logReplication.info("ReplicationService.handleUpdateRequest - Deleted sql: " + delsql.toString());
1437

    
1438
			// Get docid list of local host
1439
			pstmt = dbConn.prepareStatement(docsql.toString());
1440
			pstmt.execute();
1441
			ResultSet rs = pstmt.getResultSet();
1442
			boolean tablehasrows = rs.next();
1443
			//If metacat configed to replicate data file
1444
			//if ((util.getProperty("replicationsenddata")).equals("on"))
1445
			boolean replicateData = serverList.getDataReplicationValue(server);
1446
			if (replicateData) {
1447
				while (tablehasrows) {
1448
					String recordDoctype = rs.getString(3);
1449
					Vector<String> packagedoctypes = MetacatUtil
1450
							.getOptionList(PropertyService
1451
									.getProperty("xml.packagedoctype"));
1452
					//if this is a package file, put it at the end
1453
					//because if a package file is read before all of the files it
1454
					//refers to are loaded then there is an error
1455
					if (recordDoctype != null && !packagedoctypes.contains(recordDoctype)) {
1456
						//If this is not data file
1457
						if (!recordDoctype.equals("BIN")) {
1458
							//for non-data file document
1459
							doclist.append("<updatedDocument>");
1460
							doclist.append("<docid>").append(rs.getString(1));
1461
							doclist.append("</docid><rev>").append(rs.getInt(2));
1462
							doclist.append("</rev>");
1463
							doclist.append("</updatedDocument>");
1464
						}//if
1465
						else {
1466
							//for data file document, in datafile attributes
1467
							//we put "datafile" value there
1468
							doclist.append("<updatedDocument>");
1469
							doclist.append("<docid>").append(rs.getString(1));
1470
							doclist.append("</docid><rev>").append(rs.getInt(2));
1471
							doclist.append("</rev>");
1472
							doclist.append("<datafile>");
1473
							doclist.append(DATA_FILE_FLAG);
1474
							doclist.append("</datafile>");
1475
							doclist.append("</updatedDocument>");
1476
						}//else
1477
					}//if packagedoctpes
1478
					else { //the package files are saved to be put into the xml later.
1479
						Vector<String> v = new Vector<String>();
1480
						v.add(rs.getString(1));
1481
						v.add(String.valueOf(rs.getInt(2)));
1482
						packageFiles.add(v);
1483
					}//esle
1484
					tablehasrows = rs.next();
1485
				}//while
1486
			}//if
1487
			else //metacat was configured not to send data file
1488
			{
1489
				while (tablehasrows) {
1490
					String recordDoctype = rs.getString(3);
1491
					if (!recordDoctype.equals("BIN")) { //don't replicate data files
1492
						Vector<String> packagedoctypes = MetacatUtil
1493
								.getOptionList(PropertyService
1494
										.getProperty("xml.packagedoctype"));
1495
						if (recordDoctype != null
1496
								&& !packagedoctypes.contains(recordDoctype)) { //if this is a package file, put it at the end
1497
							//because if a package file is read before all of the files it
1498
							//refers to are loaded then there is an error
1499
							doclist.append("<updatedDocument>");
1500
							doclist.append("<docid>").append(rs.getString(1));
1501
							doclist.append("</docid><rev>").append(rs.getInt(2));
1502
							doclist.append("</rev>");
1503
							doclist.append("</updatedDocument>");
1504
						} else { //the package files are saved to be put into the xml later.
1505
							Vector<String> v = new Vector<String>();
1506
							v.add(rs.getString(1));
1507
							v.add(String.valueOf(rs.getInt(2)));
1508
							packageFiles.add(v);
1509
						}
1510
					}//if
1511
					tablehasrows = rs.next();
1512
				}//while
1513
			}//else
1514

    
1515
			pstmt = dbConn.prepareStatement(delsql.toString());
1516
			//usage count should increas 1
1517
			dbConn.increaseUsageCount(1);
1518

    
1519
			pstmt.execute();
1520
			rs = pstmt.getResultSet();
1521
			tablehasrows = rs.next();
1522
			while (tablehasrows) { //handle the deleted documents
1523
				doclist.append("<deletedDocument><docid>").append(rs.getString(1));
1524
				doclist.append("</docid><rev></rev></deletedDocument>");
1525
				//note that rev is always empty for deleted docs
1526
				tablehasrows = rs.next();
1527
			}
1528

    
1529
			//now we can put the package files into the xml results
1530
			for (int i = 0; i < packageFiles.size(); i++) {
1531
				Vector<String> v = packageFiles.elementAt(i);
1532
				doclist.append("<updatedDocument>");
1533
				doclist.append("<docid>").append(v.elementAt(0));
1534
				doclist.append("</docid><rev>");
1535
				doclist.append(v.elementAt(1));
1536
				doclist.append("</rev>");
1537
				doclist.append("</updatedDocument>");
1538
			}
1539
			// add revision doc list  
1540
			doclist.append(prepareRevisionDoc(dbConn, revisionSql.toString(),
1541
					replicateData));
1542

    
1543
			doclist.append("</updates></replication>");
1544
			logReplication.info("ReplicationService.handleUpdateRequest - doclist: " + doclist.toString());
1545
			pstmt.close();
1546
			//conn.close();
1547
			out.write(doclist.toString());
1548

    
1549
		} catch (Exception e) {
1550
			logMetacat.error("ReplicationService.handleUpdateRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
1551
			logReplication.error("ReplicationService.handleUpdateRequest - error in MetacatReplication." + "handleupdaterequest: "
1552
					+ e.getMessage());
1553
			//e.printStackTrace(System.out);
1554
			try {
1555
				out.write("<error>" + e.getMessage() + "</error>");
1556
			} catch (IOException e1) {
1557
				logMetacat.error(e1.getMessage(), e1);
1558
			}
1559
		} finally {
1560
			try {
1561
				pstmt.close();
1562
			}//try
1563
			catch (SQLException ee) {
1564
				logMetacat.error("ReplicationService.handleUpdateRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
1565
				logReplication.error("ReplicationService.handleUpdateRequest - Error in MetacatReplication."
1566
						+ "handleUpdaterequest to close pstmt: " + ee.getMessage());
1567
			}//catch
1568
			finally {
1569
				DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1570
			}//finally
1571
			try {
1572
				out.close();
1573
			} catch (IOException e) {
1574
				logMetacat.error(e.getMessage(), e);
1575
			}
1576
		}//finally
1577

    
1578
	}//handlUpdateRequest
1579

    
1580
	/**
1581
	 * 
1582
	 * @param dbConn connection for doing the update
1583
	 * @param docid the document id to update
1584
	 * @param owner the user_owner
1585
	 * @param updater the user_updated
1586
	 * @throws SQLException
1587
	 */
1588
	public static void updateUserOwner(DBConnection dbConn, String docid, String owner, String updater) throws SQLException {
1589
	
1590
		String sql = 
1591
			"UPDATE xml_documents " +
1592
			"SET user_owner = ?, " +
1593
			"user_updated = ? " +
1594
			"WHERE docid = ?;";
1595
		PreparedStatement pstmt = dbConn.prepareStatement(sql);
1596
		//usage count should increas 1
1597
		dbConn.increaseUsageCount(1);
1598

    
1599
		docid = DocumentUtil.getSmartDocId(docid);
1600
		pstmt.setString(1, owner);
1601
		pstmt.setString(2, updater);
1602
		pstmt.setString(3, docid);
1603
		pstmt.execute();
1604
		pstmt.close();
1605
		
1606
		dbConn.commit();
1607
	}
1608
	
1609
	/*
1610
	 * This method will get the xml string for document in xml_revision
1611
	 * The schema look like <!ELEMENT revisionDocument (docid, rev, datafile*)>
1612
	 */
1613
	private static String prepareRevisionDoc(DBConnection dbConn, String revSql,
1614
			boolean replicateData) throws Exception {
1615
		logReplication.warn("ReplicationService.prepareRevisionDoc - The revision document sql is " + revSql);
1616
		StringBuffer revDocList = new StringBuffer();
1617
		PreparedStatement pstmt = dbConn.prepareStatement(revSql);
1618
		//usage count should increas 1
1619
		dbConn.increaseUsageCount(1);
1620

    
1621
		pstmt.execute();
1622
		ResultSet rs = pstmt.getResultSet();
1623
		logReplication.warn("Processing replication revision for documents");
1624
		while (rs.next()) {
1625
			String recordDoctype = rs.getString(3);
1626

    
1627
			//If this is data file and it isn't configured to replicate data
1628
			if (recordDoctype.equals("BIN") && !replicateData) {
1629
				logMetacat.debug("SKipping data file because data replication is not configured");
1630

    
1631
				// do nothing
1632
			} else {
1633
				String docid = rs.getString(1);
1634
				int rev = rs.getInt(2);
1635
				logMetacat.debug("Processing replication revision for docid: " + docid + "." + rev);
1636

    
1637
				revDocList.append("<revisionDocument>");
1638
				revDocList.append("<docid>").append(docid);
1639
				revDocList.append("</docid><rev>").append(rev);
1640
				revDocList.append("</rev>");
1641
				// data file
1642
				if (recordDoctype.equals("BIN")) {
1643
					revDocList.append("<datafile>");
1644
					revDocList.append(DATA_FILE_FLAG);
1645
					revDocList.append("</datafile>");
1646
				}
1647
				revDocList.append("</revisionDocument>");
1648

    
1649
			}//else
1650
		}
1651
		//System.out.println("The revision list is"+ revDocList.toString());
1652
		return revDocList.toString();
1653
	}
1654

    
1655
	/**
1656
	 * Returns the xml_catalog table encoded in xml
1657
	 */
1658
	public static String getCatalogXML() {
1659
		return handleGetCatalogRequest(null, null, false);
1660
	}
1661

    
1662
	/**
1663
	 * Sends the contents of the xml_catalog table encoded in xml
1664
	 * The xml format is:
1665
	 * <!ELEMENT xml_catalog (row*)>
1666
	 * <!ELEMENT row (entry_type, source_doctype, target_doctype, public_id,
1667
	 *                system_id)>
1668
	 * All of the sub elements of row are #PCDATA
1669

    
1670
	 * If printFlag == false then do not print to out.
1671
	 */
1672
	protected static String handleGetCatalogRequest(
1673
			Hashtable<String, String[]> params, HttpServletResponse response,
1674
			boolean printFlag) {
1675
		DBConnection dbConn = null;
1676
		int serialNumber = -1;
1677
		PreparedStatement pstmt = null;
1678
		Writer out = null;
1679
		try {
1680
			// get writer, TODO: encoding?
1681
		    if(printFlag)
1682
		    {
1683
		        response.setContentType("text/xml");
1684
		        out = response.getWriter();
1685
		    }
1686
			/*conn = MetacatReplication.getDBConnection("MetacatReplication." +
1687
			                                          "handleGetCatalogRequest");*/
1688
			dbConn = DBConnectionPool
1689
					.getDBConnection("MetacatReplication.handleGetCatalogRequest");
1690
			serialNumber = dbConn.getCheckOutSerialNumber();
1691
			pstmt = dbConn.prepareStatement("select entry_type, "
1692
					+ "source_doctype, target_doctype, public_id, "
1693
					+ "system_id from xml_catalog");
1694
			pstmt.execute();
1695
			ResultSet rs = pstmt.getResultSet();
1696
			boolean tablehasrows = rs.next();
1697
			StringBuffer sb = new StringBuffer();
1698
			sb.append("<?xml version=\"1.0\"?><xml_catalog>");
1699
			while (tablehasrows) {
1700
				sb.append("<row><entry_type>").append(rs.getString(1));
1701
				sb.append("</entry_type><source_doctype>").append(rs.getString(2));
1702
				sb.append("</source_doctype><target_doctype>").append(rs.getString(3));
1703
				sb.append("</target_doctype><public_id>").append(rs.getString(4));
1704
				// system id may not have server url on front.  Add it if not.
1705
				String systemID = rs.getString(5);
1706
				if (!systemID.startsWith("http://")) {
1707
					systemID = SystemUtil.getContextURL() + systemID;
1708
				}
1709
				sb.append("</public_id><system_id>").append(systemID);
1710
				sb.append("</system_id></row>");
1711

    
1712
				tablehasrows = rs.next();
1713
			}
1714
			sb.append("</xml_catalog>");
1715
			//conn.close();
1716
			if (printFlag) {
1717
				response.setContentType("text/xml");
1718
				out.write(sb.toString());
1719
			}
1720
			pstmt.close();
1721
			return sb.toString();
1722
		} catch (Exception e) {
1723
			logMetacat.error("ReplicationService.handleGetCatalogRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
1724
			logReplication.error("ReplicationService.handleGetCatalogRequest - error in MetacatReplication.handleGetCatalogRequest:"
1725
					+ e.getMessage());
1726
			e.printStackTrace(System.out);
1727
			if (printFlag) {
1728
				try {
1729
					out.write("<error>" + e.getMessage() + "</error>");
1730
				} catch (IOException e1) {
1731
					logMetacat.error(e1.getMessage(), e1);
1732
				}
1733
			}
1734
		} finally {
1735
			try {
1736
				pstmt.close();
1737
			}//try
1738
			catch (SQLException ee) {
1739
				logMetacat.error("ReplicationService.handleGetCatalogRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
1740
				logReplication.error("ReplicationService.handleGetCatalogRequest - Error in MetacatReplication.handleGetCatalogRequest: "
1741
						+ ee.getMessage());
1742
			}//catch
1743
			finally {
1744
				DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1745
			}//finally
1746
			if (out != null) {
1747
				try {
1748
					out.close();
1749
				} catch (IOException e1) {
1750
					logMetacat.error(e1.getMessage(), e1);
1751
				}
1752
			}
1753
		}//finally
1754

    
1755
		return null;
1756
	}
1757

    
1758
	/**
1759
	 * Sends the current system date to the remote server.  Using this action
1760
	 * for replication gets rid of any problems with syncronizing clocks
1761
	 * because a time specific to a document is always kept on its home server.
1762
	 */
1763
	protected static void handleGetTimeRequest(
1764
			Hashtable<String, String[]> params, HttpServletResponse response) {
1765
		
1766
		// use standard format -- the receiving end wants this too
1767
		String dateString = DateTimeMarshaller.serializeDateToUTC(Calendar.getInstance().getTime());
1768
		
1769
		// get a writer for sending back to response
1770
		response.setContentType("text/xml");
1771
		Writer out = null;
1772
		try {
1773
			out = response.getWriter();
1774
			out.write("<timestamp>" + dateString + "</timestamp>");
1775
			out.close();
1776
		} catch (IOException e) {
1777
			logMetacat.error(e.getMessage(), e);
1778
		}
1779
		
1780
	}
1781

    
1782
	/**
1783
	 * this method handles the timeout for a file lock.  when a lock is
1784
	 * granted it is granted for 30 seconds.  When this thread runs out
1785
	 * it deletes the docid from the queue, thus eliminating the lock.
1786
	 */
1787
	public void run() {
1788
		try {
1789
			logReplication.info("ReplicationService.run - thread started for docid: "
1790
					+ (String) fileLocks.elementAt(0));
1791

    
1792
			Thread.sleep(30000); //the lock will expire in 30 seconds
1793
			logReplication.info("thread for docid: "
1794
					+ (String) fileLocks.elementAt(fileLocks.size() - 1) + " exiting.");
1795

    
1796
			fileLocks.remove(fileLocks.size() - 1);
1797
			//fileLocks is treated as a FIFO queue.  If there are more than one lock
1798
			//in the vector, the first one inserted will be removed.
1799
		} catch (Exception e) {
1800
			logMetacat.error("ReplicationService.run - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
1801
			logReplication.error("ReplicationService.run - error in file lock thread from "
1802
					+ "MetacatReplication.run: " + e.getMessage());
1803
		}
1804
	}
1805

    
1806
	/**
1807
	 * Returns the name of a server given a serverCode
1808
	 * @param serverCode the serverid of the server
1809
	 * @return the servername or null if the specified serverCode does not
1810
	 *         exist.
1811
	 */
1812
	public static String getServerNameForServerCode(int serverCode) {
1813
		//System.out.println("serverid: " + serverCode);
1814
		DBConnection dbConn = null;
1815
		int serialNumber = -1;
1816
		PreparedStatement pstmt = null;
1817
		try {
1818
			dbConn = DBConnectionPool.getDBConnection("MetacatReplication.getServer");
1819
			serialNumber = dbConn.getCheckOutSerialNumber();
1820
			String sql = new String("select server from "
1821
					+ "xml_replication where serverid = ?");
1822
			pstmt = dbConn.prepareStatement(sql);
1823
			pstmt.setInt(1, serverCode);
1824
			//System.out.println("getserver sql: " + sql);
1825
			pstmt.execute();
1826
			ResultSet rs = pstmt.getResultSet();
1827
			boolean tablehasrows = rs.next();
1828
			if (tablehasrows) {
1829
				//System.out.println("server: " + rs.getString(1));
1830
				return rs.getString(1);
1831
			}
1832

    
1833
			//conn.close();
1834
		} catch (Exception e) {
1835
			logMetacat.error("ReplicationService.getServerNameForServerCode - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
1836
			logReplication.error("ReplicationService.getServerNameForServerCode - Error in MetacatReplication.getServer: " + e.getMessage());
1837
		} finally {
1838
			try {
1839
				pstmt.close();
1840
			}//try
1841
			catch (SQLException ee) {
1842
				logMetacat.error("ReplicationService.getServerNameForServerCode - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
1843
				logReplication.error("ReplicationService.getServerNameForServerCode - Error in MetacactReplication.getserver: "
1844
						+ ee.getMessage());
1845
			}//catch
1846
			finally {
1847
				DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1848
			}//fianlly
1849
		}//finally
1850

    
1851
		return null;
1852
		//return null if the server does not exist
1853
	}
1854

    
1855
	/**
1856
	 * Returns a server code given a server name
1857
	 * @param server the name of the server
1858
	 * @return integer > 0 representing the code of the server, 0 if the server
1859
	 *  does not exist.
1860
	 */
1861
	public static int getServerCodeForServerName(String server) throws ServiceException {
1862
		DBConnection dbConn = null;
1863
		int serialNumber = -1;
1864
		PreparedStatement pstmt = null;
1865
		int serverCode = 0;
1866

    
1867
		try {
1868

    
1869
			//conn = util.openDBConnection();
1870
			dbConn = DBConnectionPool.getDBConnection("MetacatReplication.getServerCode");
1871
			serialNumber = dbConn.getCheckOutSerialNumber();
1872
			pstmt = dbConn.prepareStatement("SELECT serverid FROM xml_replication "
1873
					+ "WHERE server LIKE ?");
1874
			pstmt.setString(1, server);
1875
			pstmt.execute();
1876
			ResultSet rs = pstmt.getResultSet();
1877
			boolean tablehasrows = rs.next();
1878
			if (tablehasrows) {
1879
				serverCode = rs.getInt(1);
1880
				pstmt.close();
1881
				//conn.close();
1882
				return serverCode;
1883
			}
1884

    
1885
		} catch (SQLException sqle) {
1886
			throw new ServiceException("ReplicationService.getServerCodeForServerName - " 
1887
					+ "SQL error when getting server code: " + sqle.getMessage());
1888

    
1889
		} finally {
1890
			try {
1891
				pstmt.close();
1892
				//conn.close();
1893
			}//try
1894
			catch (Exception ee) {
1895
				logMetacat.error("ReplicationService.getServerCodeForServerName - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
1896
				logReplication.error("ReplicationService.getServerNameForServerCode - Error in MetacatReplicatio.getServerCode: "
1897
						+ ee.getMessage());
1898

    
1899
			}//catch
1900
			finally {
1901
				DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1902
			}//finally
1903
		}//finally
1904

    
1905
		return serverCode;
1906
	}
1907

    
1908
	/**
1909
	 * Method to get a host server information for given docid
1910
	 * @param conn a connection to the database
1911
	 */
1912
	public static Hashtable<String, String> getHomeServerInfoForDocId(String docId) {
1913
		Hashtable<String, String> sl = new Hashtable<String, String>();
1914
		DBConnection dbConn = null;
1915
		int serialNumber = -1;
1916
		docId = DocumentUtil.getDocIdFromString(docId);
1917
		PreparedStatement pstmt = null;
1918
		int serverLocation;
1919
		try {
1920
			//get conection
1921
			dbConn = DBConnectionPool.getDBConnection("ReplicationHandler.getHomeServer");
1922
			serialNumber = dbConn.getCheckOutSerialNumber();
1923
			//get a server location from xml_document table
1924
			pstmt = dbConn.prepareStatement("select server_location from xml_documents "
1925
					+ "where docid = ?");
1926
			pstmt.setString(1, docId);
1927
			pstmt.execute();
1928
			ResultSet serverName = pstmt.getResultSet();
1929
			//get a server location
1930
			if (serverName.next()) {
1931
				serverLocation = serverName.getInt(1);
1932
				pstmt.close();
1933
			} else {
1934
				pstmt.close();
1935
				//ut.returnConnection(conn);
1936
				return null;
1937
			}
1938
			pstmt = dbConn.prepareStatement("select server, last_checked, replicate "
1939
					+ "from xml_replication where serverid = ?");
1940
			//increase usage count
1941
			dbConn.increaseUsageCount(1);
1942
			pstmt.setInt(1, serverLocation);
1943
			pstmt.execute();
1944
			ResultSet rs = pstmt.getResultSet();
1945
			boolean tableHasRows = rs.next();
1946
			if (tableHasRows) {
1947

    
1948
				String server = rs.getString(1);
1949
				String last_checked = rs.getString(2);
1950
				if (!server.equals("localhost")) {
1951
					sl.put(server, last_checked);
1952
				}
1953

    
1954
			} else {
1955
				pstmt.close();
1956
				//ut.returnConnection(conn);
1957
				return null;
1958
			}
1959
			pstmt.close();
1960
		} catch (Exception e) {
1961
			logMetacat.error("ReplicationService.getHomeServerInfoForDocId - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
1962
			logReplication.error("ReplicationService.getHomeServerInfoForDocId - error in replicationHandler.getHomeServer(): "
1963
					+ e.getMessage());
1964
		} finally {
1965
			try {
1966
				pstmt.close();
1967
				//ut.returnConnection(conn);
1968
			} catch (Exception ee) {
1969
				logMetacat.error("ReplicationService.getHomeServerInfoForDocId - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
1970
				logReplication.error("ReplicationService.getHomeServerInfoForDocId - Eror irn rplicationHandler.getHomeServer() "
1971
						+ "to close pstmt: " + ee.getMessage());
1972
			} finally {
1973
				DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1974
			}
1975

    
1976
		}//finally
1977
		return sl;
1978
	}
1979

    
1980
	/**
1981
	 * Returns a home server location  given a accnum
1982
	 * @param accNum , given accNum for a document
1983
	 *
1984
	 */
1985
	public static int getHomeServerCodeForDocId(String accNum) throws ServiceException {
1986
		DBConnection dbConn = null;
1987
		int serialNumber = -1;
1988
		PreparedStatement pstmt = null;
1989
		int serverCode = 1;
1990
		String docId = DocumentUtil.getDocIdFromString(accNum);
1991

    
1992
		try {
1993

    
1994
			// Get DBConnection
1995
			dbConn = DBConnectionPool
1996
					.getDBConnection("ReplicationHandler.getServerLocation");
1997
			serialNumber = dbConn.getCheckOutSerialNumber();
1998
			pstmt = dbConn.prepareStatement("SELECT server_location FROM xml_documents "
1999
					+ "WHERE docid LIKE ? ");
2000
			pstmt.setString(1, docId);
2001
			pstmt.execute();
2002
			ResultSet rs = pstmt.getResultSet();
2003
			boolean tablehasrows = rs.next();
2004
			//If a document is find, return the server location for it
2005
			if (tablehasrows) {
2006
				serverCode = rs.getInt(1);
2007
				pstmt.close();
2008
				//conn.close();
2009
				return serverCode;
2010
			}
2011
			//if couldn't find in xml_documents table, we think server code is 1
2012
			//(this is new document)
2013
			else {
2014
				pstmt.close();
2015
				//conn.close();
2016
				return serverCode;
2017
			}
2018

    
2019
		} catch (SQLException sqle) {
2020
			throw new ServiceException("ReplicationService.getHomeServerCodeForDocId - " 
2021
					+ "SQL error when getting home server code for docid: " + docId + " : " 
2022
					+ sqle.getMessage());
2023

    
2024
		} finally {
2025
			try {
2026
				pstmt.close();
2027
				//conn.close();
2028

    
2029
			} catch (SQLException sqle) {
2030
				logMetacat.error("ReplicationService.getHomeServerCodeForDocId - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
2031
				logReplication.error("ReplicationService.getHomeServerCodeForDocId - ReplicationService.getHomeServerCodeForDocId - " 
2032
						+ "SQL error when getting home server code for docid: " + docId + " : " 
2033
						+ sqle.getMessage());
2034
			} finally {
2035
				DBConnectionPool.returnDBConnection(dbConn, serialNumber);
2036
			}//finally
2037
		}//finally
2038
		//return serverCode;
2039
	}
2040

    
2041
	/**
2042
	 * This method returns the content of a url
2043
	 * @param u the url to return the content from
2044
	 * @return a string representing the content of the url
2045
	 * @throws java.io.IOException
2046
	 */
2047
	public static String getURLContent(URL u) throws java.io.IOException {
2048
		char istreamChar;
2049
		int istreamInt;
2050
		// get the response content
2051
		InputStream input = getURLStream(u);
2052
		logReplication.info("ReplicationService.getURLContent - After getting response from: " + u.toString());
2053
		InputStreamReader istream = new InputStreamReader(input);
2054
		StringBuffer serverResponse = new StringBuffer();
2055
		while ((istreamInt = istream.read()) != -1) {
2056
			istreamChar = (char) istreamInt;
2057
			serverResponse.append(istreamChar);
2058
		}
2059
		istream.close();
2060
		input.close();
2061

    
2062
		return serverResponse.toString();
2063
	}
2064
	
2065
	/**
2066
	 * This method returns the InputStream after opening a url
2067
	 * @param u the url to return the content from
2068
	 * @return a InputStream representing the content of the url
2069
	 * @throws java.io.IOException
2070
	 */
2071
	public static InputStream getURLStream(URL u) throws java.io.IOException {
2072
	    logReplication.info("Getting url stream from " + u.toString());
2073
		logReplication.info("ReplicationService.getURLStream - Before sending request to: " + u.toString());
2074
		// use httpclient to set up SSL
2075
		RestClient client = getSSLClient();
2076
		HttpResponse response = client.doGetRequest(u.toString());
2077
		// get the response content
2078
		InputStream input = response.getEntity().getContent();
2079
		logReplication.info("ReplicationService.getURLStream - After getting response from: " + u.toString());
2080
		
2081
		return input;		
2082
	}
2083
	
2084
	/**
2085
	 * Sets up an HttpClient with SSL connection.
2086
	 * Sends client certificate to the server when doing the request.
2087
	 * @return
2088
	 */
2089
	private static RestClient getSSLClient() {
2090
		RestClient client = new RestClient();
2091
		
2092
		// set up this server's client identity
2093
		String subject = null;
2094
		try {
2095
			// TODO: should there be alternative ways to get the key and certificate?
2096
			String certificateFile = PropertyService.getProperty("replication.certificate.file");
2097
	    	String keyFile = PropertyService.getProperty("replication.privatekey.file");
2098
			String keyPassword = PropertyService.getProperty("replication.privatekey.password");
2099
			X509Certificate certificate = CertificateManager.getInstance().loadCertificateFromFile(certificateFile);
2100
			PrivateKey privateKey = CertificateManager.getInstance().loadPrivateKeyFromFile(keyFile, keyPassword);
2101
			subject = CertificateManager.getInstance().getSubjectDN(certificate);
2102
			CertificateManager.getInstance().registerCertificate(subject, certificate, privateKey);
2103
		} catch (Exception e) {
2104
			// this is pretty much required for replication communication
2105
			logReplication.warn("Could not find server's client certificate/private key: " + e.getMessage());
2106
		}
2107
		
2108
		// set the configured timeout
2109
		client.setTimeouts(CLIENTTIMEOUT);
2110

    
2111
		SSLSocketFactory socketFactory = null;
2112
		try {
2113
			socketFactory = CertificateManager.getInstance().getSSLSocketFactory(subject);
2114
		} catch (FileNotFoundException e) {
2115
			// these are somewhat expected for anonymous client use
2116
			logReplication.warn("Could not set up SSL connection for client - likely because the certificate could not be located: " + e.getMessage());
2117
		} catch (Exception e) {
2118
			// this is likely more severe
2119
			logReplication.warn("Funky SSL going on: " + e.getClass() + ":: " + e.getMessage());
2120
		}
2121
		try {
2122
			//443 is the default port, this value is overridden if explicitly set in the URL
2123
			Scheme sch = new Scheme("https", 443, socketFactory);
2124
			client.getHttpClient().getConnectionManager().getSchemeRegistry().register(sch);
2125
		} catch (Exception e) {
2126
			// this is likely more severe
2127
			logReplication.error("Failed to set up SSL connection for client. Continuing. " + e.getClass() + ":: " + e.getMessage(), e);
2128
		}
2129
		return client;
2130
	}
2131
	
2132

    
2133
//	/**
2134
//	 * Method for writing replication messages to a log file specified in
2135
//	 * metacat.properties
2136
//	 */
2137
//	public static void replLog(String message) {
2138
//		try {
2139
//			FileOutputStream fos = new FileOutputStream(PropertyService
2140
//					.getProperty("replication.logdir")
2141
//					+ "/metacatreplication.log", true);
2142
//			PrintWriter pw = new PrintWriter(fos);
2143
//			SimpleDateFormat formatter = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
2144
//			java.util.Date localtime = new java.util.Date();
2145
//			String dateString = formatter.format(localtime);
2146
//			dateString += " :: " + message;
2147
//			// time stamp each entry
2148
//			pw.println(dateString);
2149
//			pw.flush();
2150
//		} catch (Exception e) {
2151
//			logReplication.error("error writing to replication log from "
2152
//					+ "MetacatReplication.replLog: " + e.getMessage());
2153
//			// e.printStackTrace(System.out);
2154
//		}
2155
//	}
2156

    
2157
//	/**
2158
//	 * Method for writing replication messages to a log file specified in
2159
//	 * metacat.properties
2160
//	 */
2161
//	public static void replErrorLog(String message) {
2162
//		try {
2163
//			FileOutputStream fos = new FileOutputStream(PropertyService
2164
//					.getProperty("replication.logdir")
2165
//					+ "/metacatreplicationerror.log", true);
2166
//			PrintWriter pw = new PrintWriter(fos);
2167
//			SimpleDateFormat formatter = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
2168
//			java.util.Date localtime = new java.util.Date();
2169
//			String dateString = formatter.format(localtime);
2170
//			dateString += " :: " + message;
2171
//			//time stamp each entry
2172
//			pw.println(dateString);
2173
//			pw.flush();
2174
//		} catch (Exception e) {
2175
//			logReplication.error("error writing to replication error log from "
2176
//					+ "MetacatReplication.replErrorLog: " + e.getMessage());
2177
//			//e.printStackTrace(System.out);
2178
//		}
2179
//	}
2180

    
2181
	/**
2182
	 * Returns true if the replicate field for server in xml_replication is 1.
2183
	 * Returns false otherwise
2184
	 */
2185
	public static boolean replToServer(String server) {
2186
		DBConnection dbConn = null;
2187
		int serialNumber = -1;
2188
		PreparedStatement pstmt = null;
2189
		try {
2190
			dbConn = DBConnectionPool.getDBConnection("MetacatReplication.repltoServer");
2191
			serialNumber = dbConn.getCheckOutSerialNumber();
2192
			pstmt = dbConn.prepareStatement("select replicate from "
2193
					+ "xml_replication where server like ? ");
2194
			pstmt.setString(1, server);
2195
			pstmt.execute();
2196
			ResultSet rs = pstmt.getResultSet();
2197
			boolean tablehasrows = rs.next();
2198
			if (tablehasrows) {
2199
				int i = rs.getInt(1);
2200
				if (i == 1) {
2201
					pstmt.close();
2202
					//conn.close();
2203
					return true;
2204
				} else {
2205
					pstmt.close();
2206
					//conn.close();
2207
					return false;
2208
				}
2209
			}
2210
		} catch (SQLException sqle) {
2211
			logMetacat.error("ReplicationService.replToServer - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
2212
			logReplication.error("ReplicationService.replToServer - SQL error in MetacatReplication.replToServer: "
2213
					+ sqle.getMessage());
2214
		} finally {
2215
			try {
2216
				pstmt.close();
2217
				//conn.close();
2218
			}//try
2219
			catch (Exception ee) {
2220
				logMetacat.error("ReplicationService.replToServer - " + ReplicationService.METACAT_REPL_ERROR_MSG);                         
2221
				logReplication.error("ReplicationService.replToServer - Error in MetacatReplication.replToServer: "
2222
						+ ee.getMessage());
2223
			}//catch
2224
			finally {
2225
				DBConnectionPool.returnDBConnection(dbConn, serialNumber);
2226
			}//finally
2227
		}//finally
2228
		return false;
2229
		//the default if this server does not exist is to not replicate to it.
2230
	}
2231

    
2232
}
(6-6/7)