Project

General

Profile

« Previous | Next » 

Revision 1585

Added by Jing Tao over 21 years ago

Add a method to write error message into log file.

View differences:

src/edu/ucsb/nceas/metacat/ReplicationHandler.java
119 119
       should be kept anyway)
120 120
    */
121 121
    
122
    DBConnection dbConn = null;
123
    int serialNumber = -1;
124
   
125 122
    ReplicationServer replServer = null; // Variable to store the 
126 123
                                        // ReplicationServer got from 
127 124
                                        // Server list
......
129 126
    String update;
130 127
    ReplMessageHandler message = new ReplMessageHandler();
131 128
    Vector responses = new Vector();
132
    PreparedStatement pstmt = null;
133
    ResultSet rs;
134
    boolean tablehasrows;
135 129
    boolean flag=false; // If a document need to update
136 130
    boolean dataFile=false;
137 131
    String action = new String();
138 132
    XMLReader parser;
139 133
    URL u;
140 134
    
135
  
141 136
    try
142 137
    {
143
      // Get DBConnection from pool
144
      dbConn=DBConnectionPool.
145
                  getDBConnection("ReplicationHandler.update");
146
      serialNumber=dbConn.getCheckOutSerialNumber();
147
      MetaCatUtil.debugMessage("init parser", 50);
148 138
      parser = initParser(message);
149
      //Check for every server in server list
150
      for (int i=0; i<serverList.size(); i++)
151
      {
139
    }
140
    catch (Exception e)
141
    {
142
      MetacatReplication.replErrorLog("Failed to replicate becaue couldn't "+
143
                                 " initParser for message and" +e.getMessage());
144
      MetaCatUtil.debugMessage("Failed to replicate becaue couldn't " +
145
                            " initParser for message and " +e.getMessage(), 30);
146
       // stop replication
147
       return;
148
    }
149
    //Check for every server in server list to get updated list and put
150
    // them in to response
151
    for (int i=0; i<serverList.size(); i++)
152
    {
152 153
        // Get ReplicationServer object from server list
153 154
        replServer = serverList.serverAt(i);
154 155
        // Get server name from ReplicationServer object
155 156
        server = replServer.getServerName();
157
        String result = null;
156 158
        MetacatReplication.replLog("full update started to: " + server);
157 159
        // Send command to that server to get updated docid information
158
        u = new URL("https://" + server + "?server="
159
        +util.getLocalReplicationServerName()+"&action=update");
160
        MetaCatUtil.debugMessage("Sending infomation " +u.toString(), 50);
161
        // Get information from remote server
162
        String result = null;
163 160
        try
164 161
        {
162
          u = new URL("https://" + server + "?server="
163
          +util.getLocalReplicationServerName()+"&action=update");
164
          MetaCatUtil.debugMessage("Sending infomation " +u.toString(), 50);
165 165
          result = MetacatReplication.getURLContent(u);
166
        }//try
166
        }
167 167
        catch (Exception e)
168 168
        {
169
          
170
          MetaCatUtil.debugMessage("Error in get docid information from "+
171
                                server+" :"+e.getMessage(), 30);
169
          MetacatReplication.replErrorLog("Failed to get updated doc list "+
170
                          "for server " + server + " because "+e.getMessage());
171
          MetaCatUtil.debugMessage( "Failed to get updated doc list "+
172
                       "for server " + server + " because "+e.getMessage(), 30);
172 173
          continue;
173
        }//catch
174
        }
175
        
174 176
        MetaCatUtil.debugMessage("docid: "+server+" "+result, 50);
175 177
        //check if result have error or not, if has skip it.
176 178
        if (result.indexOf("error")!=-1)
177 179
        {
180
          MetacatReplication.replErrorLog("Failed to get updated doc list "+
181
                          "for server " + server + " because "+result);
182
          MetaCatUtil.debugMessage( "Failed to get updated doc list "+
183
                       "for server " + server + " because "+result, 30);
178 184
          continue;
179 185
        }
186
        //Add result to vector
180 187
        responses.add(result);
181
      }
188
    }
182 189
      
183
      //make sure that there is updated file list
184
      //If response is null, metacat don't need do anything
185
       if (responses==null||responses.isEmpty())
186
      {
190
    //make sure that there is updated file list
191
    //If response is null, metacat don't need do anything
192
    if (responses==null || responses.isEmpty())
193
    {
194
        MetacatReplication.replErrorLog("No updated doc list for "+
195
                           "every server and failed to replicate");
196
        MetaCatUtil.debugMessage( "No updated doc list for "+
197
                           "every server and failed to replicate", 30);
187 198
        return;
188
      }
199
    }
189 200
     
190 201
      
191
      MetaCatUtil.debugMessage("Responses from remote metacat about updated "+
192
                   "document information: "+ responses.toString(), 30);
193
      
194
      for(int i=0; i<responses.size(); i++)
195
      { //check each server for updated files
196
        parser.parse(new InputSource(
202
    MetaCatUtil.debugMessage("Responses from remote metacat about updated "+
203
                   "document information: "+ responses.toString(), 35);
204
    // go through response vector(it contains updated vector and delete vector
205
    for(int i=0; i<responses.size(); i++)
206
    { 
207
        try
208
        {
209
          parser.parse(new InputSource(
197 210
                     new StringReader(
198 211
                     (String)(responses.elementAt(i)))));
212
        }
213
        catch(Exception e)
214
        {
215
          MetacatReplication.replErrorLog("Couldn't parse one responses "+
216
                           "because "+ e.getMessage());
217
          MetaCatUtil.debugMessage("Couldn't parse one responses "+
218
                                   "because "+ e.getMessage(), 30);
219
          continue;
220
        }
199 221
        //v is the list of updated documents
200
        Vector v = new Vector(message.getUpdatesVect());
222
        Vector updateList = new Vector(message.getUpdatesVect());
201 223
        //System.out.println("v: " + v.toString());
202 224
        //d is the list of deleted documents
203
        Vector d = new Vector(message.getDeletesVect());
225
        Vector deleteList = new Vector(message.getDeletesVect());
204 226
        //System.out.println("d: " + d.toString());
205
        MetaCatUtil.debugMessage("Update vector size: "+ v.size(), 40);
206
        MetaCatUtil.debugMessage("Delete vector size: "+ d.size(),40);
207
        //check the revs in u to see if there are any newer ones than
208
        //in the local DB.
209
        for(int j=0; j<v.size(); j++)
227
        MetaCatUtil.debugMessage("Update vector size: "+ updateList.size(), 40);
228
        MetaCatUtil.debugMessage("Delete vector size: "+ deleteList.size(),40);
229
        // go though every element in updated document vector
230
        for(int j=0; j<updateList.size(); j++)
210 231
        {
211 232
          //initial dataFile is false
212 233
          dataFile=false;
213 234
          //w is information for one document, information contain
214 235
          //docid, rev, server or datafile.
215
          Vector w = new Vector((Vector)(v.elementAt(j)));
236
          Vector w = new Vector((Vector)(updateList.elementAt(j)));
216 237
          //Check if the vector w contain "datafile"
217 238
          //If it has, this document is data file
218 239
          if (w.contains((String)util.getOption("datafileflag")))
......
230 251
          // the new hub feature
231 252
          String remoteServer = (String)w.elementAt(2);
232 253
          
233
          // Choose revision number from local host
234
          pstmt = dbConn.prepareStatement("select rev from xml_documents where "
235
                                            + "docid like '" + docid + "'");
236
          pstmt.execute();
237
          rs = pstmt.getResultSet();
238
          tablehasrows = rs.next();
239
          if(tablehasrows)
240
          { //check the revs for an update because this document is in the
241
            //local DB, it might be out of date.
242
            int localrev = rs.getInt(1);
254
          
255
          // compare the update rev and local rev to see what need happen
256
          int localrev = -1;
257
          try
258
          {
259
            localrev = DocumentImpl.getLatestRevisionNumber(docid);
260
          }
261
          catch (SQLException e)
262
          {
263
            MetaCatUtil.debugMessage("Local rev for docid "+ docid + " could not "+ 
264
                                   " be found because " + e.getMessage(), 45);
265
            MetacatReplication.replErrorLog("Docid "+ docid + " could not be "+
266
                    "written because error happend to find it's local revision");
267
            continue;
268
          }
269
          MetaCatUtil.debugMessage("Local rev for docid "+ docid + " is "+ 
270
                                  localrev, 45);
271
          
272
          //check the revs for an update because this document is in the
273
          //local DB, it might be out of date.
274
          if (localrev == -1)
275
          {
276
            //insert this document as new because it is not in the local DB
277
            action = "INSERT";
278
            flag = true;
279
          }
280
          else
281
          {
243 282
            if(localrev == rev)
244 283
            {
245 284
              // Local meatacat has the same rev to remote host, don't need 
......
247 286
              flag = false;
248 287
            }
249 288
            else if(localrev < rev)
250
            {//this document needs to be updated so send an read request
289
            {
290
              //this document needs to be updated so send an read request
251 291
              action = "UPDATE";
252 292
              flag = true;
253 293
            }
254 294
          }
255
          else
256
          { //insert this document as new because it is not in the local DB
257
            action = "INSERT";
258
            flag = true;
259
          }
260
          
261
          // Close result set
262
          rs.close();
263
          // Close preparedstatement
264
          pstmt.close();
265
          
295
    
266 296
          // this is non-data file
267 297
          if(flag && !dataFile)
268
          { //if the document needs to be updated or inserted, this is executed
269
            u = new URL("https://" + remoteServer + "?server="+
270
              util.getLocalReplicationServerName()+"&action=read&docid="+docid);
271
            //System.out.println("Sending message: " + u.toString());
272
            // Get docid content
273
            String newxmldoc = MetacatReplication.getURLContent(u);
274
            // If couldn't get skip it
275
            if ( newxmldoc.indexOf("error")!= -1)
276
            {
277
              continue;
278
            }
279
            MetaCatUtil.debugMessage("xml documnet:", 50);
280
            MetaCatUtil.debugMessage(newxmldoc, 50);
281
            
282
            // Try get the docid info from remote server
283
            DocInfoHandler dih = new DocInfoHandler();
284
            XMLReader docinfoParser = initParser(dih);
285
            URL docinfoUrl = new URL("https://" + remoteServer + 
286
                  "?server="+util.getLocalReplicationServerName()+
287
                  "&action=getdocumentinfo&docid="+docid);
288
            MetaCatUtil.debugMessage("Sending message: " + 
289
                                                  docinfoUrl.toString(), 50);
290
            String docInfoStr = MetacatReplication.getURLContent(docinfoUrl);
291
            docinfoParser.parse(new InputSource(new StringReader(docInfoStr)));
292
            Hashtable docinfoHash = dih.getDocInfo();
293
            // Get home server of the docid
294
            String docHomeServer = (String)docinfoHash.get("home_server");
295
                
296
            //docid should include rev number too
297
            String accnum=docid+util.getOption("accNumSeparator")+
298
                                              (String)docinfoHash.get("rev");
299
            String docType = (String)docinfoHash.get("doctype");
298
          { 
300 299
            try
301 300
            {
302
              String parserBase = null;
303
              // this for eml2 and we need user eml2 parser
304
              if (docType != null && 
305
              (docType.trim()).equals(DocumentImpl.EMLNAMESPACE))
306
              {
307
                parserBase = DocumentImpl.EML2;
308
              }
309
              // Write the document into local host
310
              DocumentImplWrapper wrapper = 
311
                                     new DocumentImplWrapper(parserBase, false);
312
              String newDocid = wrapper.writeReplication(dbConn, 
313
                              new StringReader(newxmldoc),
314
                              (String)docinfoHash.get("public_access"),
315
                              null,  /* the dtd text */
316
                              action, 
317
                              accnum, 
318
                              (String)docinfoHash.get("user_owner"),
319
                              null, /* null for groups[] */
320
                              docHomeServer, 
321
                              remoteServer);
322
              //increase usage
323
              dbConn.increaseUsageCount(1);
324
              MetacatReplication.replLog("wrote doc " + docid + " from " + 
325
                                         remoteServer);
326
              /*System.out.println("wrote doc " + docid + " from " + 
327
                                 docServer);*/
301
              handleSingleXMLDocument(remoteServer, action, docid);
328 302
            }
329 303
            catch(Exception e)
330 304
            {
331
              System.out.println("error writing document in " + 
332
                                 "ReplicationHandler.update: " + docid +
333
                                 ": " + e.getMessage());
305
              // skip this document
306
              continue;
334 307
            }
335 308
          }//if for non-data file
336 309
          
337 310
           // this is for data file
338 311
          if(flag && dataFile)
339 312
          { 
340
            //if the document needs to be updated or inserted, this is executed
341
            
342
            // Try get docid info from remote server
343
            DocInfoHandler dih = new DocInfoHandler();
344
            XMLReader docinfoParser = initParser(dih);
345
            URL docinfoUrl = new URL("https://" + remoteServer + 
346
                  "?server="+util.getLocalReplicationServerName()+
347
                  "&action=getdocumentinfo&docid="+docid);
348
            
349
            String docInfoStr = MetacatReplication.getURLContent(docinfoUrl);
350
            docinfoParser.parse(new InputSource(new StringReader(docInfoStr)));
351
            Hashtable docinfoHash = dih.getDocInfo();
352
            // Get doicd owner
353
            String user = (String)docinfoHash.get("user_owner");
354
            // Get docid name (such as acl or dataset)
355
            String docName = (String)docinfoHash.get("docname");
356
            // Get doc type (eml public id) 
357
            String docType = (String)docinfoHash.get("doctype");
358
            // Get docid home sever. it might be different to remoteserver
359
            // becuause of hub feature
360
            String docHomeServer = (String)docinfoHash.get("home_server");
361
         
362
            //System.out.println("updating doc: " + docid +" action: "+ action);
363
            //docid should include rev number too
364
            String accnum=docid+util.getOption("accNumSeparator")+
365
                                              (String)docinfoHash.get("rev");
366
            String datafilePath = util.getOption("datafilepath");
367
            // Get data file content
368
            u = new URL("https://" + remoteServer + "?server="+
369
                                        util.getLocalReplicationServerName()+
370
                                            "&action=readdata&docid="+accnum);
371
           
372 313
            try
373 314
            {
374
              //register data file into xml_documents table and wite data file
375
              //into file system
376
              if (u.openStream() != null)
377
              {
378
                DocumentImpl.writeDataFileInReplication(u.openStream(), 
379
                                                        datafilePath, 
380
                                                        docName,docType, 
381
                                                        accnum, user,
382
                                                        docHomeServer,
383
                                                        remoteServer);
384
              
385
                MetacatReplication.replLog("wrote doc " + docid + " from " + 
386
                                         remoteServer);
387
              }//if
388
              else
389
              {
390
                // If get null, skip it
391
                continue;
392
              }//else
393
            
394
            }//try
315
              handleSingleDataFile(remoteServer, action, docid);
316
            }
395 317
            catch(Exception e)
396 318
            {
397
              System.out.println("error writing document in " + 
398
                                 "ReplicationHandler.update: " + docid +
399
                                 ": " + e.getMessage());
400
            }//catch
319
              // skip this datafile
320
              continue;
321
            }
322
         
401 323
          }//for datafile
402 324
        }//for update docs
403 325
        
404 326
        //handle deleted docs
405
        for(int k=0; k<d.size(); k++)
327
        for(int k=0; k<deleteList.size(); k++)
406 328
        { //delete the deleted documents;
407
          Vector w = new Vector((Vector)d.elementAt(k));
408
          String docid = (String)w.elementAt(0);
409
          if(!alreadyDeleted(docid))
329
          Vector w = new Vector((Vector)deleteList.elementAt(k));
330
          String docId = (String)w.elementAt(0);
331
          try
410 332
          {
411
          
412
            //because delete method docid should have rev number
413
            //so we just add one for it. This rev number is no sence.
414
            String accnum=docid+util.getOption("accNumSeparator")+"1";
415
            //System.out.println("accnum: "+accnum);
416
            DocumentImpl.delete(accnum, null, null);
417
            MetacatReplication.replLog("doc " + docid + " deleted");
333
            handleDeleteSingleDocument(docId);
418 334
          }
335
          catch (Exception ee)
336
          {
337
            continue;
338
          }
419 339
        }//for delete docs
420
      }//for
340
    }//for response
421 341
      
422
      //updated last_checked
423
      for (int i=0;i<serverList.size(); i++)
342
    //updated last_checked
343
    for (int i=0;i<serverList.size(); i++)
344
    {
345
       // Get ReplicationServer object from server list
346
       replServer = serverList.serverAt(i);
347
       try
348
       {
349
         updateLastCheckTimeForSingleServer(replServer);
350
       }
351
       catch(Exception e)
352
       {
353
         continue;
354
       }
355
    }//for
356
   
357
  }//update
358
  
359
  /* Handle replicate single xml document*/
360
  private void handleSingleXMLDocument(String remoteserver, String actions, 
361
                                       String docId) 
362
               throws Exception
363
  {
364
    DBConnection dbConn = null;
365
    int serialNumber = -1;
366
    try
367
    {
368
      // Get DBConnection from pool
369
      dbConn=DBConnectionPool.
370
                  getDBConnection("ReplicationHandler.handleSingleXMLDocument");
371
      serialNumber=dbConn.getCheckOutSerialNumber();
372
      //if the document needs to be updated or inserted, this is executed
373
      URL u = new URL("https://" + remoteserver + "?server="+
374
              util.getLocalReplicationServerName()+"&action=read&docid="+docId);
375
      
376
      // Get docid content
377
      String newxmldoc = MetacatReplication.getURLContent(u);
378
      // If couldn't get skip it
379
      if ( newxmldoc.indexOf("error")!= -1)
424 380
      {
425
          // Get ReplicationServer object from server list
426
          replServer = serverList.serverAt(i);
427
          // Get server name from ReplicationServer object
428
          server = replServer.getServerName();
429
          // Get time from remote server
430
          URL dateurl = new URL("https://" + server + "?server="+
431
          util.getLocalReplicationServerName()+"&action=gettime");
432
          String datexml = MetacatReplication.getURLContent(dateurl);
433
          MetaCatUtil.debugMessage("datexml: "+datexml, 40);
434
          if (datexml!=null && !datexml.equals(""))
435
          {
436
            String datestr = datexml.substring(11, datexml.indexOf('<', 11));
437
            StringBuffer sql = new StringBuffer();
438
            sql.append("update xml_replication set last_checked = to_date('");
439
            sql.append(datestr).append("', 'YY-MM-DD HH24:MI:SS') where ");
440
            sql.append("server like '").append(server).append("'");
441
            //System.out.println("sql: " + sql.toString());
442
            pstmt.close();
443
            pstmt = dbConn.prepareStatement(sql.toString());
444
            //increase usage count
445
            dbConn.increaseUsageCount(1);
446
            pstmt.executeUpdate();
447
            dbConn.commit();
448
            MetaCatUtil.debugMessage("last_checked updated to "+datestr+" on " 
449
                                      + server, 45);
450
          }//if
451
      }//for
452
    }//try
381
         throw new Exception(newxmldoc);
382
      }
383
      MetaCatUtil.debugMessage("xml documnet:", 45);
384
      MetaCatUtil.debugMessage(newxmldoc, 45);
385
            
386
      // Try get the docid info from remote server
387
      DocInfoHandler dih = new DocInfoHandler();
388
      XMLReader docinfoParser = initParser(dih);
389
      URL docinfoUrl = new URL("https://" + remoteserver + 
390
                       "?server="+util.getLocalReplicationServerName()+
391
                       "&action=getdocumentinfo&docid="+docId);
392
      MetaCatUtil.debugMessage("Sending message: " + 
393
                                                  docinfoUrl.toString(), 45);
394
      String docInfoStr = MetacatReplication.getURLContent(docinfoUrl);
395
      docinfoParser.parse(new InputSource(new StringReader(docInfoStr)));
396
      Hashtable docinfoHash = dih.getDocInfo();
397
      // Get home server of the docid
398
      String docHomeServer = (String)docinfoHash.get("home_server");
399
      MetaCatUtil.debugMessage("doc home server in repl: "+docHomeServer, 45);
400
                
401
      //docid should include rev number too
402
      String accnum=docId+util.getOption("accNumSeparator")+
403
                                              (String)docinfoHash.get("rev");
404
      MetaCatUtil.debugMessage("docid in repl: "+accnum, 45);
405
      String docType = (String)docinfoHash.get("doctype");
406
      MetaCatUtil.debugMessage("doctype in repl: "+docType, 45);
407
           
408
      String parserBase = null;
409
      // this for eml2 and we need user eml2 parser
410
      if (docType != null && (docType.trim()).equals(DocumentImpl.EMLNAMESPACE))
411
      {
412
         parserBase = DocumentImpl.EML2;
413
      }
414
      // Write the document into local host
415
      DocumentImplWrapper wrapper = new DocumentImplWrapper(parserBase, false);
416
      String newDocid = wrapper.writeReplication(dbConn, 
417
                              new StringReader(newxmldoc),
418
                              (String)docinfoHash.get("public_access"),
419
                              null,  /* the dtd text */
420
                              actions, 
421
                              accnum, 
422
                              (String)docinfoHash.get("user_owner"),
423
                              null, /* null for groups[] */
424
                              docHomeServer, 
425
                              remoteserver);
426
      MetaCatUtil.debugMessage("Successfully replicated doc " + accnum, 35);
427
      MetacatReplication.replLog("wrote doc " + accnum + " from " + 
428
                                         remoteserver);
429
            
430
    }//try      
453 431
    catch(Exception e)
454 432
    {
455
      /*System.out.println("error in ReplicationHandler.update: " + 
456
                          e.getMessage());*/
457
      e.printStackTrace(System.out);
433
      MetacatReplication.replErrorLog("Failed to write doc " + docId +
434
                                      " into db because " +e.getMessage());
435
      MetaCatUtil.debugMessage("Failed to write doc " + docId +
436
                                      " into db because " +e.getMessage(), 30);
437
      throw e;
458 438
    }
459 439
    finally
460 440
    {
461
      try
441
       //return DBConnection
442
       DBConnectionPool.returnDBConnection(dbConn, serialNumber);
443
    }//finally
444
  }
445
  
446
  
447
  
448
  /* Handle replicate single xml document*/
449
  private void handleSingleDataFile(String remoteserver, String actions, 
450
                                    String docId) 
451
               throws Exception
452
  {
453
    MetaCatUtil.debugMessage("Try to replicate data file: "+docId, 40);
454
    DBConnection dbConn = null;
455
    int serialNumber = -1;
456
    try
457
    {
458
      // Get DBConnection from pool
459
      dbConn=DBConnectionPool.
460
                  getDBConnection("ReplicationHandler.handleSinlgeDataFile");
461
      serialNumber=dbConn.getCheckOutSerialNumber();
462
      // Try get docid info from remote server
463
      DocInfoHandler dih = new DocInfoHandler();
464
      XMLReader docinfoParser = initParser(dih);
465
      URL docinfoUrl = new URL("https://" + remoteserver + 
466
                  "?server="+util.getLocalReplicationServerName()+
467
                  "&action=getdocumentinfo&docid="+docId);
468
            
469
      String docInfoStr = MetacatReplication.getURLContent(docinfoUrl);
470
      docinfoParser.parse(new InputSource(new StringReader(docInfoStr)));
471
      Hashtable docinfoHash = dih.getDocInfo();
472
      // Get doicd owner
473
      String user = (String)docinfoHash.get("user_owner");
474
      // Get docid name (such as acl or dataset)
475
      String docName = (String)docinfoHash.get("docname");
476
      // Get doc type (eml public id) 
477
      String docType = (String)docinfoHash.get("doctype");
478
      // Get docid home sever. it might be different to remoteserver
479
      // becuause of hub feature
480
      String docHomeServer = (String)docinfoHash.get("home_server");
481
         
482
      //docid should include rev number too
483
      String accnum=docId+util.getOption("accNumSeparator")+
484
                                              (String)docinfoHash.get("rev");
485
      String datafilePath = util.getOption("datafilepath");
486
      // Get data file content
487
      URL u = new URL("https://" + remoteserver + "?server="+
488
                                        util.getLocalReplicationServerName()+
489
                                            "&action=readdata&docid="+accnum);
490
          
491
      //register data file into xml_documents table and wite data file
492
      //into file system
493
      if (u.openStream() != null)
462 494
      {
463
        
464
          pstmt.close();
465
       
466
      }//try
467
      catch(Exception ee) 
495
         DocumentImpl.writeDataFileInReplication(u.openStream(), 
496
                                                        datafilePath, 
497
                                                        docName,docType, 
498
                                                        accnum, user,
499
                                                        docHomeServer,
500
                                                        remoteserver);
501
        MetaCatUtil.debugMessage("Successfully to write datafile " + docId, 30);      
502
        MetacatReplication.replLog("wrote datafile " + accnum + " from " + 
503
                                    remoteserver);
504
      }//if
505
      else
468 506
      {
469
        MetaCatUtil.debugMessage("Error in ReplicationHandler.update to close"+
470
                                " pstmt: "+ee.getMessage(), 30);
471
      }//catch
472
      finally
507
         throw new Exception("Couldn't open the data file: " + accnum);
508
      }//else
509
 
510
    }//try      
511
    catch(Exception e)
512
    {
513
      MetacatReplication.replErrorLog("Failed to try wrote datafile " + docId +
514
                                      " because " +e.getMessage());
515
      MetaCatUtil.debugMessage("Failed to try wrote datafile " + docId +
516
                                      " because " +e.getMessage(), 30);
517
      throw e;
518
    }
519
    finally
520
    {
521
       //return DBConnection
522
       DBConnectionPool.returnDBConnection(dbConn, serialNumber);
523
    }//finally
524
  }
525
  
526
  
527
  
528
  /* Handle delete single document*/
529
  private void handleDeleteSingleDocument(String docId) 
530
               throws Exception
531
  {
532
    MetaCatUtil.debugMessage("Try delete doc: "+docId, 40);
533
    DBConnection dbConn = null;
534
    int serialNumber = -1;
535
    try
536
    {
537
      // Get DBConnection from pool
538
      dbConn=DBConnectionPool.
539
                  getDBConnection("ReplicationHandler.handleDeleteSingleDoc");
540
      serialNumber=dbConn.getCheckOutSerialNumber();
541
      if(!alreadyDeleted(docId))
473 542
      {
474
        DBConnectionPool.returnDBConnection(dbConn, serialNumber);
475
      }//finally
543
          
544
         //because delete method docid should have rev number
545
         //so we just add one for it. This rev number is no sence.
546
         String accnum=docId+util.getOption("accNumSeparator")+"1";
547
         //System.out.println("accnum: "+accnum);
548
         DocumentImpl.delete(accnum, null, null);
549
         MetaCatUtil.debugMessage("Successfully deleted doc " + docId, 30); 
550
         MetacatReplication.replLog("Doc " + docId + " deleted");
551
      }
552
   
553
    }//try      
554
    catch(Exception e)
555
    {
556
      MetacatReplication.replErrorLog("Failed to delete doc " + docId +
557
                                      " in db because " +e.getMessage());
558
      MetaCatUtil.debugMessage("Failed to delete doc " + docId +
559
                                 " in db because because " +e.getMessage(), 30);
560
      throw e;
561
    }
562
    finally
563
    {
564
       //return DBConnection
565
       DBConnectionPool.returnDBConnection(dbConn, serialNumber);
476 566
    }//finally
477
  }//update
567
  }
478 568
  
479
  /**
480
   * updates xml_catalog with entries from other servers.
481
   */
482
  private void updateCatalog()
569
  /* Handle updateLastCheckTimForSingleServer*/
570
  private void updateLastCheckTimeForSingleServer(ReplicationServer repServer) 
571
                                                  throws Exception
483 572
  {
484
    // DConnection
573
    String server = repServer.getServerName();
485 574
    DBConnection dbConn = null;
486
    // DBConnection checkout serial number
487 575
    int serialNumber = -1;
488
    // ReplicationServer object in server list
489
    ReplicationServer replServer = null;
490 576
    PreparedStatement pstmt = null;
491 577
    try
492 578
    {
493

  
579
      // Get DBConnection from pool
494 580
      dbConn=DBConnectionPool.
495
                  getDBConnection("ReplicationHandler.updateCatalog");
581
             getDBConnection("ReplicationHandler.updateLastCheckTimeForServer");
496 582
      serialNumber=dbConn.getCheckOutSerialNumber();
497
      String server;
498 583
      
499
      // Go through each ReplicationServer object in sererlist
500
      for (int j=0; j<serverList.size(); j++)
501
      { 
584
      MetaCatUtil.debugMessage("Try to update last_check for server: "+server, 40);
585
      // Get time from remote server
586
      URL dateurl = new URL("https://" + server + "?server="+
587
      util.getLocalReplicationServerName()+"&action=gettime");
588
      String datexml = MetacatReplication.getURLContent(dateurl);
589
      MetaCatUtil.debugMessage("datexml: "+datexml, 45);
590
      if (datexml!=null && !datexml.equals(""))
591
      {
592
         String datestr = datexml.substring(11, datexml.indexOf('<', 11));
593
         StringBuffer sql = new StringBuffer();
594
         sql.append("update xml_replication set last_checked = to_date('");
595
         sql.append(datestr).append("', 'YY-MM-DD HH24:MI:SS') where ");
596
         sql.append("server like '").append(server).append("'");
597
         //System.out.println("sql: " + sql.toString());
598
         pstmt = dbConn.prepareStatement(sql.toString());
599
         pstmt.executeUpdate();
600
         dbConn.commit();
601
         pstmt.close();
602
         MetaCatUtil.debugMessage("last_checked updated to "+datestr+" on " 
603
                                      + server, 45);
604
      }//if
605
      else
606
      {
607
         
608
         MetaCatUtil.debugMessage("Failed to update last_checked for server "  + 
609
                                  server + " in db because couldn't get time " 
610
                                  , 30);
611
         throw new Exception("Couldn't get time for server "+ server);
612
      }
613
   
614
    }//try      
615
    catch(Exception e)
616
    {
617
      
618
      MetaCatUtil.debugMessage("Failed to update last_checked for server " + 
619
                                server + " in db because because " +
620
                                e.getMessage(), 30);
621
      throw e;
622
    }
623
    finally
624
    {
625
       //return DBConnection
626
       DBConnectionPool.returnDBConnection(dbConn, serialNumber);
627
    }//finally
628
  }
629
  
630
  
631
  
632
  /**
633
   * updates xml_catalog with entries from other servers.
634
   */
635
  private void updateCatalog()
636
  {
637
    MetaCatUtil.debugMessage("Start of updateCatalog", 35 );
638
    // ReplicationServer object in server list
639
    ReplicationServer replServer = null;
640
    PreparedStatement pstmt = null;
641
    String server = null;
642
    
643
      
644
    // Go through each ReplicationServer object in sererlist
645
    for (int j=0; j<serverList.size(); j++)
646
    { 
647
      Vector remoteCatalog = new Vector();
648
      Vector publicId = new Vector();
649
      try
650
      {
502 651
        // Get ReplicationServer object from server list
503 652
        replServer = serverList.serverAt(j);
504 653
        // Get server name from the ReplicationServer object
......
512 661
        // Make sure there are not error, no empty string
513 662
        if (catxml.indexOf("error")!=-1 || catxml==null||catxml.equals(""))
514 663
        {
515
          continue;
664
          throw new Exception("Couldn't get catalog list form server " +server);
516 665
        }
517
        MetaCatUtil.debugMessage("catxml: " + catxml, 50);
666
        MetaCatUtil.debugMessage("catxml: " + catxml, 40);
518 667
        CatalogMessageHandler cmh = new CatalogMessageHandler();
519 668
        XMLReader catparser = initParser(cmh);
520 669
        catparser.parse(new InputSource(new StringReader(catxml)));
521 670
        //parse the returned catalog xml and put it into a vector
522
        Vector remoteCatalog = cmh.getCatalogVect();
671
        remoteCatalog = cmh.getCatalogVect();
523 672
        
524 673
        // Makse sure remoteCatalog is not empty
525 674
        if (remoteCatalog.isEmpty())
526 675
        {
527
          continue;
676
          throw new Exception("Couldn't get catalog list form server " +server);
528 677
        }
529 678
        
530 679
        String localcatxml = MetacatReplication.getCatalogXML();
......
532 681
        // Make sure local catalog is no empty
533 682
        if (localcatxml==null||localcatxml.equals(""))
534 683
        {
535
          continue;
684
          throw new Exception("Couldn't get catalog list form server " +server);
536 685
        }
537 686
        
538 687
        cmh = new CatalogMessageHandler();
......
544 693
        //we now need to compare the two and merge the differences.
545 694
        //the comparison is base on the public_id fields which is the 4th
546 695
        //entry in each row vector.
547
        Vector publicId = new Vector();
696
        publicId = new Vector();
548 697
        for(int i=0; i<localCatalog.size(); i++)
549 698
        {
550 699
          Vector v = new Vector((Vector)localCatalog.elementAt(i));
......
552 701
          publicId.add(new String((String)v.elementAt(3)));
553 702
          //System.out.println("adding " + (String)v.elementAt(3));
554 703
        }
555
        
556
        for(int i=0; i<remoteCatalog.size(); i++)
704
      }//try
705
      catch (Exception e)
706
      {
707
        MetacatReplication.replErrorLog("Failed to update catalog for server "+
708
                                    server + " because " +e.getMessage());
709
        MetaCatUtil.debugMessage("Failed to update catalog for server "+
710
                                    server + " because " +e.getMessage(), 30);
711
      }//catch
712
      
713
      for(int i=0; i<remoteCatalog.size(); i++)
714
      {
715
         // DConnection
716
        DBConnection dbConn = null;
717
        // DBConnection checkout serial number
718
        int serialNumber = -1;
719
        try
557 720
        {
558
          Vector v = (Vector)remoteCatalog.elementAt(i);
559
          //System.out.println("v2: " + v.toString());
560
          //System.out.println("i: " + i);
561
          //System.out.println("remoteCatalog.size(): " + remoteCatalog.size());
562
          //System.out.println("publicID: " + publicId.toString());
563
          MetaCatUtil.debugMessage
721
            dbConn=DBConnectionPool.
722
                  getDBConnection("ReplicationHandler.updateCatalog");
723
            serialNumber=dbConn.getCheckOutSerialNumber();
724
            Vector v = (Vector)remoteCatalog.elementAt(i);
725
            //System.out.println("v2: " + v.toString());
726
            //System.out.println("i: " + i);
727
            //System.out.println("remoteCatalog.size(): " + remoteCatalog.size());
728
            //System.out.println("publicID: " + publicId.toString());
729
            MetaCatUtil.debugMessage
564 730
                              ("v.elementAt(3): " + (String)v.elementAt(3), 50);
565
          if(!publicId.contains(v.elementAt(3)))
566
          { //so we don't have this public id in our local table so we need to
567
            //add it.
568
            //System.out.println("in if");
569
            StringBuffer sql = new StringBuffer();
570
            sql.append("insert into xml_catalog (entry_type, source_doctype, ");
571
            sql.append("target_doctype, public_id, system_id) values (?,?,?,");
572
            sql.append("?,?)");
573
            //System.out.println("sql: " + sql.toString());
574
            pstmt = dbConn.prepareStatement(sql.toString());
575
            //increase usage count 1
576
            dbConn.increaseUsageCount(1);
577
            pstmt.setString(1, (String)v.elementAt(0));
578
            pstmt.setString(2, (String)v.elementAt(1));
579
            pstmt.setString(3, (String)v.elementAt(2));
580
            pstmt.setString(4, (String)v.elementAt(3));
581
            pstmt.setString(5, (String)v.elementAt(4));
582
            pstmt.execute();
583
            pstmt.close();
584
          }
731
           if(!publicId.contains(v.elementAt(3)))
732
           { //so we don't have this public id in our local table so we need to
733
             //add it.
734
             //System.out.println("in if");
735
             StringBuffer sql = new StringBuffer();
736
             sql.append("insert into xml_catalog (entry_type, source_doctype, ");
737
             sql.append("target_doctype, public_id, system_id) values (?,?,?,");
738
             sql.append("?,?)");
739
             //System.out.println("sql: " + sql.toString());
740
             pstmt = dbConn.prepareStatement(sql.toString());
741
             pstmt.setString(1, (String)v.elementAt(0));
742
             pstmt.setString(2, (String)v.elementAt(1));
743
             pstmt.setString(3, (String)v.elementAt(2));
744
             pstmt.setString(4, (String)v.elementAt(3));
745
             pstmt.setString(5, (String)v.elementAt(4));
746
             pstmt.execute();
747
             pstmt.close();
748
             MetacatReplication.replLog("Success fully to insert new publicid "+
749
                               (String)v.elementAt(3) + " from server"+server);
750
             MetaCatUtil.debugMessage("Success fully to insert new publicid "+
751
                             (String)v.elementAt(3) + " from server" +server, 30);
752
           }
585 753
        }
586
      }
587
     
588
    }
589
    catch(Exception e)
590
    {
591
      System.out.println("error in ReplicationHandler.updateCatalog: " + 
592
                          e.getMessage());
593
      e.printStackTrace(System.out);
594
    }//catch
595
    finally
596
    {
597
      DBConnectionPool.returnDBConnection(dbConn, serialNumber);
598
    }//finall
754
        catch(Exception e)
755
        {
756
           MetacatReplication.replErrorLog("Failed to update catalog for server "+
757
                                    server + " because " +e.getMessage());
758
           MetaCatUtil.debugMessage("Failed to update catalog for server "+
759
                                    server + " because " +e.getMessage(), 30);
760
        }//catch
761
        finally
762
        {
763
           DBConnectionPool.returnDBConnection(dbConn, serialNumber);
764
        }//finall
765
      }//for remote catalog
766
    }//for server list
767
    MetaCatUtil.debugMessage("End of updateCatalog", 35);
599 768
  }
600 769
  
601 770
  /**
......
609 778
   *   _             X             TRUE
610 779
   *   _             _             TRUE
611 780
   */
612
  private static boolean alreadyDeleted(String docid)
781
  private static boolean alreadyDeleted(String docid) throws Exception
613 782
  {
614 783
    DBConnection dbConn = null;
615 784
    int serialNumber = -1;
......
669 838
    }
670 839
    catch(Exception e)
671 840
    {
672
      System.out.println("error in ReplicationHandler.alreadyDeleted: " + 
673
                          e.getMessage());
674
      e.printStackTrace(System.out);
841
      MetaCatUtil.debugMessage("error in ReplicationHandler.alreadyDeleted: " + 
842
                          e.getMessage(), 30);
843
      throw e;
675 844
    }
676 845
    finally
677 846
    {
......
683 852
      {
684 853
        MetaCatUtil.debugMessage("Error in replicationHandler.alreadyDeleted "+
685 854
                          "to close pstmt: "+ee.getMessage(), 30);
855
        throw ee;
686 856
      }//catch
687 857
      finally
688 858
      {

Also available in: Unified diff