Revision 1037
Added by Jing Tao over 22 years ago
src/edu/ucsb/nceas/metacat/ReplicationHandler.java | ||
---|---|---|
145 | 145 |
Connection conn = null; |
146 | 146 |
try |
147 | 147 |
{ |
148 |
try |
|
149 |
{ //this connection is prone to error for some reason so we |
|
150 |
//try to connect it three times before quiting. |
|
151 |
conn = util.openDBConnection(); |
|
152 |
} |
|
153 |
catch(SQLException sqle) |
|
154 |
{ |
|
155 |
try |
|
156 |
{ |
|
157 |
conn = util.openDBConnection(); |
|
158 |
} |
|
159 |
catch(SQLException sqlee) |
|
160 |
{ |
|
161 |
try |
|
162 |
{ |
|
163 |
conn = util.openDBConnection(); |
|
164 |
} |
|
165 |
catch(SQLException sqleee) |
|
166 |
{ |
|
167 |
/*System.out.println("error getting db connection in " + |
|
168 |
"ReplicationHandler.update: " + |
|
169 |
sqleee.getMessage());*/ |
|
170 |
} |
|
171 |
} |
|
172 |
} |
|
148 |
conn = util.getConnection(); |
|
173 | 149 |
} |
174 |
catch(Exception e) |
|
150 |
catch (Exception e)
|
|
175 | 151 |
{ |
176 |
/*System.out.println("error in ReplicationHandler.update: " + |
|
177 |
e.getMessage());*/ |
|
152 |
util.debugMessage("There is problem to get conncetion: "+e.getMessage()); |
|
178 | 153 |
} |
179 | 154 |
|
180 | 155 |
Enumeration keys; |
... | ... | |
186 | 161 |
ResultSet rs; |
187 | 162 |
boolean tablehasrows; |
188 | 163 |
boolean flag=false; |
164 |
boolean dataFile=false; |
|
189 | 165 |
String action = new String(); |
190 | 166 |
XMLReader parser; |
191 | 167 |
URL u; |
... | ... | |
223 | 199 |
parser.parse(new InputSource( |
224 | 200 |
new StringReader( |
225 | 201 |
(String)(responses.elementAt(i))))); |
202 |
//v is the list of updated documents |
|
226 | 203 |
Vector v = new Vector(message.getUpdatesVect()); |
227 | 204 |
//System.out.println("v: " + v.toString()); |
205 |
//d is the list of deleted documents |
|
228 | 206 |
Vector d = new Vector(message.getDeletesVect()); |
207 |
//System.out.println("d: " + d.toString()); |
|
229 | 208 |
//check the revs in u to see if there are any newer ones than |
230 | 209 |
//in the local DB. |
231 | 210 |
for(int j=0; j<v.size(); j++) |
232 | 211 |
{ |
212 |
//initial dataFile is false |
|
213 |
dataFile=false; |
|
214 |
//w is information for one document, information contain |
|
215 |
//docid, rev, server or datafile. |
|
233 | 216 |
Vector w = new Vector((Vector)(v.elementAt(j))); |
217 |
//Check if the vector w contain "datafile" |
|
218 |
//If it has, this document is data file |
|
219 |
if (w.contains((String)util.getOption("datafileflag"))) |
|
220 |
{ |
|
221 |
dataFile=true; |
|
222 |
} |
|
234 | 223 |
//System.out.println("w: " + w.toString()); |
235 | 224 |
String docid = (String)w.elementAt(0); |
236 | 225 |
//System.out.println("docid: " + docid); |
... | ... | |
264 | 253 |
flag = true; |
265 | 254 |
} |
266 | 255 |
|
267 |
if(flag) |
|
256 |
// this is non-data file |
|
257 |
if(flag && !dataFile) |
|
268 | 258 |
{ //if the document needs to be updated or inserted, this is executed |
269 | 259 |
u = new URL("https://" + docServer + "?server="+ |
270 | 260 |
util.getLocalReplicationServerName()+"&action=read&docid="+docid); |
... | ... | |
309 | 299 |
"ReplicationHandler.update: " + docid + |
310 | 300 |
": " + e.getMessage()); |
311 | 301 |
} |
312 |
} |
|
313 |
} |
|
302 |
}//if for non-data file |
|
303 |
|
|
304 |
// this is for data file |
|
305 |
if(flag && dataFile) |
|
306 |
{ |
|
307 |
//if the document needs to be updated or inserted, this is executed |
|
308 |
//String newxmldoc = MetacatReplication.getURLContent(u); |
|
309 |
DocInfoHandler dih = new DocInfoHandler(); |
|
310 |
XMLReader docinfoParser = initParser(dih); |
|
311 |
URL docinfoUrl = new URL("https://" + docServer + |
|
312 |
"?server="+util.getLocalReplicationServerName()+ |
|
313 |
"&action=getdocumentinfo&docid="+docid); |
|
314 |
//System.out.println("Sending message: " + docinfoUrl.toString()); |
|
315 |
String docInfoStr = MetacatReplication.getURLContent(docinfoUrl); |
|
316 |
docinfoParser.parse(new InputSource(new StringReader(docInfoStr))); |
|
317 |
Hashtable docinfoHash = dih.getDocInfo(); |
|
318 |
String user = (String)docinfoHash.get("user_owner"); |
|
319 |
String docName = (String)docinfoHash.get("docname"); |
|
320 |
String docType = (String)docinfoHash.get("doctype"); |
|
321 |
int serverCode = MetacatReplication.getServerCode(docServer); |
|
322 |
//System.out.println("updating doc: " + docid +" action: "+ action); |
|
323 |
//docid should include rev number too |
|
324 |
String accnum=docid+util.getOption("accNumSeparator")+ |
|
325 |
(String)docinfoHash.get("rev"); |
|
326 |
String datafilePath = util.getOption("datafilepath"); |
|
327 |
u = new URL("https://" + docServer + "?server="+ |
|
328 |
util.getLocalReplicationServerName()+ |
|
329 |
"&action=readdata&docid="+accnum); |
|
330 |
//System.out.println("accnum: "+accnum); |
|
331 |
try |
|
332 |
{ |
|
333 |
|
|
334 |
//register data file into xml_documents table and wite data file |
|
335 |
//into file system |
|
336 |
DocumentImpl.writeDataFile(u.openStream(), datafilePath, |
|
337 |
docName,docType, accnum, user,serverCode); |
|
338 |
|
|
339 |
MetacatReplication.replLog("wrote doc " + docid + " from " + |
|
340 |
docServer); |
|
341 |
/*System.out.println("wrote doc " + docid + " from " + |
|
342 |
docServer);*/ |
|
343 |
} |
|
344 |
catch(Exception e) |
|
345 |
{ |
|
346 |
System.out.println("error writing document in " + |
|
347 |
"ReplicationHandler.update: " + docid + |
|
348 |
": " + e.getMessage()); |
|
349 |
} |
|
350 |
}//for datafile |
|
351 |
}//for update docs |
|
314 | 352 |
|
353 |
//handle deleted docs |
|
315 | 354 |
for(int k=0; k<d.size(); k++) |
316 | 355 |
{ //delete the deleted documents; |
317 | 356 |
Vector w = new Vector((Vector)d.elementAt(k)); |
318 | 357 |
String docid = (String)w.elementAt(0); |
319 | 358 |
if(!alreadyDeleted(docid, conn)) |
320 | 359 |
{ |
321 |
DocumentImpl.delete(conn, docid, null, null); |
|
322 |
System.out.println("Document " + docid + " deleted."); |
|
360 |
|
|
361 |
//because delete method docid should have rev number |
|
362 |
//so we just add one for it. This rev number is no sence. |
|
363 |
String accnum=docid+util.getOption("accNumSeparator")+"1"; |
|
364 |
//System.out.println("accnum: "+accnum); |
|
365 |
DocumentImpl.delete(conn, accnum, null, null); |
|
323 | 366 |
MetacatReplication.replLog("doc " + docid + " deleted"); |
324 | 367 |
} |
325 |
} |
|
368 |
}//for delete docs
|
|
326 | 369 |
|
370 |
//updated last_checked |
|
327 | 371 |
keys = serverList.keys(); |
328 | 372 |
while(keys.hasMoreElements()) |
329 | 373 |
{ |
... | ... | |
343 | 387 |
//conn.commit(); |
344 | 388 |
System.out.println("last_checked updated to " + datestr + " on " + |
345 | 389 |
server); |
346 |
} |
|
347 |
} |
|
348 |
} |
|
390 |
}//wile
|
|
391 |
}//for
|
|
392 |
}//try
|
|
349 | 393 |
catch(Exception e) |
350 | 394 |
{ |
351 | 395 |
/*System.out.println("error in ReplicationHandler.update: " + |
... | ... | |
357 | 401 |
try |
358 | 402 |
{ |
359 | 403 |
pstmt.close(); |
404 |
util.returnConnection(conn); |
|
360 | 405 |
} |
361 | 406 |
catch(Exception ee) {} |
362 |
} |
|
363 |
} |
|
407 |
}//finally
|
|
408 |
}//update
|
|
364 | 409 |
|
365 | 410 |
/** |
366 | 411 |
* updates xml_catalog with entries from other servers. |
... | ... | |
459 | 504 |
} |
460 | 505 |
} |
461 | 506 |
} |
507 |
conn.close(); |
|
462 | 508 |
} |
463 | 509 |
catch(Exception e) |
464 | 510 |
{ |
465 | 511 |
System.out.println("error in ReplicationHandler.updateCatalog: " + |
466 | 512 |
e.getMessage()); |
467 | 513 |
e.printStackTrace(System.out); |
514 |
try |
|
515 |
{ |
|
516 |
conn.close(); |
|
517 |
} |
|
518 |
catch (Exception ee) |
|
519 |
{ |
|
520 |
} |
|
468 | 521 |
} |
469 | 522 |
} |
470 | 523 |
|
Also available in: Unified diff
In update method, adding some code to handle replicate data file. Fixed the bug in deleting documents.