Project

General

Profile

« Previous | Next » 

Revision 533

Added by berkley about 24 years ago

can now download a document from a server

View differences:

src/edu/ucsb/nceas/metacat/ReplicationHandler.java
52 52
   */
53 53
  public void run()
54 54
  {
55
    System.out.println("replicationHandler is running");
56 55
    //find out the last_checked time of each server in the server list and
57 56
    //send a query to each server to see if there are any documents in 
58 57
    //xml_documents with an update_date > last_checked
......
60 59
    {
61 60
      conn = util.openDBConnection();
62 61
      serverList = buildServerList(conn);
63
      System.out.println("Server list: " + serverList.toString());
64 62
      update(serverList, conn);
63
      conn.close();
65 64
    }
66 65
    catch (Exception e)
67 66
    {
......
93 92
    String update;
94 93
    Vector responses = new Vector();
95 94
    ReplMessageHandler message = new ReplMessageHandler();
95
    Hashtable updateDocs = new Hashtable();
96
    URL u;
97
    InputStreamReader istream;
96 98
    
97 99
    try
98 100
    {
......
109 111
        
110 112
        update = update.replace(' ', '+');
111 113
        
112
        URL u = new URL("http://" + server + "?update=" + update);
113
        InputStreamReader istream = new InputStreamReader(u.openStream());
114
        u = new URL("http://" + server + "?update=" + update);
115
        istream = new InputStreamReader(u.openStream());
114 116
        while((istreamInt = istream.read()) != -1)
115 117
        {
116 118
          istreamChar = (char)istreamInt;
......
118 120
        }
119 121
        responses.add(serverResponse.toString()); //list of updates
120 122
      }
121
      //System.out.println("responses: " + responses.toString());
122 123

  
123 124
      //initialize the parser
124 125
      XMLReader parser = initParser(message);
......
129 130
                     (String)(responses.elementAt(i)))));
130 131
        Vector v = new Vector(message.getResultVect());
131 132
        for(int j=0; j<v.size(); j++)
132
        {
133
        { //go through each update vector and update or insert
134
          //a new document
133 135
          Vector w = new Vector((Vector)(v.elementAt(j)));
134
          System.out.print("param " + j + ": " + w.toString());
135
          //so now we have a list of the documents that need to be updated, so 
136
          //now we need to request them. from the server and update them here  
136
          String docid = (String)w.elementAt(0);
137
          String docServer = (String)w.elementAt(2);
138

  
139
          //send a message to the server requesting each document
140
          URL getDocURL = new URL("http://" + docServer + "?getdocument="+
141
                      docid);
142
          InputStreamReader getDocIstream = new InputStreamReader(
143
                                                getDocURL.openStream());
144

  
145
          //update the document into the DB
146
          String action = getAction(docid);
147
          String newDocid = DocumentImpl.write(conn, getDocIstream, action, 
148
                                               docid, null, null);
149
          //System.out.println("newDocid: " + newDocid);
137 150
        }
138
        System.out.println("");
139 151
      }
152
      //update the last_update field for each server to the current date/time
140 153
      
141 154
      
142 155
    }
......
144 157
    {
145 158
      System.out.println("Error in replicationHandler.update(): " + 
146 159
                         e.getMessage());
160
      e.printStackTrace(System.out);
147 161
    }
148 162
  }
149 163
  
150 164
  /**
165
   * Checks to see if a document is already in the DB.  Returns
166
   * "UPDATE" if it is, "INSERT" if it isn't
167
   */
168
  private static String getAction(String docid)
169
  {
170
    try
171
    {
172
      MetaCatUtil util = new MetaCatUtil();
173
      StringBuffer sql = new StringBuffer();
174
      sql.append("select docid from xml_documents where docid like '");
175
      sql.append(docid).append("'");
176
      Connection conn = util.openDBConnection();
177
      PreparedStatement pstmt = conn.prepareStatement(sql.toString());
178
      pstmt.execute();
179
      ResultSet rs = pstmt.getResultSet();
180

  
181
      if(rs.next())
182
      {
183
        conn.close();
184
        return "UPDATE";
185
      }
186
      else
187
      {
188
        conn.close();
189
        return "INSERT";
190
      }
191
    }
192
    catch(Exception e)
193
    {
194
      System.out.println("error in replicationHandler.getAction: " + 
195
                          e.getMessage());
196
    }
197
    return "";
198
  }
199
  
200
  /**
151 201
   * Method to initialize the message parser
152 202
   */
153 203
  private static XMLReader initParser(ReplMessageHandler rmh)

Also available in: Unified diff