Revision 561
Added by berkley over 23 years ago
MetacatReplication.java | ||
---|---|---|
24 | 24 |
import oracle.xml.parser.v2.*; |
25 | 25 |
import org.xml.sax.*; |
26 | 26 |
|
27 |
public class MetacatReplication extends HttpServlet |
|
27 |
public class MetacatReplication extends HttpServlet implements Runnable
|
|
28 | 28 |
{ |
29 | 29 |
private String deltaT; |
30 | 30 |
Timer replicationDaemon; |
31 |
MetaCatUtil util = new MetaCatUtil(); |
|
31 |
private static MetaCatUtil util = new MetaCatUtil(); |
|
32 |
private Vector fileLocks = new Vector(); |
|
33 |
private Thread lockThread = null; |
|
32 | 34 |
|
33 | 35 |
/** |
34 | 36 |
* Initialize the servlet by creating appropriate database connections |
... | ... | |
126 | 128 |
//handleGetDocumentAction(). |
127 | 129 |
handleGetDocumentRequest(out, params, response); |
128 | 130 |
} |
131 |
else if(((String[])params.get("action"))[0].equals("getlock")) |
|
132 |
{ |
|
133 |
handleGetLockRequest(out, params, response); |
|
134 |
} |
|
135 |
else if(((String[])params.get("action"))[0].equals("getdocumentinfo")) |
|
136 |
{ |
|
137 |
handleGetDocumentInfoRequest(out, params, response); |
|
138 |
} |
|
129 | 139 |
} |
130 | 140 |
} |
131 | 141 |
|
142 |
/** |
|
143 |
* Grants or denies a lock to a requesting host. |
|
144 |
* The servlet parameters of interrest are: |
|
145 |
* docid: the docid of the file the lock is being requested for |
|
146 |
* currentdate: the timestamp of the document on the remote server |
|
147 |
* |
|
148 |
*/ |
|
149 |
private void handleGetLockRequest(PrintWriter out, Hashtable params, |
|
150 |
HttpServletResponse response) |
|
151 |
{ |
|
152 |
java.util.Date remoteDate = new java.util.Date(); |
|
153 |
java.util.Date localDate = new java.util.Date(); |
|
154 |
try |
|
155 |
{ |
|
156 |
Connection conn = util.openDBConnection(); |
|
157 |
String docid = ((String[])params.get("docid"))[0]; |
|
158 |
String remoteDateStr = ((String[])params.get("updatedate"))[0]; |
|
159 |
DocumentImpl requestDoc = new DocumentImpl(conn, docid); |
|
160 |
|
|
161 |
String localDateStr = requestDoc.getUpdateDate(); |
|
162 |
SimpleDateFormat formatter = new SimpleDateFormat ("yy-MM-dd HH:mm:ss"); |
|
163 |
ParsePosition pos = new ParsePosition(0); |
|
164 |
remoteDate = formatter.parse(remoteDateStr, pos); |
|
165 |
localDate = formatter.parse(localDateStr, pos); |
|
166 |
if(remoteDate.compareTo(localDate) >= 0) |
|
167 |
{ |
|
168 |
if(!fileLocks.contains(docid)) |
|
169 |
{ //grant the lock |
|
170 |
fileLocks.add(0, docid); //insert at the beginning of the queue Vector |
|
171 |
//send a message back to the the remote host authorizing the insert |
|
172 |
out.println("<lockgranted><docid>" +docid+ "</docid></lockgranted>"); |
|
173 |
lockThread = new Thread(this); |
|
174 |
lockThread.setPriority(Thread.MIN_PRIORITY); |
|
175 |
lockThread.start(); |
|
176 |
} |
|
177 |
else |
|
178 |
{ //deny the lock |
|
179 |
out.println("<filelocked><docid>" + docid + "</docid></filelocked>"); |
|
180 |
} |
|
181 |
} |
|
182 |
else |
|
183 |
{//deny the lock. |
|
184 |
out.println("<outdatedfile><docid>" + docid + "</docid></filelocked>"); |
|
185 |
} |
|
186 |
conn.close(); |
|
187 |
} |
|
188 |
catch(Exception e) |
|
189 |
{ |
|
190 |
System.out.println("error requesting file lock: " + e.getMessage()); |
|
191 |
} |
|
192 |
} |
|
193 |
|
|
194 |
/** |
|
195 |
* Sends all of the xml_documents information encoded in xml to a requestor |
|
196 |
*/ |
|
197 |
private void handleGetDocumentInfoRequest(PrintWriter out, Hashtable params, |
|
198 |
HttpServletResponse response) |
|
199 |
{ |
|
200 |
String docid = ((String[])(params.get("docid")))[0]; |
|
201 |
StringBuffer sb = new StringBuffer(); |
|
202 |
try |
|
203 |
{ |
|
204 |
Connection conn = util.openDBConnection(); |
|
205 |
DocumentImpl doc = new DocumentImpl(conn, docid); |
|
206 |
sb.append("<documentinfo><docid>").append(docid); |
|
207 |
sb.append("</docid><docname>").append(doc.getDocname()); |
|
208 |
sb.append("</docname><doctype>").append(doc.getDoctype()); |
|
209 |
sb.append("</doctype><doctitle>").append(doc.getDocTitle()); |
|
210 |
sb.append("</doctitle><user_owner>").append(doc.getUserowner()); |
|
211 |
sb.append("</user_owner><user_updated>").append(doc.getUserupdated()); |
|
212 |
sb.append("</user_updated><public_access>").append(doc.getPublicaccess()); |
|
213 |
sb.append("</public_access></documentinfo>"); |
|
214 |
response.setContentType("text/xml"); |
|
215 |
out.println(sb.toString()); |
|
216 |
conn.close(); |
|
217 |
} |
|
218 |
catch (Exception e) |
|
219 |
{ |
|
220 |
System.out.println("error in metacatReplication.handlegetdocumentinforequest: " + |
|
221 |
e.getMessage()); |
|
222 |
} |
|
223 |
|
|
224 |
} |
|
225 |
|
|
226 |
/** |
|
227 |
* Sends a document to a remote host |
|
228 |
*/ |
|
132 | 229 |
private void handleGetDocumentRequest(PrintWriter out, Hashtable params, |
133 |
HttpServletResponse response) |
|
230 |
HttpServletResponse response)
|
|
134 | 231 |
{ |
135 | 232 |
try |
136 | 233 |
{ |
... | ... | |
176 | 273 |
java.util.Date update = new java.util.Date(); |
177 | 274 |
ParsePosition pos = new ParsePosition(0); |
178 | 275 |
update = formatter.parse(updateStr, pos); |
179 |
//update = new java.util.Date(update.getTime() - 10000); |
|
180 | 276 |
String dateString = formatter.format(update); |
181 | 277 |
sql.append("select docid, date_updated, server_location from "); |
182 | 278 |
sql.append("xml_documents where date_updated > "); |
183 |
sql.append("to_date('").append(dateString).append("','YY-MM-DD HH24:MI:SS')"); |
|
279 |
sql.append("to_date('").append(dateString); |
|
280 |
sql.append("','YY-MM-DD HH24:MI:SS')"); |
|
184 | 281 |
//System.out.println("sql: " + sql.toString()); |
185 | 282 |
|
186 | 283 |
//get any recently deleted documents |
187 | 284 |
StringBuffer delsql = new StringBuffer(); |
188 |
delsql.append("select docid, date_created, server_location from ");
|
|
285 |
delsql.append("select docid, date_updated, server_location from ");
|
|
189 | 286 |
delsql.append("xml_revisions where docid not in (select docid from "); |
190 |
delsql.append("xml_documents) and date_created > to_date('");
|
|
287 |
delsql.append("xml_documents) and date_updated > to_date('");
|
|
191 | 288 |
delsql.append(dateString).append("','YY-MM-DD HH24:MI:SS')"); |
192 | 289 |
|
193 | 290 |
try |
... | ... | |
247 | 344 |
System.out.println("Exception in metacatReplication: " + e.getMessage()); |
248 | 345 |
} |
249 | 346 |
} |
347 |
|
|
348 |
/** |
|
349 |
* This method is what is run when a seperate thread is broken off to handle |
|
350 |
* inserting a document from a remote replicated server. |
|
351 |
*/ |
|
352 |
public void run() |
|
353 |
{ |
|
354 |
try |
|
355 |
{ |
|
356 |
Thread.sleep(30000); //the lock will expire in 30 seconds |
|
357 |
fileLocks.remove(fileLocks.size() - 1); |
|
358 |
//the vector is treated as a FIFO queue. If there are more than one lock |
|
359 |
//in the vector, the first one inserted will be removed. |
|
360 |
} |
|
361 |
catch(Exception e) |
|
362 |
{ |
|
363 |
System.out.println("error in file lock thread: " + e.getMessage()); |
|
364 |
} |
|
365 |
} |
|
366 |
|
|
367 |
/** |
|
368 |
* Returns the name of a server given a serverCode |
|
369 |
* @param serverCode the serverid of the server |
|
370 |
* @return the servername or null if the specified serverCode does not |
|
371 |
* exist. |
|
372 |
*/ |
|
373 |
public static String getServer(int serverCode) |
|
374 |
{ |
|
375 |
try |
|
376 |
{ |
|
377 |
Connection conn = util.openDBConnection(); |
|
378 |
PreparedStatement pstmt = conn.prepareStatement("select server from " + |
|
379 |
"xml_replication where serverid = " + |
|
380 |
serverCode); |
|
381 |
pstmt.execute(); |
|
382 |
ResultSet rs = pstmt.getResultSet(); |
|
383 |
boolean tablehasrows = rs.next(); |
|
384 |
if(tablehasrows) |
|
385 |
{ |
|
386 |
conn.close(); |
|
387 |
return rs.getString(1); |
|
388 |
} |
|
389 |
conn.close(); |
|
390 |
} |
|
391 |
catch(Exception e) |
|
392 |
{ |
|
393 |
System.out.println("Error in MetacatReplication.getServer: " + |
|
394 |
e.getMessage()); |
|
395 |
} |
|
396 |
return null; |
|
397 |
//return null if the server does not exist |
|
398 |
} |
|
250 | 399 |
} |
Also available in: Unified diff
added functionality to allow the replication servlet to assertain and insert user and group info into the local database. started implementation of insert replication handler (it is commented out in this commit)