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: berkley $'
|
10
|
* '$Date: 2000-11-13 15:31:56 -0800 (Mon, 13 Nov 2000) $'
|
11
|
* '$Revision: 534 $'
|
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
|
if(params.containsKey("getdocument"))
|
121
|
{
|
122
|
handleGetDocumentRequest(out, params, response);
|
123
|
}
|
124
|
}
|
125
|
|
126
|
private void handleGetDocumentRequest(PrintWriter out, Hashtable params,
|
127
|
HttpServletResponse response)
|
128
|
{
|
129
|
try
|
130
|
{
|
131
|
String docid = ((String[])(params.get("getdocument")))[0];
|
132
|
System.out.println("incoming get request for document: " +
|
133
|
docid);
|
134
|
Connection conn = util.openDBConnection();
|
135
|
DocumentImpl di = new DocumentImpl(conn, docid);
|
136
|
response.setContentType("text/xml");
|
137
|
out.println(di.toString());
|
138
|
conn.close();
|
139
|
}
|
140
|
catch(Exception e)
|
141
|
{
|
142
|
System.out.println("error getting document: " + e.getMessage());
|
143
|
}
|
144
|
|
145
|
}
|
146
|
|
147
|
private void handleUpdateRequest(PrintWriter out, Hashtable params,
|
148
|
HttpServletResponse response)
|
149
|
{
|
150
|
System.out.println("incoming update request for dt/time " +
|
151
|
((String[])params.get("update"))[0] +
|
152
|
" from external metacat");
|
153
|
response.setContentType("text/xml");
|
154
|
out.println("<replication><textmessage>In metacatReplication</textmessage>");
|
155
|
|
156
|
StringBuffer sql = new StringBuffer();
|
157
|
StringBuffer returnXML = new StringBuffer();
|
158
|
String updateStr = ((String[])params.get("update"))[0];
|
159
|
updateStr = updateStr.replace('+', ' ');
|
160
|
//pseudo algorithm:
|
161
|
///////////////////////////////////////////////////////////////////////
|
162
|
//get the date/time from the requestor, query the db for any documents
|
163
|
//that have an update date later than the requested date and send
|
164
|
//those docids back to the requestor. If there are no newer documents
|
165
|
//then send back an up-to-date message.
|
166
|
///////////////////////////////////////////////////////////////////////
|
167
|
|
168
|
//Timestamp update = Timestamp.valueOf(updateStr);
|
169
|
SimpleDateFormat formatter = new SimpleDateFormat ("yy-MM-dd HH:mm:ss");
|
170
|
java.util.Date update = new java.util.Date();
|
171
|
ParsePosition pos = new ParsePosition(0);
|
172
|
update = formatter.parse(updateStr, pos);
|
173
|
String dateString = formatter.format(update);
|
174
|
sql.append("select docid, date_updated from xml_documents where ");
|
175
|
sql.append("date_updated > ");
|
176
|
sql.append("to_date('").append(dateString).append("','YY-MM-DD HH24:MI:SS')");
|
177
|
//System.out.println("sql: " + sql.toString());
|
178
|
|
179
|
try
|
180
|
{
|
181
|
Connection conn = util.openDBConnection();
|
182
|
PreparedStatement pstmt = conn.prepareStatement(sql.toString());
|
183
|
pstmt.execute();
|
184
|
ResultSet rs = pstmt.getResultSet();
|
185
|
boolean tablehasrows = rs.next();
|
186
|
returnXML.append("<server>").append(util.getOption("server"));
|
187
|
returnXML.append(util.getOption("replicationpath"));
|
188
|
returnXML.append("</server><updates>");
|
189
|
while(tablehasrows)
|
190
|
{
|
191
|
returnXML.append("<updatedDocument><docid>").append(rs.getString(1));
|
192
|
returnXML.append("</docid><date_updated>").append(rs.getString(2));
|
193
|
returnXML.append("</date_updated></updatedDocument>");
|
194
|
tablehasrows = rs.next();
|
195
|
}
|
196
|
returnXML.append("</updates></replication>");
|
197
|
conn.close();
|
198
|
out.print(returnXML.toString());
|
199
|
}
|
200
|
catch(Exception e)
|
201
|
{
|
202
|
System.out.println("Exception in metacatReplication: " + e.getMessage());
|
203
|
}
|
204
|
}
|
205
|
}
|