Project

General

Profile

« Previous | Next » 

Revision 1292

Added by Jing Tao almost 22 years ago

Merge cvs branch replication to cvs head.

View differences:

MetacatReplication.java
88 88
    Hashtable params = new Hashtable();
89 89
    Enumeration paramlist = request.getParameterNames();
90 90
    
91
    //First check if administrate conifgure the replcation feature on
92
    //This configuration is set in build.xml file
93
    //If it is not on, reject any request.
94
    if (!(util.getOption("replication")).equals("on"))
95
    {
96
     (response.getWriter()).println("MetaCat is not set to handle replication");
97
      (response.getWriter()).close();
98
      return;
99
    }
91
   
100 92
    
101 93
// NOT NEEDED - doesn't provide enough security because of possible IP spoofing
102 94
// REPLACED with running replication comminications over HTTPS
......
121 113
           !action.equals("getall") ) {
122 114

  
123 115
        server = ((String[])params.get("server"))[0];
124
        if ( getServerCode(server) == 0 ) {
116
        if ( getServerCodeForServerName(server) == 0 ) {
125 117
          System.out.println("Action \"" + action + 
126 118
                             "\" rejected for server: " + server);
127 119
          return;
......
144 136
    }
145 137
    else if ( action.equals("forcereplicatedatafile") ) 
146 138
    {
147
     
148
      //OutputStream out=response.getOutputStream();
149 139
      //read a specific docid from remote host, and store it into local host
150 140
      handleForceReplicateDataFileRequest(params);
151
      //out.close();
141
     
152 142
    }
153 143
    else
154 144
    {
......
244 234
    DBConnection dbConn = null;
245 235
    int serialNumber = -1;
246 236
    PreparedStatement pstmt = null;
247

  
237
    String replicate =null;
238
    String server = null;
239
    String dataReplicate = null;
240
    String hub = null;
248 241
    try {
249 242
      //conn = util.openDBConnection();
250 243
      dbConn=DBConnectionPool.
......
253 246
      
254 247
      // add server to server list
255 248
      if ( subaction.equals("add") ) {
256
        String replicate = ((String[])params.get("replicate"))[0];
257
        String server = ((String[])params.get("server"))[0];
249
        replicate = ((String[])params.get("replicate"))[0];
250
        server = ((String[])params.get("server"))[0];
251
        //Get data replication value
252
        dataReplicate = ((String[])params.get("datareplicate"))[0];
253
        //Get hub value
254
        hub = ((String[])params.get("hub"))[0];
255
        
258 256
        pstmt = dbConn.prepareStatement("INSERT INTO xml_replication " +
259
                                      "(server, last_checked, replicate) " +
257
                  "(server, last_checked, replicate, datareplicate, hub) " +
260 258
                                      "VALUES ('" + server + "', to_date(" +
261 259
                                      "'01/01/00', 'MM/DD/YY'), '" +
262
                                      replicate + "')");
260
                                      replicate +"', '" +dataReplicate+"', '"
261
                                      + hub +"')");
263 262
        pstmt.execute();
264 263
        pstmt.close();
265 264
        dbConn.commit();
......
267 266
        response.setContentType("text/html");
268 267
        out.println("<html><body><table border=\"1\">");
269 268
        out.println("<tr><td><b>server</b></td><td><b>last_checked</b></td><td>");
270
        out.println("<b>replicate</b></td></tr>");
269
        out.println("<b>replicate</b></td>");
270
        out.println("<td><b>datareplicate</b></td>");
271
        out.println("<td><b>hub</b></td></tr>");
271 272
        pstmt = dbConn.prepareStatement("SELECT * FROM xml_replication");
272 273
        //increase dbconnection usage
273 274
        dbConn.increaseUsageCount(1);
......
278 279
        while(tablehasrows) {
279 280
          out.println("<tr><td>" + rs.getString(2) + "</td><td>");
280 281
          out.println(rs.getString(3) + "</td><td>");
281
          out.println(rs.getString(4) + "</td></tr>");
282
          out.println(rs.getString(4) + "</td><td>");
283
          out.println(rs.getString(5) + "</td><td>");
284
          out.println(rs.getString(6) + "</td></tr>");
285
          
282 286
          tablehasrows = rs.next();
283 287
        }
284 288
        out.println("</table></body></html>");
......
290 294
        
291 295
      // delete server from server list
292 296
      } else if ( subaction.equals("delete") ) {
293
        String server = ((String[])params.get("server"))[0];
297
        server = ((String[])params.get("server"))[0];
294 298
        pstmt = dbConn.prepareStatement("DELETE FROM xml_replication " +
295 299
                                      "WHERE server LIKE '" + server + "'");
296 300
        pstmt.execute();
......
300 304
        response.setContentType("text/html");
301 305
        out.println("<html><body><table border=\"1\">");
302 306
        out.println("<tr><td><b>server</b></td><td><b>last_checked</b></td><td>");
303
        out.println("<b>replicate</b></td></tr>");
307
        out.println("<b>replicate</b></td>");
308
        out.println("<td><b>datareplicate</b></td>");
309
        out.println("<td><b>hub</b></td></tr>");
310
  
304 311
        pstmt = dbConn.prepareStatement("SELECT * FROM xml_replication");
305 312
        //increase dbconnection usage
306 313
        dbConn.increaseUsageCount(1);
......
311 318
        {
312 319
          out.println("<tr><td>" + rs.getString(2) + "</td><td>");
313 320
          out.println(rs.getString(3) + "</td><td>");
314
          out.println(rs.getString(4) + "</td></tr>");
321
          out.println(rs.getString(4) + "</td><td>");
322
          out.println(rs.getString(5) + "</td><td>");
323
          out.println(rs.getString(6) + "</td></tr>");
315 324
          tablehasrows = rs.next();
316 325
        }
317 326
        out.println("</table></body></html>");
......
321 330
        response.setContentType("text/html");
322 331
        out.println("<html><body><table border=\"1\">");
323 332
        out.println("<tr><td><b>server</b></td><td><b>last_checked</b></td><td>");
324
        out.println("<b>replicate</b></td></tr>");
333
        out.println("<b>replicate</b></td>");
334
        out.println("<td><b>datareplicate</b></td>");
335
        out.println("<td><b>hub</b></td></tr>");
325 336
        pstmt = dbConn.prepareStatement("SELECT * FROM xml_replication");
326 337
        pstmt.execute();
327 338
        ResultSet rs = pstmt.getResultSet();
......
329 340
        while(tablehasrows) {
330 341
          out.println("<tr><td>" + rs.getString(2) + "</td><td>");
331 342
          out.println(rs.getString(3) + "</td><td>");
332
          out.println(rs.getString(4) + "</td></tr>");
343
          out.println(rs.getString(4) + "</td><td>");
344
          out.println(rs.getString(5) + "</td><td>");
345
          out.println(rs.getString(6) + "</td></tr>");
333 346
          tablehasrows = rs.next();
334 347
        }
335 348
        out.println("</table></body></html>");
349
      } 
350
      else
351
      {
352
       
353
        out.println("<error>Unkonwn subaction</error>");
354
       
336 355
      }
337 356
      pstmt.close();
338 357
      //conn.close();
......
445 464
        //serverCode = MetacatReplication.getServerCode(server);
446 465
        //override = true; //we are now overriding the default action
447 466
      }
448
      MetacatReplication.replLog("force replication request from " + server); 
449
      util.debugMessage("Force replication docid: "+docid, 20);
450
      util.debugMessage("Force replication action: "+dbaction, 20);
451
      // sending back read request to server
452
     
467
      MetacatReplication.replLog("force replication request from " + server);
468
      MetaCatUtil.debugMessage("Force replication request from: "+ server, 34);
469
      MetaCatUtil.debugMessage("Force replication docid: "+docid, 34);
470
      MetaCatUtil.debugMessage("Force replication action: "+dbaction, 34);
471
      // sending back read request to remote server
453 472
      URL u = new URL("https://" + server + "?server="
454 473
                +util.getLocalReplicationServerName()
455 474
                +"&action=read&docid=" + docid);
......
467 486
      XMLReader docinfoParser = ReplicationHandler.initParser(dih);
468 487
      docinfoParser.parse(new InputSource(new StringReader(docInfoStr)));
469 488
      Hashtable docinfoHash = dih.getDocInfo();
489
      
490
      // Get user owner of this docid
470 491
      String user = (String)docinfoHash.get("user_owner");
471
      
492
      // Get home server of this docid
472 493
      String homeServer=(String)docinfoHash.get("home_server");
473
      util.debugMessage("homeServer: "+homeServer,20);
474
      // write the document here
475
      //Connection conn = util.openDBConnection();
494
      MetaCatUtil.debugMessage("homeServer: "+homeServer, 34);
495
    
496
      // Get DBConnection from pool
476 497
      dbConn=DBConnectionPool.
477 498
              getDBConnection("MetacatReplication.handleForceReplicateRequest");
478 499
      serialNumber=dbConn.getCheckOutSerialNumber();
500
      // write the document to local database
479 501
      DocumentImpl.writeReplication(dbConn, 
480 502
                  new StringReader(xmldoc), null, null, dbaction, docid, user, 
481
                  null, homeServer, false);
503
                  null, homeServer, false, server);
482 504
              
483
       
484
      //conn.close();
485

  
486 505
      MetacatReplication.replLog("document " + docid + " added to DB with " +
487 506
                                 "action " + dbaction);
488
    } catch(Exception e) {
507
    }//try
508
    catch(Exception e) 
509
    {
489 510
      
490 511
      System.out.println("ERROR in MetacatReplication.handleForceReplicate" +
491 512
                         "Request(): " + e.getMessage());
492 513
       e.printStackTrace();
493
    }
514
    }//catch
494 515
    finally
495 516
    {
517
      // Return the checked out DBConnection
496 518
      DBConnectionPool.returnDBConnection(dbConn, serialNumber);
497 519
    }//finally
498 520
  }
......
504 526
   */
505 527
  private void handleForceReplicateDataFileRequest(Hashtable params)
506 528
  {
507
    //check if the metacat was configured to handle replication 
508
    if (!(util.getOption("replicationacceptdata")).equals("on"))
509
    {
510
      return;
511
    }
529
    
512 530
    //make sure there is some parameters
513 531
    if(params.isEmpty())
514 532
    {
515 533
      return;
516 534
    }
517
    String server = ((String[])params.get("server"))[0]; // the server that
518
    //docid should include rev number
519
    String docid = ((String[])params.get("docid"))[0]; // sent the document
520
    if (docid==null)
535
    // Get remote server 
536
    String server = ((String[])params.get("server"))[0]; 
537
    // the docid should include rev number
538
    String docid = ((String[])params.get("docid"))[0]; 
539
    // Make sure there is a docid and server
540
    if (docid==null || server==null || server.equals(""))
521 541
    {
522
      util.debugMessage("Didn't specify docid for replication", 20);
542
      MetaCatUtil.debugMessage("Didn't specify docid or server for replication"
543
                              , 20);
523 544
      return;
524 545
    }
525
    //docid was switch to two parts
546
    
547
    // Overide or not
526 548
    boolean override = false;
527
    int serverCode = 1;
549
    // dbaction - update or insert
528 550
    String dbaction=null;
529
    //Connection conn=null;
530
    
551
   
531 552
    try 
532 553
    {
533
      //docid was switch to two parts
554
      //docid was switch to two parts uinque code and rev
534 555
      String uniqueCode=MetaCatUtil.getDocIdFromString(docid);
535 556
      int rev=MetaCatUtil.getVersionFromString(docid);
536 557
      if(params.containsKey("dbaction")) 
......
541 562
      {
542 563
        dbaction = "update";
543 564
      }
544
      //serverCode = MetacatReplication.getServerCode(server);
565
      
545 566
      MetacatReplication.replLog("force replication request from " + server); 
546
      
567
      MetaCatUtil.debugMessage("Force replication request from: "+ server, 34);
568
      MetaCatUtil.debugMessage("Force replication docid: "+docid, 34);
569
      MetaCatUtil.debugMessage("Force replication action: "+dbaction, 34);
547 570
      // get the document info from server
548 571
      URL docinfourl = new URL("https://" + server + 
549 572
                               "?server="+util.getLocalReplicationServerName()
......
563 586
      String docType = (String)docinfoHash.get("doctype");
564 587
      
565 588
      String docHomeServer= (String)docinfoHash.get("home_server");
566
      MetaCatUtil.debugMessage("docHomeServer of data file: "+docHomeServer);
589
      MetaCatUtil.debugMessage("docHomeServer of datafile: "+docHomeServer, 34);
567 590
      
568 591
      
569 592
      
......
586 609
        String datafilePath = util.getOption("datafilepath");
587 610
        //register data file into xml_documents table and wite data file
588 611
        //into file system
589
        DocumentImpl.writeDataFile(url.openStream(), datafilePath, docName,
590
                              docType, docid, user,docHomeServer);
612
        DocumentImpl.writeDataFileInReplication(url.openStream(), datafilePath, 
613
                            docName, docType, docid, user,docHomeServer,server);
591 614
     }
592 615
    
593 616
  
......
598 621
    {
599 622
      
600 623
      //util.returnConnection(conn);
601
      util.debugMessage("ERROR in MetacatReplication.handleForceReplicate" +
624
      util.debugMessage
625
              ("ERROR in MetacatReplication.handleForceDataFileReplicate" +
602 626
                         "Request(): " + e.getMessage(), 30);
603 627
    }
604 628
  }
......
612 636
  private void handleGetLockRequest(PrintWriter out, Hashtable params,
613 637
                                    HttpServletResponse response)
614 638
  {
615
    //Connection conn = null;
639

  
616 640
    try
617 641
    {
618
      /*conn = MetacatReplication.getDBConnection("MetacatReplication." +
619
                                                "handleGetLockRequest");*/
642
    
620 643
      String docid = ((String[])params.get("docid"))[0];
621 644
      String remoteRev = ((String[])params.get("updaterev"))[0];
622 645
      DocumentImpl requestDoc = new DocumentImpl(docid);
......
671 694
  {
672 695
    String docid = ((String[])(params.get("docid")))[0];
673 696
    StringBuffer sb = new StringBuffer();
674
    //Connection conn = null;
697
  
675 698
    try
676 699
    {
677
      /*conn = MetacatReplication.getDBConnection("MetacatReplication." +
678
                                              "handleGetDocumentInfoRequest");*/
700
      
679 701
      DocumentImpl doc = new DocumentImpl(docid);
680 702
      sb.append("<documentinfo><docid>").append(docid);
681 703
      sb.append("</docid><docname>").append(doc.getDocname());
......
692 714
      sb.append("</rev></documentinfo>");
693 715
      response.setContentType("text/xml");
694 716
      out.println(sb.toString());
695
      //conn.close();
717
      
696 718
    }
697 719
    catch (Exception e)
698 720
    {
......
710 732
                            Hashtable params, HttpServletResponse response)
711 733
                                 
712 734
  {
713
    //check the metacat was configured to handle data replication
714
    if (!(util.getOption("replicationsenddata")).equals("on"))
715
    {
716
      return;
717
    }
718
    
735
    // File path for data file    
719 736
    String filepath = util.getOption("datafilepath");
737
    // Request docid
720 738
    String docId = ((String[])(params.get("docid")))[0];
721 739
    //check if the doicd is null
722 740
    if (docId==null)
......
752 770
    {
753 771
          filepath += "/";
754 772
    }
755
    String filename = filepath + docId;      //MIME type
773
    // Get file aboslute file name
774
    String filename = filepath + docId;      
756 775
    
757
    String contentType = null;//getServletContext().getMimeType(filename);
776
    //MIME type
777
    String contentType = null;
778
    if (filename.endsWith(".xml")) 
779
    {
780
        contentType="text/xml";
781
    } 
782
    else if (filename.endsWith(".css")) 
783
    {
784
        contentType="text/css";
785
    } 
786
    else if (filename.endsWith(".dtd")) 
787
    {
788
        contentType="text/plain";
789
    } 
790
    else if (filename.endsWith(".xsd")) 
791
    {
792
        contentType="text/xml";
793
    } 
794
    else if (filename.endsWith("/")) 
795
    {
796
        contentType="text/html";
797
    } 
798
    else 
799
    {
800
        File f = new File(filename);
801
        if ( f.isDirectory() ) 
802
        {
803
           contentType="text/html";
804
        } 
805
        else
806
        {
807
           contentType="application/octet-stream";
808
        }
809
     }
758 810
   
759
    if (contentType == null) 
760
    {
761
       if (filename.endsWith(".xml")) 
762
       {
763
          contentType="text/xml";
764
       } 
765
       else if (filename.endsWith(".css")) 
766
       {
767
          contentType="text/css";
768
       } 
769
       else if (filename.endsWith(".dtd")) 
770
       {
771
          contentType="text/plain";
772
       } 
773
       else if (filename.endsWith(".xsd")) 
774
       {
775
          contentType="text/xml";
776
       } 
777
       else if (filename.endsWith("/")) 
778
       {
779
          contentType="text/html";
780
       } 
781
       else 
782
       {
783
          File f = new File(filename);
784
          if ( f.isDirectory() ) 
785
          {
786
             contentType="text/html";
787
          } 
788
          else
789
          {
790
             contentType="application/octet-stream";
791
          }
792
       }
793
   }
794
  
811
   // Set the mime type
795 812
   response.setContentType(contentType);
796
   // if we decide to use "application/octet-stream" for all data returns
797
   // response.setContentType("application/octet-stream");
813
   
814
   // Get the content of the file
798 815
   FileInputStream fin = null;
799 816
   try 
800 817
   {
801
      
818
      // FileInputStream to metacat
802 819
      fin = new FileInputStream(filename);
803
      
804
      byte[] buf = new byte[4 * 1024]; // 4K buffer
820
      // 4K buffer
821
      byte[] buf = new byte[4 * 1024];
822
      // Read data from file input stream to byte array
805 823
      int b = fin.read(buf);
824
      // Write to outStream from byte array
806 825
      while (b != -1) 
807 826
      {
808
        
809 827
        outPut.write(buf, 0, b);
810 828
        b = fin.read(buf);
811 829
      }
812
    
830
      // close file input stream
813 831
      fin.close();
814 832
   
815
   }  
833
   }//try
816 834
   catch(Exception e)
817 835
   {
818 836
      System.out.println("error getting data file from MetacatReplication." +
819 837
                         "handlGetDataFileRequest " + e.getMessage());
820 838
      e.printStackTrace(System.out);
821
   }
839
   }//catch
822 840
  
823 841
}
824 842
  
......
829 847
  private void handleGetDocumentRequest(PrintWriter out, Hashtable params, 
830 848
                                        HttpServletResponse response)
831 849
  {
832
    //Connection dbConn = null;
850
  
833 851
    try
834 852
    {
835 853
      //try to open a https stream to test if the request server's public key
......
844 862
      {
845 863
        response.setContentType("text/xml");
846 864
        out.println("<error>Couldn't pass the trust test</error>");
865
        out.close();
847 866
        return;
848 867
      }
849 868
      
850 869
      String docid = ((String[])(params.get("docid")))[0];
851
      /*conn = MetacatReplication.getDBConnection("MetacatReplication." +
852
                                                "handleGetDocumentRequest");*/
870
      
853 871
      DocumentImpl di = new DocumentImpl(docid);
854 872
      response.setContentType("text/xml");
855 873
      out.print(di.toString());
856
      //conn.close();
874
   
857 875
      MetacatReplication.replLog("document " + docid + " sent");
858 876
    
859 877
    }
......
863 881
                          +"handlGetDocumentRequest " + e.getMessage(), 30);
864 882
      //e.printStackTrace(System.out);
865 883
      response.setContentType("text/xml");
866
      out.println("<error>e.getMessage()</error>");
884
      out.println("<error>"+e.getMessage()+"</error>");
867 885
    }
868 886
    
869 887
  }
......
886 904
  private void handleUpdateRequest(PrintWriter out, Hashtable params, 
887 905
                                    HttpServletResponse response)
888 906
  {
907
    // Checked out DBConnection 
889 908
    DBConnection dbConn = null;
909
    // DBConenction serial number when checked it out
890 910
    int serialNumber = -1;
891 911
    PreparedStatement pstmt = null;
912
    // Server list to store server info of xml_replication table
913
    ReplicationServerList serverList = null;
914
  
892 915
    try
893 916
    {
917
      // Check out a DBConnection from pool
918
      dbConn=DBConnectionPool.
919
                  getDBConnection("MetacatReplication.handleUpdateRequest");
920
      serialNumber=dbConn.getCheckOutSerialNumber();
921
      // Create a server list from xml_replication table
922
      serverList = new ReplicationServerList();
923
      
924
      // Get remote server name from param
925
      String server = ((String[])params.get("server"))[0];
926
      // If no servr name in param, return a error
927
      if ( server == null || server.equals(""))
928
      {
929
        response.setContentType("text/xml");
930
        out.println("<error>Request didn't specify server name</error>");
931
        out.close();
932
        return;
933
      }//if
934
      
894 935
      //try to open a https stream to test if the request server's public key
895 936
      //in the key store, this is security issue
896
      String server = ((String[])params.get("server"))[0];
897 937
      URL u = new URL("https://" + server + "?server="
898 938
                +util.getLocalReplicationServerName()
899 939
                +"&action=test");
......
903 943
      {
904 944
        response.setContentType("text/xml");
905 945
        out.println("<error>Couldn't pass the trust test</error>");
946
        out.close();
906 947
        return;
907 948
      }
908 949
      
950
      
951
      // Check if local host configure to replicate xml documents to remote
952
      // server. If not send back a error message
953
      if (!serverList.getReplicationValue(server))
954
      {
955
        response.setContentType("text/xml");
956
        out.println
957
        ("<error>Configuration not allow to replicate document to you</error>");
958
        out.close();
959
        return;
960
      }//if
961
      
962
      // Store the sql command
909 963
      StringBuffer docsql = new StringBuffer();
964
      // Stroe the docid list
910 965
      StringBuffer doclist = new StringBuffer();
966
      // Store the deleted docid list
967
      StringBuffer delsql = new StringBuffer();
968
      // Store the data set file
911 969
      Vector packageFiles = new Vector();
912 970
      
913
      //get all docs that reside on this server
971
      // Append local server's name and replication servlet to doclist
914 972
      doclist.append("<?xml version=\"1.0\"?><replication>");
915 973
      doclist.append("<server>").append(util.getOption("server"));
916 974
      doclist.append(util.getOption("replicationpath"));
917 975
      doclist.append("</server><updates>");
918 976
      
977
      // Get correct docid that reside on this server according the requesting
978
      // server's replicate and data replicate value in xml_replication table
919 979
      docsql.append("select docid, rev, doctype from xml_documents ");
920
      if (!(util.getOption("hub")).equals("super"))
980
      // If the localhost is not a hub to the remote server, only replicate
981
      // the docid' which home server is local host (server_location =1)
982
      if (!serverList.getHubValue(server))
921 983
      {
922 984
        docsql.append("where server_location = 1");
923 985
      }
986
      MetaCatUtil.debugMessage("Doc sql: "+docsql.toString(), 30);
924 987
      
925
      //get any deleted documents
926
      StringBuffer delsql = new StringBuffer();
988
      // Get any deleted documents
927 989
      delsql.append("select distinct docid from ");
928 990
      delsql.append("xml_revisions where docid not in (select docid from ");
929 991
      delsql.append("xml_documents) ");
930
      if (!(util.getOption("hub")).equals("super"))
992
      // If the localhost is not a hub to the remote server, only replicate
993
      // the docid' which home server is local host (server_location =1)
994
      if (!serverList.getHubValue(server))
931 995
      {
932 996
        delsql.append("and server_location = 1");
933 997
      }
934
     
935
      /*conn = MetacatReplication.getDBConnection("MetacatReplication." +
936
                                                "handleUpdateRequest");*/
937
      dbConn=DBConnectionPool.
938
                  getDBConnection("MetacatReplication.handleUpdateRequest");
939
      serialNumber=dbConn.getCheckOutSerialNumber();
998
      MetaCatUtil.debugMessage("Deleted sql: "+delsql.toString(), 30);
999
      
1000
      
1001
      
1002
      // Get docid list of local host
940 1003
      pstmt = dbConn.prepareStatement(docsql.toString());
941 1004
      pstmt.execute();
942 1005
      ResultSet rs = pstmt.getResultSet();
943 1006
      boolean tablehasrows = rs.next();
944 1007
      //If metacat configed to replicate data file
945
      if ((util.getOption("replicationsenddata")).equals("on"))
1008
      //if ((util.getOption("replicationsenddata")).equals("on"))
1009
      if (serverList.getDataReplicationValue(server))
946 1010
      {
947 1011
        while(tablehasrows)
948 1012
        {
949 1013
          String recordDoctype = rs.getString(3);
950 1014
          Vector packagedoctypes = MetaCatUtil.getOptionList(
951 1015
                                     MetaCatUtil.getOption("packagedoctype"));
1016
          //if this is a package file, put it at the end
1017
          //because if a package file is read before all of the files it
1018
          //refers to are loaded then there is an error
952 1019
          if(!packagedoctypes.contains(recordDoctype))
953
          {   //if this is a package file, put it at the end
954
              //because if a package file is read before all of the files it
955
              //refers to are loaded then there is an error
1020
          {   
1021
              //If this is not data file
956 1022
              if (!recordDoctype.equals("BIN"))
957 1023
              {
958 1024
                //for non-data file document
......
961 1027
                doclist.append("</docid><rev>").append(rs.getInt(2));
962 1028
                doclist.append("</rev>");
963 1029
                doclist.append("</updatedDocument>");
964
              }
1030
              }//if
965 1031
              else
966 1032
              {
967 1033
                //for data file document, in datafile attributes
......
974 1040
                doclist.append(MetaCatUtil.getOption("datafileflag"));
975 1041
                doclist.append("</datafile>");
976 1042
                doclist.append("</updatedDocument>");
977
              } 
978
          }
1043
              }//else 
1044
          }//if packagedoctpes
979 1045
          else
980 1046
          { //the package files are saved to be put into the xml later.
981 1047
              Vector v = new Vector();
982 1048
              v.add(new String(rs.getString(1)));
983 1049
              v.add(new Integer(rs.getInt(2)));
984 1050
              packageFiles.add(new Vector(v));
985
          }
1051
          }//esle
986 1052
          tablehasrows = rs.next();
987 1053
        }//while
988 1054
      }//if
......
1058 1124
                         "handleupdaterequest: " + e.getMessage(), 30);
1059 1125
      //e.printStackTrace(System.out);
1060 1126
      response.setContentType("text/xml");
1061
      out.println("<error>e.getMessage()</error>");
1127
      out.println("<error>"+e.getMessage()+"</error>");
1062 1128
    }
1063 1129
    finally
1064 1130
    {
......
1076 1142
        DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1077 1143
      }//finally
1078 1144
    }//finally
1079
        
1080
                        
1081
      
1082
    
1083
  }
1145
 
1146
  }//handlUpdateRequest
1084 1147
  
1085 1148
  /**
1086 1149
   * Returns the xml_catalog table encoded in xml
......
1150 1213
      System.out.println("error in MetacatReplication.handleGetCatalogRequest:"+ 
1151 1214
                          e.getMessage());
1152 1215
      e.printStackTrace(System.out);
1216
      if(printFlag)
1217
      {
1218
        out.println("<error>"+e.getMessage()+"</error>");
1219
      }
1153 1220
    }
1154 1221
    finally
1155 1222
    {
......
1222 1289
   * @return the servername or null if the specified serverCode does not
1223 1290
   *         exist.
1224 1291
   */
1225
  public static String getServer(int serverCode)
1292
  public static String getServerNameForServerCode(int serverCode)
1226 1293
  {
1227 1294
    //System.out.println("serverid: " + serverCode);
1228 1295
    DBConnection dbConn = null;
......
1283 1350
   * @return integer > 0 representing the code of the server, 0 if the server
1284 1351
   *  does not exist.
1285 1352
   */
1286
  public static int getServerCode(String server) throws Exception
1353
  public static int getServerCodeForServerName(String server) throws Exception
1287 1354
  {
1288 1355
    DBConnection dbConn = null;
1289 1356
    int serialNumber = -1;
......
1333 1400
  }
1334 1401
  
1335 1402
  /**
1403
   * Method to get a host server information for given docid
1404
   * @param conn a connection to the database
1405
   */
1406
  public static Hashtable getHomeServerInfoForDocId(String docId)
1407
  {
1408
    Hashtable sl = new Hashtable();
1409
    DBConnection dbConn = null;
1410
    int serialNumber = -1;
1411
    //MetaCatUtil ut=new MetaCatUtil();
1412
    docId=MetaCatUtil.getDocIdFromString(docId);
1413
    PreparedStatement pstmt=null;
1414
    int serverLocation;
1415
    try
1416
    {
1417
      //get conection
1418
      dbConn=DBConnectionPool.
1419
                  getDBConnection("ReplicationHandler.getHomeServer");
1420
      serialNumber=dbConn.getCheckOutSerialNumber();
1421
      //get a server location from xml_document table
1422
      pstmt=dbConn.prepareStatement("select server_location from xml_documents "
1423
                                            +"where docid = ?");
1424
      pstmt.setString(1, docId);
1425
      pstmt.execute();
1426
      ResultSet serverName = pstmt.getResultSet();
1427
      //get a server location
1428
      if(serverName.next())
1429
      {
1430
        serverLocation=serverName.getInt(1);
1431
        pstmt.close();
1432
      }
1433
      else
1434
      {
1435
        pstmt.close();
1436
        //ut.returnConnection(conn);
1437
        return null;
1438
      }
1439
      pstmt=dbConn.prepareStatement("select server, last_checked, replicate " +
1440
                        "from xml_replication where serverid = ?");
1441
      //increase usage count
1442
      dbConn.increaseUsageCount(1);
1443
      pstmt.setInt(1, serverLocation);
1444
      pstmt.execute();
1445
      ResultSet rs = pstmt.getResultSet();
1446
      boolean tableHasRows = rs.next();
1447
      if (tableHasRows)
1448
      {
1449
        
1450
          String server = rs.getString(1);
1451
          String last_checked = rs.getString(2);
1452
          if(!server.equals("localhost"))
1453
          {
1454
            sl.put(server, last_checked);
1455
          }
1456
        
1457
      }
1458
      else
1459
      {
1460
        pstmt.close();
1461
        //ut.returnConnection(conn);
1462
        return null;
1463
      }
1464
      pstmt.close();
1465
    }
1466
    catch(Exception e)
1467
    {
1468
      System.out.println("error in replicationHandler.getHomeServer(): " +
1469
                         e.getMessage());
1470
    }
1471
    finally
1472
    {
1473
      try
1474
      {
1475
        pstmt.close();
1476
        //ut.returnConnection(conn);
1477
      }
1478
      catch (Exception ee)
1479
      {
1480
        MetaCatUtil.debugMessage("Eror irn rplicationHandler.getHomeServer() "+
1481
                          "to close pstmt: "+ee.getMessage(), 30);
1482
      }
1483
      finally
1484
      {
1485
        DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1486
      }
1487
    
1488
    }//finally
1489
    return sl;
1490
  }
1491
  
1492
  /**
1493
   * Returns a home server location  given a accnum
1494
   * @param accNum , given accNum for a document
1495
   * 
1496
   */
1497
  public static int getHomeServerCodeForDocId(String accNum) throws Exception
1498
  {
1499
    DBConnection dbConn = null;
1500
    int serialNumber = -1;
1501
    PreparedStatement pstmt = null;
1502
    int serverCode = 1;
1503
    //MetaCatUtil ut = new MetaCatUtil();
1504
    String docId=MetaCatUtil.getDocIdFromString(accNum);
1505

  
1506
    try 
1507
    {
1508

  
1509
      // Get DBConnection
1510
      dbConn=DBConnectionPool.
1511
                  getDBConnection("ReplicationHandler.getServerLocation");
1512
      serialNumber=dbConn.getCheckOutSerialNumber();
1513
      pstmt=dbConn.prepareStatement("SELECT server_location FROM xml_documents " 
1514
                              + "WHERE docid LIKE '" + docId + "'");
1515
      pstmt.execute();
1516
      ResultSet rs = pstmt.getResultSet();
1517
      boolean tablehasrows = rs.next();
1518
      //If a document is find, return the server location for it
1519
      if ( tablehasrows ) 
1520
      {  
1521
        serverCode = rs.getInt(1);
1522
        pstmt.close();
1523
        //conn.close();
1524
        return serverCode;
1525
      }
1526
      //if couldn't find in xml_documents table, we think server code is 1
1527
      //(this is new document)
1528
      else
1529
      {
1530
        pstmt.close();
1531
        //conn.close();
1532
        return serverCode;
1533
      }
1534
      
1535
    } 
1536
    catch(Exception e) 
1537
    {
1538
      
1539
      throw e;
1540

  
1541
    } 
1542
    finally 
1543
    {
1544
      try 
1545
      {
1546
        pstmt.close();
1547
        //conn.close();
1548
        
1549
      } 
1550
      catch(Exception ee) 
1551
      {
1552
        MetaCatUtil.debugMessage("Erorr in Replication.getServerLocation "+
1553
                     "to close pstmt"+ee.getMessage(), 30);
1554
      }
1555
      finally
1556
      {
1557
        DBConnectionPool.returnDBConnection(dbConn, serialNumber);
1558
      }//finally
1559
    }//finally
1560
   //return serverCode;
1561
  }
1562
  
1563
  
1564
  
1565
  /**
1336 1566
   * This method returns the content of a url
1337 1567
   * @param u the url to return the content from
1338 1568
   * @return a string representing the content of the url

Also available in: Unified diff