Project

General

Profile

1 522 berkley
/**
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$'
9
 *     '$Date$'
10
 * '$Revision$'
11 669 jones
 *
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 522 berkley
 */
26
27 5014 daigle
package edu.ucsb.nceas.metacat.replication;
28 522 berkley
29 5755 leinfelder
import java.io.BufferedInputStream;
30
import java.io.BufferedOutputStream;
31
import java.io.File;
32
import java.io.FileInputStream;
33
import java.io.FileNotFoundException;
34
import java.io.FileOutputStream;
35
import java.io.IOException;
36
import java.io.InputStream;
37
import java.io.InputStreamReader;
38
import java.io.OutputStream;
39
import java.io.StringReader;
40
import java.io.Writer;
41
import java.net.MalformedURLException;
42
import java.net.URL;
43
import java.sql.PreparedStatement;
44
import java.sql.ResultSet;
45
import java.sql.SQLException;
46
import java.text.SimpleDateFormat;
47 2572 tao
import java.util.Date;
48 5895 berkley
import java.util.Enumeration;
49 5755 leinfelder
import java.util.Hashtable;
50
import java.util.Timer;
51
import java.util.Vector;
52 4080 daigle
53 5755 leinfelder
import javax.servlet.http.HttpServletRequest;
54
import javax.servlet.http.HttpServletResponse;
55 574 berkley
56 5755 leinfelder
import org.apache.log4j.Logger;
57
import org.xml.sax.InputSource;
58
import org.xml.sax.SAXException;
59
import org.xml.sax.XMLReader;
60
61 5014 daigle
import edu.ucsb.nceas.metacat.DocInfoHandler;
62
import edu.ucsb.nceas.metacat.DocumentImpl;
63
import edu.ucsb.nceas.metacat.DocumentImplWrapper;
64
import edu.ucsb.nceas.metacat.EventLog;
65 5755 leinfelder
import edu.ucsb.nceas.metacat.IdentifierManager;
66
import edu.ucsb.nceas.metacat.McdbDocNotFoundException;
67 5014 daigle
import edu.ucsb.nceas.metacat.McdbException;
68 5195 daigle
import edu.ucsb.nceas.metacat.accesscontrol.AccessControlException;
69 5089 daigle
import edu.ucsb.nceas.metacat.accesscontrol.AccessControlForSingleFile;
70 5195 daigle
import edu.ucsb.nceas.metacat.accesscontrol.PermOrderException;
71 5098 daigle
import edu.ucsb.nceas.metacat.accesscontrol.XMLAccessDAO;
72 5014 daigle
import edu.ucsb.nceas.metacat.database.DBConnection;
73
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
74
import edu.ucsb.nceas.metacat.database.DatabaseService;
75 5030 daigle
import edu.ucsb.nceas.metacat.properties.PropertyService;
76 5014 daigle
import edu.ucsb.nceas.metacat.shared.BaseService;
77
import edu.ucsb.nceas.metacat.shared.HandlerException;
78
import edu.ucsb.nceas.metacat.shared.ServiceException;
79 5027 daigle
import edu.ucsb.nceas.metacat.util.DocumentUtil;
80 4698 daigle
import edu.ucsb.nceas.metacat.util.MetacatUtil;
81 4080 daigle
import edu.ucsb.nceas.metacat.util.SystemUtil;
82 4488 daigle
import edu.ucsb.nceas.utilities.FileUtil;
83 4087 daigle
import edu.ucsb.nceas.utilities.GeneralPropertyException;
84 4080 daigle
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
85
86 5014 daigle
public class ReplicationService extends BaseService {
87 4335 daigle
88 5014 daigle
	private static ReplicationService replicationService = null;
89 2663 sgarg
90 5014 daigle
	private long timeInterval;
91
	private Date firstTime;
92
	private boolean timedReplicationIsOn = false;
93
	Timer replicationDaemon;
94
	private static Vector<String> fileLocks = new Vector<String>();
95
//	private Thread lockThread = null;
96
	public static final String FORCEREPLICATEDELETE = "forcereplicatedelete";
97
	private static String TIMEREPLICATION = "replication.timedreplication";
98
	private static String TIMEREPLICATIONINTERVAl ="replication.timedreplicationinterval";
99
	private static String FIRSTTIME = "replication.firsttimedreplication";
100
	private static final int TIMEINTERVALLIMIT = 7200000;
101
	public static final String REPLICATIONUSER = "replication";
102 2286 tao
103 5014 daigle
	public static final String REPLICATION_LOG_FILE_NAME = "metacatreplication.log";
104
	public static String METACAT_REPL_ERROR_MSG = null;
105
	private static Logger logReplication = Logger.getLogger("ReplicationLogging");
106
	private static Logger logMetacat = Logger.getLogger(ReplicationService.class);
107 2286 tao
108 5014 daigle
	private ReplicationService() throws ServiceException {
109
		_serviceName = "ReplicationService";
110
111
		initialize();
112
	}
113
114
	private void initialize() throws ServiceException {
115
116
		// initialize db connections to handle any update requests
117
		// deltaT = util.getProperty("replication.deltaT");
118
		// the default deltaT can be set from metacat.properties
119
		// create a thread to do the delta-T check but don't execute it yet
120
		replicationDaemon = new Timer(true);
121
		try {
122
			String replLogFile = PropertyService.getProperty("replication.logdir")
123
				+ FileUtil.getFS() + REPLICATION_LOG_FILE_NAME;
124
			METACAT_REPL_ERROR_MSG = "An error occurred in replication.  Please see the " +
125
				"replication log at: " + replLogFile;
126
127
			String timedRepIsOnStr =
128
				PropertyService.getProperty("replication.timedreplication");
129
			timedReplicationIsOn = (new Boolean(timedRepIsOnStr)).booleanValue();
130
			logReplication.info("ReplicationService.initialize - The timed replication on is" + timedReplicationIsOn);
131 522 berkley
132 5014 daigle
			String timeIntervalStr =
133
				PropertyService.getProperty("replication.timedreplicationinterval");
134
			timeInterval = (new Long(timeIntervalStr)).longValue();
135
			logReplication.info("ReplicationService.initialize - The timed replication time Interval is " + timeInterval);
136 2286 tao
137 5014 daigle
			String firstTimeStr =
138
				PropertyService.getProperty("replication.firsttimedreplication");
139
			logReplication.info("ReplicationService.initialize - first replication time form property is " + firstTimeStr);
140
			firstTime = ReplicationHandler.combinateCurrentDateAndGivenTime(firstTimeStr);
141 2286 tao
142 5014 daigle
			logReplication.info("ReplicationService.initialize - After combine current time, the real first time is "
143
					+ firstTime.toString() + " minisec");
144 2286 tao
145 5014 daigle
			// set up time replication if it is on
146
			if (timedReplicationIsOn) {
147
				replicationDaemon.scheduleAtFixedRate(new ReplicationHandler(),
148
						firstTime, timeInterval);
149
				logReplication.info("ReplicationService.initialize - deltaT handler started with rate="
150
						+ timeInterval + " mini seconds at " + firstTime.toString());
151
			}
152 2286 tao
153 5014 daigle
		} catch (PropertyNotFoundException pnfe) {
154
			throw new ServiceException(
155
					"ReplicationService.initialize - Property error while instantiating "
156
							+ "replication service: " + pnfe.getMessage());
157
		} catch (HandlerException he) {
158
			throw new ServiceException(
159
					"ReplicationService.initialize - Handler error while instantiating "
160
							+ "replication service" + he.getMessage());
161
		}
162
	}
163 2286 tao
164 5014 daigle
	/**
165
	 * Get the single instance of SessionService.
166
	 *
167
	 * @return the single instance of SessionService
168
	 */
169
	public static ReplicationService getInstance() throws ServiceException {
170
		if (replicationService == null) {
171
			replicationService = new ReplicationService();
172
		}
173
		return replicationService;
174
	}
175 2286 tao
176 5014 daigle
	public boolean refreshable() {
177
		return true;
178
	}
179 2286 tao
180 5014 daigle
	protected void doRefresh() throws ServiceException {
181
		return;
182
	}
183
184
	public void stop() throws ServiceException{
185
186
	}
187 840 bojilova
188 5014 daigle
	public void stopReplication() throws ServiceException {
189
	      //stop the replication server
190
	      replicationDaemon.cancel();
191
	      replicationDaemon = new Timer(true);
192
	      timedReplicationIsOn = false;
193
	      try {
194
	    	  PropertyService.setProperty("replication.timedreplication", (new Boolean(timedReplicationIsOn)).toString());
195
	      } catch (GeneralPropertyException gpe) {
196
	    	  logReplication.warn("ReplicationService.stopReplication - Could not set replication.timedreplication property: " + gpe.getMessage());
197
	      }
198 4080 daigle
199 5014 daigle
	      logReplication.info("ReplicationService.stopReplication - deltaT handler stopped");
200
		return;
201
	}
202
203
	protected void startReplication(Hashtable<String, String[]> params) throws ServiceException {
204 2286 tao
205 5014 daigle
	       String firstTimeStr = "";
206
	      //start the replication server
207
	       if ( params.containsKey("rate") ) {
208
	        timeInterval = new Long(
209
	               new String(((String[])params.get("rate"))[0])).longValue();
210
	        if(timeInterval < TIMEINTERVALLIMIT) {
211
	            //deltaT<30 is a timing mess!
212
	            timeInterval = TIMEINTERVALLIMIT;
213
	            throw new ServiceException("Replication deltaT rate cannot be less than "+
214
	                    TIMEINTERVALLIMIT + " millisecs and system automatically setup the rate to "+TIMEINTERVALLIMIT);
215
	        }
216
	      } else {
217
	        timeInterval = TIMEINTERVALLIMIT ;
218
	      }
219
	      logReplication.info("ReplicationService.startReplication - New rate is: " + timeInterval + " mini seconds.");
220
	      if ( params.containsKey("firsttime"))
221
	      {
222
	         firstTimeStr = ((String[])params.get("firsttime"))[0];
223
	         try
224
	         {
225
	           firstTime = ReplicationHandler.combinateCurrentDateAndGivenTime(firstTimeStr);
226
	           logReplication.info("ReplicationService.startReplication - The first time setting is "+firstTime.toString());
227
	         }
228
	         catch (HandlerException e)
229
	         {
230
	            throw new ServiceException(e.getMessage());
231
	         }
232
	         logReplication.warn("After combine current time, the real first time is "
233
	                                  +firstTime.toString()+" minisec");
234
	      }
235
	      else
236
	      {
237
	    	  logMetacat.error("ReplicationService.startReplication - " + ReplicationService.METACAT_REPL_ERROR_MSG);
238
	          logReplication.error("ReplicationService.startReplication - You should specify the first time " +
239
	                                  "to start a time replication");
240
	          return;
241
	      }
242
243
	      timedReplicationIsOn = true;
244
	      try {
245
	      // save settings to property file
246
	      PropertyService.setProperty(TIMEREPLICATION, (new Boolean(timedReplicationIsOn)).toString());
247
	      // note we couldn't use firstTime object because it has date info
248
	      // we only need time info such as 10:00 PM
249
	      PropertyService.setProperty(FIRSTTIME, firstTimeStr);
250
	      PropertyService.setProperty(TIMEREPLICATIONINTERVAl, (new Long(timeInterval)).toString());
251
	      } catch (GeneralPropertyException gpe) {
252
	    	  logReplication.warn("ReplicationService.startReplication - Could not set property: " + gpe.getMessage());
253
	      }
254
	      replicationDaemon.cancel();
255
	      replicationDaemon = new Timer(true);
256
	      replicationDaemon.scheduleAtFixedRate(new ReplicationHandler(), firstTime,
257
	                                            timeInterval);
258
259
	      logReplication.info("ReplicationService.startReplication - deltaT handler started with rate=" +
260
	                                    timeInterval + " milliseconds at " +firstTime.toString());
261 837 bojilova
262 5014 daigle
	}
263
264
	public void runOnce() throws ServiceException {
265
	      //updates this server exactly once
266
	      replicationDaemon.schedule(new ReplicationHandler(), 0);
267
	}
268 2286 tao
269 5014 daigle
	/**
270
	 * This method can add, delete and list the servers currently included in
271
	 * xml_replication.
272
	 * action           subaction            other needed params
273
	 * ---------------------------------------------------------
274
	 * servercontrol    add                  server
275
	 * servercontrol    delete               server
276
	 * servercontrol    list
277
	 */
278 5755 leinfelder
	protected static void handleServerControlRequest(
279 5014 daigle
			Hashtable<String, String[]> params, HttpServletResponse response) {
280
		String subaction = ((String[]) params.get("subaction"))[0];
281
		DBConnection dbConn = null;
282
		int serialNumber = -1;
283
		PreparedStatement pstmt = null;
284
		String replicate = null;
285
		String server = null;
286
		String dataReplicate = null;
287
		String hub = null;
288 5755 leinfelder
		Writer out = null;
289 5014 daigle
		try {
290 5755 leinfelder
			response.setContentType("text/xml");
291
			out = response.getWriter();
292
293 5014 daigle
			//conn = util.openDBConnection();
294
			dbConn = DBConnectionPool
295
					.getDBConnection("MetacatReplication.handleServerControlRequest");
296
			serialNumber = dbConn.getCheckOutSerialNumber();
297 837 bojilova
298 5014 daigle
			// add server to server list
299
			if (subaction.equals("add")) {
300
				replicate = ((String[]) params.get("replicate"))[0];
301
				server = ((String[]) params.get("server"))[0];
302 2286 tao
303 5014 daigle
				//Get data replication value
304
				dataReplicate = ((String[]) params.get("datareplicate"))[0];
305
				//Get hub value
306
				hub = ((String[]) params.get("hub"))[0];
307 837 bojilova
308 5319 jones
				String toDateSql = DatabaseService.getInstance().getDBAdapter().toDate("01/01/1980","MM/DD/YYYY");
309 5151 daigle
				String sql = "INSERT INTO xml_replication "
310 5014 daigle
						+ "(server, last_checked, replicate, datareplicate, hub) "
311 5151 daigle
						+ "VALUES (?," + toDateSql + ",?,?,?)";
312
313
				pstmt = dbConn.prepareStatement(sql);
314
315
				pstmt.setString(1, server);
316
				pstmt.setInt(2, Integer.parseInt(replicate));
317
				pstmt.setInt(3, Integer.parseInt(dataReplicate));
318
				pstmt.setInt(4, Integer.parseInt(hub));
319
320
				String sqlReport = "XMLAccessAccess.getXMLAccessForDoc - SQL: " + sql;
321
				sqlReport += " [" + server + "," + replicate +
322
					"," + dataReplicate + "," + hub + "]";
323
324
				logMetacat.info(sqlReport);
325
326 5014 daigle
				pstmt.execute();
327
				pstmt.close();
328
				dbConn.commit();
329 5755 leinfelder
				out.write("Server " + server + " added");
330 5014 daigle
				response.setContentType("text/html");
331 5755 leinfelder
				out.write("<html><body><table border=\"1\">");
332
				out.write("<tr><td><b>server</b></td><td><b>last_checked</b></td><td>");
333
				out.write("<b>replicate</b></td>");
334
				out.write("<td><b>datareplicate</b></td>");
335
				out.write("<td><b>hub</b></td></tr>");
336 5014 daigle
				pstmt = dbConn.prepareStatement("SELECT * FROM xml_replication");
337
				//increase dbconnection usage
338
				dbConn.increaseUsageCount(1);
339 2298 tao
340 5014 daigle
				pstmt.execute();
341
				ResultSet rs = pstmt.getResultSet();
342
				boolean tablehasrows = rs.next();
343
				while (tablehasrows) {
344 5755 leinfelder
					out.write("<tr><td>" + rs.getString(2) + "</td><td>");
345
					out.write(rs.getString(3) + "</td><td>");
346
					out.write(rs.getString(4) + "</td><td>");
347
					out.write(rs.getString(5) + "</td><td>");
348
					out.write(rs.getString(6) + "</td></tr>");
349 837 bojilova
350 5014 daigle
					tablehasrows = rs.next();
351
				}
352 5755 leinfelder
				out.write("</table></body></html>");
353 837 bojilova
354 5014 daigle
				// download certificate with the public key on this server
355
				// and import it as a trusted certificate
356
				String certURL = ((String[]) params.get("certificate"))[0];
357 5151 daigle
				if (certURL != null && !certURL.equals("")) {
358
					downloadCertificate(certURL);
359
				}
360 837 bojilova
361 5014 daigle
				// delete server from server list
362
			} else if (subaction.equals("delete")) {
363
				server = ((String[]) params.get("server"))[0];
364
				pstmt = dbConn.prepareStatement("DELETE FROM xml_replication "
365
						+ "WHERE server LIKE '" + server + "'");
366
				pstmt.execute();
367
				pstmt.close();
368
				dbConn.commit();
369 5755 leinfelder
				out.write("Server " + server + " deleted");
370 5014 daigle
				response.setContentType("text/html");
371 5755 leinfelder
				out.write("<html><body><table border=\"1\">");
372
				out.write("<tr><td><b>server</b></td><td><b>last_checked</b></td><td>");
373
				out.write("<b>replicate</b></td>");
374
				out.write("<td><b>datareplicate</b></td>");
375
				out.write("<td><b>hub</b></td></tr>");
376 837 bojilova
377 5014 daigle
				pstmt = dbConn.prepareStatement("SELECT * FROM xml_replication");
378
				//increase dbconnection usage
379
				dbConn.increaseUsageCount(1);
380
				pstmt.execute();
381
				ResultSet rs = pstmt.getResultSet();
382
				boolean tablehasrows = rs.next();
383
				while (tablehasrows) {
384 5755 leinfelder
					out.write("<tr><td>" + rs.getString(2) + "</td><td>");
385
					out.write(rs.getString(3) + "</td><td>");
386
					out.write(rs.getString(4) + "</td><td>");
387
					out.write(rs.getString(5) + "</td><td>");
388
					out.write(rs.getString(6) + "</td></tr>");
389 5014 daigle
					tablehasrows = rs.next();
390
				}
391 5755 leinfelder
				out.write("</table></body></html>");
392 837 bojilova
393 5014 daigle
				// list servers in server list
394
			} else if (subaction.equals("list")) {
395
				response.setContentType("text/html");
396 5755 leinfelder
				out.write("<html><body><table border=\"1\">");
397
				out.write("<tr><td><b>server</b></td><td><b>last_checked</b></td><td>");
398
				out.write("<b>replicate</b></td>");
399
				out.write("<td><b>datareplicate</b></td>");
400
				out.write("<td><b>hub</b></td></tr>");
401 5014 daigle
				pstmt = dbConn.prepareStatement("SELECT * FROM xml_replication");
402
				pstmt.execute();
403
				ResultSet rs = pstmt.getResultSet();
404
				boolean tablehasrows = rs.next();
405
				while (tablehasrows) {
406 5755 leinfelder
					out.write("<tr><td>" + rs.getString(2) + "</td><td>");
407
					out.write(rs.getString(3) + "</td><td>");
408
					out.write(rs.getString(4) + "</td><td>");
409
					out.write(rs.getString(5) + "</td><td>");
410
					out.write(rs.getString(6) + "</td></tr>");
411 5014 daigle
					tablehasrows = rs.next();
412
				}
413 5755 leinfelder
				out.write("</table></body></html>");
414 5014 daigle
			} else {
415 2286 tao
416 5755 leinfelder
				out.write("<error>Unkonwn subaction</error>");
417 2286 tao
418 5014 daigle
			}
419
			pstmt.close();
420
			//conn.close();
421 2286 tao
422 5014 daigle
		} catch (Exception e) {
423
			logMetacat.error("ReplicationService.handleServerControlRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);
424
			logReplication.error("ReplicationService.handleServerControlRequest - Error in "
425
					+ "MetacatReplication.handleServerControlRequest " + e.getMessage());
426
			e.printStackTrace(System.out);
427
		} finally {
428
			try {
429
				pstmt.close();
430
			}//try
431
			catch (SQLException ee) {
432
				logMetacat.error("ReplicationService.handleServerControlRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);
433
				logReplication.error("ReplicationService.handleServerControlRequest - Error in MetacatReplication.handleServerControlRequest to close pstmt "
434
						+ ee.getMessage());
435
			}//catch
436
			finally {
437
				DBConnectionPool.returnDBConnection(dbConn, serialNumber);
438
			}//finally
439 5755 leinfelder
			if (out != null) {
440
				try {
441
					out.close();
442
				} catch (IOException e) {
443
					logMetacat.error(e.getMessage(), e);
444
				}
445
			}
446 5014 daigle
		}//finally
447 2286 tao
448 5014 daigle
	}
449 2286 tao
450 5014 daigle
	// download certificate with the public key from certURL and
451 4080 daigle
	// upload it onto this server; it then must be imported as a
452
	// trusted certificate
453 5014 daigle
	private static void downloadCertificate(String certURL) throws FileNotFoundException,
454 4080 daigle
			IOException, MalformedURLException, PropertyNotFoundException {
455 5014 daigle
456 4080 daigle
		// the path to be uploaded to
457 5014 daigle
		String certPath = SystemUtil.getContextDir();
458 2286 tao
459 4080 daigle
		// get filename from the URL of the certificate
460
		String filename = certURL;
461
		int slash = Math.max(filename.lastIndexOf('/'), filename.lastIndexOf('\\'));
462
		if (slash > -1) {
463
			filename = filename.substring(slash + 1);
464
		}
465 2286 tao
466 4080 daigle
		// open file output strem to write the input into it
467
		File f = new File(certPath, filename);
468
		synchronized (f) {
469
			try {
470
				if (f.exists()) {
471
					throw new IOException("File already exist: " + f.getCanonicalFile());
472
					// if ( f.exists() && !f.canWrite() ) {
473
					// throw new IOException("Not writable: " +
474
					// f.getCanonicalFile());
475
				}
476
			} catch (SecurityException se) {
477
				// if a security manager exists,
478
				// its checkRead method is called for f.exist()
479
				// or checkWrite method is called for f.canWrite()
480
				throw se;
481
			}
482 2286 tao
483 4080 daigle
			// create a buffered byte output stream
484
			// that uses a default-sized output buffer
485
			FileOutputStream fos = new FileOutputStream(f);
486
			BufferedOutputStream out = new BufferedOutputStream(fos);
487 840 bojilova
488 4080 daigle
			// this should be http url
489
			URL url = new URL(certURL);
490
			BufferedInputStream bis = null;
491
			try {
492
				bis = new BufferedInputStream(url.openStream());
493
				byte[] buf = new byte[4 * 1024]; // 4K buffer
494
				int b = bis.read(buf);
495
				while (b != -1) {
496
					out.write(buf, 0, b);
497
					b = bis.read(buf);
498
				}
499
			} finally {
500
				if (bis != null)
501
					bis.close();
502
			}
503
			// the input and the output streams must be closed
504
			bis.close();
505
			out.flush();
506
			out.close();
507
			fos.close();
508
		} // end of synchronized(f)
509
	}
510 2286 tao
511 5014 daigle
	/**
512 4080 daigle
	 * when a forcereplication request comes in, local host sends a read request
513
	 * to the requesting server (remote server) for the specified docid. Then
514
	 * store it in local database.
515
	 */
516 5755 leinfelder
	protected static void handleForceReplicateRequest(
517 5014 daigle
			Hashtable<String, String[]> params, HttpServletResponse response,
518
			HttpServletRequest request) {
519
		String server = ((String[]) params.get("server"))[0]; // the server that
520
		String docid = ((String[]) params.get("docid"))[0]; // sent the document
521
		String dbaction = "UPDATE"; // the default action is UPDATE
522
		//    boolean override = false;
523
		//    int serverCode = 1;
524
		DBConnection dbConn = null;
525
		int serialNumber = -1;
526 5459 berkley
		String docName = null;
527 2286 tao
528 5014 daigle
		try {
529
			//if the url contains a dbaction then the default action is overridden
530
			if (params.containsKey("dbaction")) {
531
				dbaction = ((String[]) params.get("dbaction"))[0];
532
				//serverCode = MetacatReplication.getServerCode(server);
533
				//override = true; //we are now overriding the default action
534
			}
535
			logReplication.info("ReplicationService.handleForceReplicateRequest - Force replication request from: " + server);
536
			logReplication.info("ReplicationService.handleForceReplicateRequest - Force replication docid: " + docid);
537
			logReplication.info("ReplicationService.handleForceReplicateRequest - Force replication action: " + dbaction);
538
			// sending back read request to remote server
539
			URL u = new URL("https://" + server + "?server="
540
					+ MetacatUtil.getLocalReplicationServerName() + "&action=read&docid="
541
					+ docid);
542
			String xmldoc = ReplicationService.getURLContent(u);
543 2286 tao
544 5014 daigle
			// get the document info from server
545
			URL docinfourl = new URL("https://" + server + "?server="
546
					+ MetacatUtil.getLocalReplicationServerName()
547
					+ "&action=getdocumentinfo&docid=" + docid);
548 5440 berkley
549 2286 tao
550 5014 daigle
			String docInfoStr = ReplicationService.getURLContent(docinfourl);
551 2286 tao
552 5014 daigle
			//dih is the parser for the docinfo xml format
553
			DocInfoHandler dih = new DocInfoHandler();
554
			XMLReader docinfoParser = ReplicationHandler.initParser(dih);
555
			docinfoParser.parse(new InputSource(new StringReader(docInfoStr)));
556
			//      Hashtable<String,Vector<AccessControlForSingleFile>> docinfoHash = dih.getDocInfo();
557
			Hashtable<String, String> docinfoHash = dih.getDocInfo();
558 2286 tao
559 5014 daigle
			// Get user owner of this docid
560
			String user = (String) docinfoHash.get("user_owner");
561
			// Get home server of this docid
562
			String homeServer = (String) docinfoHash.get("home_server");
563 5322 berkley
			String guid = (String) docinfoHash.get("guid");
564 5440 berkley
			logReplication.info("XXXXXXXXXXXXXXXX GUID found in dociinfoHash: " + guid);
565 5324 berkley
566 5451 berkley
			logReplication.info("Processing guid " + guid +
567 5324 berkley
			  " information from handleForceReplicationRequest: " +
568
			  docinfoHash.toString());
569
      IdentifierManager idman = IdentifierManager.getInstance();
570 5455 berkley
      if(guid != null)
571 5324 berkley
      { //if the guid was passed in, put it in the identifiers table
572 5451 berkley
        logReplication.info("YYYYYYYYYYYYYY Creating guid/docid mapping for docid " +
573 5324 berkley
          docinfoHash.get("docid") + " and guid: " + guid);
574 5451 berkley
575 5459 berkley
        docName = (String) docinfoHash.get("docname");
576 5451 berkley
        logReplication.info("ZZZZZZZZZZZZ docName: " + docName);
577
        if(docName.trim().equals("systemMetadata"))
578
        {
579
            logReplication.info("creating mapping for systemMetadata: guid: " + guid + " localId: " + docinfoHash.get("docid"));
580
            idman.createSystemMetadataMapping(guid, docinfoHash.get("docid"));
581
        }
582
        else
583
        {
584
            logReplication.info("creating mapping: guid: " + guid + " localId: " + docinfoHash.get("docid"));
585
            idman.createMapping(guid, docinfoHash.get("docid"));
586
        }
587 5324 berkley
      }
588
      else
589
      {
590
        logReplication.debug("No guid information was included with the replicated document");
591
      }
592
593 5014 daigle
			String createdDate = (String) docinfoHash.get("date_created");
594
			String updatedDate = (String) docinfoHash.get("date_updated");
595
			logReplication.info("ReplicationService.handleForceReplicateRequest - homeServer: " + homeServer);
596
			// Get Document type
597
			String docType = (String) docinfoHash.get("doctype");
598
			logReplication.info("ReplicationService.handleForceReplicateRequest - docType: " + docType);
599
			String parserBase = null;
600
			// this for eml2 and we need user eml2 parser
601
			if (docType != null
602
					&& (docType.trim()).equals(DocumentImpl.EML2_0_0NAMESPACE)) {
603
				logReplication.warn("ReplicationService.handleForceReplicateRequest - This is an eml200 document!");
604
				parserBase = DocumentImpl.EML200;
605
			} else if (docType != null
606
					&& (docType.trim()).equals(DocumentImpl.EML2_0_1NAMESPACE)) {
607
				logReplication.warn("ReplicationService.handleForceReplicateRequest - This is an eml2.0.1 document!");
608
				parserBase = DocumentImpl.EML200;
609
			} else if (docType != null
610
					&& (docType.trim()).equals(DocumentImpl.EML2_1_0NAMESPACE)) {
611
				logReplication.warn("ReplicationService.handleForceReplicateRequest - This is an eml2.1.0 document!");
612
				parserBase = DocumentImpl.EML210;
613 5709 leinfelder
			} else if (docType != null
614
					&& (docType.trim()).equals(DocumentImpl.EML2_1_1NAMESPACE)) {
615
				logReplication.warn("ReplicationService.handleForceReplicateRequest - This is an eml2.1.1 document!");
616
				parserBase = DocumentImpl.EML210;
617 5014 daigle
			}
618
			logReplication.warn("ReplicationService.handleForceReplicateRequest - The parserBase is: " + parserBase);
619 2286 tao
620 5014 daigle
			// Get DBConnection from pool
621
			dbConn = DBConnectionPool
622
					.getDBConnection("MetacatReplication.handleForceReplicateRequest");
623
			serialNumber = dbConn.getCheckOutSerialNumber();
624
			// write the document to local database
625
			DocumentImplWrapper wrapper = new DocumentImplWrapper(parserBase, false);
626
			//try this independently so we can set
627 5195 daigle
//			Exception writeException = null;
628 5014 daigle
			try {
629 5195 daigle
				wrapper.writeReplication(dbConn, xmldoc, null, null,
630 5014 daigle
						dbaction, docid, user, null, homeServer, server, createdDate,
631
						updatedDate);
632 5195 daigle
			} finally {
633
//				writeException = e;
634 3230 tao
635 5195 daigle
				//process extra access rules before dealing with the write exception (doc exist already)
636
		        Vector<XMLAccessDAO> accessControlList = dih.getAccessControlList();
637
		        if (accessControlList != null) {
638
		        	AccessControlForSingleFile acfsf = new AccessControlForSingleFile(docid);
639
		        	for (XMLAccessDAO xmlAccessDAO : accessControlList) {
640
		        		if (!acfsf.accessControlExists(xmlAccessDAO)) {
641
		        			acfsf.insertPermissions(xmlAccessDAO);
642
							logReplication.info("ReplicationService.handleForceReplicateRequest - document " + docid
643
									+ " permissions added to DB");
644
		        		}
645
		            }
646
		        }
647
//				if (accessControlList != null) {
648
//					for (int i = 0; i < accessControlList.size(); i++) {
649
//						AccessControlForSingleFile acfsf = (AccessControlForSingleFile) accessControlList
650
//								.get(i);
651
//						if (!acfsf.accessControlExists()) {
652
//							acfsf.insertPermissions();
653
//							logReplication.info("ReplicationService.handleForceReplicateRequest - document " + docid
654
//									+ " permissions added to DB");
655
//						}
656 5098 daigle
//					}
657
//				}
658 2286 tao
659 5195 daigle
//				if (writeException != null) {
660
//					throw writeException;
661
//				}
662
663
				logReplication.info("ReplicationService.handleForceReplicateRequest - document " + docid + " added to DB with "
664
						+ "action " + dbaction);
665 5458 berkley
666
				if(guid != null)
667
                {
668 5459 berkley
                    if(!docName.trim().equals("systemMetadata"))
669
                    {
670
                        logReplication.info("replicate D1GUID:" + guid + ":D1SCIMETADATA:" +
671
                                docid + ":");
672
                    }
673
                    else
674
                    {
675
                        logReplication.info("replicate D1GUID:" + guid + ":D1SYSMETADATA:" +
676
                                docid + ":");
677
                    }
678 5458 berkley
                }
679 5195 daigle
				EventLog.getInstance().log(request.getRemoteAddr(), REPLICATIONUSER, docid,
680
						dbaction);
681 5014 daigle
			}
682 5195 daigle
		} catch (SQLException sqle) {
683 5014 daigle
			logMetacat.error("ReplicationService.handleForceReplicateRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);
684 5195 daigle
			logReplication.error("ReplicationService.handleForceReplicateRequest - SQL error when adding doc " + docid +
685
					" to DB with action " + dbaction + ": " + sqle.getMessage());
686
		} catch (MalformedURLException mue) {
687
			logMetacat.error("ReplicationService.handleForceReplicateRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);
688
			logReplication.error("ReplicationService.handleForceReplicateRequest - URL error when adding doc " + docid +
689
					" to DB with action " + dbaction + ": " + mue.getMessage());
690
		} catch (SAXException se) {
691
			logMetacat.error("ReplicationService.handleForceReplicateRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);
692
			logReplication.error("ReplicationService.handleForceReplicateRequest - SAX parsing error when adding doc " + docid +
693
					" to DB with action " + dbaction + ": " + se.getMessage());
694
		} catch (HandlerException he) {
695
			logMetacat.error("ReplicationService.handleForceReplicateRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);
696
			logReplication.error("ReplicationService.handleForceReplicateRequest - Handler error when adding doc " + docid +
697
					" to DB with action " + dbaction + ": " + he.getMessage());
698
		} catch (IOException ioe) {
699
			logMetacat.error("ReplicationService.handleForceReplicateRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);
700
			logReplication.error("ReplicationService.handleForceReplicateRequest - I/O error when adding doc " + docid +
701
					" to DB with action " + dbaction + ": " + ioe.getMessage());
702
		} catch (PermOrderException poe) {
703
			logMetacat.error("ReplicationService.handleForceReplicateRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);
704
			logReplication.error("ReplicationService.handleForceReplicateRequest - Permissions order error when adding doc " + docid +
705
					" to DB with action " + dbaction + ": " + poe.getMessage());
706
		} catch (AccessControlException ace) {
707
			logMetacat.error("ReplicationService.handleForceReplicateRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);
708
			logReplication.error("ReplicationService.handleForceReplicateRequest - Permissions order error when adding doc " + docid +
709
					" to DB with action " + dbaction + ": " + ace.getMessage());
710
		} catch (Exception e) {
711
			logMetacat.error("ReplicationService.handleForceReplicateRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);
712
			logReplication.error("ReplicationService.handleForceReplicateRequest - General error when adding doc " + docid +
713
					" to DB with action " + dbaction + ": " + e.getMessage());
714
		} finally {
715 5014 daigle
			// Return the checked out DBConnection
716
			DBConnectionPool.returnDBConnection(dbConn, serialNumber);
717
		}//finally
718
	}
719 2298 tao
720 5014 daigle
	/*
721
	 * when a forcereplication delete request comes in, local host will delete this
722
	 * document
723
	 */
724 5755 leinfelder
	protected static void handleForceReplicateDeleteRequest(
725 5014 daigle
			Hashtable<String, String[]> params, HttpServletResponse response,
726
			HttpServletRequest request) {
727
		String server = ((String[]) params.get("server"))[0]; // the server that
728
		String docid = ((String[]) params.get("docid"))[0]; // sent the document
729
		try {
730
			logReplication.info("ReplicationService.handleForceReplicateDeleteRequest - force replication delete request from " + server);
731
			logReplication.info("ReplicationService.handleForceReplicateDeleteRequest - force replication delete docid " + docid);
732
			logReplication.info("ReplicationService.handleForceReplicateDeleteRequest - Force replication delete request from: " + server);
733
			logReplication.info("ReplicationService.handleForceReplicateDeleteRequest - Force replication delete docid: " + docid);
734
			DocumentImpl.delete(docid, null, null, server);
735
			logReplication.info("ReplicationService.handleForceReplicateDeleteRequest - document " + docid + " was successfully deleted ");
736
			EventLog.getInstance().log(request.getRemoteAddr(), REPLICATIONUSER, docid,
737
					"delete");
738
			logReplication.info("ReplicationService.handleForceReplicateDeleteRequest - document " + docid + " was successfully deleted ");
739
		} catch (Exception e) {
740
			logMetacat.error("ReplicationService.handleForceReplicateDeleteRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);
741
			logReplication.error("document " + docid
742
					+ " failed to delete because " + e.getMessage());
743
			logReplication.error("ReplicationService.handleForceReplicateDeleteRequest - error: " + e.getMessage());
744 2298 tao
745 5014 daigle
		}//catch
746 2298 tao
747 5014 daigle
	}
748 2286 tao
749 5014 daigle
	/**
750
	 * when a forcereplication data file request comes in, local host sends a
751
	 * readdata request to the requesting server (remote server) for the specified
752
	 * docid. Then store it in local database and file system
753
	 */
754
	protected static void handleForceReplicateDataFileRequest(Hashtable<String, String[]> params,
755
			HttpServletRequest request) {
756 2286 tao
757 5014 daigle
		//make sure there is some parameters
758
		if (params.isEmpty()) {
759
			return;
760
		}
761
		// Get remote server
762
		String server = ((String[]) params.get("server"))[0];
763
		// the docid should include rev number
764
		String docid = ((String[]) params.get("docid"))[0];
765
		// Make sure there is a docid and server
766
		if (docid == null || server == null || server.equals("")) {
767
			logMetacat.error("ReplicationService.handleForceReplicateDataFileRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);
768
			logReplication.error("ReplicationService.handleForceReplicateDataFileRequest - Didn't specify docid or server for replication");
769
			return;
770
		}
771 2286 tao
772 5014 daigle
		// Overide or not
773
		//    boolean override = false;
774
		// dbaction - update or insert
775
		String dbaction = null;
776 2286 tao
777 5014 daigle
		try {
778
			//docid was switch to two parts uinque code and rev
779
			//String uniqueCode=MetacatUtil.getDocIdFromString(docid);
780
			//int rev=MetacatUtil.getVersionFromString(docid);
781
			if (params.containsKey("dbaction")) {
782
				dbaction = ((String[]) params.get("dbaction"))[0];
783
			} else//default value is update
784
			{
785
				dbaction = "update";
786
			}
787 2286 tao
788 5014 daigle
			logReplication.info("ReplicationService.handleForceReplicateDataFileRequest - force replication request from " + server);
789
			logReplication.info("ReplicationService.handleForceReplicateDataFileRequest - Force replication request from: " + server);
790
			logReplication.info("ReplicationService.handleForceReplicateDataFileRequest - Force replication docid: " + docid);
791
			logReplication.info("ReplicationService.handleForceReplicateDataFileRequest - Force replication action: " + dbaction);
792
			// get the document info from server
793
			URL docinfourl = new URL("https://" + server + "?server="
794
					+ MetacatUtil.getLocalReplicationServerName()
795
					+ "&action=getdocumentinfo&docid=" + docid);
796 1023 tao
797 5014 daigle
			String docInfoStr = ReplicationService.getURLContent(docinfourl);
798 2286 tao
799 5014 daigle
			//dih is the parser for the docinfo xml format
800
			DocInfoHandler dih = new DocInfoHandler();
801
			XMLReader docinfoParser = ReplicationHandler.initParser(dih);
802
			docinfoParser.parse(new InputSource(new StringReader(docInfoStr)));
803
			Hashtable<String, String> docinfoHash = dih.getDocInfo();
804
			String user = (String) docinfoHash.get("user_owner");
805 2286 tao
806 5014 daigle
			String docName = (String) docinfoHash.get("docname");
807 2286 tao
808 5014 daigle
			String docType = (String) docinfoHash.get("doctype");
809 2286 tao
810 5014 daigle
			String docHomeServer = (String) docinfoHash.get("home_server");
811 2286 tao
812 5014 daigle
			String createdDate = (String) docinfoHash.get("date_created");
813 2286 tao
814 5014 daigle
			String updatedDate = (String) docinfoHash.get("date_updated");
815
			logReplication.info("ReplicationService.handleForceReplicateDataFileRequest - docHomeServer of datafile: " + docHomeServer);
816
817
			//if action is delete, we don't delete the data file. Just archieve
818
			//the xml_documents
819
			/*if (dbaction.equals("delete"))
820
			{
821
			  //conn = util.getConnection();
822
			  DocumentImpl.delete(docid,user,null);
823
			  //util.returnConnection(conn);
824
			}*/
825
			//To data file insert or update is same
826
			if (dbaction.equals("insert") || dbaction.equals("update")) {
827
				//Get data file and store it into local file system.
828
				// sending back readdata request to server
829
				URL url = new URL("https://" + server + "?server="
830
						+ MetacatUtil.getLocalReplicationServerName()
831
						+ "&action=readdata&docid=" + docid);
832
				String datafilePath = PropertyService
833
						.getProperty("application.datafilepath");
834
835
				Exception writeException = null;
836
				//register data file into xml_documents table and wite data file
837
				//into file system
838
				try {
839
					DocumentImpl.writeDataFileInReplication(url.openStream(),
840
							datafilePath, docName, docType, docid, user, docHomeServer,
841
							server, DocumentImpl.DOCUMENTTABLE, false, createdDate,
842
							updatedDate);
843
				} catch (Exception e) {
844
					writeException = e;
845
				}
846
				//process extra access rules
847 5098 daigle
//				Vector<AccessControlForSingleFile> accessControlList = dih
848
//						.getAccessControlList();
849
//				if (accessControlList != null) {
850
//					for (int i = 0; i < accessControlList.size(); i++) {
851
//						AccessControlForSingleFile acfsf = (AccessControlForSingleFile) accessControlList
852
//								.get(i);
853
//						if (!acfsf.accessControlExists()) {
854
//							acfsf.insertPermissions();
855
//							logReplication.info("ReplicationService.handleForceReplicateDataFileRequest - datafile " + docid
856
//									+ " permissions added to DB");
857
//						}
858
//					}
859
//				}
860
861
		        Vector<XMLAccessDAO> accessControlList = dih.getAccessControlList();
862
		        if (accessControlList != null) {
863
		        	AccessControlForSingleFile acfsf = new AccessControlForSingleFile(docid);
864
		        	for (XMLAccessDAO xmlAccessDAO : accessControlList) {
865
		        		if (!acfsf.accessControlExists(xmlAccessDAO)) {
866
		        			acfsf.insertPermissions(xmlAccessDAO);
867
							logReplication.info("ReplicationService.handleForceReplicateRequest - document " + docid
868 5014 daigle
									+ " permissions added to DB");
869 5098 daigle
		        		}
870
		            }
871
		        }
872 5014 daigle
873
				if (writeException != null) {
874
					throw writeException;
875
				}
876
877
				//false means non-timed replication
878
				logReplication.info("ReplicationService.handleForceReplicateDataFileRequest - datafile " + docid + " added to DB with "
879
						+ "action " + dbaction);
880
				EventLog.getInstance().log(request.getRemoteAddr(), REPLICATIONUSER,
881
						docid, dbaction);
882
			}
883
884
		} catch (Exception e) {
885
			logMetacat.error("ReplicationService.handleForceReplicateDataFileRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);
886
			logReplication.error("ReplicationService.handleForceReplicateDataFileRequest - Datafile " + docid
887
					+ " failed to added to DB with " + "action " + dbaction + " because "
888
					+ e.getMessage());
889
			logReplication.error("ReplicationService.handleForceReplicateDataFileRequest - ERROR in MetacatReplication.handleForceDataFileReplicate"
890
					+ "Request(): " + e.getMessage());
891 4449 leinfelder
		}
892 5014 daigle
	}
893 2286 tao
894 5014 daigle
	/**
895
	 * Grants or denies a lock to a requesting host.
896
	 * The servlet parameters of interrest are:
897
	 * docid: the docid of the file the lock is being requested for
898
	 * currentdate: the timestamp of the document on the remote server
899
	 *
900
	 */
901 5755 leinfelder
	protected static void handleGetLockRequest(
902 5014 daigle
			Hashtable<String, String[]> params, HttpServletResponse response) {
903 1292 tao
904 5014 daigle
		try {
905 2286 tao
906 5014 daigle
			String docid = ((String[]) params.get("docid"))[0];
907
			String remoteRev = ((String[]) params.get("updaterev"))[0];
908
			DocumentImpl requestDoc = new DocumentImpl(docid);
909
			logReplication.info("ReplicationService.handleGetLockRequest - lock request for " + docid);
910
			int localRevInt = requestDoc.getRev();
911
			int remoteRevInt = Integer.parseInt(remoteRev);
912 2286 tao
913 5755 leinfelder
			// get a writer for sending back to response
914
			response.setContentType("text/xml");
915
			Writer out = response.getWriter();
916
917 5014 daigle
			if (remoteRevInt >= localRevInt) {
918
				if (!fileLocks.contains(docid)) { //grant the lock if it is not already locked
919
					fileLocks.add(0, docid); //insert at the beginning of the queue Vector
920
					//send a message back to the the remote host authorizing the insert
921 5755 leinfelder
					out.write("<lockgranted><docid>" + docid
922 5014 daigle
									+ "</docid></lockgranted>");
923
					//          lockThread = new Thread(this);
924
					//          lockThread.setPriority(Thread.MIN_PRIORITY);
925
					//          lockThread.start();
926
					logReplication.info("ReplicationService.handleGetLockRequest - lock granted for " + docid);
927
				} else { //deny the lock
928 5755 leinfelder
					out.write("<filelocked><docid>" + docid + "</docid></filelocked>");
929 5014 daigle
					logReplication.info("ReplicationService.handleGetLockRequest - lock denied for " + docid
930
							+ "reason: file already locked");
931
				}
932
			} else {//deny the lock.
933 5755 leinfelder
				out.write("<outdatedfile><docid>" + docid + "</docid></filelocked>");
934 5014 daigle
				logReplication.info("ReplicationService.handleGetLockRequest - lock denied for " + docid
935
						+ "reason: client has outdated file");
936
			}
937 5755 leinfelder
			out.close();
938 5014 daigle
			//conn.close();
939
		} catch (Exception e) {
940
			logMetacat.error("ReplicationService.handleGetLockRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);
941
			logReplication.error("ReplicationService.handleGetLockRequest - error requesting file lock from MetacatReplication."
942
					+ "handleGetLockRequest: " + e.getMessage());
943
			e.printStackTrace(System.out);
944
		}
945
	}
946 2286 tao
947 5014 daigle
	/**
948
	 * Sends all of the xml_documents information encoded in xml to a requestor
949
	 * the format is:
950
	 * <!ELEMENT documentinfo (docid, docname, doctype, doctitle, user_owner,
951
	 *                  user_updated, home_server, public_access, rev)/>
952
	 * all of the subelements of document info are #PCDATA
953
	 */
954 5755 leinfelder
	protected static void handleGetDocumentInfoRequest(
955 5014 daigle
			Hashtable<String, String[]> params, HttpServletResponse response) {
956
		String docid = ((String[]) (params.get("docid")))[0];
957
		StringBuffer sb = new StringBuffer();
958 2286 tao
959 5014 daigle
		try {
960 5322 berkley
		  IdentifierManager idman = IdentifierManager.getInstance();
961 2286 tao
962 5014 daigle
			DocumentImpl doc = new DocumentImpl(docid);
963
			sb.append("<documentinfo><docid>").append(docid);
964 5322 berkley
			sb.append("</docid>");
965
			try
966
			{
967 5323 berkley
			  String guid = idman.getGUID(doc.getDocID(), doc.getRev());
968
			  sb.append("<guid>").append(guid).append("</guid>");
969 5454 berkley
			  String smLocalId = idman.getSystemMetadataLocalId(guid);
970 5895 berkley
			  Hashtable<String, String> sysmetaInfo = idman.getSystemMetadataInfo(smLocalId);
971 5454 berkley
			  if(smLocalId != null && !smLocalId.trim().equals(""))
972
			  {
973
			      sb.append("<systemmetadatalocalid>").append(smLocalId).append("</systemmetadatalocalid>");
974
			  }
975 5895 berkley
976
			  Enumeration<String> sysmetaKeys = sysmetaInfo.keys();
977
			  while(sysmetaKeys.hasMoreElements())
978
			  {
979
			      String key = sysmetaKeys.nextElement();
980
			      sb.append("<" + key + ">" + sysmetaInfo.get(key).toString() + "</" + key + ">");
981
			  }
982
983 5322 berkley
			}
984
			catch(McdbDocNotFoundException e)
985
			{
986
			  //do nothing, there was no guid for this document
987
			}
988
			sb.append("<docname>").append(doc.getDocname());
989 5014 daigle
			sb.append("</docname><doctype>").append(doc.getDoctype());
990
			sb.append("</doctype>");
991
			sb.append("<user_owner>").append(doc.getUserowner());
992
			sb.append("</user_owner><user_updated>").append(doc.getUserupdated());
993
			sb.append("</user_updated>");
994
			sb.append("<date_created>");
995
			sb.append(doc.getCreateDate());
996
			sb.append("</date_created>");
997
			sb.append("<date_updated>");
998
			sb.append(doc.getUpdateDate());
999
			sb.append("</date_updated>");
1000
			sb.append("<home_server>");
1001
			sb.append(doc.getDocHomeServer());
1002
			sb.append("</home_server>");
1003
			sb.append("<public_access>").append(doc.getPublicaccess());
1004
			sb.append("</public_access><rev>").append(doc.getRev());
1005
			sb.append("</rev>");
1006 2286 tao
1007 5014 daigle
			sb.append("<accessControl>");
1008 5098 daigle
1009
			AccessControlForSingleFile acfsf = new AccessControlForSingleFile(docid);
1010
			sb.append(acfsf.getAccessString());
1011
1012 5014 daigle
			sb.append("</accessControl>");
1013 2286 tao
1014 5014 daigle
			sb.append("</documentinfo>");
1015 5755 leinfelder
			// get a writer for sending back to response
1016 5014 daigle
			response.setContentType("text/xml");
1017 5755 leinfelder
			Writer out = response.getWriter();
1018
			out.write(sb.toString());
1019
			out.close();
1020 2286 tao
1021 5014 daigle
		} catch (Exception e) {
1022
			logMetacat.error("ReplicationService.handleGetDocumentInfoRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);
1023
			logReplication.error("ReplicationService.handleGetDocumentInfoRequest - error in metacatReplication.handlegetdocumentinforequest "
1024
					+ "for doc: " + docid + " : " + e.getMessage());
1025
		}
1026 2286 tao
1027 5014 daigle
	}
1028 2286 tao
1029 5014 daigle
	/**
1030
	 * Sends a datafile to a remote host
1031
	 */
1032
	protected static void handleGetDataFileRequest(OutputStream outPut,
1033
			Hashtable<String, String[]> params, HttpServletResponse response)
1034 2286 tao
1035 5014 daigle
	{
1036
		// File path for data file
1037
		String filepath;
1038
		// Request docid
1039
		String docId = ((String[]) (params.get("docid")))[0];
1040
		//check if the doicd is null
1041
		if (docId == null) {
1042
			logMetacat.error("ReplicationService.handleGetDataFileRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);
1043
			logReplication.error("ReplicationService.handleGetDataFileRequest - Didn't specify docid for replication");
1044
			return;
1045
		}
1046 2286 tao
1047 5014 daigle
		//try to open a https stream to test if the request server's public key
1048
		//in the key store, this is security issue
1049
		try {
1050
			filepath = PropertyService.getProperty("application.datafilepath");
1051
			String server = params.get("server")[0];
1052
			URL u = new URL("https://" + server + "?server="
1053
					+ MetacatUtil.getLocalReplicationServerName() + "&action=test");
1054
			String test = ReplicationService.getURLContent(u);
1055
			//couldn't pass the test
1056
			if (test.indexOf("successfully") == -1) {
1057
				//response.setContentType("text/xml");
1058
				//outPut.println("<error>Couldn't pass the trust test</error>");
1059
				logMetacat.error("ReplicationService.handleGetDataFileRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);
1060
				logReplication.error("ReplicationService.handleGetDataFileRequest - Couldn't pass the trust test");
1061
				return;
1062
			}
1063
		}//try
1064
		catch (Exception ee) {
1065
			return;
1066
		}//catch
1067 2286 tao
1068 5014 daigle
		if (!filepath.endsWith("/")) {
1069
			filepath += "/";
1070
		}
1071
		// Get file aboslute file name
1072
		String filename = filepath + docId;
1073 2286 tao
1074 5014 daigle
		//MIME type
1075
		String contentType = null;
1076
		if (filename.endsWith(".xml")) {
1077
			contentType = "text/xml";
1078
		} else if (filename.endsWith(".css")) {
1079
			contentType = "text/css";
1080
		} else if (filename.endsWith(".dtd")) {
1081
			contentType = "text/plain";
1082
		} else if (filename.endsWith(".xsd")) {
1083
			contentType = "text/xml";
1084
		} else if (filename.endsWith("/")) {
1085
			contentType = "text/html";
1086
		} else {
1087
			File f = new File(filename);
1088
			if (f.isDirectory()) {
1089
				contentType = "text/html";
1090
			} else {
1091
				contentType = "application/octet-stream";
1092
			}
1093
		}
1094 2286 tao
1095 5014 daigle
		// Set the mime type
1096
		response.setContentType(contentType);
1097 2286 tao
1098 5014 daigle
		// Get the content of the file
1099
		FileInputStream fin = null;
1100
		try {
1101
			// FileInputStream to metacat
1102
			fin = new FileInputStream(filename);
1103
			// 4K buffer
1104
			byte[] buf = new byte[4 * 1024];
1105
			// Read data from file input stream to byte array
1106
			int b = fin.read(buf);
1107
			// Write to outStream from byte array
1108
			while (b != -1) {
1109
				outPut.write(buf, 0, b);
1110
				b = fin.read(buf);
1111
			}
1112
			// close file input stream
1113
			fin.close();
1114 2286 tao
1115 5014 daigle
		}//try
1116
		catch (Exception e) {
1117
			logMetacat.error("ReplicationService.handleGetDataFileRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);
1118
			logReplication.error("ReplicationService.handleGetDataFileRequest - error getting data file from MetacatReplication."
1119
					+ "handlGetDataFileRequest " + e.getMessage());
1120
			e.printStackTrace(System.out);
1121
		}//catch
1122 2286 tao
1123 5014 daigle
	}
1124
1125
	/**
1126 4854 daigle
	 * Sends a document to a remote host
1127
	 */
1128 5755 leinfelder
	protected static void handleGetDocumentRequest(
1129 4854 daigle
			Hashtable<String, String[]> params, HttpServletResponse response) {
1130 2286 tao
1131 4854 daigle
		String urlString = null;
1132
		String documentPath = null;
1133 5755 leinfelder
		String errorMsg = null;
1134 4854 daigle
		try {
1135
			// try to open a https stream to test if the request server's public
1136
			// key
1137
			// in the key store, this is security issue
1138
			String server = params.get("server")[0];
1139
			urlString = "https://" + server + "?server="
1140
					+ MetacatUtil.getLocalReplicationServerName() + "&action=test";
1141
			URL u = new URL(urlString);
1142 5014 daigle
			String test = ReplicationService.getURLContent(u);
1143 4854 daigle
			// couldn't pass the test
1144
			if (test.indexOf("successfully") == -1) {
1145
				response.setContentType("text/xml");
1146 5755 leinfelder
				Writer out = response.getWriter();
1147
				out.write("<error>Couldn't pass the trust test " + test + " </error>");
1148 4854 daigle
				out.close();
1149
				return;
1150
			}
1151 2286 tao
1152 4854 daigle
			String docid = params.get("docid")[0];
1153 5014 daigle
			logReplication.debug("ReplicationService.handleGetDocumentRequest - MetacatReplication.handleGetDocumentRequest for docid: "
1154
					+ docid);
1155 4854 daigle
			DocumentImpl di = new DocumentImpl(docid);
1156 2286 tao
1157 5014 daigle
			String documentDir = PropertyService
1158
					.getProperty("application.documentfilepath");
1159 4854 daigle
			documentPath = documentDir + FileUtil.getFS() + docid;
1160 4488 daigle
1161 4854 daigle
			// if the document does not exist on disk, read it from db and write
1162
			// it to disk.
1163
			if (FileUtil.getFileStatus(documentPath) == FileUtil.DOES_NOT_EXIST
1164
					|| FileUtil.getFileSize(documentPath) == 0) {
1165 5752 leinfelder
				FileOutputStream fos = new FileOutputStream(documentPath);
1166
				di.toXml(fos, null, null, true);
1167 4854 daigle
			}
1168 2286 tao
1169 5752 leinfelder
			// read the file from disk and send it to outputstream
1170
			OutputStream outputStream = response.getOutputStream();
1171
			di.readFromFileSystem(outputStream, null, null, documentPath);
1172 2286 tao
1173 5014 daigle
			logReplication.info("ReplicationService.handleGetDocumentRequest - document " + docid + " sent");
1174 4854 daigle
1175 5755 leinfelder
			// return to avoid continuing to the error reporting section at the end
1176
			return;
1177
1178 4854 daigle
		} catch (MalformedURLException mue) {
1179 5014 daigle
			logMetacat.error("ReplicationService.handleGetDocumentRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);
1180
			logReplication.error("ReplicationService.handleGetDocumentRequest - Url error when getting document from MetacatReplication."
1181 4854 daigle
					+ "handlGetDocumentRequest for url: " + urlString + " : "
1182
					+ mue.getMessage());
1183
			// e.printStackTrace(System.out);
1184 5755 leinfelder
1185 4854 daigle
		} catch (IOException ioe) {
1186 5014 daigle
			logMetacat.error("ReplicationService.handleGetDocumentRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);
1187
			logReplication.error("ReplicationService.handleGetDocumentRequest - I/O error when getting document from MetacatReplication."
1188
					+ "handlGetDocumentRequest for file: " + documentPath + " : "
1189
					+ ioe.getMessage());
1190 5755 leinfelder
			errorMsg = ioe.getMessage();
1191 4854 daigle
		} catch (PropertyNotFoundException pnfe) {
1192 5014 daigle
			logMetacat.error("ReplicationService.handleGetDocumentRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);
1193
			logReplication
1194
					.error("ReplicationService.handleGetDocumentRequest - Error getting property when getting document from MetacatReplication."
1195
							+ "handlGetDocumentRequest for file: "
1196
							+ documentPath
1197
							+ " : "
1198
							+ pnfe.getMessage());
1199 4854 daigle
			// e.printStackTrace(System.out);
1200 5755 leinfelder
			errorMsg = pnfe.getMessage();
1201 4854 daigle
		} catch (McdbException me) {
1202 5014 daigle
			logReplication
1203
					.error("ReplicationService.handleGetDocumentRequest - Document implementation error  getting property when getting document from MetacatReplication."
1204
							+ "handlGetDocumentRequest for file: "
1205
							+ documentPath
1206
							+ " : "
1207
							+ me.getMessage());
1208 4854 daigle
			// e.printStackTrace(System.out);
1209 5755 leinfelder
			errorMsg = me.getMessage();
1210 4854 daigle
		}
1211 5755 leinfelder
1212
		// report any errors if we got here
1213
		response.setContentType("text/xml");
1214
		Writer out = null;
1215
		try {
1216
			response.getWriter();
1217
			out = response.getWriter();
1218
			out.write("<error>" + errorMsg + "</error>");
1219
		} catch (Exception e) {
1220
			logMetacat.error(e.getMessage(), e);
1221
		} finally {
1222
			try {
1223
				out.close();
1224
			} catch (IOException e) {
1225
				logMetacat.error(e.getMessage(), e);
1226
			}
1227
		}
1228
1229 4854 daigle
1230
	}
1231
1232 5014 daigle
	/**
1233 4854 daigle
	 * Sends a list of all of the documents on this sever along with their
1234
	 * revision numbers. The format is: <!ELEMENT replication (server, updates)>
1235
	 * <!ELEMENT server (#PCDATA)> <!ELEMENT updates ((updatedDocument |
1236
	 * deleteDocument | revisionDocument)*)> <!ELEMENT updatedDocument (docid,
1237
	 * rev, datafile*)> <!ELEMENT deletedDocument (docid, rev)> <!ELEMENT
1238
	 * revisionDocument (docid, rev, datafile*)> <!ELEMENT docid (#PCDATA)>
1239
	 * <!ELEMENT rev (#PCDATA)> <!ELEMENT datafile (#PCDATA)> note that the rev
1240
	 * in deletedDocument is always empty. I just left it in there to make the
1241
	 * parser implementation easier.
1242
	 */
1243 5755 leinfelder
	protected static void handleUpdateRequest(Hashtable<String, String[]> params,
1244 5014 daigle
			HttpServletResponse response) {
1245
		// Checked out DBConnection
1246
		DBConnection dbConn = null;
1247
		// DBConenction serial number when checked it out
1248
		int serialNumber = -1;
1249
		PreparedStatement pstmt = null;
1250
		// Server list to store server info of xml_replication table
1251
		ReplicationServerList serverList = null;
1252 5755 leinfelder
1253
		// a writer for response
1254
		Writer out = null;
1255 2286 tao
1256 5014 daigle
		try {
1257 5755 leinfelder
			// get writer, TODO: encoding?
1258
			response.setContentType("text/xml");
1259
			out = response.getWriter();
1260
1261 5014 daigle
			// Check out a DBConnection from pool
1262
			dbConn = DBConnectionPool
1263
					.getDBConnection("MetacatReplication.handleUpdateRequest");
1264
			serialNumber = dbConn.getCheckOutSerialNumber();
1265
			// Create a server list from xml_replication table
1266
			serverList = new ReplicationServerList();
1267 2286 tao
1268 5014 daigle
			// Get remote server name from param
1269
			String server = ((String[]) params.get("server"))[0];
1270
			// If no servr name in param, return a error
1271
			if (server == null || server.equals("")) {
1272 5755 leinfelder
				out.write("<error>Request didn't specify server name</error>");
1273 5014 daigle
				out.close();
1274
				return;
1275
			}//if
1276 2286 tao
1277 5014 daigle
			//try to open a https stream to test if the request server's public key
1278
			//in the key store, this is security issue
1279 5440 berkley
			String testUrl = "https://" + server + "?server="
1280 5441 berkley
            + MetacatUtil.getLocalReplicationServerName() + "&action=test";
1281 5440 berkley
			logReplication.info("Running trust test: " + testUrl);
1282
			URL u = new URL(testUrl);
1283 5014 daigle
			String test = ReplicationService.getURLContent(u);
1284 5441 berkley
			logReplication.info("Ouput from test is '" + test + "'");
1285 5014 daigle
			//couldn't pass the test
1286
			if (test.indexOf("successfully") == -1) {
1287 5441 berkley
			    logReplication.error("Trust test failed.");
1288 5755 leinfelder
				out.write("<error>Couldn't pass the trust test</error>");
1289 5014 daigle
				out.close();
1290
				return;
1291
			}
1292 5441 berkley
			logReplication.info("Trust test succeeded.");
1293 2286 tao
1294 5014 daigle
			// Check if local host configure to replicate xml documents to remote
1295
			// server. If not send back a error message
1296
			if (!serverList.getReplicationValue(server)) {
1297 5755 leinfelder
				out.write("<error>Configuration not allow to replicate document to you</error>");
1298 5014 daigle
				out.close();
1299
				return;
1300
			}//if
1301 2286 tao
1302 5014 daigle
			// Store the sql command
1303
			StringBuffer docsql = new StringBuffer();
1304
			StringBuffer revisionSql = new StringBuffer();
1305
			// Stroe the docid list
1306
			StringBuffer doclist = new StringBuffer();
1307
			// Store the deleted docid list
1308
			StringBuffer delsql = new StringBuffer();
1309
			// Store the data set file
1310
			Vector<Vector<String>> packageFiles = new Vector<Vector<String>>();
1311 2286 tao
1312 5014 daigle
			// Append local server's name and replication servlet to doclist
1313
			doclist.append("<?xml version=\"1.0\"?><replication>");
1314
			doclist.append("<server>")
1315
					.append(MetacatUtil.getLocalReplicationServerName());
1316
			//doclist.append(util.getProperty("replicationpath"));
1317
			doclist.append("</server><updates>");
1318 2286 tao
1319 5014 daigle
			// Get correct docid that reside on this server according the requesting
1320
			// server's replicate and data replicate value in xml_replication table
1321 5319 jones
			docsql.append(DatabaseService.getInstance().getDBAdapter().getReplicationDocumentListSQL());
1322 5014 daigle
			//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)) ");
1323
			revisionSql.append("select docid, rev, doctype from xml_revisions ");
1324
			// If the localhost is not a hub to the remote server, only replicate
1325
			// the docid' which home server is local host (server_location =1)
1326
			if (!serverList.getHubValue(server)) {
1327
				String serverLocationDoc = " and a.server_location = 1";
1328
				String serverLocationRev = "where server_location = 1";
1329
				docsql.append(serverLocationDoc);
1330
				revisionSql.append(serverLocationRev);
1331
			}
1332
			logReplication.info("ReplicationService.handleUpdateRequest - Doc sql: " + docsql.toString());
1333 2286 tao
1334 5014 daigle
			// Get any deleted documents
1335
			delsql.append("select distinct docid from ");
1336
			delsql.append("xml_revisions where docid not in (select docid from ");
1337
			delsql.append("xml_documents) ");
1338
			// If the localhost is not a hub to the remote server, only replicate
1339
			// the docid' which home server is local host (server_location =1)
1340
			if (!serverList.getHubValue(server)) {
1341
				delsql.append("and server_location = 1");
1342
			}
1343
			logReplication.info("ReplicationService.handleUpdateRequest - Deleted sql: " + delsql.toString());
1344 2286 tao
1345 5014 daigle
			// Get docid list of local host
1346
			pstmt = dbConn.prepareStatement(docsql.toString());
1347
			pstmt.execute();
1348
			ResultSet rs = pstmt.getResultSet();
1349
			boolean tablehasrows = rs.next();
1350
			//If metacat configed to replicate data file
1351
			//if ((util.getProperty("replicationsenddata")).equals("on"))
1352
			boolean replicateData = serverList.getDataReplicationValue(server);
1353
			if (replicateData) {
1354
				while (tablehasrows) {
1355
					String recordDoctype = rs.getString(3);
1356
					Vector<String> packagedoctypes = MetacatUtil
1357
							.getOptionList(PropertyService
1358
									.getProperty("xml.packagedoctype"));
1359
					//if this is a package file, put it at the end
1360
					//because if a package file is read before all of the files it
1361
					//refers to are loaded then there is an error
1362
					if (recordDoctype != null && !packagedoctypes.contains(recordDoctype)) {
1363
						//If this is not data file
1364
						if (!recordDoctype.equals("BIN")) {
1365
							//for non-data file document
1366
							doclist.append("<updatedDocument>");
1367
							doclist.append("<docid>").append(rs.getString(1));
1368
							doclist.append("</docid><rev>").append(rs.getInt(2));
1369
							doclist.append("</rev>");
1370
							doclist.append("</updatedDocument>");
1371
						}//if
1372
						else {
1373
							//for data file document, in datafile attributes
1374
							//we put "datafile" value there
1375
							doclist.append("<updatedDocument>");
1376
							doclist.append("<docid>").append(rs.getString(1));
1377
							doclist.append("</docid><rev>").append(rs.getInt(2));
1378
							doclist.append("</rev>");
1379
							doclist.append("<datafile>");
1380
							doclist.append(PropertyService
1381
									.getProperty("replication.datafileflag"));
1382
							doclist.append("</datafile>");
1383
							doclist.append("</updatedDocument>");
1384
						}//else
1385
					}//if packagedoctpes
1386
					else { //the package files are saved to be put into the xml later.
1387
						Vector<String> v = new Vector<String>();
1388
						v.add(rs.getString(1));
1389
						v.add(String.valueOf(rs.getInt(2)));
1390
						packageFiles.add(v);
1391
					}//esle
1392
					tablehasrows = rs.next();
1393
				}//while
1394
			}//if
1395
			else //metacat was configured not to send data file
1396
			{
1397
				while (tablehasrows) {
1398
					String recordDoctype = rs.getString(3);
1399
					if (!recordDoctype.equals("BIN")) { //don't replicate data files
1400
						Vector<String> packagedoctypes = MetacatUtil
1401
								.getOptionList(PropertyService
1402
										.getProperty("xml.packagedoctype"));
1403
						if (recordDoctype != null
1404
								&& !packagedoctypes.contains(recordDoctype)) { //if this is a package file, put it at the end
1405
							//because if a package file is read before all of the files it
1406
							//refers to are loaded then there is an error
1407
							doclist.append("<updatedDocument>");
1408
							doclist.append("<docid>").append(rs.getString(1));
1409
							doclist.append("</docid><rev>").append(rs.getInt(2));
1410
							doclist.append("</rev>");
1411
							doclist.append("</updatedDocument>");
1412
						} else { //the package files are saved to be put into the xml later.
1413
							Vector<String> v = new Vector<String>();
1414
							v.add(rs.getString(1));
1415
							v.add(String.valueOf(rs.getInt(2)));
1416
							packageFiles.add(v);
1417
						}
1418
					}//if
1419
					tablehasrows = rs.next();
1420
				}//while
1421
			}//else
1422 2286 tao
1423 5014 daigle
			pstmt = dbConn.prepareStatement(delsql.toString());
1424
			//usage count should increas 1
1425
			dbConn.increaseUsageCount(1);
1426 2286 tao
1427 5014 daigle
			pstmt.execute();
1428
			rs = pstmt.getResultSet();
1429
			tablehasrows = rs.next();
1430
			while (tablehasrows) { //handle the deleted documents
1431
				doclist.append("<deletedDocument><docid>").append(rs.getString(1));
1432
				doclist.append("</docid><rev></rev></deletedDocument>");
1433
				//note that rev is always empty for deleted docs
1434
				tablehasrows = rs.next();
1435
			}
1436 2286 tao
1437 5014 daigle
			//now we can put the package files into the xml results
1438
			for (int i = 0; i < packageFiles.size(); i++) {
1439
				Vector<String> v = packageFiles.elementAt(i);
1440
				doclist.append("<updatedDocument>");
1441
				doclist.append("<docid>").append(v.elementAt(0));
1442
				doclist.append("</docid><rev>");
1443
				doclist.append(v.elementAt(1));
1444
				doclist.append("</rev>");
1445
				doclist.append("</updatedDocument>");
1446
			}
1447
			// add revision doc list
1448
			doclist.append(prepareRevisionDoc(dbConn, revisionSql.toString(),
1449
					replicateData));
1450 2286 tao
1451 5014 daigle
			doclist.append("</updates></replication>");
1452
			logReplication.info("ReplicationService.handleUpdateRequest - doclist: " + doclist.toString());
1453
			pstmt.close();
1454
			//conn.close();
1455 5755 leinfelder
			out.write(doclist.toString());
1456 2286 tao
1457 5014 daigle
		} catch (Exception e) {
1458
			logMetacat.error("ReplicationService.handleUpdateRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);
1459
			logReplication.error("ReplicationService.handleUpdateRequest - error in MetacatReplication." + "handleupdaterequest: "
1460
					+ e.getMessage());
1461
			//e.printStackTrace(System.out);
1462 5755 leinfelder
			try {
1463
				out.write("<error>" + e.getMessage() + "</error>");
1464
			} catch (IOException e1) {
1465
				logMetacat.error(e1.getMessage(), e1);
1466
			}
1467 5014 daigle
		} finally {
1468
			try {
1469
				pstmt.close();
1470
			}//try
1471
			catch (SQLException ee) {
1472
				logMetacat.error("ReplicationService.handleUpdateRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);
1473
				logReplication.error("ReplicationService.handleUpdateRequest - Error in MetacatReplication."
1474
						+ "handleUpdaterequest to close pstmt: " + ee.getMessage());
1475
			}//catch
1476
			finally {
1477
				DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1478
			}//finally
1479 5755 leinfelder
			try {
1480
				out.close();
1481
			} catch (IOException e) {
1482
				logMetacat.error(e.getMessage(), e);
1483
			}
1484 5014 daigle
		}//finally
1485 2286 tao
1486 5014 daigle
	}//handlUpdateRequest
1487 2286 tao
1488 5014 daigle
	/*
1489
	 * This method will get the xml string for document in xml_revision
1490
	 * The schema look like <!ELEMENT revisionDocument (docid, rev, datafile*)>
1491
	 */
1492
	private static String prepareRevisionDoc(DBConnection dbConn, String revSql,
1493
			boolean replicateData) throws Exception {
1494
		logReplication.warn("ReplicationService.prepareRevisionDoc - The revision document sql is " + revSql);
1495
		StringBuffer revDocList = new StringBuffer();
1496
		PreparedStatement pstmt = dbConn.prepareStatement(revSql);
1497
		//usage count should increas 1
1498
		dbConn.increaseUsageCount(1);
1499 2286 tao
1500 5014 daigle
		pstmt.execute();
1501
		ResultSet rs = pstmt.getResultSet();
1502
		boolean tablehasrows = rs.next();
1503
		while (tablehasrows) {
1504
			String recordDoctype = rs.getString(3);
1505 2286 tao
1506 5014 daigle
			//If this is data file and it isn't configured to replicate data
1507
			if (recordDoctype.equals("BIN") && !replicateData) {
1508
				// do nothing
1509
				continue;
1510
			} else {
1511 2597 tao
1512 5014 daigle
				revDocList.append("<revisionDocument>");
1513
				revDocList.append("<docid>").append(rs.getString(1));
1514
				revDocList.append("</docid><rev>").append(rs.getInt(2));
1515
				revDocList.append("</rev>");
1516
				// data file
1517
				if (recordDoctype.equals("BIN")) {
1518
					revDocList.append("<datafile>");
1519
					revDocList.append(PropertyService
1520
							.getProperty("replication.datafileflag"));
1521
					revDocList.append("</datafile>");
1522
				}
1523
				revDocList.append("</revisionDocument>");
1524 2286 tao
1525 5014 daigle
			}//else
1526
			tablehasrows = rs.next();
1527
		}
1528
		//System.out.println("The revision list is"+ revDocList.toString());
1529
		return revDocList.toString();
1530
	}
1531 2286 tao
1532 5014 daigle
	/**
1533
	 * Returns the xml_catalog table encoded in xml
1534
	 */
1535
	public static String getCatalogXML() {
1536 5755 leinfelder
		return handleGetCatalogRequest(null, null, false);
1537 5014 daigle
	}
1538 2286 tao
1539 5014 daigle
	/**
1540
	 * Sends the contents of the xml_catalog table encoded in xml
1541
	 * The xml format is:
1542
	 * <!ELEMENT xml_catalog (row*)>
1543
	 * <!ELEMENT row (entry_type, source_doctype, target_doctype, public_id,
1544
	 *                system_id)>
1545
	 * All of the sub elements of row are #PCDATA
1546 2286 tao
1547 5014 daigle
	 * If printFlag == false then do not print to out.
1548
	 */
1549 5755 leinfelder
	protected static String handleGetCatalogRequest(
1550 5014 daigle
			Hashtable<String, String[]> params, HttpServletResponse response,
1551
			boolean printFlag) {
1552
		DBConnection dbConn = null;
1553
		int serialNumber = -1;
1554
		PreparedStatement pstmt = null;
1555 5755 leinfelder
		Writer out = null;
1556 5014 daigle
		try {
1557 5755 leinfelder
			// get writer, TODO: encoding?
1558 5944 berkley
		    if(printFlag)
1559
		    {
1560
		        response.setContentType("text/xml");
1561
		        out = response.getWriter();
1562
		    }
1563 5014 daigle
			/*conn = MetacatReplication.getDBConnection("MetacatReplication." +
1564
			                                          "handleGetCatalogRequest");*/
1565
			dbConn = DBConnectionPool
1566
					.getDBConnection("MetacatReplication.handleGetCatalogRequest");
1567
			serialNumber = dbConn.getCheckOutSerialNumber();
1568
			pstmt = dbConn.prepareStatement("select entry_type, "
1569
					+ "source_doctype, target_doctype, public_id, "
1570
					+ "system_id from xml_catalog");
1571
			pstmt.execute();
1572
			ResultSet rs = pstmt.getResultSet();
1573
			boolean tablehasrows = rs.next();
1574
			StringBuffer sb = new StringBuffer();
1575
			sb.append("<?xml version=\"1.0\"?><xml_catalog>");
1576
			while (tablehasrows) {
1577
				sb.append("<row><entry_type>").append(rs.getString(1));
1578
				sb.append("</entry_type><source_doctype>").append(rs.getString(2));
1579
				sb.append("</source_doctype><target_doctype>").append(rs.getString(3));
1580
				sb.append("</target_doctype><public_id>").append(rs.getString(4));
1581
				// system id may not have server url on front.  Add it if not.
1582
				String systemID = rs.getString(5);
1583
				if (!systemID.startsWith("http://")) {
1584
					systemID = SystemUtil.getContextURL() + systemID;
1585
				}
1586
				sb.append("</public_id><system_id>").append(systemID);
1587
				sb.append("</system_id></row>");
1588 2286 tao
1589 5014 daigle
				tablehasrows = rs.next();
1590
			}
1591
			sb.append("</xml_catalog>");
1592
			//conn.close();
1593
			if (printFlag) {
1594
				response.setContentType("text/xml");
1595 5755 leinfelder
				out.write(sb.toString());
1596 5014 daigle
			}
1597
			pstmt.close();
1598
			return sb.toString();
1599
		} catch (Exception e) {
1600
			logMetacat.error("ReplicationService.handleGetCatalogRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);
1601
			logReplication.error("ReplicationService.handleGetCatalogRequest - error in MetacatReplication.handleGetCatalogRequest:"
1602
					+ e.getMessage());
1603
			e.printStackTrace(System.out);
1604
			if (printFlag) {
1605 5755 leinfelder
				try {
1606
					out.write("<error>" + e.getMessage() + "</error>");
1607
				} catch (IOException e1) {
1608
					logMetacat.error(e1.getMessage(), e1);
1609
				}
1610 5014 daigle
			}
1611
		} finally {
1612
			try {
1613
				pstmt.close();
1614
			}//try
1615
			catch (SQLException ee) {
1616
				logMetacat.error("ReplicationService.handleGetCatalogRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);
1617
				logReplication.error("ReplicationService.handleGetCatalogRequest - Error in MetacatReplication.handleGetCatalogRequest: "
1618
						+ ee.getMessage());
1619
			}//catch
1620
			finally {
1621
				DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1622
			}//finally
1623 5755 leinfelder
			if (out != null) {
1624
				try {
1625
					out.close();
1626
				} catch (IOException e1) {
1627
					logMetacat.error(e1.getMessage(), e1);
1628
				}
1629
			}
1630 5014 daigle
		}//finally
1631 2286 tao
1632 5014 daigle
		return null;
1633
	}
1634 2286 tao
1635 5014 daigle
	/**
1636
	 * Sends the current system date to the remote server.  Using this action
1637
	 * for replication gets rid of any problems with syncronizing clocks
1638
	 * because a time specific to a document is always kept on its home server.
1639
	 */
1640 5755 leinfelder
	protected static void handleGetTimeRequest(
1641 5014 daigle
			Hashtable<String, String[]> params, HttpServletResponse response) {
1642
		SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yy HH:mm:ss");
1643
		java.util.Date localtime = new java.util.Date();
1644
		String dateString = formatter.format(localtime);
1645 5755 leinfelder
1646
		// get a writer for sending back to response
1647 5014 daigle
		response.setContentType("text/xml");
1648 5755 leinfelder
		Writer out = null;
1649
		try {
1650
			out = response.getWriter();
1651
			out.write("<timestamp>" + dateString + "</timestamp>");
1652
			out.close();
1653
		} catch (IOException e) {
1654
			logMetacat.error(e.getMessage(), e);
1655
		}
1656
1657 5014 daigle
	}
1658 2286 tao
1659 5014 daigle
	/**
1660
	 * this method handles the timeout for a file lock.  when a lock is
1661
	 * granted it is granted for 30 seconds.  When this thread runs out
1662
	 * it deletes the docid from the queue, thus eliminating the lock.
1663
	 */
1664
	public void run() {
1665
		try {
1666
			logReplication.info("ReplicationService.run - thread started for docid: "
1667
					+ (String) fileLocks.elementAt(0));
1668 2286 tao
1669 5014 daigle
			Thread.sleep(30000); //the lock will expire in 30 seconds
1670
			logReplication.info("thread for docid: "
1671
					+ (String) fileLocks.elementAt(fileLocks.size() - 1) + " exiting.");
1672 2286 tao
1673 5014 daigle
			fileLocks.remove(fileLocks.size() - 1);
1674
			//fileLocks is treated as a FIFO queue.  If there are more than one lock
1675
			//in the vector, the first one inserted will be removed.
1676
		} catch (Exception e) {
1677
			logMetacat.error("ReplicationService.run - " + ReplicationService.METACAT_REPL_ERROR_MSG);
1678
			logReplication.error("ReplicationService.run - error in file lock thread from "
1679
					+ "MetacatReplication.run: " + e.getMessage());
1680
		}
1681
	}
1682 2286 tao
1683 5014 daigle
	/**
1684
	 * Returns the name of a server given a serverCode
1685
	 * @param serverCode the serverid of the server
1686
	 * @return the servername or null if the specified serverCode does not
1687
	 *         exist.
1688
	 */
1689
	public static String getServerNameForServerCode(int serverCode) {
1690
		//System.out.println("serverid: " + serverCode);
1691
		DBConnection dbConn = null;
1692
		int serialNumber = -1;
1693
		PreparedStatement pstmt = null;
1694
		try {
1695
			dbConn = DBConnectionPool.getDBConnection("MetacatReplication.getServer");
1696
			serialNumber = dbConn.getCheckOutSerialNumber();
1697
			String sql = new String("select server from "
1698
					+ "xml_replication where serverid = " + serverCode);
1699
			pstmt = dbConn.prepareStatement(sql);
1700
			//System.out.println("getserver sql: " + sql);
1701
			pstmt.execute();
1702
			ResultSet rs = pstmt.getResultSet();
1703
			boolean tablehasrows = rs.next();
1704
			if (tablehasrows) {
1705
				//System.out.println("server: " + rs.getString(1));
1706
				return rs.getString(1);
1707
			}
1708 2286 tao
1709 5014 daigle
			//conn.close();
1710
		} catch (Exception e) {
1711
			logMetacat.error("ReplicationService.getServerNameForServerCode - " + ReplicationService.METACAT_REPL_ERROR_MSG);
1712
			logReplication.error("ReplicationService.getServerNameForServerCode - Error in MetacatReplication.getServer: " + e.getMessage());
1713
		} finally {
1714
			try {
1715
				pstmt.close();
1716
			}//try
1717
			catch (SQLException ee) {
1718
				logMetacat.error("ReplicationService.getServerNameForServerCode - " + ReplicationService.METACAT_REPL_ERROR_MSG);
1719
				logReplication.error("ReplicationService.getServerNameForServerCode - Error in MetacactReplication.getserver: "
1720
						+ ee.getMessage());
1721
			}//catch
1722
			finally {
1723
				DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1724
			}//fianlly
1725
		}//finally
1726 2286 tao
1727 5014 daigle
		return null;
1728
		//return null if the server does not exist
1729
	}
1730 2286 tao
1731 5014 daigle
	/**
1732
	 * Returns a server code given a server name
1733
	 * @param server the name of the server
1734
	 * @return integer > 0 representing the code of the server, 0 if the server
1735
	 *  does not exist.
1736
	 */
1737
	public static int getServerCodeForServerName(String server) throws ServiceException {
1738
		DBConnection dbConn = null;
1739
		int serialNumber = -1;
1740
		PreparedStatement pstmt = null;
1741
		int serverCode = 0;
1742 2286 tao
1743 5014 daigle
		try {
1744 837 bojilova
1745 5014 daigle
			//conn = util.openDBConnection();
1746
			dbConn = DBConnectionPool.getDBConnection("MetacatReplication.getServerCode");
1747
			serialNumber = dbConn.getCheckOutSerialNumber();
1748
			pstmt = dbConn.prepareStatement("SELECT serverid FROM xml_replication "
1749
					+ "WHERE server LIKE '" + server + "'");
1750
			pstmt.execute();
1751
			ResultSet rs = pstmt.getResultSet();
1752
			boolean tablehasrows = rs.next();
1753
			if (tablehasrows) {
1754
				serverCode = rs.getInt(1);
1755
				pstmt.close();
1756
				//conn.close();
1757
				return serverCode;
1758
			}
1759 837 bojilova
1760 5014 daigle
		} catch (SQLException sqle) {
1761
			throw new ServiceException("ReplicationService.getServerCodeForServerName - "
1762
					+ "SQL error when getting server code: " + sqle.getMessage());
1763 2286 tao
1764 5014 daigle
		} finally {
1765
			try {
1766
				pstmt.close();
1767
				//conn.close();
1768
			}//try
1769
			catch (Exception ee) {
1770
				logMetacat.error("ReplicationService.getServerCodeForServerName - " + ReplicationService.METACAT_REPL_ERROR_MSG);
1771
				logReplication.error("ReplicationService.getServerNameForServerCode - Error in MetacatReplicatio.getServerCode: "
1772
						+ ee.getMessage());
1773 837 bojilova
1774 5014 daigle
			}//catch
1775
			finally {
1776
				DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1777
			}//finally
1778
		}//finally
1779 2286 tao
1780 5014 daigle
		return serverCode;
1781
	}
1782 2286 tao
1783 5014 daigle
	/**
1784
	 * Method to get a host server information for given docid
1785
	 * @param conn a connection to the database
1786
	 */
1787
	public static Hashtable<String, String> getHomeServerInfoForDocId(String docId) {
1788
		Hashtable<String, String> sl = new Hashtable<String, String>();
1789
		DBConnection dbConn = null;
1790
		int serialNumber = -1;
1791 5027 daigle
		docId = DocumentUtil.getDocIdFromString(docId);
1792 5014 daigle
		PreparedStatement pstmt = null;
1793
		int serverLocation;
1794
		try {
1795
			//get conection
1796
			dbConn = DBConnectionPool.getDBConnection("ReplicationHandler.getHomeServer");
1797
			serialNumber = dbConn.getCheckOutSerialNumber();
1798
			//get a server location from xml_document table
1799
			pstmt = dbConn.prepareStatement("select server_location from xml_documents "
1800
					+ "where docid = ?");
1801
			pstmt.setString(1, docId);
1802
			pstmt.execute();
1803
			ResultSet serverName = pstmt.getResultSet();
1804
			//get a server location
1805
			if (serverName.next()) {
1806
				serverLocation = serverName.getInt(1);
1807
				pstmt.close();
1808
			} else {
1809
				pstmt.close();
1810
				//ut.returnConnection(conn);
1811
				return null;
1812
			}
1813
			pstmt = dbConn.prepareStatement("select server, last_checked, replicate "
1814
					+ "from xml_replication where serverid = ?");
1815
			//increase usage count
1816
			dbConn.increaseUsageCount(1);
1817
			pstmt.setInt(1, serverLocation);
1818
			pstmt.execute();
1819
			ResultSet rs = pstmt.getResultSet();
1820
			boolean tableHasRows = rs.next();
1821
			if (tableHasRows) {
1822 2286 tao
1823 5014 daigle
				String server = rs.getString(1);
1824
				String last_checked = rs.getString(2);
1825
				if (!server.equals("localhost")) {
1826
					sl.put(server, last_checked);
1827
				}
1828 2286 tao
1829 5014 daigle
			} else {
1830
				pstmt.close();
1831
				//ut.returnConnection(conn);
1832
				return null;
1833
			}
1834
			pstmt.close();
1835
		} catch (Exception e) {
1836
			logMetacat.error("ReplicationService.getHomeServerInfoForDocId - " + ReplicationService.METACAT_REPL_ERROR_MSG);
1837
			logReplication.error("ReplicationService.getHomeServerInfoForDocId - error in replicationHandler.getHomeServer(): "
1838
					+ e.getMessage());
1839
		} finally {
1840
			try {
1841
				pstmt.close();
1842
				//ut.returnConnection(conn);
1843
			} catch (Exception ee) {
1844
				logMetacat.error("ReplicationService.getHomeServerInfoForDocId - " + ReplicationService.METACAT_REPL_ERROR_MSG);
1845
				logReplication.error("ReplicationService.getHomeServerInfoForDocId - Eror irn rplicationHandler.getHomeServer() "
1846
						+ "to close pstmt: " + ee.getMessage());
1847
			} finally {
1848
				DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1849
			}
1850 2286 tao
1851 5014 daigle
		}//finally
1852
		return sl;
1853
	}
1854 2286 tao
1855 5014 daigle
	/**
1856
	 * Returns a home server location  given a accnum
1857
	 * @param accNum , given accNum for a document
1858
	 *
1859
	 */
1860
	public static int getHomeServerCodeForDocId(String accNum) throws ServiceException {
1861
		DBConnection dbConn = null;
1862
		int serialNumber = -1;
1863
		PreparedStatement pstmt = null;
1864
		int serverCode = 1;
1865 5027 daigle
		String docId = DocumentUtil.getDocIdFromString(accNum);
1866 2286 tao
1867 5014 daigle
		try {
1868 1292 tao
1869 5014 daigle
			// Get DBConnection
1870
			dbConn = DBConnectionPool
1871
					.getDBConnection("ReplicationHandler.getServerLocation");
1872
			serialNumber = dbConn.getCheckOutSerialNumber();
1873
			pstmt = dbConn.prepareStatement("SELECT server_location FROM xml_documents "
1874
					+ "WHERE docid LIKE '" + docId + "'");
1875
			pstmt.execute();
1876
			ResultSet rs = pstmt.getResultSet();
1877
			boolean tablehasrows = rs.next();
1878
			//If a document is find, return the server location for it
1879
			if (tablehasrows) {
1880
				serverCode = rs.getInt(1);
1881
				pstmt.close();
1882
				//conn.close();
1883
				return serverCode;
1884
			}
1885
			//if couldn't find in xml_documents table, we think server code is 1
1886
			//(this is new document)
1887
			else {
1888
				pstmt.close();
1889
				//conn.close();
1890
				return serverCode;
1891
			}
1892 1292 tao
1893 5014 daigle
		} catch (SQLException sqle) {
1894
			throw new ServiceException("ReplicationService.getHomeServerCodeForDocId - "
1895
					+ "SQL error when getting home server code for docid: " + docId + " : "
1896
					+ sqle.getMessage());
1897 2286 tao
1898 5014 daigle
		} finally {
1899
			try {
1900
				pstmt.close();
1901
				//conn.close();
1902 2286 tao
1903 5014 daigle
			} catch (SQLException sqle) {
1904
				logMetacat.error("ReplicationService.getHomeServerCodeForDocId - " + ReplicationService.METACAT_REPL_ERROR_MSG);
1905
				logReplication.error("ReplicationService.getHomeServerCodeForDocId - ReplicationService.getHomeServerCodeForDocId - "
1906
						+ "SQL error when getting home server code for docid: " + docId + " : "
1907
						+ sqle.getMessage());
1908
			} finally {
1909
				DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1910
			}//finally
1911
		}//finally
1912
		//return serverCode;
1913
	}
1914 1292 tao
1915 5014 daigle
	/**
1916
	 * This method returns the content of a url
1917
	 * @param u the url to return the content from
1918
	 * @return a string representing the content of the url
1919
	 * @throws java.io.IOException
1920
	 */
1921
	public static String getURLContent(URL u) throws java.io.IOException {
1922 5441 berkley
	    logReplication.info("Getting url content from " + u.toString());
1923 5014 daigle
		char istreamChar;
1924
		int istreamInt;
1925 5440 berkley
		logReplication.info("ReplicationService.getURLContent - Before open the stream" + u.toString());
1926 5014 daigle
		InputStream input = u.openStream();
1927 5440 berkley
		logReplication.info("ReplicationService.getURLContent - After open the stream" + u.toString());
1928 5014 daigle
		InputStreamReader istream = new InputStreamReader(input);
1929
		StringBuffer serverResponse = new StringBuffer();
1930
		while ((istreamInt = istream.read()) != -1) {
1931
			istreamChar = (char) istreamInt;
1932
			serverResponse.append(istreamChar);
1933
		}
1934
		istream.close();
1935
		input.close();
1936 2286 tao
1937 5014 daigle
		return serverResponse.toString();
1938
	}
1939 2286 tao
1940 5014 daigle
//	/**
1941
//	 * Method for writing replication messages to a log file specified in
1942
//	 * metacat.properties
1943
//	 */
1944
//	public static void replLog(String message) {
1945
//		try {
1946
//			FileOutputStream fos = new FileOutputStream(PropertyService
1947
//					.getProperty("replication.logdir")
1948
//					+ "/metacatreplication.log", true);
1949
//			PrintWriter pw = new PrintWriter(fos);
1950
//			SimpleDateFormat formatter = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
1951
//			java.util.Date localtime = new java.util.Date();
1952
//			String dateString = formatter.format(localtime);
1953
//			dateString += " :: " + message;
1954
//			// time stamp each entry
1955
//			pw.println(dateString);
1956
//			pw.flush();
1957
//		} catch (Exception e) {
1958
//			logReplication.error("error writing to replication log from "
1959
//					+ "MetacatReplication.replLog: " + e.getMessage());
1960
//			// e.printStackTrace(System.out);
1961
//		}
1962
//	}
1963 2286 tao
1964 5014 daigle
//	/**
1965
//	 * Method for writing replication messages to a log file specified in
1966
//	 * metacat.properties
1967
//	 */
1968
//	public static void replErrorLog(String message) {
1969
//		try {
1970
//			FileOutputStream fos = new FileOutputStream(PropertyService
1971
//					.getProperty("replication.logdir")
1972
//					+ "/metacatreplicationerror.log", true);
1973
//			PrintWriter pw = new PrintWriter(fos);
1974
//			SimpleDateFormat formatter = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
1975
//			java.util.Date localtime = new java.util.Date();
1976
//			String dateString = formatter.format(localtime);
1977
//			dateString += " :: " + message;
1978
//			//time stamp each entry
1979
//			pw.println(dateString);
1980
//			pw.flush();
1981
//		} catch (Exception e) {
1982
//			logReplication.error("error writing to replication error log from "
1983
//					+ "MetacatReplication.replErrorLog: " + e.getMessage());
1984
//			//e.printStackTrace(System.out);
1985
//		}
1986
//	}
1987 2286 tao
1988 5014 daigle
	/**
1989
	 * Returns true if the replicate field for server in xml_replication is 1.
1990
	 * Returns false otherwise
1991 4080 daigle
	 */
1992 5014 daigle
	public static boolean replToServer(String server) {
1993
		DBConnection dbConn = null;
1994
		int serialNumber = -1;
1995
		PreparedStatement pstmt = null;
1996 4080 daigle
		try {
1997 5014 daigle
			dbConn = DBConnectionPool.getDBConnection("MetacatReplication.repltoServer");
1998
			serialNumber = dbConn.getCheckOutSerialNumber();
1999
			pstmt = dbConn.prepareStatement("select replicate from "
2000
					+ "xml_replication where server like '" + server + "'");
2001
			pstmt.execute();
2002
			ResultSet rs = pstmt.getResultSet();
2003
			boolean tablehasrows = rs.next();
2004
			if (tablehasrows) {
2005
				int i = rs.getInt(1);
2006
				if (i == 1) {
2007
					pstmt.close();
2008
					//conn.close();
2009
					return true;
2010
				} else {
2011
					pstmt.close();
2012
					//conn.close();
2013
					return false;
2014
				}
2015
			}
2016
		} catch (SQLException sqle) {
2017
			logMetacat.error("ReplicationService.replToServer - " + ReplicationService.METACAT_REPL_ERROR_MSG);
2018
			logReplication.error("ReplicationService.replToServer - SQL error in MetacatReplication.replToServer: "
2019
					+ sqle.getMessage());
2020
		} finally {
2021
			try {
2022
				pstmt.close();
2023
				//conn.close();
2024
			}//try
2025
			catch (Exception ee) {
2026
				logMetacat.error("ReplicationService.replToServer - " + ReplicationService.METACAT_REPL_ERROR_MSG);
2027
				logReplication.error("ReplicationService.replToServer - Error in MetacatReplication.replToServer: "
2028
						+ ee.getMessage());
2029
			}//catch
2030
			finally {
2031
				DBConnectionPool.returnDBConnection(dbConn, serialNumber);
2032
			}//finally
2033
		}//finally
2034
		return false;
2035
		//the default if this server does not exist is to not replicate to it.
2036 4080 daigle
	}
2037 2286 tao
2038 522 berkley
}