Project

General

Profile

« Previous | Next » 

Revision 1043

Added by Jing Tao almost 22 years ago

Add two methods getHomeServer and getServerLocation. Given a docid, getHomeServer can get the information of its home host. Given a docid, getServerLocation can return server code for it.

View differences:

src/edu/ucsb/nceas/metacat/ReplicationHandler.java
671 671
    }
672 672
    return sl;
673 673
  }
674
   /**
675
   * Method to get a host server information for given docid
676
   * @param conn a connection to the database
677
   */
678
  public static Hashtable getHomeServer(String docId)
679
  {
680
    Hashtable sl = new Hashtable();
681
    Connection conn = null;
682
    MetaCatUtil ut=new MetaCatUtil();
683
    PreparedStatement pstmt=null;
684
    int serverLocation;
685
    try
686
    {
687
      conn=ut.getConnection();
688
      //get a server location from xml_document table
689
      pstmt = conn.prepareStatement("select server_location where docid = ?");
690
      pstmt.setString(1, docId);
691
      pstmt.execute();
692
      ResultSet serverName = pstmt.getResultSet();
693
      //get a server location
694
      if(serverName.next())
695
      {
696
        serverLocation=serverName.getInt(1);
697
        pstmt.close();
698
      }
699
      else
700
      {
701
        pstmt.close();
702
        ut.returnConnection(conn);
703
        return null;
704
      }
705
      pstmt = conn.prepareStatement("select server, last_checked, replicate " +
706
                        "from xml_replication where serverid = ?");
707
      pstmt.setInt(1, serverLocation);
708
      pstmt.execute();
709
      ResultSet rs = pstmt.getResultSet();
710
      boolean tableHasRows = rs.next();
711
      if (tableHasRows)
712
      {
713
        
714
          String server = rs.getString(1);
715
          String last_checked = rs.getString(2);
716
          if(!server.equals("localhost"))
717
          {
718
            sl.put(server, last_checked);
719
          }
720
        
721
      }
722
      else
723
      {
724
        pstmt.close();
725
        ut.returnConnection(conn);
726
        return null;
727
      }
728
      pstmt.close();
729
    }
730
    catch(Exception e)
731
    {
732
      System.out.println("error in replicationHandler.buildServerList(): " +
733
                         e.getMessage());
734
    }
735
    finally
736
    {
737
      try
738
      {
739
        pstmt.close();
740
        ut.returnConnection(conn);
741
      }
742
      catch (Exception ee)
743
      {
744
      }
745
      
746
    }
747
    return sl;
748
  }
674 749
  
675 750
  /**
751
   * Returns a server location  given a accnum
752
   * @param accNum , given accNum for a document
753
   * 
754
   */
755
  public static int getServerLocation(String accNum) throws Exception
756
  {
757
    Connection conn = null;
758
    PreparedStatement pstmt = null;
759
    int serverCode = 1;
760
    MetaCatUtil ut = new MetaCatUtil();
761
    String docId=ut.getDocIdFromString(accNum);
762

  
763
    try 
764
    {
765

  
766
      conn = ut.openDBConnection();
767
      pstmt = conn.prepareStatement("SELECT server_location FROM xml_documents " 
768
                              + "WHERE docid LIKE '" + docId + "'");
769
      pstmt.execute();
770
      ResultSet rs = pstmt.getResultSet();
771
      boolean tablehasrows = rs.next();
772
      //If a document is find, return the server location for it
773
      if ( tablehasrows ) 
774
      {  
775
        serverCode = rs.getInt(1);
776
        pstmt.close();
777
        ut.returnConnection(conn);
778
        return serverCode;
779
      }
780
      //if couldn't find in xml_documents table, we think server code is 1
781
      //(this is new document)
782
      else
783
      {
784
        pstmt.close();
785
        ut.returnConnection(conn);
786
        return serverCode;
787
      }
788
      
789
    } 
790
    catch(Exception e) 
791
    {
792
      
793
      throw e;
794

  
795
    } 
796
    finally 
797
    {
798
      try 
799
      {
800
        pstmt.close();
801
        ut.returnConnection(conn);
802
        return serverCode;
803
      } 
804
      catch(Exception ee) 
805
      {
806
        
807
      }
808
    }//finally
809
  
810
  }
811
  
812
  
813
  
814
  /**
676 815
   * Method to initialize the message parser
677 816
   */
678 817
  public static XMLReader initParser(DefaultHandler dh)

Also available in: Unified diff