40 |
40 |
import org.xml.sax.*;
|
41 |
41 |
|
42 |
42 |
public class MetacatReplication extends HttpServlet implements Runnable
|
43 |
|
{
|
|
43 |
{
|
44 |
44 |
private String deltaT;
|
45 |
45 |
Timer replicationDaemon;
|
46 |
46 |
private static MetaCatUtil util = new MetaCatUtil();
|
47 |
47 |
private Vector fileLocks = new Vector();
|
48 |
48 |
private Thread lockThread = null;
|
49 |
49 |
private static final AbstractDatabase dbAdapter = MetaCatUtil.dbAdapter;
|
50 |
|
|
|
50 |
|
51 |
51 |
/**
|
52 |
52 |
* Initialize the servlet by creating appropriate database connections
|
53 |
53 |
*/
|
54 |
|
public void init(ServletConfig config) throws ServletException
|
|
54 |
public void init(ServletConfig config) throws ServletException
|
55 |
55 |
{
|
56 |
56 |
//initialize db connections to handle any update requests
|
57 |
57 |
MetaCatUtil util = new MetaCatUtil();
|
... | ... | |
65 |
65 |
public java.net.URLStreamHandler createURLStreamHandler(
|
66 |
66 |
final String protocol)
|
67 |
67 |
{
|
68 |
|
if ("http".equals(protocol))
|
|
68 |
if ("http".equals(protocol))
|
69 |
69 |
{
|
70 |
|
try
|
|
70 |
try
|
71 |
71 |
{
|
72 |
72 |
URLStreamHandler urlsh = new HTTPClient.http.Handler();
|
73 |
73 |
MetaCatUtil.debugMessage("Using HttpClient Protocol Handler for http", 45);
|
74 |
74 |
return urlsh;
|
75 |
|
}
|
76 |
|
catch (Exception e)
|
|
75 |
}
|
|
76 |
catch (Exception e)
|
77 |
77 |
{
|
78 |
78 |
MetaCatUtil.debugMessage(
|
79 |
79 |
"Error setting URL StreamHandler!", 30);
|
80 |
80 |
return null;
|
81 |
81 |
}
|
82 |
82 |
}//if
|
83 |
|
else if ("https".equals(protocol))
|
|
83 |
else if ("https".equals(protocol))
|
84 |
84 |
{
|
85 |
|
try
|
|
85 |
try
|
86 |
86 |
{
|
87 |
87 |
URLStreamHandler urlsh = new HTTPClient.https.Handler();
|
88 |
88 |
MetaCatUtil.debugMessage("Using HttpClient Protocol Handler for https", 45);
|
89 |
89 |
return urlsh;
|
90 |
|
}
|
91 |
|
catch (Exception e)
|
|
90 |
}
|
|
91 |
catch (Exception e)
|
92 |
92 |
{
|
93 |
93 |
MetaCatUtil.debugMessage(
|
94 |
94 |
"Error setting URL StreamHandler!", 30);
|
... | ... | |
98 |
98 |
return null;
|
99 |
99 |
}//createURLStreamHandler
|
100 |
100 |
});*/
|
101 |
|
|
|
101 |
|
102 |
102 |
}
|
103 |
|
|
104 |
|
public void destroy()
|
|
103 |
|
|
104 |
public void destroy()
|
105 |
105 |
{
|
106 |
106 |
replicationDaemon.cancel();
|
107 |
107 |
// System.out.println("Replication daemon cancelled.");
|
108 |
108 |
}
|
109 |
|
|
|
109 |
|
110 |
110 |
public void doGet (HttpServletRequest request, HttpServletResponse response)
|
111 |
|
throws ServletException, IOException
|
|
111 |
throws ServletException, IOException
|
112 |
112 |
{
|
113 |
113 |
// Process the data and send back the response
|
114 |
114 |
handleGetOrPost(request, response);
|
115 |
115 |
}
|
116 |
116 |
|
117 |
117 |
public void doPost(HttpServletRequest request, HttpServletResponse response)
|
118 |
|
throws ServletException, IOException
|
|
118 |
throws ServletException, IOException
|
119 |
119 |
{
|
120 |
120 |
// Process the data and send back the response
|
121 |
121 |
handleGetOrPost(request, response);
|
122 |
122 |
}
|
123 |
|
|
124 |
|
private void handleGetOrPost(HttpServletRequest request,
|
125 |
|
HttpServletResponse response)
|
126 |
|
throws ServletException, IOException
|
|
123 |
|
|
124 |
private void handleGetOrPost(HttpServletRequest request,
|
|
125 |
HttpServletResponse response)
|
|
126 |
throws ServletException, IOException
|
127 |
127 |
{
|
128 |
128 |
//PrintWriter out = response.getWriter();
|
129 |
129 |
//ServletOutputStream outPut = response.getOutputStream();
|
130 |
130 |
Hashtable params = new Hashtable();
|
131 |
131 |
Enumeration paramlist = request.getParameterNames();
|
132 |
|
|
133 |
|
|
134 |
|
|
|
132 |
|
|
133 |
|
|
134 |
|
135 |
135 |
// NOT NEEDED - doesn't provide enough security because of possible IP spoofing
|
136 |
136 |
// REPLACED with running replication comminications over HTTPS
|
137 |
137 |
// String requestingServerIP = request.getRemoteAddr();
|
138 |
138 |
// InetAddress iaddr = InetAddress.getByName(requestingServerIP);
|
139 |
139 |
// String requestingServer = iaddr.getHostName();
|
140 |
|
|
|
140 |
|
141 |
141 |
while (paramlist.hasMoreElements()) {
|
142 |
142 |
String name = (String)paramlist.nextElement();
|
143 |
143 |
String[] value = request.getParameterValues(name);
|
144 |
|
params.put(name, value);
|
|
144 |
params.put(name, value);
|
145 |
145 |
}
|
146 |
|
|
|
146 |
|
147 |
147 |
String action = ((String[])params.get("action"))[0];
|
148 |
148 |
String server = null;
|
149 |
|
|
|
149 |
|
150 |
150 |
try {
|
151 |
151 |
// check if the server is included in the list of replicated servers
|
152 |
|
if ( !action.equals("servercontrol") &&
|
|
152 |
if ( !action.equals("servercontrol") &&
|
153 |
153 |
!action.equals("stop") &&
|
154 |
154 |
!action.equals("start") &&
|
155 |
155 |
!action.equals("getall") ) {
|
156 |
156 |
|
157 |
157 |
server = ((String[])params.get("server"))[0];
|
158 |
158 |
if ( getServerCodeForServerName(server) == 0 ) {
|
159 |
|
System.out.println("Action \"" + action +
|
|
159 |
System.out.println("Action \"" + action +
|
160 |
160 |
"\" rejected for server: " + server);
|
161 |
161 |
return;
|
162 |
162 |
} else {
|
163 |
|
System.out.println("Action \"" + action +
|
|
163 |
System.out.println("Action \"" + action +
|
164 |
164 |
"\" accepted for server: " + server);
|
165 |
165 |
}
|
166 |
166 |
}
|
... | ... | |
169 |
169 |
e.getMessage() );
|
170 |
170 |
return;
|
171 |
171 |
}
|
172 |
|
if ( action.equals("readdata") )
|
|
172 |
if ( action.equals("readdata") )
|
173 |
173 |
{
|
174 |
174 |
OutputStream out=response.getOutputStream();
|
175 |
175 |
//to get the data file.
|
176 |
176 |
handleGetDataFileRequest(out, params, response);
|
177 |
177 |
out.close();
|
178 |
178 |
}
|
179 |
|
else if ( action.equals("forcereplicatedatafile") )
|
|
179 |
else if ( action.equals("forcereplicatedatafile") )
|
180 |
180 |
{
|
181 |
181 |
//read a specific docid from remote host, and store it into local host
|
182 |
182 |
handleForceReplicateDataFileRequest(params);
|
183 |
|
|
|
183 |
|
184 |
184 |
}
|
185 |
185 |
else
|
186 |
186 |
{
|
... | ... | |
191 |
191 |
replicationDaemon = new Timer(true);
|
192 |
192 |
out.println("Replication Handler Stopped");
|
193 |
193 |
MetacatReplication.replLog("deltaT handler stopped");
|
194 |
|
|
195 |
194 |
|
|
195 |
|
196 |
196 |
} else if ( action.equals("start") ) {
|
197 |
|
|
|
197 |
|
198 |
198 |
//start the replication server
|
199 |
199 |
int rate;
|
200 |
200 |
if ( params.containsKey("rate") ) {
|
... | ... | |
211 |
211 |
out.println("New rate is: " + rate + " seconds.");
|
212 |
212 |
replicationDaemon.cancel();
|
213 |
213 |
replicationDaemon = new Timer(true);
|
214 |
|
replicationDaemon.scheduleAtFixedRate(new ReplicationHandler(out), 0,
|
|
214 |
replicationDaemon.scheduleAtFixedRate(new ReplicationHandler(out), 0,
|
215 |
215 |
rate * 1000);
|
216 |
216 |
out.println("Replication Handler Started");
|
217 |
|
MetacatReplication.replLog("deltaT handler started with rate=" +
|
|
217 |
MetacatReplication.replLog("deltaT handler started with rate=" +
|
218 |
218 |
rate + " seconds");
|
219 |
|
|
220 |
219 |
|
|
220 |
|
221 |
221 |
} else if ( action.equals("getall") ) {
|
222 |
222 |
//updates this server exactly once
|
223 |
223 |
replicationDaemon.schedule(new ReplicationHandler(out), 0);
|
... | ... | |
227 |
227 |
} else if ( action.equals("forcereplicate") ) {
|
228 |
228 |
//read a specific docid from remote host, and store it into local host
|
229 |
229 |
handleForceReplicateRequest(out, params, response);
|
230 |
|
|
|
230 |
|
231 |
231 |
} else if ( action.equals("update") ) {
|
232 |
232 |
//request an update list from the server
|
233 |
233 |
handleUpdateRequest(out, params, response);
|
... | ... | |
255 |
255 |
response.setContentType("text/html");
|
256 |
256 |
out.println("<html><body>Test successfully</body></html>");
|
257 |
257 |
}
|
258 |
|
|
|
258 |
|
259 |
259 |
out.close();
|
260 |
260 |
}//else
|
261 |
261 |
}
|
262 |
|
|
263 |
|
/**
|
|
262 |
|
|
263 |
/**
|
264 |
264 |
* This method can add, delete and list the servers currently included in
|
265 |
265 |
* xml_replication.
|
266 |
266 |
* action subaction other needed params
|
267 |
267 |
* ---------------------------------------------------------
|
268 |
268 |
* servercontrol add server
|
269 |
269 |
* servercontrol delete server
|
270 |
|
* servercontrol list
|
|
270 |
* servercontrol list
|
271 |
271 |
*/
|
272 |
272 |
private void handleServerControlRequest(PrintWriter out, Hashtable params,
|
273 |
273 |
HttpServletResponse response)
|
... | ... | |
285 |
285 |
dbConn=DBConnectionPool.
|
286 |
286 |
getDBConnection("MetacatReplication.handleServerControlRequest");
|
287 |
287 |
serialNumber=dbConn.getCheckOutSerialNumber();
|
288 |
|
|
|
288 |
|
289 |
289 |
// add server to server list
|
290 |
290 |
if ( subaction.equals("add") ) {
|
291 |
291 |
replicate = ((String[])params.get("replicate"))[0];
|
292 |
292 |
server = ((String[])params.get("server"))[0];
|
293 |
|
|
|
293 |
|
294 |
294 |
//Get data replication value
|
295 |
295 |
dataReplicate = ((String[])params.get("datareplicate"))[0];
|
296 |
296 |
//Get hub value
|
297 |
297 |
hub = ((String[])params.get("hub"))[0];
|
298 |
|
|
|
298 |
|
299 |
299 |
/*pstmt = dbConn.prepareStatement("INSERT INTO xml_replication " +
|
300 |
300 |
"(server, last_checked, replicate, datareplicate, hub) " +
|
301 |
301 |
"VALUES ('" + server + "', to_date(" +
|
... | ... | |
304 |
304 |
+ hub +"')");*/
|
305 |
305 |
pstmt = dbConn.prepareStatement("INSERT INTO xml_replication " +
|
306 |
306 |
"(server, last_checked, replicate, datareplicate, hub) " +
|
307 |
|
"VALUES ('" + server + "', "+
|
308 |
|
dbAdapter.toDate("01/01/1980", "MM/DD/YYYY")
|
|
307 |
"VALUES ('" + server + "', "+
|
|
308 |
dbAdapter.toDate("01/01/1980", "MM/DD/YYYY")
|
309 |
309 |
+ ", '" +
|
310 |
310 |
replicate +"', '" +dataReplicate+"', '"
|
311 |
311 |
+ hub +"')");
|
312 |
|
|
|
312 |
|
313 |
313 |
pstmt.execute();
|
314 |
314 |
pstmt.close();
|
315 |
315 |
dbConn.commit();
|
316 |
|
out.println("Server " + server + " added");
|
|
316 |
out.println("Server " + server + " added");
|
317 |
317 |
response.setContentType("text/html");
|
318 |
318 |
out.println("<html><body><table border=\"1\">");
|
319 |
319 |
out.println("<tr><td><b>server</b></td><td><b>last_checked</b></td><td>");
|
... | ... | |
323 |
323 |
pstmt = dbConn.prepareStatement("SELECT * FROM xml_replication");
|
324 |
324 |
//increase dbconnection usage
|
325 |
325 |
dbConn.increaseUsageCount(1);
|
326 |
|
|
|
326 |
|
327 |
327 |
pstmt.execute();
|
328 |
328 |
ResultSet rs = pstmt.getResultSet();
|
329 |
329 |
boolean tablehasrows = rs.next();
|
... | ... | |
333 |
333 |
out.println(rs.getString(4) + "</td><td>");
|
334 |
334 |
out.println(rs.getString(5) + "</td><td>");
|
335 |
335 |
out.println(rs.getString(6) + "</td></tr>");
|
336 |
|
|
|
336 |
|
337 |
337 |
tablehasrows = rs.next();
|
338 |
338 |
}
|
339 |
339 |
out.println("</table></body></html>");
|
340 |
|
|
|
340 |
|
341 |
341 |
// download certificate with the public key on this server
|
342 |
342 |
// and import it as a trusted certificate
|
343 |
343 |
String certURL = ((String[])params.get("certificate"))[0];
|
344 |
344 |
downloadCertificate(certURL);
|
345 |
|
|
|
345 |
|
346 |
346 |
// delete server from server list
|
347 |
347 |
} else if ( subaction.equals("delete") ) {
|
348 |
348 |
server = ((String[])params.get("server"))[0];
|
... | ... | |
358 |
358 |
out.println("<b>replicate</b></td>");
|
359 |
359 |
out.println("<td><b>datareplicate</b></td>");
|
360 |
360 |
out.println("<td><b>hub</b></td></tr>");
|
361 |
|
|
|
361 |
|
362 |
362 |
pstmt = dbConn.prepareStatement("SELECT * FROM xml_replication");
|
363 |
363 |
//increase dbconnection usage
|
364 |
364 |
dbConn.increaseUsageCount(1);
|
... | ... | |
397 |
397 |
tablehasrows = rs.next();
|
398 |
398 |
}
|
399 |
399 |
out.println("</table></body></html>");
|
400 |
|
}
|
|
400 |
}
|
401 |
401 |
else
|
402 |
402 |
{
|
403 |
|
|
|
403 |
|
404 |
404 |
out.println("<error>Unkonwn subaction</error>");
|
405 |
|
|
|
405 |
|
406 |
406 |
}
|
407 |
407 |
pstmt.close();
|
408 |
408 |
//conn.close();
|
409 |
409 |
|
410 |
410 |
} catch(Exception e) {
|
411 |
|
System.out.println("Error in " +
|
412 |
|
"MetacatReplication.handleServerControlRequest " +
|
|
411 |
System.out.println("Error in " +
|
|
412 |
"MetacatReplication.handleServerControlRequest " +
|
413 |
413 |
e.getMessage());
|
414 |
414 |
e.printStackTrace(System.out);
|
415 |
415 |
}
|
... | ... | |
421 |
421 |
}//try
|
422 |
422 |
catch (SQLException ee)
|
423 |
423 |
{
|
424 |
|
MetaCatUtil.debugMessage("Error in " +
|
|
424 |
MetaCatUtil.debugMessage("Error in " +
|
425 |
425 |
"MetacatReplication.handleServerControlRequest to close pstmt "
|
426 |
426 |
+ ee.getMessage(), 30);
|
427 |
427 |
}//catch
|
... | ... | |
430 |
430 |
DBConnectionPool.returnDBConnection(dbConn, serialNumber);
|
431 |
431 |
}//finally
|
432 |
432 |
}//finally
|
433 |
|
|
|
433 |
|
434 |
434 |
}
|
435 |
|
|
436 |
|
// download certificate with the public key from certURL and
|
437 |
|
// upload it onto this server; it then must be imported as a
|
438 |
|
// trusted certificate
|
|
435 |
|
|
436 |
// download certificate with the public key from certURL and
|
|
437 |
// upload it onto this server; it then must be imported as a
|
|
438 |
// trusted certificate
|
439 |
439 |
private void downloadCertificate (String certURL)
|
440 |
440 |
throws FileNotFoundException, IOException, MalformedURLException
|
441 |
441 |
{
|
442 |
442 |
MetaCatUtil util = new MetaCatUtil();
|
443 |
443 |
String certPath = util.getOption("certPath"); //the path to be uploaded to
|
444 |
|
|
|
444 |
|
445 |
445 |
// get filename from the URL of the certificate
|
446 |
446 |
String filename = certURL;
|
447 |
447 |
int slash = Math.max(filename.lastIndexOf('/'), filename.lastIndexOf('\\'));
|
448 |
448 |
if ( slash > -1 ) {
|
449 |
449 |
filename = filename.substring(slash + 1);
|
450 |
450 |
}
|
451 |
|
|
|
451 |
|
452 |
452 |
// open file output strem to write the input into it
|
453 |
453 |
File f = new File(certPath, filename);
|
454 |
|
synchronized (f) {
|
|
454 |
synchronized (f) {
|
455 |
455 |
try {
|
456 |
456 |
if ( f.exists() ) {
|
457 |
457 |
throw new IOException("File already exist: " + f.getCanonicalFile());
|
... | ... | |
464 |
464 |
// or checkWrite method is called for f.canWrite()
|
465 |
465 |
throw se;
|
466 |
466 |
}
|
467 |
|
|
|
467 |
|
468 |
468 |
// create a buffered byte output stream
|
469 |
469 |
// that uses a default-sized output buffer
|
470 |
470 |
FileOutputStream fos = new FileOutputStream(f);
|
... | ... | |
486 |
486 |
}
|
487 |
487 |
// the input and the output streams must be closed
|
488 |
488 |
bis.close();
|
489 |
|
out.flush();
|
490 |
|
out.close();
|
491 |
|
fos.close();
|
|
489 |
out.flush();
|
|
490 |
out.close();
|
|
491 |
fos.close();
|
492 |
492 |
} // end of synchronized(f)
|
493 |
493 |
}
|
494 |
|
|
|
494 |
|
495 |
495 |
/**
|
496 |
496 |
* when a forcereplication request comes in, local host sends a read request
|
497 |
497 |
* to the requesting server (remote server) for the specified docid.
|
... | ... | |
507 |
507 |
int serverCode = 1;
|
508 |
508 |
DBConnection dbConn = null;
|
509 |
509 |
int serialNumber = -1;
|
510 |
|
|
|
510 |
|
511 |
511 |
try {
|
512 |
512 |
//if the url contains a dbaction then the default action is overridden
|
513 |
513 |
if(params.containsKey("dbaction")) {
|
... | ... | |
524 |
524 |
+util.getLocalReplicationServerName()
|
525 |
525 |
+"&action=read&docid=" + docid);
|
526 |
526 |
String xmldoc = MetacatReplication.getURLContent(u);
|
527 |
|
|
|
527 |
|
528 |
528 |
// get the document info from server
|
529 |
|
URL docinfourl = new URL("https://" + server +
|
|
529 |
URL docinfourl = new URL("https://" + server +
|
530 |
530 |
"?server="+util.getLocalReplicationServerName()
|
531 |
531 |
+"&action=getdocumentinfo&docid=" + docid);
|
532 |
|
|
|
532 |
|
533 |
533 |
String docInfoStr = MetacatReplication.getURLContent(docinfourl);
|
534 |
|
|
|
534 |
|
535 |
535 |
//dih is the parser for the docinfo xml format
|
536 |
536 |
DocInfoHandler dih = new DocInfoHandler();
|
537 |
537 |
XMLReader docinfoParser = ReplicationHandler.initParser(dih);
|
538 |
538 |
docinfoParser.parse(new InputSource(new StringReader(docInfoStr)));
|
539 |
539 |
Hashtable docinfoHash = dih.getDocInfo();
|
540 |
|
|
|
540 |
|
541 |
541 |
// Get user owner of this docid
|
542 |
542 |
String user = (String)docinfoHash.get("user_owner");
|
543 |
543 |
// Get home server of this docid
|
... | ... | |
548 |
548 |
MetaCatUtil.debugMessage("docType: "+docType, 34);
|
549 |
549 |
String parserBase = null;
|
550 |
550 |
// this for eml2 and we need user eml2 parser
|
551 |
|
if (docType != null &&
|
|
551 |
if (docType != null &&
|
552 |
552 |
(docType.trim()).equals(DocumentImpl.EML2_0_0NAMESPACE))
|
553 |
553 |
{
|
554 |
|
MetaCatUtil.debugMessage("This is an eml2 document!", 30);
|
|
554 |
MetaCatUtil.debugMessage("This is an eml200 document!", 30);
|
555 |
555 |
parserBase = DocumentImpl.EML200;
|
556 |
556 |
}
|
|
557 |
else if (docType != null && (docType.trim()).equals(DocumentImpl.EML2_0_1NAMESPACE))
|
|
558 |
{
|
|
559 |
MetaCatUtil.debugMessage("This is an eml2.0.1 document!", 30);
|
|
560 |
parserBase = DocumentImpl.EML200;
|
|
561 |
}
|
|
562 |
else if (docType != null && (docType.trim()).equals(DocumentImpl.EML2_1_0NAMESPACE))
|
|
563 |
{
|
|
564 |
MetaCatUtil.debugMessage("This is an eml2.1.0 document!", 30);
|
|
565 |
parserBase = DocumentImpl.EML210;
|
|
566 |
}
|
557 |
567 |
MetaCatUtil.debugMessage("The parserBase is: "+parserBase, 30);
|
558 |
|
|
|
568 |
|
559 |
569 |
// Get DBConnection from pool
|
560 |
570 |
dbConn=DBConnectionPool.
|
561 |
571 |
getDBConnection("MetacatReplication.handleForceReplicateRequest");
|
562 |
572 |
serialNumber=dbConn.getCheckOutSerialNumber();
|
563 |
573 |
// write the document to local database
|
564 |
574 |
DocumentImplWrapper wrapper = new DocumentImplWrapper(parserBase, false);
|
565 |
|
wrapper.writeReplication(dbConn, new StringReader(xmldoc), null, null,
|
|
575 |
wrapper.writeReplication(dbConn, new StringReader(xmldoc), null, null,
|
566 |
576 |
dbaction, docid, user, null, homeServer, server);
|
567 |
|
|
|
577 |
|
568 |
578 |
MetacatReplication.replLog("document " + docid + " added to DB with " +
|
569 |
579 |
"action " + dbaction);
|
570 |
580 |
}//try
|
571 |
|
catch(Exception e)
|
|
581 |
catch(Exception e)
|
572 |
582 |
{
|
573 |
|
MetacatReplication.replErrorLog("document " + docid +
|
|
583 |
MetacatReplication.replErrorLog("document " + docid +
|
574 |
584 |
" failed to added to DB with " +
|
575 |
585 |
"action " + dbaction + " because "+
|
576 |
586 |
e.getMessage());
|
577 |
587 |
MetaCatUtil.debugMessage("ERROR in MetacatReplication.handleForceReplicate" +
|
578 |
588 |
"Request(): " + e.getMessage(), 30);
|
579 |
|
|
|
589 |
|
580 |
590 |
}//catch
|
581 |
591 |
finally
|
582 |
592 |
{
|
... | ... | |
584 |
594 |
DBConnectionPool.returnDBConnection(dbConn, serialNumber);
|
585 |
595 |
}//finally
|
586 |
596 |
}
|
587 |
|
|
|
597 |
|
588 |
598 |
/**
|
589 |
|
* when a forcereplication data file request comes in, local host sends a
|
590 |
|
* readdata request to the requesting server (remote server) for the specified
|
|
599 |
* when a forcereplication data file request comes in, local host sends a
|
|
600 |
* readdata request to the requesting server (remote server) for the specified
|
591 |
601 |
* docid. Then store it in local database and file system
|
592 |
602 |
*/
|
593 |
603 |
private void handleForceReplicateDataFileRequest(Hashtable params)
|
594 |
604 |
{
|
595 |
|
|
|
605 |
|
596 |
606 |
//make sure there is some parameters
|
597 |
607 |
if(params.isEmpty())
|
598 |
608 |
{
|
599 |
609 |
return;
|
600 |
610 |
}
|
601 |
|
// Get remote server
|
602 |
|
String server = ((String[])params.get("server"))[0];
|
|
611 |
// Get remote server
|
|
612 |
String server = ((String[])params.get("server"))[0];
|
603 |
613 |
// the docid should include rev number
|
604 |
|
String docid = ((String[])params.get("docid"))[0];
|
|
614 |
String docid = ((String[])params.get("docid"))[0];
|
605 |
615 |
// Make sure there is a docid and server
|
606 |
616 |
if (docid==null || server==null || server.equals(""))
|
607 |
617 |
{
|
... | ... | |
609 |
619 |
, 20);
|
610 |
620 |
return;
|
611 |
621 |
}
|
612 |
|
|
|
622 |
|
613 |
623 |
// Overide or not
|
614 |
624 |
boolean override = false;
|
615 |
625 |
// dbaction - update or insert
|
616 |
626 |
String dbaction=null;
|
617 |
|
|
618 |
|
try
|
|
627 |
|
|
628 |
try
|
619 |
629 |
{
|
620 |
630 |
//docid was switch to two parts uinque code and rev
|
621 |
631 |
String uniqueCode=MetaCatUtil.getDocIdFromString(docid);
|
622 |
632 |
int rev=MetaCatUtil.getVersionFromString(docid);
|
623 |
|
if(params.containsKey("dbaction"))
|
|
633 |
if(params.containsKey("dbaction"))
|
624 |
634 |
{
|
625 |
635 |
dbaction = ((String[])params.get("dbaction"))[0];
|
626 |
636 |
}
|
... | ... | |
628 |
638 |
{
|
629 |
639 |
dbaction = "update";
|
630 |
640 |
}
|
631 |
|
|
632 |
|
MetacatReplication.replLog("force replication request from " + server);
|
|
641 |
|
|
642 |
MetacatReplication.replLog("force replication request from " + server);
|
633 |
643 |
MetaCatUtil.debugMessage("Force replication request from: "+ server, 34);
|
634 |
644 |
MetaCatUtil.debugMessage("Force replication docid: "+docid, 34);
|
635 |
645 |
MetaCatUtil.debugMessage("Force replication action: "+dbaction, 34);
|
636 |
646 |
// get the document info from server
|
637 |
|
URL docinfourl = new URL("https://" + server +
|
|
647 |
URL docinfourl = new URL("https://" + server +
|
638 |
648 |
"?server="+util.getLocalReplicationServerName()
|
639 |
649 |
+"&action=getdocumentinfo&docid=" + uniqueCode);
|
640 |
|
|
|
650 |
|
641 |
651 |
String docInfoStr = MetacatReplication.getURLContent(docinfourl);
|
642 |
652 |
|
643 |
653 |
//dih is the parser for the docinfo xml format
|
... | ... | |
646 |
656 |
docinfoParser.parse(new InputSource(new StringReader(docInfoStr)));
|
647 |
657 |
Hashtable docinfoHash = dih.getDocInfo();
|
648 |
658 |
String user = (String)docinfoHash.get("user_owner");
|
649 |
|
|
|
659 |
|
650 |
660 |
String docName = (String)docinfoHash.get("docname");
|
651 |
|
|
|
661 |
|
652 |
662 |
String docType = (String)docinfoHash.get("doctype");
|
653 |
|
|
|
663 |
|
654 |
664 |
String docHomeServer= (String)docinfoHash.get("home_server");
|
655 |
665 |
MetaCatUtil.debugMessage("docHomeServer of datafile: "+docHomeServer, 34);
|
656 |
|
|
657 |
|
|
658 |
|
|
|
666 |
|
|
667 |
|
|
668 |
|
659 |
669 |
//if action is delete, we don't delete the data file. Just archieve
|
660 |
670 |
//the xml_documents
|
661 |
671 |
if (dbaction.equals("delete"))
|
... | ... | |
675 |
685 |
String datafilePath = util.getOption("datafilepath");
|
676 |
686 |
//register data file into xml_documents table and wite data file
|
677 |
687 |
//into file system
|
678 |
|
DocumentImpl.writeDataFileInReplication(url.openStream(), datafilePath,
|
|
688 |
DocumentImpl.writeDataFileInReplication(url.openStream(), datafilePath,
|
679 |
689 |
docName, docType, docid, user,docHomeServer,server);
|
680 |
690 |
}
|
681 |
|
|
682 |
|
|
683 |
|
|
|
691 |
|
|
692 |
|
|
693 |
|
684 |
694 |
MetacatReplication.replLog("datafile " + docid + " added to DB with " +
|
685 |
695 |
"action " + dbaction);
|
686 |
|
}
|
687 |
|
catch(Exception e)
|
|
696 |
}
|
|
697 |
catch(Exception e)
|
688 |
698 |
{
|
689 |
|
|
690 |
|
MetacatReplication.replErrorLog("Datafile " + docid +
|
|
699 |
|
|
700 |
MetacatReplication.replErrorLog("Datafile " + docid +
|
691 |
701 |
" failed to added to DB with " +
|
692 |
702 |
"action " + dbaction + " because "+
|
693 |
703 |
e.getMessage());
|
... | ... | |
701 |
711 |
* The servlet parameters of interrest are:
|
702 |
712 |
* docid: the docid of the file the lock is being requested for
|
703 |
713 |
* currentdate: the timestamp of the document on the remote server
|
704 |
|
*
|
|
714 |
*
|
705 |
715 |
*/
|
706 |
716 |
private void handleGetLockRequest(PrintWriter out, Hashtable params,
|
707 |
717 |
HttpServletResponse response)
|
... | ... | |
709 |
719 |
|
710 |
720 |
try
|
711 |
721 |
{
|
712 |
|
|
|
722 |
|
713 |
723 |
String docid = ((String[])params.get("docid"))[0];
|
714 |
724 |
String remoteRev = ((String[])params.get("updaterev"))[0];
|
715 |
725 |
DocumentImpl requestDoc = new DocumentImpl(docid);
|
716 |
726 |
MetacatReplication.replLog("lock request for " + docid);
|
717 |
727 |
int localRevInt = requestDoc.getRev();
|
718 |
728 |
int remoteRevInt = Integer.parseInt(remoteRev);
|
719 |
|
|
|
729 |
|
720 |
730 |
if(remoteRevInt >= localRevInt)
|
721 |
731 |
{
|
722 |
732 |
if(!fileLocks.contains(docid))
|
... | ... | |
732 |
742 |
else
|
733 |
743 |
{ //deny the lock
|
734 |
744 |
out.println("<filelocked><docid>" + docid + "</docid></filelocked>");
|
735 |
|
MetacatReplication.replLog("lock denied for " + docid +
|
|
745 |
MetacatReplication.replLog("lock denied for " + docid +
|
736 |
746 |
"reason: file already locked");
|
737 |
747 |
}
|
738 |
748 |
}
|
739 |
749 |
else
|
740 |
750 |
{//deny the lock.
|
741 |
751 |
out.println("<outdatedfile><docid>" + docid + "</docid></filelocked>");
|
742 |
|
MetacatReplication.replLog("lock denied for " + docid +
|
|
752 |
MetacatReplication.replLog("lock denied for " + docid +
|
743 |
753 |
"reason: client has outdated file");
|
744 |
754 |
}
|
745 |
755 |
//conn.close();
|
... | ... | |
751 |
761 |
e.printStackTrace(System.out);
|
752 |
762 |
}
|
753 |
763 |
}
|
754 |
|
|
|
764 |
|
755 |
765 |
/**
|
756 |
766 |
* Sends all of the xml_documents information encoded in xml to a requestor
|
757 |
767 |
* the format is:
|
... | ... | |
759 |
769 |
* user_updated, home_server, public_access, rev)/>
|
760 |
770 |
* all of the subelements of document info are #PCDATA
|
761 |
771 |
*/
|
762 |
|
private void handleGetDocumentInfoRequest(PrintWriter out, Hashtable params,
|
|
772 |
private void handleGetDocumentInfoRequest(PrintWriter out, Hashtable params,
|
763 |
773 |
HttpServletResponse response)
|
764 |
774 |
{
|
765 |
775 |
String docid = ((String[])(params.get("docid")))[0];
|
766 |
776 |
StringBuffer sb = new StringBuffer();
|
767 |
|
|
|
777 |
|
768 |
778 |
try
|
769 |
779 |
{
|
770 |
|
|
|
780 |
|
771 |
781 |
DocumentImpl doc = new DocumentImpl(docid);
|
772 |
782 |
sb.append("<documentinfo><docid>").append(docid);
|
773 |
783 |
sb.append("</docid><docname>").append(doc.getDocname());
|
... | ... | |
784 |
794 |
sb.append("</rev></documentinfo>");
|
785 |
795 |
response.setContentType("text/xml");
|
786 |
796 |
out.println(sb.toString());
|
787 |
|
|
|
797 |
|
788 |
798 |
}
|
789 |
799 |
catch (Exception e)
|
790 |
800 |
{
|
791 |
801 |
System.out.println("error in " +
|
792 |
|
"metacatReplication.handlegetdocumentinforequest: " +
|
|
802 |
"metacatReplication.handlegetdocumentinforequest: " +
|
793 |
803 |
e.getMessage());
|
794 |
804 |
}
|
795 |
|
|
|
805 |
|
796 |
806 |
}
|
797 |
|
|
|
807 |
|
798 |
808 |
/**
|
799 |
809 |
* Sends a datafile to a remote host
|
800 |
810 |
*/
|
801 |
|
private void handleGetDataFileRequest(OutputStream outPut,
|
|
811 |
private void handleGetDataFileRequest(OutputStream outPut,
|
802 |
812 |
Hashtable params, HttpServletResponse response)
|
803 |
|
|
|
813 |
|
804 |
814 |
{
|
805 |
|
// File path for data file
|
|
815 |
// File path for data file
|
806 |
816 |
String filepath = util.getOption("datafilepath");
|
807 |
817 |
// Request docid
|
808 |
818 |
String docId = ((String[])(params.get("docid")))[0];
|
... | ... | |
812 |
822 |
util.debugMessage("Didn't specify docid for replication", 20);
|
813 |
823 |
return;
|
814 |
824 |
}
|
815 |
|
|
|
825 |
|
816 |
826 |
//try to open a https stream to test if the request server's public key
|
817 |
827 |
//in the key store, this is security issue
|
818 |
828 |
try
|
... | ... | |
835 |
845 |
{
|
836 |
846 |
return;
|
837 |
847 |
}//catch
|
838 |
|
|
839 |
|
if(!filepath.endsWith("/"))
|
|
848 |
|
|
849 |
if(!filepath.endsWith("/"))
|
840 |
850 |
{
|
841 |
851 |
filepath += "/";
|
842 |
852 |
}
|
843 |
853 |
// Get file aboslute file name
|
844 |
|
String filename = filepath + docId;
|
845 |
|
|
|
854 |
String filename = filepath + docId;
|
|
855 |
|
846 |
856 |
//MIME type
|
847 |
857 |
String contentType = null;
|
848 |
|
if (filename.endsWith(".xml"))
|
|
858 |
if (filename.endsWith(".xml"))
|
849 |
859 |
{
|
850 |
860 |
contentType="text/xml";
|
851 |
|
}
|
852 |
|
else if (filename.endsWith(".css"))
|
|
861 |
}
|
|
862 |
else if (filename.endsWith(".css"))
|
853 |
863 |
{
|
854 |
864 |
contentType="text/css";
|
855 |
|
}
|
856 |
|
else if (filename.endsWith(".dtd"))
|
|
865 |
}
|
|
866 |
else if (filename.endsWith(".dtd"))
|
857 |
867 |
{
|
858 |
868 |
contentType="text/plain";
|
859 |
|
}
|
860 |
|
else if (filename.endsWith(".xsd"))
|
|
869 |
}
|
|
870 |
else if (filename.endsWith(".xsd"))
|
861 |
871 |
{
|
862 |
872 |
contentType="text/xml";
|
863 |
|
}
|
864 |
|
else if (filename.endsWith("/"))
|
|
873 |
}
|
|
874 |
else if (filename.endsWith("/"))
|
865 |
875 |
{
|
866 |
876 |
contentType="text/html";
|
867 |
|
}
|
868 |
|
else
|
|
877 |
}
|
|
878 |
else
|
869 |
879 |
{
|
870 |
880 |
File f = new File(filename);
|
871 |
|
if ( f.isDirectory() )
|
|
881 |
if ( f.isDirectory() )
|
872 |
882 |
{
|
873 |
883 |
contentType="text/html";
|
874 |
|
}
|
|
884 |
}
|
875 |
885 |
else
|
876 |
886 |
{
|
877 |
887 |
contentType="application/octet-stream";
|
878 |
888 |
}
|
879 |
889 |
}
|
880 |
|
|
|
890 |
|
881 |
891 |
// Set the mime type
|
882 |
892 |
response.setContentType(contentType);
|
883 |
|
|
|
893 |
|
884 |
894 |
// Get the content of the file
|
885 |
895 |
FileInputStream fin = null;
|
886 |
|
try
|
|
896 |
try
|
887 |
897 |
{
|
888 |
898 |
// FileInputStream to metacat
|
889 |
899 |
fin = new FileInputStream(filename);
|
... | ... | |
892 |
902 |
// Read data from file input stream to byte array
|
893 |
903 |
int b = fin.read(buf);
|
894 |
904 |
// Write to outStream from byte array
|
895 |
|
while (b != -1)
|
|
905 |
while (b != -1)
|
896 |
906 |
{
|
897 |
907 |
outPut.write(buf, 0, b);
|
898 |
908 |
b = fin.read(buf);
|
899 |
909 |
}
|
900 |
910 |
// close file input stream
|
901 |
911 |
fin.close();
|
902 |
|
|
|
912 |
|
903 |
913 |
}//try
|
904 |
914 |
catch(Exception e)
|
905 |
915 |
{
|
... | ... | |
907 |
917 |
"handlGetDataFileRequest " + e.getMessage());
|
908 |
918 |
e.printStackTrace(System.out);
|
909 |
919 |
}//catch
|
910 |
|
|
|
920 |
|
911 |
921 |
}
|
912 |
|
|
913 |
|
|
|
922 |
|
|
923 |
|
914 |
924 |
/**
|
915 |
925 |
* Sends a document to a remote host
|
916 |
926 |
*/
|
917 |
|
private void handleGetDocumentRequest(PrintWriter out, Hashtable params,
|
|
927 |
private void handleGetDocumentRequest(PrintWriter out, Hashtable params,
|
918 |
928 |
HttpServletResponse response)
|
919 |
929 |
{
|
920 |
|
|
|
930 |
|
921 |
931 |
try
|
922 |
932 |
{
|
923 |
933 |
//try to open a https stream to test if the request server's public key
|
... | ... | |
935 |
945 |
out.close();
|
936 |
946 |
return;
|
937 |
947 |
}
|
938 |
|
|
|
948 |
|
939 |
949 |
String docid = ((String[])(params.get("docid")))[0];
|
940 |
|
|
|
950 |
|
941 |
951 |
DocumentImpl di = new DocumentImpl(docid);
|
942 |
952 |
response.setContentType("text/xml");
|
943 |
953 |
out.print(di.toString(null, null, true));
|
944 |
|
|
|
954 |
|
945 |
955 |
MetacatReplication.replLog("document " + docid + " sent");
|
946 |
|
|
|
956 |
|
947 |
957 |
}
|
948 |
958 |
catch(Exception e)
|
949 |
959 |
{
|
... | ... | |
953 |
963 |
response.setContentType("text/xml");
|
954 |
964 |
out.println("<error>"+e.getMessage()+"</error>");
|
955 |
965 |
}
|
956 |
|
|
|
966 |
|
957 |
967 |
}
|
958 |
|
|
|
968 |
|
959 |
969 |
/**
|
960 |
970 |
* Sends a list of all of the documents on this sever along with their
|
961 |
|
* revision numbers.
|
|
971 |
* revision numbers.
|
962 |
972 |
* The format is:
|
963 |
973 |
* <!ELEMENT replication (server, updates)>
|
964 |
974 |
* <!ELEMENT server (#PCDATA)>
|
... | ... | |
971 |
981 |
* note that the rev in deletedDocument is always empty. I just left
|
972 |
982 |
* it in there to make the parser implementation easier.
|
973 |
983 |
*/
|
974 |
|
private void handleUpdateRequest(PrintWriter out, Hashtable params,
|
|
984 |
private void handleUpdateRequest(PrintWriter out, Hashtable params,
|
975 |
985 |
HttpServletResponse response)
|
976 |
986 |
{
|
977 |
|
// Checked out DBConnection
|
|
987 |
// Checked out DBConnection
|
978 |
988 |
DBConnection dbConn = null;
|
979 |
989 |
// DBConenction serial number when checked it out
|
980 |
990 |
int serialNumber = -1;
|
981 |
991 |
PreparedStatement pstmt = null;
|
982 |
992 |
// Server list to store server info of xml_replication table
|
983 |
993 |
ReplicationServerList serverList = null;
|
984 |
|
|
|
994 |
|
985 |
995 |
try
|
986 |
996 |
{
|
987 |
997 |
// Check out a DBConnection from pool
|
... | ... | |
990 |
1000 |
serialNumber=dbConn.getCheckOutSerialNumber();
|
991 |
1001 |
// Create a server list from xml_replication table
|
992 |
1002 |
serverList = new ReplicationServerList();
|
993 |
|
|
|
1003 |
|
994 |
1004 |
// Get remote server name from param
|
995 |
1005 |
String server = ((String[])params.get("server"))[0];
|
996 |
1006 |
// If no servr name in param, return a error
|
... | ... | |
1001 |
1011 |
out.close();
|
1002 |
1012 |
return;
|
1003 |
1013 |
}//if
|
1004 |
|
|
|
1014 |
|
1005 |
1015 |
//try to open a https stream to test if the request server's public key
|
1006 |
1016 |
//in the key store, this is security issue
|
1007 |
1017 |
URL u = new URL("https://" + server + "?server="
|
... | ... | |
1016 |
1026 |
out.close();
|
1017 |
1027 |
return;
|
1018 |
1028 |
}
|
1019 |
|
|
1020 |
|
|
|
1029 |
|
|
1030 |
|
1021 |
1031 |
// Check if local host configure to replicate xml documents to remote
|
1022 |
1032 |
// server. If not send back a error message
|
1023 |
1033 |
if (!serverList.getReplicationValue(server))
|
... | ... | |
1028 |
1038 |
out.close();
|
1029 |
1039 |
return;
|
1030 |
1040 |
}//if
|
1031 |
|
|
|
1041 |
|
1032 |
1042 |
// Store the sql command
|
1033 |
1043 |
StringBuffer docsql = new StringBuffer();
|
1034 |
1044 |
// Stroe the docid list
|
... | ... | |
1037 |
1047 |
StringBuffer delsql = new StringBuffer();
|
1038 |
1048 |
// Store the data set file
|
1039 |
1049 |
Vector packageFiles = new Vector();
|
1040 |
|
|
|
1050 |
|
1041 |
1051 |
// Append local server's name and replication servlet to doclist
|
1042 |
1052 |
doclist.append("<?xml version=\"1.0\"?><replication>");
|
1043 |
1053 |
doclist.append("<server>").append(util.getOption("server"));
|
1044 |
1054 |
doclist.append(util.getOption("replicationpath"));
|
1045 |
1055 |
doclist.append("</server><updates>");
|
1046 |
|
|
|
1056 |
|
1047 |
1057 |
// Get correct docid that reside on this server according the requesting
|
1048 |
1058 |
// server's replicate and data replicate value in xml_replication table
|
1049 |
1059 |
docsql.append("select docid, rev, doctype from xml_documents ");
|
... | ... | |
1054 |
1064 |
docsql.append("where server_location = 1");
|
1055 |
1065 |
}
|
1056 |
1066 |
MetaCatUtil.debugMessage("Doc sql: "+docsql.toString(), 30);
|
1057 |
|
|
|
1067 |
|
1058 |
1068 |
// Get any deleted documents
|
1059 |
1069 |
delsql.append("select distinct docid from ");
|
1060 |
1070 |
delsql.append("xml_revisions where docid not in (select docid from ");
|
... | ... | |
1066 |
1076 |
delsql.append("and server_location = 1");
|
1067 |
1077 |
}
|
1068 |
1078 |
MetaCatUtil.debugMessage("Deleted sql: "+delsql.toString(), 30);
|
1069 |
|
|
1070 |
|
|
1071 |
|
|
|
1079 |
|
|
1080 |
|
|
1081 |
|
1072 |
1082 |
// Get docid list of local host
|
1073 |
1083 |
pstmt = dbConn.prepareStatement(docsql.toString());
|
1074 |
1084 |
pstmt.execute();
|
... | ... | |
1087 |
1097 |
//because if a package file is read before all of the files it
|
1088 |
1098 |
//refers to are loaded then there is an error
|
1089 |
1099 |
if(recordDoctype != null && !packagedoctypes.contains(recordDoctype))
|
1090 |
|
{
|
|
1100 |
{
|
1091 |
1101 |
//If this is not data file
|
1092 |
1102 |
if (!recordDoctype.equals("BIN"))
|
1093 |
1103 |
{
|
... | ... | |
1110 |
1120 |
doclist.append(MetaCatUtil.getOption("datafileflag"));
|
1111 |
1121 |
doclist.append("</datafile>");
|
1112 |
1122 |
doclist.append("</updatedDocument>");
|
1113 |
|
}//else
|
|
1123 |
}//else
|
1114 |
1124 |
}//if packagedoctpes
|
1115 |
1125 |
else
|
1116 |
1126 |
{ //the package files are saved to be put into the xml later.
|
... | ... | |
1127 |
1137 |
while(tablehasrows)
|
1128 |
1138 |
{
|
1129 |
1139 |
String recordDoctype = rs.getString(3);
|
1130 |
|
if(!recordDoctype.equals("BIN"))
|
|
1140 |
if(!recordDoctype.equals("BIN"))
|
1131 |
1141 |
{ //don't replicate data files
|
1132 |
1142 |
Vector packagedoctypes = MetaCatUtil.getOptionList(
|
1133 |
1143 |
MetaCatUtil.getOption("packagedoctype"));
|
... | ... | |
1152 |
1162 |
tablehasrows = rs.next();
|
1153 |
1163 |
}//while
|
1154 |
1164 |
}//else
|
1155 |
|
|
|
1165 |
|
1156 |
1166 |
pstmt = dbConn.prepareStatement(delsql.toString());
|
1157 |
1167 |
//usage count should increas 1
|
1158 |
1168 |
dbConn.increaseUsageCount(1);
|
1159 |
|
|
|
1169 |
|
1160 |
1170 |
pstmt.execute();
|
1161 |
1171 |
rs = pstmt.getResultSet();
|
1162 |
1172 |
tablehasrows = rs.next();
|
... | ... | |
1167 |
1177 |
//note that rev is always empty for deleted docs
|
1168 |
1178 |
tablehasrows = rs.next();
|
1169 |
1179 |
}
|
1170 |
|
|
|
1180 |
|
1171 |
1181 |
//now we can put the package files into the xml results
|
1172 |
1182 |
for(int i=0; i<packageFiles.size(); i++)
|
1173 |
1183 |
{
|
... | ... | |
1179 |
1189 |
doclist.append("</rev>");
|
1180 |
1190 |
doclist.append("</updatedDocument>");
|
1181 |
1191 |
}
|
1182 |
|
|
|
1192 |
|
1183 |
1193 |
doclist.append("</updates></replication>");
|
1184 |
1194 |
MetaCatUtil.debugMessage("doclist: " + doclist.toString(), 40);
|
1185 |
1195 |
pstmt.close();
|
1186 |
1196 |
//conn.close();
|
1187 |
1197 |
response.setContentType("text/xml");
|
1188 |
1198 |
out.println(doclist.toString());
|
1189 |
|
|
|
1199 |
|
1190 |
1200 |
}
|
1191 |
1201 |
catch(Exception e)
|
1192 |
1202 |
{
|
... | ... | |
1212 |
1222 |
DBConnectionPool.returnDBConnection(dbConn, serialNumber);
|
1213 |
1223 |
}//finally
|
1214 |
1224 |
}//finally
|
1215 |
|
|
|
1225 |
|
1216 |
1226 |
}//handlUpdateRequest
|
1217 |
|
|
|
1227 |
|
1218 |
1228 |
/**
|
1219 |
1229 |
* Returns the xml_catalog table encoded in xml
|
1220 |
1230 |
*/
|
... | ... | |
1222 |
1232 |
{
|
1223 |
1233 |
return handleGetCatalogRequest(null, null, null, false);
|
1224 |
1234 |
}
|
1225 |
|
|
|
1235 |
|
1226 |
1236 |
/**
|
1227 |
1237 |
* Sends the contents of the xml_catalog table encoded in xml
|
1228 |
1238 |
* The xml format is:
|
... | ... | |
1230 |
1240 |
* <!ELEMENT row (entry_type, source_doctype, target_doctype, public_id,
|
1231 |
1241 |
* system_id)>
|
1232 |
1242 |
* All of the sub elements of row are #PCDATA
|
1233 |
|
|
|
1243 |
|
1234 |
1244 |
* If printFlag == false then do not print to out.
|
1235 |
1245 |
*/
|
1236 |
|
private static String handleGetCatalogRequest(PrintWriter out,
|
|
1246 |
private static String handleGetCatalogRequest(PrintWriter out,
|
1237 |
1247 |
Hashtable params,
|
1238 |
1248 |
HttpServletResponse response,
|
1239 |
1249 |
boolean printFlag)
|
... | ... | |
1242 |
1252 |
int serialNumber = -1;
|
1243 |
1253 |
PreparedStatement pstmt = null;
|
1244 |
1254 |
try
|
1245 |
|
{
|
|
1255 |
{
|
1246 |
1256 |
/*conn = MetacatReplication.getDBConnection("MetacatReplication." +
|
1247 |
1257 |
"handleGetCatalogRequest");*/
|
1248 |
1258 |
dbConn=DBConnectionPool.
|
... | ... | |
1264 |
1274 |
sb.append("</target_doctype><public_id>").append(rs.getString(4));
|
1265 |
1275 |
sb.append("</public_id><system_id>").append(rs.getString(5));
|
1266 |
1276 |
sb.append("</system_id></row>");
|
1267 |
|
|
|
1277 |
|
1268 |
1278 |
tablehasrows = rs.next();
|
1269 |
1279 |
}
|
1270 |
1280 |
sb.append("</xml_catalog>");
|
... | ... | |
1279 |
1289 |
}
|
1280 |
1290 |
catch(Exception e)
|
1281 |
1291 |
{
|
1282 |
|
|
1283 |
|
System.out.println("error in MetacatReplication.handleGetCatalogRequest:"+
|
|
1292 |
|
|
1293 |
System.out.println("error in MetacatReplication.handleGetCatalogRequest:"+
|
1284 |
1294 |
e.getMessage());
|
1285 |
1295 |
e.printStackTrace(System.out);
|
1286 |
1296 |
if(printFlag)
|
... | ... | |
1305 |
1315 |
DBConnectionPool.returnDBConnection(dbConn, serialNumber);
|
1306 |
1316 |
}//finally
|
1307 |
1317 |
}//finally
|
1308 |
|
|
|
1318 |
|
1309 |
1319 |
return null;
|
1310 |
1320 |
}
|
1311 |
|
|
|
1321 |
|
1312 |
1322 |
/**
|
1313 |
1323 |
* Sends the current system date to the remote server. Using this action
|
1314 |
|
* for replication gets rid of any problems with syncronizing clocks
|
|
1324 |
* for replication gets rid of any problems with syncronizing clocks
|
1315 |
1325 |
* because a time specific to a document is always kept on its home server.
|
1316 |
1326 |
*/
|
1317 |
|
private void handleGetTimeRequest(PrintWriter out, Hashtable params,
|
|
1327 |
private void handleGetTimeRequest(PrintWriter out, Hashtable params,
|
1318 |
1328 |
HttpServletResponse response)
|
1319 |
1329 |
{
|
1320 |
1330 |
SimpleDateFormat formatter = new SimpleDateFormat ("MM/dd/yy HH:mm:ss");
|
1321 |
1331 |
java.util.Date localtime = new java.util.Date();
|
1322 |
1332 |
String dateString = formatter.format(localtime);
|
1323 |
1333 |
response.setContentType("text/xml");
|
1324 |
|
|
|
1334 |
|
1325 |
1335 |
out.println("<timestamp>" + dateString + "</timestamp>");
|
1326 |
1336 |
}
|
1327 |
|
|
|
1337 |
|
1328 |
1338 |
/**
|
1329 |
|
* this method handles the timeout for a file lock. when a lock is
|
|
1339 |
* this method handles the timeout for a file lock. when a lock is
|
1330 |
1340 |
* granted it is granted for 30 seconds. When this thread runs out
|
1331 |
1341 |
* it deletes the docid from the queue, thus eliminating the lock.
|
1332 |
1342 |
*/
|
... | ... | |
1334 |
1344 |
{
|
1335 |
1345 |
try
|
1336 |
1346 |
{
|
1337 |
|
MetaCatUtil.debugMessage("thread started for docid: " +
|
|
1347 |
MetaCatUtil.debugMessage("thread started for docid: " +
|
1338 |
1348 |
(String)fileLocks.elementAt(0), 45);
|
1339 |
|
|
|
1349 |
|
1340 |
1350 |
Thread.sleep(30000); //the lock will expire in 30 seconds
|
1341 |
|
MetaCatUtil.debugMessage("thread for docid: " +
|
1342 |
|
(String)fileLocks.elementAt(fileLocks.size() - 1) +
|
|
1351 |
MetaCatUtil.debugMessage("thread for docid: " +
|
|
1352 |
(String)fileLocks.elementAt(fileLocks.size() - 1) +
|
1343 |
1353 |
" exiting.", 45);
|
1344 |
|
|
|
1354 |
|
1345 |
1355 |
fileLocks.remove(fileLocks.size() - 1);
|
1346 |
1356 |
//fileLocks is treated as a FIFO queue. If there are more than one lock
|
1347 |
1357 |
//in the vector, the first one inserted will be removed.
|
1348 |
1358 |
}
|
1349 |
1359 |
catch(Exception e)
|
1350 |
1360 |
{
|
1351 |
|
MetaCatUtil.debugMessage("error in file lock thread from " +
|
|
1361 |
MetaCatUtil.debugMessage("error in file lock thread from " +
|
1352 |
1362 |
"MetacatReplication.run: " + e.getMessage(), 30);
|
1353 |
1363 |
}
|
1354 |
1364 |
}
|
1355 |
|
|
|
1365 |
|
1356 |
1366 |
/**
|
1357 |
1367 |
* Returns the name of a server given a serverCode
|
1358 |
1368 |
* @param serverCode the serverid of the server
|
... | ... | |
1371 |
1381 |
getDBConnection("MetacatReplication.getServer");
|
1372 |
1382 |
serialNumber=dbConn.getCheckOutSerialNumber();
|
1373 |
1383 |
String sql = new String("select server from " +
|
1374 |
|
"xml_replication where serverid = " +
|
|
1384 |
"xml_replication where serverid = " +
|
1375 |
1385 |
serverCode);
|
1376 |
1386 |
pstmt = dbConn.prepareStatement(sql);
|
1377 |
1387 |
//System.out.println("getserver sql: " + sql);
|
... | ... | |
1383 |
1393 |
//System.out.println("server: " + rs.getString(1));
|
1384 |
1394 |
return rs.getString(1);
|
1385 |
1395 |
}
|
1386 |
|
|
|
1396 |
|
1387 |
1397 |
//conn.close();
|
1388 |
1398 |
}
|
1389 |
1399 |
catch(Exception e)
|
1390 |
1400 |
{
|
1391 |
|
System.out.println("Error in MetacatReplication.getServer: " +
|
|
1401 |
System.out.println("Error in MetacatReplication.getServer: " +
|
1392 |
1402 |
e.getMessage());
|
1393 |
1403 |
}
|
1394 |
1404 |
finally
|
... | ... | |
1407 |
1417 |
DBConnectionPool.returnDBConnection(dbConn, serialNumber);
|
1408 |
1418 |
}//fianlly
|
1409 |
1419 |
}//finally
|
1410 |
|
|
1411 |
|
|
1412 |
|
|
|
1420 |
|
|
1421 |
|
|
1422 |
|
1413 |
1423 |
return null;
|
1414 |
1424 |
//return null if the server does not exist
|
1415 |
1425 |
}
|
1416 |
|
|
|
1426 |
|
1417 |
1427 |
/**
|
1418 |
1428 |
* Returns a server code given a server name
|
1419 |
1429 |
* @param server the name of the server
|
... | ... | |
1438 |
1448 |
pstmt.execute();
|
1439 |
1449 |
ResultSet rs = pstmt.getResultSet();
|
1440 |
1450 |
boolean tablehasrows = rs.next();
|
1441 |
|
if ( tablehasrows ) {
|
|
1451 |
if ( tablehasrows ) {
|
1442 |
1452 |
serverCode = rs.getInt(1);
|
1443 |
1453 |
pstmt.close();
|
1444 |
1454 |
//conn.close();
|
1445 |
1455 |
return serverCode;
|
1446 |
1456 |
}
|
1447 |
|
|
|
1457 |
|
1448 |
1458 |
} catch(Exception e) {
|
1449 |
1459 |
throw e;
|
1450 |
1460 |
|
1451 |
1461 |
} finally {
|
1452 |
|
try
|
|
1462 |
try
|
1453 |
1463 |
{
|
1454 |
1464 |
pstmt.close();
|
1455 |
1465 |
//conn.close();
|
1456 |
1466 |
}//try
|
1457 |
|
catch(Exception ee)
|
|
1467 |
catch(Exception ee)
|
1458 |
1468 |
{
|
1459 |
1469 |
MetaCatUtil.debugMessage("Error in MetacatReplicatio.getServerCode: "
|
1460 |
1470 |
+ee.getMessage(), 30);
|
1461 |
|
|
|
1471 |
|
1462 |
1472 |
}//catch
|
1463 |
1473 |
finally
|
1464 |
1474 |
{
|
1465 |
1475 |
DBConnectionPool.returnDBConnection(dbConn, serialNumber);
|
1466 |
1476 |
}//finally
|
1467 |
1477 |
}//finally
|
1468 |
|
|
|
1478 |
|
1469 |
1479 |
return serverCode;
|
1470 |
1480 |
}
|
1471 |
|
|
|
1481 |
|
1472 |
1482 |
/**
|
1473 |
1483 |
* Method to get a host server information for given docid
|
1474 |
1484 |
* @param conn a connection to the database
|
... | ... | |
1516 |
1526 |
boolean tableHasRows = rs.next();
|
1517 |
1527 |
if (tableHasRows)
|
1518 |
1528 |
{
|
1519 |
|
|
|
1529 |
|
1520 |
1530 |
String server = rs.getString(1);
|
1521 |
1531 |
String last_checked = rs.getString(2);
|
1522 |
1532 |
if(!server.equals("localhost"))
|
1523 |
1533 |
{
|
1524 |
1534 |
sl.put(server, last_checked);
|
1525 |
1535 |
}
|
1526 |
|
|
|
1536 |
|
1527 |
1537 |
}
|
1528 |
1538 |
else
|
1529 |
1539 |
{
|
... | ... | |
1554 |
1564 |
{
|
1555 |
1565 |
DBConnectionPool.returnDBConnection(dbConn, serialNumber);
|
1556 |
1566 |
}
|
1557 |
|
|
|
1567 |
|
1558 |
1568 |
}//finally
|
1559 |
1569 |
return sl;
|
1560 |
1570 |
}
|
1561 |
|
|
|
1571 |
|
1562 |
1572 |
/**
|
1563 |
1573 |
* Returns a home server location given a accnum
|
1564 |
1574 |
* @param accNum , given accNum for a document
|
1565 |
|
*
|
|
1575 |
*
|
1566 |
1576 |
*/
|
1567 |
1577 |
public static int getHomeServerCodeForDocId(String accNum) throws Exception
|
1568 |
1578 |
{
|
... | ... | |
1573 |
1583 |
//MetaCatUtil ut = new MetaCatUtil();
|
1574 |
1584 |
String docId=MetaCatUtil.getDocIdFromString(accNum);
|
1575 |
1585 |
|
1576 |
|
try
|
|
1586 |
try
|
1577 |
1587 |
{
|
1578 |
1588 |
|
1579 |
1589 |
// Get DBConnection
|
1580 |
1590 |
dbConn=DBConnectionPool.
|
1581 |
1591 |
getDBConnection("ReplicationHandler.getServerLocation");
|
1582 |
1592 |
serialNumber=dbConn.getCheckOutSerialNumber();
|
1583 |
|
pstmt=dbConn.prepareStatement("SELECT server_location FROM xml_documents "
|
|
1593 |
pstmt=dbConn.prepareStatement("SELECT server_location FROM xml_documents "
|
1584 |
1594 |
+ "WHERE docid LIKE '" + docId + "'");
|
1585 |
1595 |
pstmt.execute();
|
1586 |
1596 |
ResultSet rs = pstmt.getResultSet();
|
1587 |
1597 |
boolean tablehasrows = rs.next();
|
1588 |
1598 |
//If a document is find, return the server location for it
|
1589 |
|
if ( tablehasrows )
|
1590 |
|
{
|
|
1599 |
if ( tablehasrows )
|
|
1600 |
{
|
1591 |
1601 |
serverCode = rs.getInt(1);
|
1592 |
1602 |
pstmt.close();
|
1593 |
1603 |
//conn.close();
|
... | ... | |
1601 |
1611 |
//conn.close();
|
1602 |
1612 |
return serverCode;
|
1603 |
1613 |
}
|
1604 |
|
|
1605 |
|
}
|
1606 |
|
catch(Exception e)
|
|
1614 |
|
|
1615 |
}
|
|
1616 |
catch(Exception e)
|
1607 |
1617 |
{
|
1608 |
|
|
|
1618 |
|
1609 |
1619 |
throw e;
|
1610 |
1620 |
|
1611 |
|
}
|
1612 |
|
finally
|
|
1621 |
}
|
|
1622 |
finally
|
1613 |
1623 |
{
|
1614 |
|
try
|
|
1624 |
try
|
1615 |
1625 |
{
|
1616 |
1626 |
pstmt.close();
|
1617 |
1627 |
//conn.close();
|
1618 |
|
|
1619 |
|
}
|
1620 |
|
catch(Exception ee)
|
|
1628 |
|
|
1629 |
}
|
|
1630 |
catch(Exception ee)
|
1621 |
1631 |
{
|
1622 |
1632 |
MetaCatUtil.debugMessage("Erorr in Replication.getServerLocation "+
|
1623 |
1633 |
"to close pstmt"+ee.getMessage(), 30);
|
... | ... | |
1629 |
1639 |
}//finally
|
1630 |
1640 |
//return serverCode;
|
1631 |
1641 |
}
|
1632 |
|
|
1633 |
|
|
1634 |
|
|
|
1642 |
|
|
1643 |
|
|
1644 |
|
1635 |
1645 |
/**
|
1636 |
1646 |
* This method returns the content of a url
|
1637 |
1647 |
* @param u the url to return the content from
|
... | ... | |
1654 |
1664 |
}
|
1655 |
1665 |
istream.close();
|
1656 |
1666 |
input.close();
|
1657 |
|
|
|
1667 |
|
1658 |
1668 |
return serverResponse.toString();
|
1659 |
1669 |
}
|
1660 |
|
|
|
1670 |
|
1661 |
1671 |
/**
|
1662 |
|
* Method for writing replication messages to a log file specified in
|
|
1672 |
* Method for writing replication messages to a log file specified in
|
1663 |
1673 |
* metacat.properties
|
1664 |
1674 |
*/
|
1665 |
1675 |
public static void replLog(String message)
|
... | ... | |
1684 |
1694 |
//e.printStackTrace(System.out);
|
1685 |
1695 |
}
|
1686 |
1696 |
}
|
1687 |
|
|
|
1697 |
|
1688 |
1698 |
/**
|
1689 |
|
* Method for writing replication messages to a log file specified in
|
|
1699 |
* Method for writing replication messages to a log file specified in
|
1690 |
1700 |
* metacat.properties
|
1691 |
1701 |
*/
|
1692 |
1702 |
public static void replErrorLog(String message)
|
... | ... | |
1711 |
1721 |
//e.printStackTrace(System.out);
|
1712 |
1722 |
}
|
1713 |
1723 |
}
|
1714 |
|
|
|
1724 |
|
1715 |
1725 |
/**
|
1716 |
1726 |
* Returns true if the replicate field for server in xml_replication is 1.
|
1717 |
1727 |
* Returns false otherwise
|
... | ... | |
1726 |
1736 |
dbConn=DBConnectionPool.
|
1727 |
1737 |
getDBConnection("MetacatReplication.repltoServer");
|
1728 |
1738 |
serialNumber=dbConn.getCheckOutSerialNumber();
|
1729 |
|
pstmt = dbConn.prepareStatement("select replicate from " +
|
|
1739 |
pstmt = dbConn.prepareStatement("select replicate from " +
|
1730 |
1740 |
"xml_replication where server like '" +
|
1731 |
1741 |
server + "'");
|
1732 |
1742 |
pstmt.execute();
|
... | ... | |
1751 |
1761 |
}
|
1752 |
1762 |
catch(Exception e)
|
1753 |
1763 |
{
|
1754 |
|
System.out.println("error in MetacatReplication.replToServer: " +
|
|
1764 |
System.out.println("error in MetacatReplication.replToServer: " +
|
1755 |
1765 |
e.getMessage());
|
1756 |
1766 |
}
|
1757 |
1767 |
finally
|
... | ... | |
1774 |
1784 |
return false;
|
1775 |
1785 |
//the default if this server does not exist is to not replicate to it.
|
1776 |
1786 |
}
|
1777 |
|
|
1778 |
|
|
|
1787 |
|
|
1788 |
|
1779 |
1789 |
}
|
Add a code to load eml201 parser to fix the bug that couldn't generate access rule for eml201 doc.