Project

General

Profile

« Previous | Next » 

Revision 549

Added by berkley about 24 years ago

removed dependence on a server code in the accession number of each document. the file's resident server is now located in xml_documents.server_location.

View differences:

src/edu/ucsb/nceas/metacat/MetacatReplication.java
94 94
      { //start the replication server
95 95
        int rate;
96 96
        if(params.containsKey("rate"))
97
        {
97 98
          rate = new Integer(
98 99
                 new String(((String[])params.get("rate"))[0])).intValue();
100
          if(rate < 30)
101
          {
102
            out.println("Replication deltaT rate cannot be less than 30!");
103
            rate = 1000;
104
          }
105
        }
99 106
        else
107
        {
100 108
          rate = 1000;
101

  
109
        }
110
        
102 111
        out.println("New rate is: " + rate + " seconds.");
103 112
        replicationDaemon.cancel();
104 113
        replicationDaemon = new Timer(true);
......
147 156
                       ((String[])params.get("date"))[0] +
148 157
                       " from external metacat");
149 158
    response.setContentType("text/xml");
150
    out.println("<replication><textmessage>In metacatReplication</textmessage>");
159
    StringBuffer returnXML = new StringBuffer();
160
    returnXML.append("<?xml version=\"1.0\"?><replication>");
151 161
    
152 162
    StringBuffer sql = new StringBuffer();
153
    StringBuffer returnXML = new StringBuffer();
154 163
    String updateStr = ((String[])params.get("date"))[0];
155 164
    updateStr = updateStr.replace('+', ' ');
156 165
    //pseudo algorithm:
......
168 177
    update = formatter.parse(updateStr, pos);
169 178
    //update = new java.util.Date(update.getTime() - 10000);
170 179
    String dateString = formatter.format(update);
171
    sql.append("select docid, date_updated from xml_documents where ");
172
    sql.append("date_updated > ");
180
    sql.append("select docid, date_updated, server_location from ");
181
    sql.append("xml_documents where date_updated > ");
173 182
    sql.append("to_date('").append(dateString).append("','YY-MM-DD HH24:MI:SS')");
174 183
    //System.out.println("sql: " + sql.toString());
175 184
    
......
180 189
      pstmt.execute();
181 190
      ResultSet rs = pstmt.getResultSet();
182 191
      boolean tablehasrows = rs.next();
183
      String serverCode = util.getOption("servercode");
192
      
193
      //if a '1' should not represent localhost, add code here to query
194
      //xml_replication for the proper serverid number
195
      
184 196
      returnXML.append("<server>").append(util.getOption("server"));
185 197
      returnXML.append(util.getOption("replicationpath"));
186 198
      returnXML.append("</server><updates>");
......
188 200
      {
189 201
        String docid = rs.getString(1);
190 202
        String dateUpdated = rs.getString(2);
191
        int dotIndex = docid.indexOf(".");
192
        if(docid.substring(0, dotIndex).equals(serverCode))
203
        int serverCode = rs.getInt(3);
204
        if(serverCode == 1)
193 205
        { //check that this document is from this server.
194 206
          //servers only replicate their own documents!
195 207
          returnXML.append("<updatedDocument><docid>").append(docid);
......
200 212
      }
201 213
      returnXML.append("</updates></replication>");
202 214
      conn.close();
215
      //System.out.println(returnXML.toString());
203 216
      out.print(returnXML.toString());
204 217
    }
205 218
    catch(Exception e)
src/edu/ucsb/nceas/metacat/DBSAXHandler.java
50 50
   private String   user = null;
51 51
   private Thread   xmlIndex;
52 52
   private boolean endDocument = false;
53
   private int serverCode = 1;
53 54

  
54 55
   private static final int MAXDATACHARS = 4000;
55 56
   private static final int MAXTITLELEN = 1000;
......
71 72
       stackCreated = true;
72 73
     }
73 74
   }
75
   
76
   public DBSAXHandler(Connection conn,String action,String docid,String user,
77
                       int serverCode)
78
   {
79
     this(conn);
80
     this.action = action;
81
     this.docid = docid;
82
     this.user = user;
83
     this.xmlIndex = new Thread(this);
84
     this.serverCode = serverCode;
85
   }
74 86
 
75 87
   /** Construct an instance of the handler class 
76 88
    *
......
147 159
       rootNode.writeNodename(docname);
148 160
       try {
149 161
         currentDocument = new DocumentImpl(conn, rootNode.getNodeID(), 
150
                                       docname, doctype, docid, action, user);
162
                                       docname, doctype, docid, action, user, 
163
                                       this.serverCode);
151 164
       } catch (Exception ane) {
152 165
         throw (new SAXException("Error with " + action, ane));
153 166
       }
src/edu/ucsb/nceas/metacat/DocumentImpl.java
108 108
   *
109 109
   */
110 110
  public DocumentImpl(Connection conn, long rootnodeid, String docname, 
111
                      String doctype, String docid, String action, String user,
112
                      int serverCode)
113
                      throws SQLException, Exception
114
  {
115
    this.conn = conn;
116
    this.rootnodeid = rootnodeid;
117
    this.docname = docname;
118
    this.doctype = doctype;
119
    this.docid = docid;
120
    writeDocumentToDB(action, user, serverCode);
121
  }
122
  
123
  public DocumentImpl(Connection conn, long rootnodeid, String docname, 
111 124
                      String doctype, String docid, String action, String user)
112 125
                      throws SQLException, Exception
113 126
  {
......
467 480
      throw new McdbException("Error getting node data: " + docid);
468 481
    }
469 482
  }
483
  
484
  /** creates SQL code and inserts new document into DB connection 
485
   default serverCode of 1*/
486
  private void writeDocumentToDB(String action, String user)
487
               throws SQLException, Exception
488
  {
489
    writeDocumentToDB(action, user, 1);
490
  }
470 491

  
471 492
 /** creates SQL code and inserts new document into DB connection */
472
  private void writeDocumentToDB(String action, String user) 
493
  private void writeDocumentToDB(String action, String user, int serverCode) 
473 494
               throws SQLException, Exception {
474 495
    try {
475 496
      PreparedStatement pstmt = null;
......
479 500
        //this.docid = ac.generate(docid, "INSERT");
480 501
        pstmt = conn.prepareStatement(
481 502
            "INSERT INTO xml_documents " +
482
            "(docid, rootnodeid, docname, doctype, " +
483
            "user_owner, user_updated, date_created, date_updated) " +
484
            "VALUES (?, ?, ?, ?, ?, ?, sysdate, sysdate)");
503
            "(docid, rootnodeid, docname, doctype, user_owner, " +
504
            "user_updated, date_created, date_updated, server_location) " +
505
            "VALUES (?, ?, ?, ?, ?, ?, sysdate, sysdate, ?)");
506
        //note that the server_location is set to 1. 
507
        //this means that "localhost" in the xml_replication table must
508
        //always be the first entry!!!!!
509
        
485 510
        // Bind the values to the query
486 511
        pstmt.setString(1, this.docid);
487 512
        pstmt.setLong(2, rootnodeid);
......
489 514
        pstmt.setString(4, doctype);
490 515
        pstmt.setString(5, user);
491 516
        pstmt.setString(6, user);
517
        pstmt.setInt(7, serverCode);
492 518
      } else if (action.equals("UPDATE")) {
493 519

  
494 520
        // Save the old document entry in a backup table
......
505 531
        pstmt = conn.prepareStatement(
506 532
            "UPDATE xml_documents " +
507 533
            "SET rootnodeid = ?, docname = ?, doctype = ?, " +
508
            "user_updated = ?, date_updated = sysdate WHERE docid LIKE ?");
534
            "user_updated = ?, date_updated = sysdate, " +
535
            "server_location = ? WHERE docid LIKE ?");
509 536
        // Bind the values to the query
510 537
        pstmt.setLong(1, rootnodeid);
511 538
        pstmt.setString(2, docname);
512 539
        pstmt.setString(3, doctype);
513 540
        pstmt.setString(4, user);
514
        pstmt.setString(5, this.docid);
541
        pstmt.setInt(5, serverCode);
542
        pstmt.setString(6, this.docid);
515 543
      } else {
516 544
        System.err.println("Action not supported: " + action);
517 545
      }
......
639 667
                  action, docid, user, group);
640 668
  }
641 669
  
670
  public static String write(Connection conn, Reader xml, String action, 
671
                              String docid, String user, String group)
672
                throws Exception {
673
    return write(conn, xml, action, docid, user, group, 1);
674
  }
675
  
642 676
  /**
643 677
   * Write an XML file to the database, given a Reader
644 678
   *
......
648 682
   * @param docid the docid to use for the INSERT OR UPDATE
649 683
   */
650 684
  public static String write( Connection conn, Reader xml, String action, 
651
                              String docid, String user, String group )
685
                              String docid, String user, String group, int serverCode )
652 686
                throws Exception {
653 687

  
654 688
    // Determine if the docid is OK for INSERT or UPDATE
......
664 698
    }
665 699

  
666 700
    try {
667
        XMLReader parser = initializeParser(conn, action, newdocid, user);
701
        XMLReader parser = initializeParser(conn, action, newdocid, user, serverCode);
668 702
        conn.setAutoCommit(false);
669 703
        parser.parse(new InputSource(xml));
670 704
        conn.commit();
......
826 860
   * Set up the parser handlers for writing the document to the database
827 861
   */
828 862
  private static XMLReader initializeParser(Connection conn,
829
                           String action, String docid, String user) 
863
                           String action, String docid, String user, int serverCode) 
830 864
                           throws Exception {
831 865
    XMLReader parser = null;
832 866
    //
833 867
    // Set up the SAX document handlers for parsing
834 868
    //
835 869
    try {
836
      ContentHandler chandler   = new DBSAXHandler(conn, action, docid, user);
870
      ContentHandler chandler   = new DBSAXHandler(conn, action, docid, user, serverCode);
837 871
      EntityResolver dbresolver = new DBEntityResolver(conn, 
838 872
                                      (DBSAXHandler)chandler);
839 873
      DTDHandler dtdhandler     = new DBDTDHandler(conn);
src/edu/ucsb/nceas/metacat/ReplicationHandler.java
110 110
        //send the server a date and it will send back any docid that has 
111 111
        //been modified after that date
112 112
        
113
        update = update.replace(' ', '+');
113
        update = update.replace(' ', '+'); 
114 114
        
115 115
        u = new URL("http://" + server + "?action=update&date=" + update);
116 116
        istream = new InputStreamReader(u.openStream());
117
        serverResponse = new StringBuffer();
117 118
        while((istreamInt = istream.read()) != -1)
118 119
        {
119 120
          istreamChar = (char)istreamInt;
......
143 144
          URL getDocURL = new URL("http://" + docServer + 
144 145
                                  "?action=getdocument&docid="+ docid);
145 146
          InputStreamReader getDocIstream = new InputStreamReader(
146
                                                getDocURL.openStream());
147
                                                getDocURL.openStream()); 
147 148

  
148
                                                //the following while loop should not be needed.  see the note
149
          //the following while loop should not be needed.  see the note
149 150
          //below before the DocumentImpl.write() call.
150 151
          serverResponse = new StringBuffer();
151 152
          while((istreamInt = getDocIstream.read()) != -1)
......
157 158
          //System.out.println(serverResponse.toString());
158 159
          //System.out.println("<<<<<<end document>>>>>");
159 160
          
160
          //update the document into the DB
161
          String action = getAction(docid);
162
          //System.out.println("action: " + action + " docid: " + docid);
161
          pstmt = conn.prepareStatement("select serverid from " +
162
                                         "xml_replication where server " +
163
                                         "like '" + docServer + "'");
164
          pstmt.execute();
165
          ResultSet rs = pstmt.getResultSet();
166
          boolean tablehasrows = rs.next();
167
          int serverCode = 0;
168
          if(tablehasrows)
169
          {
170
            serverCode = rs.getInt(1);
171
            System.out.println("servercode: " + serverCode);
172
          }
173
          else
174
          {
175
            System.out.println("error: server not registered");
176
          }
163 177
          
164
          //note that getDocIstream is commented out below.  This should
165
          //work as a param to this method but it doesn'.  I don't know why
166
          //but putting a string reader there works but not an 
167
          //inputStreamReader.
168
          String newDocid = DocumentImpl.write(conn, 
178
          if(serverCode != 0)
179
          {
180
            //update the document into the DB
181
            String action = getAction(docid);
182
            //System.out.println("action: " + action + " docid: " + docid);
183
          
184
            //note that getDocIstream is commented out below.  This should
185
            //work as a param to this method but it doesn'.  I don't know why
186
            //but putting a string reader there works but not an 
187
            //inputStreamReader.
188
            String newDocid = DocumentImpl.write(conn, 
169 189
                                               new StringReader(serverResponse.toString())
170 190
                                               /*getDocIstream*/, 
171 191
                                               action, 
172
                                               docid, null, null);
173
          System.out.println("newDocid: " + newDocid + " " + action + "ED");
192
                                               docid, null, null, serverCode);
193
            System.out.println("newDocid: " + newDocid + " " + action + "ED");
194
          }
174 195
        }
175 196
      }
176

  
197
/*
177 198
      //update the last_update field for each server to the current date/time      
178 199
      SimpleDateFormat formatter = new SimpleDateFormat ("yy-MM-dd HH:mm:ss");
179 200
      java.util.Date newDate = new java.util.Date(System.currentTimeMillis());
......
188 209
      pstmt.executeUpdate();
189 210
      //conn.commit();
190 211
      System.out.println("last_checked updated: " + dateString);
191
      
212
*/      
192 213
    }
193 214
    catch(Exception e)
194 215
    {
......
281 302
      boolean tableHasRows = rs.next();
282 303
      while(tableHasRows)
283 304
      {
284
        sl.put(rs.getString(1), rs.getString(2));
305
        String server = rs.getString(1);
306
        String last_checked = rs.getString(2);
307
        if(!server.equals("localhost"))
308
        {
309
          sl.put(server, last_checked);
310
        }
285 311
        tableHasRows = rs.next();
286 312
      }
287 313
    }

Also available in: Unified diff