Project

General

Profile

« Previous | Next » 

Revision 574

Added by berkley almost 24 years ago

replication on insert functionality is now working.

View differences:

src/edu/ucsb/nceas/metacat/MetacatReplication.java
21 21
import java.text.*;
22 22
import javax.servlet.*;
23 23
import javax.servlet.http.*;
24
import oracle.xml.parser.v2.*;
24

  
25 25
import org.xml.sax.*;
26 26

  
27 27
public class MetacatReplication extends HttpServlet implements Runnable
......
73 73
                               HttpServletResponse response) 
74 74
                               throws ServletException, IOException 
75 75
  {
76
System.out.println("in metacatreplication");
77 76
    PrintWriter out = response.getWriter();
78 77
    Hashtable params = new Hashtable();
79 78
    Enumeration paramlist = request.getParameterNames();
......
121 120
      }
122 121
      else if(((String[])params.get("action"))[0].equals("forcereplicate"))
123 122
      {
124
        String server = ((String[])params.get("server"))[0];
125
        int serverCheckCode = 0;
126
        try
127
        {
128
          serverCheckCode = MetacatReplication.getServerCode(server);
129
        }
130
        catch(Exception e)
131
        {
132
          System.out.println("error in metacatReplication.handleUpdateRequest"+
133
                             ": could not get server code");
134
        }
135
        replicationDaemon.schedule(new ReplicationHandler(out, serverCheckCode),
136
                                   2000);
123
        handleForceReplicateRequest(out, params, response);
137 124
      }
138 125
      else if(((String[])params.get("action"))[0].equals("update"))
139 126
      { //request an update list from the server
......
170 157
    }
171 158
  }
172 159
  
160
  private void handleForceReplicateRequest(PrintWriter out, Hashtable params,
161
                                           HttpServletResponse response)
162
  {
163
    System.out.println("in handleforcereplicaterequest");
164
    String server = ((String[])params.get("server"))[0];
165
    String docid = ((String[])params.get("docid"))[0];
166
    try
167
    {
168
      int serverCheckCode = MetacatReplication.getServerCode(server);
169
      URL u = new URL("http://" + server + "?action=read&docid=" + docid);
170
      System.out.println("sending message: " + u.toString());
171
      String xmldoc = MetacatReplication.getURLContent(u);
172
      URL docinfourl = new URL("http://" + server + 
173
                               "?action=getdocumentinfo&docid=" +
174
                               docid);
175
      System.out.println("sending message: " + docinfourl.toString());
176
      String docInfoStr = MetacatReplication.getURLContent(docinfourl);
177
      DocInfoHandler dih = new DocInfoHandler();
178
      XMLReader docinfoParser = ReplicationHandler.initParser(dih);
179
      docinfoParser.parse(new InputSource(new StringReader(docInfoStr)));
180
      Hashtable docinfoHash = dih.getDocInfo();
181
      String user = (String)docinfoHash.get("user_owner");
182
      String group = new String(user);
183
      Connection conn = util.openDBConnection();
184
      DocumentImpl.write(conn, new StringReader(xmldoc), "UPDATE", docid, user,
185
                         group, 1);
186
      conn.close();
187
    }
188
    catch(Exception e)
189
    {
190
      System.out.println("error in metacatReplication.handleForceReplicate" +
191
                         "Request: " + e.getMessage());
192
    }
193
  }
194
  
173 195
  /**
174 196
   * Grants or denies a lock to a requesting host.
175 197
   * The servlet parameters of interrest are:
......
180 202
  private void handleGetLockRequest(PrintWriter out, Hashtable params,
181 203
                                    HttpServletResponse response)
182 204
  {
205
    System.out.println("in handlegetlockrequest");
183 206
    java.util.Date remoteDate = new java.util.Date();
184 207
    java.util.Date localDate = new java.util.Date();
185 208
    try
src/edu/ucsb/nceas/metacat/DocumentImpl.java
737 737
                              String group, int serverCode )
738 738
                throws Exception 
739 739
  {
740
System.out.println("in write!!!!");
740
    System.out.println("in write");
741 741
    MetaCatUtil util = new MetaCatUtil();
742 742
        // Determine if the docid is OK for INSERT or UPDATE
743 743
    AccessionNumber ac = new AccessionNumber(conn);
......
759 759
      update = update.replace(' ', '+');
760 760
      URL u = new URL("http://" + server + "?action=getlock&updatedate=" + 
761 761
                      update + "&docid=" + docid);
762
      System.out.println("sending message: " + u.toString());
762 763
      String serverResStr = MetacatReplication.getURLContent(u);
763 764
      String openingtag = serverResStr.substring(0, serverResStr.indexOf(">")+1);
764 765
      
......
766 767
      {//the lock was granted go ahead with the insert
767 768
        try 
768 769
        {
769
          XMLReader parser = initializeParser(conn,action,newdocid,user,serverCode);
770
          System.out.println("in try");
771
          XMLReader parser = initializeParser(conn,action,newdocid,user,
772
                                              serverCode);
770 773
          conn.setAutoCommit(false);
771 774
          parser.parse(new InputSource(xml));
772 775
          conn.commit();
......
778 781
              stmt.execute("DELETE FROM xml_access WHERE docid='"+newdocid +"'");
779 782
              stmt.close();
780 783
            }
781
            AccessControlList aclobj = new AccessControlList(conn, newdocid, acl);
784
            AccessControlList aclobj = new AccessControlList(conn, newdocid,acl);
782 785
            conn.commit();
783 786
          } 
784 787
          conn.setAutoCommit(true);
......
795 798
        URL comeAndGetIt = new URL("http://" + server + 
796 799
                                   "?action=forcereplicate&server=" + 
797 800
                                   util.getOption("server") + 
798
                                   util.getOption("replicationpath"));
799
        System.out.println("documentimpl url: " + comeAndGetIt.toString());
801
                                   util.getOption("replicationpath") +
802
                                   "&docid=" + docid);
803
        System.out.println("sending message: " + comeAndGetIt.toString());
800 804
        String message = MetacatReplication.getURLContent(comeAndGetIt);
801 805
        
802 806
        if ( (docid != null) && !(newdocid.equals(docid)) ) 
......
827 831
                            "merge your changes and try again.");
828 832
      }
829 833
    }
834
    System.out.println("======THIS GOT TOO FAR=======");
830 835
    
831 836
    if ( action.equals("UPDATE") ) {
832 837
      // check for 'write' permission for 'user' to update this document
src/edu/ucsb/nceas/metacat/ReplicationHandler.java
173 173
            URL docinfoUrl = new URL("http://" + docServer + 
174 174
                                     "?action=getdocumentinfo&docid=" +
175 175
                                     docid);
176
            InputStreamReader isr = new InputStreamReader(
177
                                        docinfoUrl.openStream());
178
            StringBuffer docInfoBuffer = new StringBuffer();
179
            while((istreamInt = isr.read()) != -1)
180
            {
181
              istreamChar = (char)istreamInt;
182
              docInfoBuffer.append(istreamChar);
183
            }
184
            
185
            docinfoParser.parse(new InputSource(new StringReader(
186
                                                docInfoBuffer.toString())));
176
            String docInfoStr = MetacatReplication.getURLContent(docinfoUrl);
177
            docinfoParser.parse(new InputSource(new StringReader(docInfoStr)));
187 178
            Hashtable docinfoHash = dih.getDocInfo();
188 179
            String newDocid = DocumentImpl.write(conn, 
189 180
                               new StringReader(srvrResponseStr),
......
211 202
        server = (String)(keys.nextElement()); 
212 203
        URL dateurl = new URL("http://" + server + "?action=gettime");
213 204
        String datexml = MetacatReplication.getURLContent(dateurl);
214
        System.out.println("datexml: " + datexml);
215 205
        String datestr = datexml.substring(11, datexml.indexOf('<', 11));
216
        System.out.println("datestr: " + datestr);
217 206
        StringBuffer sql = new StringBuffer();
218 207
        sql.append("update xml_replication set last_checked = to_date('");
219 208
        sql.append(datestr).append("', 'YY-MM-DD HH24:MI:SS') where ");
......
272 261
  }
273 262
  
274 263
  /**
275
   * Method to initialize the message parser
276
   */
277
  private static XMLReader initParser(DefaultHandler dh)
278
          throws Exception
279
  {
280
    XMLReader parser = null;
281

  
282
    try {
283
      ContentHandler chandler = dh;
284

  
285
      // Get an instance of the parser
286
      MetaCatUtil util = new MetaCatUtil();
287
      String parserName = util.getOption("saxparser");
288
      parser = XMLReaderFactory.createXMLReader(parserName);
289

  
290
      // Turn off validation
291
      parser.setFeature("http://xml.org/sax/features/validation", false);
292
      
293
      parser.setContentHandler((ContentHandler)chandler);
294
      parser.setErrorHandler((ErrorHandler)chandler);
295

  
296
    } catch (Exception e) {
297
      throw e;
298
    }
299

  
300
    return parser;
301
  }
302
  
303
  /**
304 264
   * Method to query xml_replication and build a hashtable of each server
305 265
   * and it's last update time.
306 266
   * @param conn a connection to the database
......
334 294
    }
335 295
    return sl;
336 296
  }
297
  
298
  /**
299
   * Method to initialize the message parser
300
   */
301
  public static XMLReader initParser(DefaultHandler dh)
302
          throws Exception
303
  {
304
    XMLReader parser = null;
305

  
306
    try {
307
      ContentHandler chandler = dh;
308

  
309
      // Get an instance of the parser
310
      MetaCatUtil util = new MetaCatUtil();
311
      String parserName = util.getOption("saxparser");
312
      parser = XMLReaderFactory.createXMLReader(parserName);
313

  
314
      // Turn off validation
315
      parser.setFeature("http://xml.org/sax/features/validation", false);
316
      
317
      parser.setContentHandler((ContentHandler)chandler);
318
      parser.setErrorHandler((ErrorHandler)chandler);
319

  
320
    } catch (Exception e) {
321
      throw e;
322
    }
323

  
324
    return parser;
325
  }
337 326
}

Also available in: Unified diff