Revision 521
Added by berkley about 24 years ago
src/edu/ucsb/nceas/metacat/metacatReplication.java | ||
---|---|---|
1 |
/** |
|
2 |
* '$RCSfile$' |
|
3 |
* Purpose: A Class that implements replication for metacat |
|
4 |
* Copyright: 2000 Regents of the University of California and the |
|
5 |
* National Center for Ecological Analysis and Synthesis |
|
6 |
* Authors: Chad Berkley |
|
7 |
* Release: @release@ |
|
8 |
* |
|
9 |
* '$Author$' |
|
10 |
* '$Date$' |
|
11 |
* '$Revision$' |
|
12 |
*/ |
|
13 |
|
|
14 |
package edu.ucsb.nceas.metacat; |
|
15 |
|
|
16 |
import java.util.*; |
|
17 |
import java.io.*; |
|
18 |
import java.sql.*; |
|
19 |
import java.net.*; |
|
20 |
import java.lang.*; |
|
21 |
import java.text.*; |
|
22 |
import javax.servlet.*; |
|
23 |
import javax.servlet.http.*; |
|
24 |
import oracle.xml.parser.v2.*; |
|
25 |
import org.xml.sax.*; |
|
26 |
|
|
27 |
public class metacatReplication extends HttpServlet |
|
28 |
{ |
|
29 |
private String deltaT; |
|
30 |
Timer replicationDaemon; |
|
31 |
MetaCatUtil util = new MetaCatUtil(); |
|
32 |
|
|
33 |
/** |
|
34 |
* Initialize the servlet by creating appropriate database connections |
|
35 |
*/ |
|
36 |
public void init(ServletConfig config) throws ServletException |
|
37 |
{ |
|
38 |
//initialize db connections to handle any update requests |
|
39 |
MetaCatUtil util = new MetaCatUtil(); |
|
40 |
deltaT = util.getOption("deltaT"); |
|
41 |
//break off a thread to do the delta-T check |
|
42 |
replicationDaemon = new Timer(true); |
|
43 |
//replicationDaemon.scheduleAtFixedRate(new replicationHandler(), 0, |
|
44 |
// new Integer(deltaT).intValue() * 1000); |
|
45 |
//System.out.println("timer scheduled at: " + new Integer(deltaT).intValue() |
|
46 |
// + " seconds"); |
|
47 |
|
|
48 |
} |
|
49 |
|
|
50 |
public void destroy() |
|
51 |
{ |
|
52 |
replicationDaemon.cancel(); |
|
53 |
System.out.println("Replication daemon cancelled."); |
|
54 |
} |
|
55 |
|
|
56 |
public void doGet (HttpServletRequest request, HttpServletResponse response) |
|
57 |
throws ServletException, IOException |
|
58 |
{ |
|
59 |
// Process the data and send back the response |
|
60 |
handleGetOrPost(request, response); |
|
61 |
} |
|
62 |
|
|
63 |
public void doPost(HttpServletRequest request, HttpServletResponse response) |
|
64 |
throws ServletException, IOException |
|
65 |
{ |
|
66 |
// Process the data and send back the response |
|
67 |
handleGetOrPost(request, response); |
|
68 |
} |
|
69 |
|
|
70 |
private void handleGetOrPost(HttpServletRequest request, |
|
71 |
HttpServletResponse response) |
|
72 |
throws ServletException, IOException |
|
73 |
{ |
|
74 |
PrintWriter out = response.getWriter(); |
|
75 |
|
|
76 |
Hashtable params = new Hashtable(); |
|
77 |
Enumeration paramlist = request.getParameterNames(); |
|
78 |
|
|
79 |
while (paramlist.hasMoreElements()) |
|
80 |
{ |
|
81 |
String name = (String)paramlist.nextElement(); |
|
82 |
String[] value = request.getParameterValues(name); |
|
83 |
params.put(name, value); |
|
84 |
} |
|
85 |
|
|
86 |
if(params.containsKey("stop")) |
|
87 |
{ |
|
88 |
if(((String[])params.get("stop"))[0].equals("true")) |
|
89 |
{ |
|
90 |
replicationDaemon.cancel(); |
|
91 |
out.println("Replication Handler Stopped"); |
|
92 |
} |
|
93 |
} |
|
94 |
|
|
95 |
if(params.containsKey("start")) |
|
96 |
{ |
|
97 |
if(((String[])params.get("start"))[0].equals("true")) |
|
98 |
{ |
|
99 |
int rate; |
|
100 |
if(params.containsKey("rate")) |
|
101 |
rate = new Integer( |
|
102 |
new String(((String[])params.get("rate"))[0])).intValue(); |
|
103 |
else |
|
104 |
rate = 1000; |
|
105 |
|
|
106 |
out.println("New rate is: " + rate + " seconds."); |
|
107 |
replicationDaemon.cancel(); |
|
108 |
replicationDaemon = new Timer(true); |
|
109 |
replicationDaemon.scheduleAtFixedRate(new replicationHandler(out), 0, |
|
110 |
rate * 1000); |
|
111 |
out.println("Replication Handler Started"); |
|
112 |
} |
|
113 |
} |
|
114 |
|
|
115 |
if(params.containsKey("update")) |
|
116 |
{ |
|
117 |
handleUpdateRequest(out, params, response); |
|
118 |
} |
|
119 |
} |
|
120 |
|
|
121 |
private void handleUpdateRequest(PrintWriter out, Hashtable params, |
|
122 |
HttpServletResponse response) |
|
123 |
{ |
|
124 |
System.out.println("incoming request for dt/time " + |
|
125 |
((String[])params.get("update"))[0] + |
|
126 |
" from external metacat"); |
|
127 |
response.setContentType("text/xml"); |
|
128 |
out.println("<replication><textmessage>In metacatReplication</textmessage>"); |
|
129 |
|
|
130 |
StringBuffer sql = new StringBuffer(); |
|
131 |
StringBuffer returnXML = new StringBuffer(); |
|
132 |
String updateStr = ((String[])params.get("update"))[0]; |
|
133 |
updateStr = updateStr.replace('+', ' '); |
|
134 |
//pseudo algorithm: |
|
135 |
/////////////////////////////////////////////////////////////////////// |
|
136 |
//get the date/time from the requestor, query the db for any documents |
|
137 |
//that have an update date later than the requested date and send |
|
138 |
//those docids back to the requestor. If there are no newer documents |
|
139 |
//then send back an up-to-date message. |
|
140 |
/////////////////////////////////////////////////////////////////////// |
|
141 |
|
|
142 |
//Timestamp update = Timestamp.valueOf(updateStr); |
|
143 |
SimpleDateFormat formatter = new SimpleDateFormat ("yy-MM-dd HH:mm:ss"); |
|
144 |
java.util.Date update = new java.util.Date(); |
|
145 |
ParsePosition pos = new ParsePosition(0); |
|
146 |
update = formatter.parse(updateStr, pos); |
|
147 |
String dateString = formatter.format(update); |
|
148 |
sql.append("select docid, date_updated from xml_documents where "); |
|
149 |
sql.append("date_updated > "); |
|
150 |
sql.append("to_date('").append(dateString).append("','YY-MM-DD HH24:MI:SS')"); |
|
151 |
//System.out.println("sql: " + sql.toString()); |
|
152 |
|
|
153 |
try |
|
154 |
{ |
|
155 |
Connection conn = util.openDBConnection(); |
|
156 |
PreparedStatement pstmt = conn.prepareStatement(sql.toString()); |
|
157 |
pstmt.execute(); |
|
158 |
ResultSet rs = pstmt.getResultSet(); |
|
159 |
boolean tablehasrows = rs.next(); |
|
160 |
returnXML.append("<server>").append(util.getOption("server")); |
|
161 |
returnXML.append("</server><updates>"); |
|
162 |
while(tablehasrows) |
|
163 |
{ |
|
164 |
returnXML.append("<updatedDocument><docid>").append(rs.getString(1)); |
|
165 |
returnXML.append("</docid><date_updated>").append(rs.getString(2)); |
|
166 |
returnXML.append("</date_updated></updatedDocument>"); |
|
167 |
tablehasrows = rs.next(); |
|
168 |
} |
|
169 |
returnXML.append("</updates></replication>"); |
|
170 |
out.print(returnXML.toString()); |
|
171 |
} |
|
172 |
catch(Exception e) |
|
173 |
{ |
|
174 |
System.out.println("Exception in metacatReplication: " + e.getMessage()); |
|
175 |
} |
|
176 |
} |
|
177 |
} |
|
0 | 178 |
Also available in: Unified diff
servlet to handle metacat replication requests.