Project

General

Profile

« Previous | Next » 

Revision 569

Added by berkley about 24 years ago

cleaned up code, added new static methods to MetacatReplication for handling static queries and getting URL content.

View differences:

src/edu/ucsb/nceas/metacat/MetacatReplication.java
402 402
   */
403 403
  public static String getServer(int serverCode)
404 404
  {
405
    //System.out.println("serverid: " + serverCode);
405 406
    try
406 407
    {
407 408
      Connection conn = util.openDBConnection();
408
      PreparedStatement pstmt = conn.prepareStatement("select server from " +
409
      String sql = new String("select server from " +
409 410
                              "xml_replication where serverid = " + 
410 411
                              serverCode);
412
      PreparedStatement pstmt = conn.prepareStatement(sql);
413
      //System.out.println("getserver sql: " + sql);
411 414
      pstmt.execute();
412 415
      ResultSet rs = pstmt.getResultSet();
413 416
      boolean tablehasrows = rs.next();
414 417
      if(tablehasrows)
415 418
      {
416
        conn.close();
419
        //System.out.println("server: " + rs.getString(1));
417 420
        return rs.getString(1);
418 421
      }
419 422
      conn.close();
......
426 429
    return null;
427 430
      //return null if the server does not exist
428 431
  }
432
  
433
  /**
434
   * Returns a server code given a server name
435
   * @param server the name of the server
436
   * @return integer > 0 representing the code of the server, 0 if the server
437
   *  does not exist.
438
   */
439
  public static int getServerCode(String server) throws Exception
440
  {
441
    try
442
    {
443
      Connection conn = util.openDBConnection();
444
      PreparedStatement pstmt = conn.prepareStatement("select serverid from " +
445
                                         "xml_replication where server " +
446
                                         "like '" + server + "'");
447
      pstmt.execute();
448
      ResultSet rs = pstmt.getResultSet();
449
      boolean tablehasrows = rs.next();
450
      int serverCode = 0;
451
      if(tablehasrows)
452
      {
453
         return rs.getInt(1);
454
      }
455
      else
456
      {
457
        return 0;
458
      }
459
    }
460
    catch(Exception e)
461
    {
462
      throw e;
463
    }
464
  }
465
  
466
  /**
467
   * This method returns the content of a url
468
   * @param u the url to return the content from
469
   * @return a string representing the content of the url
470
   * @throws java.io.IOException
471
   */
472
  public static String getURLContent(URL u) throws java.io.IOException
473
  {
474
    //System.out.println("url: " + u.toString());
475
    char istreamChar;
476
    int istreamInt;
477
    InputStreamReader istream = new InputStreamReader(u.openStream());
478
    StringBuffer serverResponse = new StringBuffer();
479
    while((istreamInt = istream.read()) != -1)
480
    {
481
      istreamChar = (char)istreamInt;
482
      serverResponse.append(istreamChar);
483
    }
484
    
485
    return serverResponse.toString();
486
  }
429 487
}
src/edu/ucsb/nceas/metacat/DocumentImpl.java
727 727
                              String action, String docid, String user,
728 728
                              String group, int serverCode )
729 729
                throws Exception {
730
System.out.println("action: " + action + " servercode: " + 
731
                    serverCode);
732

  
730
    
731
    System.out.println("action: " + action + " servercode: " + 
732
                        serverCode);
733 733
    if(serverCode != 1 && action.equals("UPDATE"))
734 734
    { //if this document being written is not a resident of this server then
735 735
      //we need to try to get a lock from it's resident server.  If the
......
744 744
      update = update.replace(' ', '+');
745 745
      URL u = new URL("http://" + server + "?action=getlock&updatedate=" + 
746 746
                      update + "&docid=" + docid);
747
      //System.out.println("url: " + u.toString());
748
      InputStreamReader istream = new InputStreamReader(u.openStream());
749
      StringBuffer serverResponse = new StringBuffer();
750
      while((istreamInt = istream.read()) != -1)
751
      {
752
        istreamChar = (char)istreamInt;
753
        serverResponse.append(istreamChar);
754
      }
755
      
756
      String serverResStr = serverResponse.toString();
747
      String serverResStr = MetacatReplication.getURLContent(u);
757 748
      System.out.println("serverResStr: " + serverResStr);
758 749
      String openingtag = serverResStr.substring(0, serverResStr.indexOf(">")+1);
759 750
      //System.out.println("openingtag: " + openingtag);
src/edu/ucsb/nceas/metacat/ReplicationHandler.java
116 116
        update = update.replace(' ', '+'); 
117 117
        
118 118
        u = new URL("http://" + server + "?action=update&date=" + update);
119
        istream = new InputStreamReader(u.openStream());
120
        serverResponse = new StringBuffer();
121
        while((istreamInt = istream.read()) != -1)
122
        {
123
          istreamChar = (char)istreamInt;
124
          serverResponse.append(istreamChar);
125
        }
126
        responses.add(serverResponse.toString()); //list of updates
119
        responses.add(MetacatReplication.getURLContent(u)); //list of updates
127 120
      }
128 121

  
129 122
      //initialize the parser
......
147 140
          //send a message to the server requesting each document
148 141
          URL getDocURL = new URL("http://" + docServer + 
149 142
                                  "?action=read&docid="+ docid);
150
          InputStreamReader getDocIstream = new InputStreamReader(
151
                                                getDocURL.openStream()); 
152

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

  
192 156
            DocInfoHandler dih = new DocInfoHandler();
193 157
            XMLReader docinfoParser = initParser(dih);
194 158
            URL docinfoUrl = new URL("http://" + docServer + 
......
207 171
                                                docInfoBuffer.toString())));
208 172
            Hashtable docinfoHash = dih.getDocInfo();
209 173
            String newDocid = DocumentImpl.write(conn, 
210
                               new StringReader(serverResponse.toString())
211
                               /*getDocIstream*/, 
174
                               new StringReader(srvrResponseStr),
212 175
                               action, 
213 176
                               docid, (String)docinfoHash.get("user_owner"), 
214 177
                               (String)docinfoHash.get("user_owner"), 

Also available in: Unified diff