1 |
522
|
berkley
|
/**
|
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 |
|
|
*
|
8 |
|
|
* '$Author$'
|
9 |
|
|
* '$Date$'
|
10 |
|
|
* '$Revision$'
|
11 |
669
|
jones
|
*
|
12 |
|
|
* This program is free software; you can redistribute it and/or modify
|
13 |
|
|
* it under the terms of the GNU General Public License as published by
|
14 |
|
|
* the Free Software Foundation; either version 2 of the License, or
|
15 |
|
|
* (at your option) any later version.
|
16 |
|
|
*
|
17 |
|
|
* This program is distributed in the hope that it will be useful,
|
18 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
19 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
20 |
|
|
* GNU General Public License for more details.
|
21 |
|
|
*
|
22 |
|
|
* You should have received a copy of the GNU General Public License
|
23 |
|
|
* along with this program; if not, write to the Free Software
|
24 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
25 |
522
|
berkley
|
*/
|
26 |
|
|
|
27 |
5014
|
daigle
|
package edu.ucsb.nceas.metacat.replication;
|
28 |
522
|
berkley
|
|
29 |
5755
|
leinfelder
|
import java.io.BufferedInputStream;
|
30 |
|
|
import java.io.BufferedOutputStream;
|
31 |
|
|
import java.io.File;
|
32 |
|
|
import java.io.FileInputStream;
|
33 |
|
|
import java.io.FileNotFoundException;
|
34 |
|
|
import java.io.FileOutputStream;
|
35 |
|
|
import java.io.IOException;
|
36 |
|
|
import java.io.InputStream;
|
37 |
|
|
import java.io.InputStreamReader;
|
38 |
|
|
import java.io.OutputStream;
|
39 |
|
|
import java.io.StringReader;
|
40 |
|
|
import java.io.Writer;
|
41 |
|
|
import java.net.MalformedURLException;
|
42 |
|
|
import java.net.URL;
|
43 |
|
|
import java.sql.PreparedStatement;
|
44 |
|
|
import java.sql.ResultSet;
|
45 |
|
|
import java.sql.SQLException;
|
46 |
|
|
import java.text.SimpleDateFormat;
|
47 |
2572
|
tao
|
import java.util.Date;
|
48 |
5895
|
berkley
|
import java.util.Enumeration;
|
49 |
5755
|
leinfelder
|
import java.util.Hashtable;
|
50 |
|
|
import java.util.Timer;
|
51 |
|
|
import java.util.Vector;
|
52 |
4080
|
daigle
|
|
53 |
5755
|
leinfelder
|
import javax.servlet.http.HttpServletRequest;
|
54 |
|
|
import javax.servlet.http.HttpServletResponse;
|
55 |
574
|
berkley
|
|
56 |
5755
|
leinfelder
|
import org.apache.log4j.Logger;
|
57 |
|
|
import org.xml.sax.InputSource;
|
58 |
|
|
import org.xml.sax.SAXException;
|
59 |
|
|
import org.xml.sax.XMLReader;
|
60 |
|
|
|
61 |
5014
|
daigle
|
import edu.ucsb.nceas.metacat.DocInfoHandler;
|
62 |
|
|
import edu.ucsb.nceas.metacat.DocumentImpl;
|
63 |
|
|
import edu.ucsb.nceas.metacat.DocumentImplWrapper;
|
64 |
|
|
import edu.ucsb.nceas.metacat.EventLog;
|
65 |
5755
|
leinfelder
|
import edu.ucsb.nceas.metacat.IdentifierManager;
|
66 |
|
|
import edu.ucsb.nceas.metacat.McdbDocNotFoundException;
|
67 |
5014
|
daigle
|
import edu.ucsb.nceas.metacat.McdbException;
|
68 |
5195
|
daigle
|
import edu.ucsb.nceas.metacat.accesscontrol.AccessControlException;
|
69 |
5089
|
daigle
|
import edu.ucsb.nceas.metacat.accesscontrol.AccessControlForSingleFile;
|
70 |
5195
|
daigle
|
import edu.ucsb.nceas.metacat.accesscontrol.PermOrderException;
|
71 |
5098
|
daigle
|
import edu.ucsb.nceas.metacat.accesscontrol.XMLAccessDAO;
|
72 |
6001
|
cjones
|
import edu.ucsb.nceas.metacat.client.InsufficientKarmaException;
|
73 |
5014
|
daigle
|
import edu.ucsb.nceas.metacat.database.DBConnection;
|
74 |
|
|
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
|
75 |
|
|
import edu.ucsb.nceas.metacat.database.DatabaseService;
|
76 |
5030
|
daigle
|
import edu.ucsb.nceas.metacat.properties.PropertyService;
|
77 |
5014
|
daigle
|
import edu.ucsb.nceas.metacat.shared.BaseService;
|
78 |
|
|
import edu.ucsb.nceas.metacat.shared.HandlerException;
|
79 |
|
|
import edu.ucsb.nceas.metacat.shared.ServiceException;
|
80 |
5027
|
daigle
|
import edu.ucsb.nceas.metacat.util.DocumentUtil;
|
81 |
4698
|
daigle
|
import edu.ucsb.nceas.metacat.util.MetacatUtil;
|
82 |
4080
|
daigle
|
import edu.ucsb.nceas.metacat.util.SystemUtil;
|
83 |
4488
|
daigle
|
import edu.ucsb.nceas.utilities.FileUtil;
|
84 |
4087
|
daigle
|
import edu.ucsb.nceas.utilities.GeneralPropertyException;
|
85 |
4080
|
daigle
|
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
|
86 |
|
|
|
87 |
5014
|
daigle
|
public class ReplicationService extends BaseService {
|
88 |
4335
|
daigle
|
|
89 |
5014
|
daigle
|
private static ReplicationService replicationService = null;
|
90 |
2663
|
sgarg
|
|
91 |
5014
|
daigle
|
private long timeInterval;
|
92 |
|
|
private Date firstTime;
|
93 |
|
|
private boolean timedReplicationIsOn = false;
|
94 |
|
|
Timer replicationDaemon;
|
95 |
|
|
private static Vector<String> fileLocks = new Vector<String>();
|
96 |
|
|
// private Thread lockThread = null;
|
97 |
|
|
public static final String FORCEREPLICATEDELETE = "forcereplicatedelete";
|
98 |
|
|
private static String TIMEREPLICATION = "replication.timedreplication";
|
99 |
|
|
private static String TIMEREPLICATIONINTERVAl ="replication.timedreplicationinterval";
|
100 |
|
|
private static String FIRSTTIME = "replication.firsttimedreplication";
|
101 |
|
|
private static final int TIMEINTERVALLIMIT = 7200000;
|
102 |
|
|
public static final String REPLICATIONUSER = "replication";
|
103 |
2286
|
tao
|
|
104 |
5014
|
daigle
|
public static final String REPLICATION_LOG_FILE_NAME = "metacatreplication.log";
|
105 |
|
|
public static String METACAT_REPL_ERROR_MSG = null;
|
106 |
|
|
private static Logger logReplication = Logger.getLogger("ReplicationLogging");
|
107 |
|
|
private static Logger logMetacat = Logger.getLogger(ReplicationService.class);
|
108 |
2286
|
tao
|
|
109 |
5014
|
daigle
|
private ReplicationService() throws ServiceException {
|
110 |
|
|
_serviceName = "ReplicationService";
|
111 |
|
|
|
112 |
|
|
initialize();
|
113 |
|
|
}
|
114 |
|
|
|
115 |
|
|
private void initialize() throws ServiceException {
|
116 |
|
|
|
117 |
|
|
// initialize db connections to handle any update requests
|
118 |
|
|
// deltaT = util.getProperty("replication.deltaT");
|
119 |
|
|
// the default deltaT can be set from metacat.properties
|
120 |
|
|
// create a thread to do the delta-T check but don't execute it yet
|
121 |
|
|
replicationDaemon = new Timer(true);
|
122 |
|
|
try {
|
123 |
|
|
String replLogFile = PropertyService.getProperty("replication.logdir")
|
124 |
|
|
+ FileUtil.getFS() + REPLICATION_LOG_FILE_NAME;
|
125 |
|
|
METACAT_REPL_ERROR_MSG = "An error occurred in replication. Please see the " +
|
126 |
|
|
"replication log at: " + replLogFile;
|
127 |
|
|
|
128 |
|
|
String timedRepIsOnStr =
|
129 |
|
|
PropertyService.getProperty("replication.timedreplication");
|
130 |
|
|
timedReplicationIsOn = (new Boolean(timedRepIsOnStr)).booleanValue();
|
131 |
|
|
logReplication.info("ReplicationService.initialize - The timed replication on is" + timedReplicationIsOn);
|
132 |
522
|
berkley
|
|
133 |
5014
|
daigle
|
String timeIntervalStr =
|
134 |
|
|
PropertyService.getProperty("replication.timedreplicationinterval");
|
135 |
|
|
timeInterval = (new Long(timeIntervalStr)).longValue();
|
136 |
|
|
logReplication.info("ReplicationService.initialize - The timed replication time Interval is " + timeInterval);
|
137 |
2286
|
tao
|
|
138 |
5014
|
daigle
|
String firstTimeStr =
|
139 |
|
|
PropertyService.getProperty("replication.firsttimedreplication");
|
140 |
|
|
logReplication.info("ReplicationService.initialize - first replication time form property is " + firstTimeStr);
|
141 |
|
|
firstTime = ReplicationHandler.combinateCurrentDateAndGivenTime(firstTimeStr);
|
142 |
2286
|
tao
|
|
143 |
5014
|
daigle
|
logReplication.info("ReplicationService.initialize - After combine current time, the real first time is "
|
144 |
|
|
+ firstTime.toString() + " minisec");
|
145 |
2286
|
tao
|
|
146 |
5014
|
daigle
|
// set up time replication if it is on
|
147 |
|
|
if (timedReplicationIsOn) {
|
148 |
|
|
replicationDaemon.scheduleAtFixedRate(new ReplicationHandler(),
|
149 |
|
|
firstTime, timeInterval);
|
150 |
|
|
logReplication.info("ReplicationService.initialize - deltaT handler started with rate="
|
151 |
|
|
+ timeInterval + " mini seconds at " + firstTime.toString());
|
152 |
|
|
}
|
153 |
2286
|
tao
|
|
154 |
5014
|
daigle
|
} catch (PropertyNotFoundException pnfe) {
|
155 |
|
|
throw new ServiceException(
|
156 |
|
|
"ReplicationService.initialize - Property error while instantiating "
|
157 |
|
|
+ "replication service: " + pnfe.getMessage());
|
158 |
|
|
} catch (HandlerException he) {
|
159 |
|
|
throw new ServiceException(
|
160 |
|
|
"ReplicationService.initialize - Handler error while instantiating "
|
161 |
|
|
+ "replication service" + he.getMessage());
|
162 |
|
|
}
|
163 |
|
|
}
|
164 |
2286
|
tao
|
|
165 |
5014
|
daigle
|
/**
|
166 |
|
|
* Get the single instance of SessionService.
|
167 |
|
|
*
|
168 |
|
|
* @return the single instance of SessionService
|
169 |
|
|
*/
|
170 |
|
|
public static ReplicationService getInstance() throws ServiceException {
|
171 |
|
|
if (replicationService == null) {
|
172 |
|
|
replicationService = new ReplicationService();
|
173 |
|
|
}
|
174 |
|
|
return replicationService;
|
175 |
|
|
}
|
176 |
2286
|
tao
|
|
177 |
5014
|
daigle
|
public boolean refreshable() {
|
178 |
|
|
return true;
|
179 |
|
|
}
|
180 |
2286
|
tao
|
|
181 |
5014
|
daigle
|
protected void doRefresh() throws ServiceException {
|
182 |
|
|
return;
|
183 |
|
|
}
|
184 |
|
|
|
185 |
|
|
public void stop() throws ServiceException{
|
186 |
|
|
|
187 |
|
|
}
|
188 |
840
|
bojilova
|
|
189 |
5014
|
daigle
|
public void stopReplication() throws ServiceException {
|
190 |
|
|
//stop the replication server
|
191 |
|
|
replicationDaemon.cancel();
|
192 |
|
|
replicationDaemon = new Timer(true);
|
193 |
|
|
timedReplicationIsOn = false;
|
194 |
|
|
try {
|
195 |
|
|
PropertyService.setProperty("replication.timedreplication", (new Boolean(timedReplicationIsOn)).toString());
|
196 |
|
|
} catch (GeneralPropertyException gpe) {
|
197 |
|
|
logReplication.warn("ReplicationService.stopReplication - Could not set replication.timedreplication property: " + gpe.getMessage());
|
198 |
|
|
}
|
199 |
4080
|
daigle
|
|
200 |
5014
|
daigle
|
logReplication.info("ReplicationService.stopReplication - deltaT handler stopped");
|
201 |
|
|
return;
|
202 |
|
|
}
|
203 |
|
|
|
204 |
|
|
protected void startReplication(Hashtable<String, String[]> params) throws ServiceException {
|
205 |
2286
|
tao
|
|
206 |
5014
|
daigle
|
String firstTimeStr = "";
|
207 |
|
|
//start the replication server
|
208 |
|
|
if ( params.containsKey("rate") ) {
|
209 |
|
|
timeInterval = new Long(
|
210 |
|
|
new String(((String[])params.get("rate"))[0])).longValue();
|
211 |
|
|
if(timeInterval < TIMEINTERVALLIMIT) {
|
212 |
|
|
//deltaT<30 is a timing mess!
|
213 |
|
|
timeInterval = TIMEINTERVALLIMIT;
|
214 |
|
|
throw new ServiceException("Replication deltaT rate cannot be less than "+
|
215 |
|
|
TIMEINTERVALLIMIT + " millisecs and system automatically setup the rate to "+TIMEINTERVALLIMIT);
|
216 |
|
|
}
|
217 |
|
|
} else {
|
218 |
|
|
timeInterval = TIMEINTERVALLIMIT ;
|
219 |
|
|
}
|
220 |
|
|
logReplication.info("ReplicationService.startReplication - New rate is: " + timeInterval + " mini seconds.");
|
221 |
|
|
if ( params.containsKey("firsttime"))
|
222 |
|
|
{
|
223 |
|
|
firstTimeStr = ((String[])params.get("firsttime"))[0];
|
224 |
|
|
try
|
225 |
|
|
{
|
226 |
|
|
firstTime = ReplicationHandler.combinateCurrentDateAndGivenTime(firstTimeStr);
|
227 |
|
|
logReplication.info("ReplicationService.startReplication - The first time setting is "+firstTime.toString());
|
228 |
|
|
}
|
229 |
|
|
catch (HandlerException e)
|
230 |
|
|
{
|
231 |
|
|
throw new ServiceException(e.getMessage());
|
232 |
|
|
}
|
233 |
|
|
logReplication.warn("After combine current time, the real first time is "
|
234 |
|
|
+firstTime.toString()+" minisec");
|
235 |
|
|
}
|
236 |
|
|
else
|
237 |
|
|
{
|
238 |
|
|
logMetacat.error("ReplicationService.startReplication - " + ReplicationService.METACAT_REPL_ERROR_MSG);
|
239 |
|
|
logReplication.error("ReplicationService.startReplication - You should specify the first time " +
|
240 |
|
|
"to start a time replication");
|
241 |
|
|
return;
|
242 |
|
|
}
|
243 |
|
|
|
244 |
|
|
timedReplicationIsOn = true;
|
245 |
|
|
try {
|
246 |
|
|
// save settings to property file
|
247 |
|
|
PropertyService.setProperty(TIMEREPLICATION, (new Boolean(timedReplicationIsOn)).toString());
|
248 |
|
|
// note we couldn't use firstTime object because it has date info
|
249 |
|
|
// we only need time info such as 10:00 PM
|
250 |
|
|
PropertyService.setProperty(FIRSTTIME, firstTimeStr);
|
251 |
|
|
PropertyService.setProperty(TIMEREPLICATIONINTERVAl, (new Long(timeInterval)).toString());
|
252 |
|
|
} catch (GeneralPropertyException gpe) {
|
253 |
|
|
logReplication.warn("ReplicationService.startReplication - Could not set property: " + gpe.getMessage());
|
254 |
|
|
}
|
255 |
|
|
replicationDaemon.cancel();
|
256 |
|
|
replicationDaemon = new Timer(true);
|
257 |
|
|
replicationDaemon.scheduleAtFixedRate(new ReplicationHandler(), firstTime,
|
258 |
|
|
timeInterval);
|
259 |
|
|
|
260 |
|
|
logReplication.info("ReplicationService.startReplication - deltaT handler started with rate=" +
|
261 |
|
|
timeInterval + " milliseconds at " +firstTime.toString());
|
262 |
837
|
bojilova
|
|
263 |
5014
|
daigle
|
}
|
264 |
|
|
|
265 |
|
|
public void runOnce() throws ServiceException {
|
266 |
|
|
//updates this server exactly once
|
267 |
|
|
replicationDaemon.schedule(new ReplicationHandler(), 0);
|
268 |
|
|
}
|
269 |
2286
|
tao
|
|
270 |
5014
|
daigle
|
/**
|
271 |
|
|
* This method can add, delete and list the servers currently included in
|
272 |
|
|
* xml_replication.
|
273 |
|
|
* action subaction other needed params
|
274 |
|
|
* ---------------------------------------------------------
|
275 |
|
|
* servercontrol add server
|
276 |
|
|
* servercontrol delete server
|
277 |
|
|
* servercontrol list
|
278 |
|
|
*/
|
279 |
5755
|
leinfelder
|
protected static void handleServerControlRequest(
|
280 |
5014
|
daigle
|
Hashtable<String, String[]> params, HttpServletResponse response) {
|
281 |
|
|
String subaction = ((String[]) params.get("subaction"))[0];
|
282 |
|
|
DBConnection dbConn = null;
|
283 |
|
|
int serialNumber = -1;
|
284 |
|
|
PreparedStatement pstmt = null;
|
285 |
|
|
String replicate = null;
|
286 |
|
|
String server = null;
|
287 |
|
|
String dataReplicate = null;
|
288 |
|
|
String hub = null;
|
289 |
5755
|
leinfelder
|
Writer out = null;
|
290 |
5014
|
daigle
|
try {
|
291 |
5755
|
leinfelder
|
response.setContentType("text/xml");
|
292 |
|
|
out = response.getWriter();
|
293 |
|
|
|
294 |
5014
|
daigle
|
//conn = util.openDBConnection();
|
295 |
|
|
dbConn = DBConnectionPool
|
296 |
|
|
.getDBConnection("MetacatReplication.handleServerControlRequest");
|
297 |
|
|
serialNumber = dbConn.getCheckOutSerialNumber();
|
298 |
837
|
bojilova
|
|
299 |
5014
|
daigle
|
// add server to server list
|
300 |
|
|
if (subaction.equals("add")) {
|
301 |
|
|
replicate = ((String[]) params.get("replicate"))[0];
|
302 |
|
|
server = ((String[]) params.get("server"))[0];
|
303 |
2286
|
tao
|
|
304 |
5014
|
daigle
|
//Get data replication value
|
305 |
|
|
dataReplicate = ((String[]) params.get("datareplicate"))[0];
|
306 |
|
|
//Get hub value
|
307 |
|
|
hub = ((String[]) params.get("hub"))[0];
|
308 |
837
|
bojilova
|
|
309 |
5319
|
jones
|
String toDateSql = DatabaseService.getInstance().getDBAdapter().toDate("01/01/1980","MM/DD/YYYY");
|
310 |
5151
|
daigle
|
String sql = "INSERT INTO xml_replication "
|
311 |
5014
|
daigle
|
+ "(server, last_checked, replicate, datareplicate, hub) "
|
312 |
5151
|
daigle
|
+ "VALUES (?," + toDateSql + ",?,?,?)";
|
313 |
|
|
|
314 |
|
|
pstmt = dbConn.prepareStatement(sql);
|
315 |
|
|
|
316 |
|
|
pstmt.setString(1, server);
|
317 |
|
|
pstmt.setInt(2, Integer.parseInt(replicate));
|
318 |
|
|
pstmt.setInt(3, Integer.parseInt(dataReplicate));
|
319 |
|
|
pstmt.setInt(4, Integer.parseInt(hub));
|
320 |
|
|
|
321 |
|
|
String sqlReport = "XMLAccessAccess.getXMLAccessForDoc - SQL: " + sql;
|
322 |
|
|
sqlReport += " [" + server + "," + replicate +
|
323 |
|
|
"," + dataReplicate + "," + hub + "]";
|
324 |
|
|
|
325 |
|
|
logMetacat.info(sqlReport);
|
326 |
|
|
|
327 |
5014
|
daigle
|
pstmt.execute();
|
328 |
|
|
pstmt.close();
|
329 |
|
|
dbConn.commit();
|
330 |
5755
|
leinfelder
|
out.write("Server " + server + " added");
|
331 |
5014
|
daigle
|
response.setContentType("text/html");
|
332 |
5755
|
leinfelder
|
out.write("<html><body><table border=\"1\">");
|
333 |
|
|
out.write("<tr><td><b>server</b></td><td><b>last_checked</b></td><td>");
|
334 |
|
|
out.write("<b>replicate</b></td>");
|
335 |
|
|
out.write("<td><b>datareplicate</b></td>");
|
336 |
|
|
out.write("<td><b>hub</b></td></tr>");
|
337 |
5014
|
daigle
|
pstmt = dbConn.prepareStatement("SELECT * FROM xml_replication");
|
338 |
|
|
//increase dbconnection usage
|
339 |
|
|
dbConn.increaseUsageCount(1);
|
340 |
2298
|
tao
|
|
341 |
5014
|
daigle
|
pstmt.execute();
|
342 |
|
|
ResultSet rs = pstmt.getResultSet();
|
343 |
|
|
boolean tablehasrows = rs.next();
|
344 |
|
|
while (tablehasrows) {
|
345 |
5755
|
leinfelder
|
out.write("<tr><td>" + rs.getString(2) + "</td><td>");
|
346 |
|
|
out.write(rs.getString(3) + "</td><td>");
|
347 |
|
|
out.write(rs.getString(4) + "</td><td>");
|
348 |
|
|
out.write(rs.getString(5) + "</td><td>");
|
349 |
|
|
out.write(rs.getString(6) + "</td></tr>");
|
350 |
837
|
bojilova
|
|
351 |
5014
|
daigle
|
tablehasrows = rs.next();
|
352 |
|
|
}
|
353 |
5755
|
leinfelder
|
out.write("</table></body></html>");
|
354 |
837
|
bojilova
|
|
355 |
5014
|
daigle
|
// download certificate with the public key on this server
|
356 |
|
|
// and import it as a trusted certificate
|
357 |
|
|
String certURL = ((String[]) params.get("certificate"))[0];
|
358 |
5151
|
daigle
|
if (certURL != null && !certURL.equals("")) {
|
359 |
|
|
downloadCertificate(certURL);
|
360 |
|
|
}
|
361 |
837
|
bojilova
|
|
362 |
5014
|
daigle
|
// delete server from server list
|
363 |
|
|
} else if (subaction.equals("delete")) {
|
364 |
|
|
server = ((String[]) params.get("server"))[0];
|
365 |
|
|
pstmt = dbConn.prepareStatement("DELETE FROM xml_replication "
|
366 |
|
|
+ "WHERE server LIKE '" + server + "'");
|
367 |
|
|
pstmt.execute();
|
368 |
|
|
pstmt.close();
|
369 |
|
|
dbConn.commit();
|
370 |
5755
|
leinfelder
|
out.write("Server " + server + " deleted");
|
371 |
5014
|
daigle
|
response.setContentType("text/html");
|
372 |
5755
|
leinfelder
|
out.write("<html><body><table border=\"1\">");
|
373 |
|
|
out.write("<tr><td><b>server</b></td><td><b>last_checked</b></td><td>");
|
374 |
|
|
out.write("<b>replicate</b></td>");
|
375 |
|
|
out.write("<td><b>datareplicate</b></td>");
|
376 |
|
|
out.write("<td><b>hub</b></td></tr>");
|
377 |
837
|
bojilova
|
|
378 |
5014
|
daigle
|
pstmt = dbConn.prepareStatement("SELECT * FROM xml_replication");
|
379 |
|
|
//increase dbconnection usage
|
380 |
|
|
dbConn.increaseUsageCount(1);
|
381 |
|
|
pstmt.execute();
|
382 |
|
|
ResultSet rs = pstmt.getResultSet();
|
383 |
|
|
boolean tablehasrows = rs.next();
|
384 |
|
|
while (tablehasrows) {
|
385 |
5755
|
leinfelder
|
out.write("<tr><td>" + rs.getString(2) + "</td><td>");
|
386 |
|
|
out.write(rs.getString(3) + "</td><td>");
|
387 |
|
|
out.write(rs.getString(4) + "</td><td>");
|
388 |
|
|
out.write(rs.getString(5) + "</td><td>");
|
389 |
|
|
out.write(rs.getString(6) + "</td></tr>");
|
390 |
5014
|
daigle
|
tablehasrows = rs.next();
|
391 |
|
|
}
|
392 |
5755
|
leinfelder
|
out.write("</table></body></html>");
|
393 |
837
|
bojilova
|
|
394 |
5014
|
daigle
|
// list servers in server list
|
395 |
|
|
} else if (subaction.equals("list")) {
|
396 |
|
|
response.setContentType("text/html");
|
397 |
5755
|
leinfelder
|
out.write("<html><body><table border=\"1\">");
|
398 |
|
|
out.write("<tr><td><b>server</b></td><td><b>last_checked</b></td><td>");
|
399 |
|
|
out.write("<b>replicate</b></td>");
|
400 |
|
|
out.write("<td><b>datareplicate</b></td>");
|
401 |
|
|
out.write("<td><b>hub</b></td></tr>");
|
402 |
5014
|
daigle
|
pstmt = dbConn.prepareStatement("SELECT * FROM xml_replication");
|
403 |
|
|
pstmt.execute();
|
404 |
|
|
ResultSet rs = pstmt.getResultSet();
|
405 |
|
|
boolean tablehasrows = rs.next();
|
406 |
|
|
while (tablehasrows) {
|
407 |
5755
|
leinfelder
|
out.write("<tr><td>" + rs.getString(2) + "</td><td>");
|
408 |
|
|
out.write(rs.getString(3) + "</td><td>");
|
409 |
|
|
out.write(rs.getString(4) + "</td><td>");
|
410 |
|
|
out.write(rs.getString(5) + "</td><td>");
|
411 |
|
|
out.write(rs.getString(6) + "</td></tr>");
|
412 |
5014
|
daigle
|
tablehasrows = rs.next();
|
413 |
|
|
}
|
414 |
5755
|
leinfelder
|
out.write("</table></body></html>");
|
415 |
5014
|
daigle
|
} else {
|
416 |
2286
|
tao
|
|
417 |
5755
|
leinfelder
|
out.write("<error>Unkonwn subaction</error>");
|
418 |
2286
|
tao
|
|
419 |
5014
|
daigle
|
}
|
420 |
|
|
pstmt.close();
|
421 |
|
|
//conn.close();
|
422 |
2286
|
tao
|
|
423 |
5014
|
daigle
|
} catch (Exception e) {
|
424 |
|
|
logMetacat.error("ReplicationService.handleServerControlRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);
|
425 |
|
|
logReplication.error("ReplicationService.handleServerControlRequest - Error in "
|
426 |
|
|
+ "MetacatReplication.handleServerControlRequest " + e.getMessage());
|
427 |
|
|
e.printStackTrace(System.out);
|
428 |
|
|
} finally {
|
429 |
|
|
try {
|
430 |
|
|
pstmt.close();
|
431 |
|
|
}//try
|
432 |
|
|
catch (SQLException ee) {
|
433 |
|
|
logMetacat.error("ReplicationService.handleServerControlRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);
|
434 |
|
|
logReplication.error("ReplicationService.handleServerControlRequest - Error in MetacatReplication.handleServerControlRequest to close pstmt "
|
435 |
|
|
+ ee.getMessage());
|
436 |
|
|
}//catch
|
437 |
|
|
finally {
|
438 |
|
|
DBConnectionPool.returnDBConnection(dbConn, serialNumber);
|
439 |
|
|
}//finally
|
440 |
5755
|
leinfelder
|
if (out != null) {
|
441 |
|
|
try {
|
442 |
|
|
out.close();
|
443 |
|
|
} catch (IOException e) {
|
444 |
|
|
logMetacat.error(e.getMessage(), e);
|
445 |
|
|
}
|
446 |
|
|
}
|
447 |
5014
|
daigle
|
}//finally
|
448 |
2286
|
tao
|
|
449 |
5014
|
daigle
|
}
|
450 |
2286
|
tao
|
|
451 |
5014
|
daigle
|
// download certificate with the public key from certURL and
|
452 |
4080
|
daigle
|
// upload it onto this server; it then must be imported as a
|
453 |
|
|
// trusted certificate
|
454 |
5014
|
daigle
|
private static void downloadCertificate(String certURL) throws FileNotFoundException,
|
455 |
4080
|
daigle
|
IOException, MalformedURLException, PropertyNotFoundException {
|
456 |
5014
|
daigle
|
|
457 |
4080
|
daigle
|
// the path to be uploaded to
|
458 |
5014
|
daigle
|
String certPath = SystemUtil.getContextDir();
|
459 |
2286
|
tao
|
|
460 |
4080
|
daigle
|
// get filename from the URL of the certificate
|
461 |
|
|
String filename = certURL;
|
462 |
|
|
int slash = Math.max(filename.lastIndexOf('/'), filename.lastIndexOf('\\'));
|
463 |
|
|
if (slash > -1) {
|
464 |
|
|
filename = filename.substring(slash + 1);
|
465 |
|
|
}
|
466 |
2286
|
tao
|
|
467 |
4080
|
daigle
|
// open file output strem to write the input into it
|
468 |
|
|
File f = new File(certPath, filename);
|
469 |
|
|
synchronized (f) {
|
470 |
|
|
try {
|
471 |
|
|
if (f.exists()) {
|
472 |
|
|
throw new IOException("File already exist: " + f.getCanonicalFile());
|
473 |
|
|
// if ( f.exists() && !f.canWrite() ) {
|
474 |
|
|
// throw new IOException("Not writable: " +
|
475 |
|
|
// f.getCanonicalFile());
|
476 |
|
|
}
|
477 |
|
|
} catch (SecurityException se) {
|
478 |
|
|
// if a security manager exists,
|
479 |
|
|
// its checkRead method is called for f.exist()
|
480 |
|
|
// or checkWrite method is called for f.canWrite()
|
481 |
|
|
throw se;
|
482 |
|
|
}
|
483 |
2286
|
tao
|
|
484 |
4080
|
daigle
|
// create a buffered byte output stream
|
485 |
|
|
// that uses a default-sized output buffer
|
486 |
|
|
FileOutputStream fos = new FileOutputStream(f);
|
487 |
|
|
BufferedOutputStream out = new BufferedOutputStream(fos);
|
488 |
840
|
bojilova
|
|
489 |
4080
|
daigle
|
// this should be http url
|
490 |
|
|
URL url = new URL(certURL);
|
491 |
|
|
BufferedInputStream bis = null;
|
492 |
|
|
try {
|
493 |
|
|
bis = new BufferedInputStream(url.openStream());
|
494 |
|
|
byte[] buf = new byte[4 * 1024]; // 4K buffer
|
495 |
|
|
int b = bis.read(buf);
|
496 |
|
|
while (b != -1) {
|
497 |
|
|
out.write(buf, 0, b);
|
498 |
|
|
b = bis.read(buf);
|
499 |
|
|
}
|
500 |
|
|
} finally {
|
501 |
|
|
if (bis != null)
|
502 |
|
|
bis.close();
|
503 |
|
|
}
|
504 |
|
|
// the input and the output streams must be closed
|
505 |
|
|
bis.close();
|
506 |
|
|
out.flush();
|
507 |
|
|
out.close();
|
508 |
|
|
fos.close();
|
509 |
|
|
} // end of synchronized(f)
|
510 |
|
|
}
|
511 |
2286
|
tao
|
|
512 |
5014
|
daigle
|
/**
|
513 |
4080
|
daigle
|
* when a forcereplication request comes in, local host sends a read request
|
514 |
|
|
* to the requesting server (remote server) for the specified docid. Then
|
515 |
|
|
* store it in local database.
|
516 |
|
|
*/
|
517 |
5755
|
leinfelder
|
protected static void handleForceReplicateRequest(
|
518 |
5014
|
daigle
|
Hashtable<String, String[]> params, HttpServletResponse response,
|
519 |
|
|
HttpServletRequest request) {
|
520 |
|
|
String server = ((String[]) params.get("server"))[0]; // the server that
|
521 |
|
|
String docid = ((String[]) params.get("docid"))[0]; // sent the document
|
522 |
|
|
String dbaction = "UPDATE"; // the default action is UPDATE
|
523 |
|
|
// boolean override = false;
|
524 |
|
|
// int serverCode = 1;
|
525 |
|
|
DBConnection dbConn = null;
|
526 |
|
|
int serialNumber = -1;
|
527 |
5459
|
berkley
|
String docName = null;
|
528 |
2286
|
tao
|
|
529 |
5014
|
daigle
|
try {
|
530 |
|
|
//if the url contains a dbaction then the default action is overridden
|
531 |
|
|
if (params.containsKey("dbaction")) {
|
532 |
|
|
dbaction = ((String[]) params.get("dbaction"))[0];
|
533 |
|
|
//serverCode = MetacatReplication.getServerCode(server);
|
534 |
|
|
//override = true; //we are now overriding the default action
|
535 |
|
|
}
|
536 |
|
|
logReplication.info("ReplicationService.handleForceReplicateRequest - Force replication request from: " + server);
|
537 |
|
|
logReplication.info("ReplicationService.handleForceReplicateRequest - Force replication docid: " + docid);
|
538 |
|
|
logReplication.info("ReplicationService.handleForceReplicateRequest - Force replication action: " + dbaction);
|
539 |
|
|
// sending back read request to remote server
|
540 |
|
|
URL u = new URL("https://" + server + "?server="
|
541 |
|
|
+ MetacatUtil.getLocalReplicationServerName() + "&action=read&docid="
|
542 |
|
|
+ docid);
|
543 |
|
|
String xmldoc = ReplicationService.getURLContent(u);
|
544 |
2286
|
tao
|
|
545 |
5014
|
daigle
|
// get the document info from server
|
546 |
|
|
URL docinfourl = new URL("https://" + server + "?server="
|
547 |
|
|
+ MetacatUtil.getLocalReplicationServerName()
|
548 |
|
|
+ "&action=getdocumentinfo&docid=" + docid);
|
549 |
5440
|
berkley
|
|
550 |
2286
|
tao
|
|
551 |
5014
|
daigle
|
String docInfoStr = ReplicationService.getURLContent(docinfourl);
|
552 |
2286
|
tao
|
|
553 |
5014
|
daigle
|
//dih is the parser for the docinfo xml format
|
554 |
|
|
DocInfoHandler dih = new DocInfoHandler();
|
555 |
|
|
XMLReader docinfoParser = ReplicationHandler.initParser(dih);
|
556 |
|
|
docinfoParser.parse(new InputSource(new StringReader(docInfoStr)));
|
557 |
|
|
// Hashtable<String,Vector<AccessControlForSingleFile>> docinfoHash = dih.getDocInfo();
|
558 |
|
|
Hashtable<String, String> docinfoHash = dih.getDocInfo();
|
559 |
2286
|
tao
|
|
560 |
6015
|
leinfelder
|
|
561 |
5014
|
daigle
|
// Get home server of this docid
|
562 |
|
|
String homeServer = (String) docinfoHash.get("home_server");
|
563 |
5322
|
berkley
|
String guid = (String) docinfoHash.get("guid");
|
564 |
5440
|
berkley
|
logReplication.info("XXXXXXXXXXXXXXXX GUID found in dociinfoHash: " + guid);
|
565 |
5324
|
berkley
|
|
566 |
5451
|
berkley
|
logReplication.info("Processing guid " + guid +
|
567 |
5324
|
berkley
|
" information from handleForceReplicationRequest: " +
|
568 |
|
|
docinfoHash.toString());
|
569 |
|
|
IdentifierManager idman = IdentifierManager.getInstance();
|
570 |
5455
|
berkley
|
if(guid != null)
|
571 |
5324
|
berkley
|
{ //if the guid was passed in, put it in the identifiers table
|
572 |
5451
|
berkley
|
logReplication.info("YYYYYYYYYYYYYY Creating guid/docid mapping for docid " +
|
573 |
5324
|
berkley
|
docinfoHash.get("docid") + " and guid: " + guid);
|
574 |
5451
|
berkley
|
|
575 |
5459
|
berkley
|
docName = (String) docinfoHash.get("docname");
|
576 |
5451
|
berkley
|
logReplication.info("ZZZZZZZZZZZZ docName: " + docName);
|
577 |
|
|
if(docName.trim().equals("systemMetadata"))
|
578 |
|
|
{
|
579 |
|
|
logReplication.info("creating mapping for systemMetadata: guid: " + guid + " localId: " + docinfoHash.get("docid"));
|
580 |
|
|
idman.createSystemMetadataMapping(guid, docinfoHash.get("docid"));
|
581 |
5950
|
berkley
|
logReplication.info("handleForceReplicateRequest: updating SM with additional info");
|
582 |
|
|
System.out.println("!!!!!!!!!!!!!handleForceReplicateRequest: updating SM with additional info");
|
583 |
|
|
Long dateUploadedLong = new Long(docinfoHash.get("date_uploaded"));
|
584 |
|
|
Long dateModifiedLong = new Long(docinfoHash.get("date_modified"));
|
585 |
|
|
idman.insertAdditionalSystemMetadataFields(
|
586 |
|
|
dateUploadedLong.longValue(),
|
587 |
|
|
docinfoHash.get("rights_holder"),
|
588 |
|
|
docinfoHash.get("checksum"),
|
589 |
|
|
docinfoHash.get("checksum_algorithm"),
|
590 |
|
|
docinfoHash.get("origin_member_node"),
|
591 |
|
|
docinfoHash.get("authoritive_member_node"),
|
592 |
|
|
dateModifiedLong.longValue(),
|
593 |
|
|
docinfoHash.get("submitter"),
|
594 |
|
|
docinfoHash.get("guid"),
|
595 |
|
|
docinfoHash.get("objectFormat"),
|
596 |
|
|
new Long(docinfoHash.get("size")).longValue());
|
597 |
5451
|
berkley
|
}
|
598 |
|
|
else
|
599 |
|
|
{
|
600 |
|
|
logReplication.info("creating mapping: guid: " + guid + " localId: " + docinfoHash.get("docid"));
|
601 |
|
|
idman.createMapping(guid, docinfoHash.get("docid"));
|
602 |
|
|
}
|
603 |
5324
|
berkley
|
}
|
604 |
|
|
else
|
605 |
|
|
{
|
606 |
|
|
logReplication.debug("No guid information was included with the replicated document");
|
607 |
|
|
}
|
608 |
|
|
|
609 |
5014
|
daigle
|
String createdDate = (String) docinfoHash.get("date_created");
|
610 |
|
|
String updatedDate = (String) docinfoHash.get("date_updated");
|
611 |
|
|
logReplication.info("ReplicationService.handleForceReplicateRequest - homeServer: " + homeServer);
|
612 |
|
|
// Get Document type
|
613 |
|
|
String docType = (String) docinfoHash.get("doctype");
|
614 |
|
|
logReplication.info("ReplicationService.handleForceReplicateRequest - docType: " + docType);
|
615 |
|
|
String parserBase = null;
|
616 |
|
|
// this for eml2 and we need user eml2 parser
|
617 |
|
|
if (docType != null
|
618 |
|
|
&& (docType.trim()).equals(DocumentImpl.EML2_0_0NAMESPACE)) {
|
619 |
|
|
logReplication.warn("ReplicationService.handleForceReplicateRequest - This is an eml200 document!");
|
620 |
|
|
parserBase = DocumentImpl.EML200;
|
621 |
|
|
} else if (docType != null
|
622 |
|
|
&& (docType.trim()).equals(DocumentImpl.EML2_0_1NAMESPACE)) {
|
623 |
|
|
logReplication.warn("ReplicationService.handleForceReplicateRequest - This is an eml2.0.1 document!");
|
624 |
|
|
parserBase = DocumentImpl.EML200;
|
625 |
|
|
} else if (docType != null
|
626 |
|
|
&& (docType.trim()).equals(DocumentImpl.EML2_1_0NAMESPACE)) {
|
627 |
|
|
logReplication.warn("ReplicationService.handleForceReplicateRequest - This is an eml2.1.0 document!");
|
628 |
|
|
parserBase = DocumentImpl.EML210;
|
629 |
5709
|
leinfelder
|
} else if (docType != null
|
630 |
|
|
&& (docType.trim()).equals(DocumentImpl.EML2_1_1NAMESPACE)) {
|
631 |
|
|
logReplication.warn("ReplicationService.handleForceReplicateRequest - This is an eml2.1.1 document!");
|
632 |
|
|
parserBase = DocumentImpl.EML210;
|
633 |
5014
|
daigle
|
}
|
634 |
|
|
logReplication.warn("ReplicationService.handleForceReplicateRequest - The parserBase is: " + parserBase);
|
635 |
2286
|
tao
|
|
636 |
5014
|
daigle
|
// Get DBConnection from pool
|
637 |
|
|
dbConn = DBConnectionPool
|
638 |
|
|
.getDBConnection("MetacatReplication.handleForceReplicateRequest");
|
639 |
|
|
serialNumber = dbConn.getCheckOutSerialNumber();
|
640 |
|
|
// write the document to local database
|
641 |
|
|
DocumentImplWrapper wrapper = new DocumentImplWrapper(parserBase, false);
|
642 |
|
|
//try this independently so we can set
|
643 |
5195
|
daigle
|
// Exception writeException = null;
|
644 |
5014
|
daigle
|
try {
|
645 |
5195
|
daigle
|
wrapper.writeReplication(dbConn, xmldoc, null, null,
|
646 |
6015
|
leinfelder
|
dbaction, docid, null, null, homeServer, server, createdDate,
|
647 |
5014
|
daigle
|
updatedDate);
|
648 |
5195
|
daigle
|
} finally {
|
649 |
|
|
// writeException = e;
|
650 |
3230
|
tao
|
|
651 |
5195
|
daigle
|
//process extra access rules before dealing with the write exception (doc exist already)
|
652 |
|
|
Vector<XMLAccessDAO> accessControlList = dih.getAccessControlList();
|
653 |
|
|
if (accessControlList != null) {
|
654 |
|
|
AccessControlForSingleFile acfsf = new AccessControlForSingleFile(docid);
|
655 |
|
|
for (XMLAccessDAO xmlAccessDAO : accessControlList) {
|
656 |
|
|
if (!acfsf.accessControlExists(xmlAccessDAO)) {
|
657 |
|
|
acfsf.insertPermissions(xmlAccessDAO);
|
658 |
|
|
logReplication.info("ReplicationService.handleForceReplicateRequest - document " + docid
|
659 |
|
|
+ " permissions added to DB");
|
660 |
|
|
}
|
661 |
|
|
}
|
662 |
|
|
}
|
663 |
6015
|
leinfelder
|
|
664 |
|
|
// process the real owner and updater
|
665 |
|
|
String user = (String) docinfoHash.get("user_owner");
|
666 |
|
|
String updated = (String) docinfoHash.get("user_updated");
|
667 |
|
|
updateUserOwner(dbConn, docid, user, updated);
|
668 |
2286
|
tao
|
|
669 |
5195
|
daigle
|
logReplication.info("ReplicationService.handleForceReplicateRequest - document " + docid + " added to DB with "
|
670 |
|
|
+ "action " + dbaction);
|
671 |
5458
|
berkley
|
|
672 |
|
|
if(guid != null)
|
673 |
|
|
{
|
674 |
5459
|
berkley
|
if(!docName.trim().equals("systemMetadata"))
|
675 |
|
|
{
|
676 |
|
|
logReplication.info("replicate D1GUID:" + guid + ":D1SCIMETADATA:" +
|
677 |
|
|
docid + ":");
|
678 |
|
|
}
|
679 |
|
|
else
|
680 |
|
|
{
|
681 |
|
|
logReplication.info("replicate D1GUID:" + guid + ":D1SYSMETADATA:" +
|
682 |
|
|
docid + ":");
|
683 |
|
|
}
|
684 |
5458
|
berkley
|
}
|
685 |
5195
|
daigle
|
EventLog.getInstance().log(request.getRemoteAddr(), REPLICATIONUSER, docid,
|
686 |
|
|
dbaction);
|
687 |
5014
|
daigle
|
}
|
688 |
5195
|
daigle
|
} catch (SQLException sqle) {
|
689 |
5014
|
daigle
|
logMetacat.error("ReplicationService.handleForceReplicateRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);
|
690 |
5195
|
daigle
|
logReplication.error("ReplicationService.handleForceReplicateRequest - SQL error when adding doc " + docid +
|
691 |
|
|
" to DB with action " + dbaction + ": " + sqle.getMessage());
|
692 |
|
|
} catch (MalformedURLException mue) {
|
693 |
|
|
logMetacat.error("ReplicationService.handleForceReplicateRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);
|
694 |
|
|
logReplication.error("ReplicationService.handleForceReplicateRequest - URL error when adding doc " + docid +
|
695 |
|
|
" to DB with action " + dbaction + ": " + mue.getMessage());
|
696 |
|
|
} catch (SAXException se) {
|
697 |
|
|
logMetacat.error("ReplicationService.handleForceReplicateRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);
|
698 |
|
|
logReplication.error("ReplicationService.handleForceReplicateRequest - SAX parsing error when adding doc " + docid +
|
699 |
|
|
" to DB with action " + dbaction + ": " + se.getMessage());
|
700 |
|
|
} catch (HandlerException he) {
|
701 |
|
|
logMetacat.error("ReplicationService.handleForceReplicateRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);
|
702 |
|
|
logReplication.error("ReplicationService.handleForceReplicateRequest - Handler error when adding doc " + docid +
|
703 |
|
|
" to DB with action " + dbaction + ": " + he.getMessage());
|
704 |
|
|
} catch (IOException ioe) {
|
705 |
|
|
logMetacat.error("ReplicationService.handleForceReplicateRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);
|
706 |
|
|
logReplication.error("ReplicationService.handleForceReplicateRequest - I/O error when adding doc " + docid +
|
707 |
|
|
" to DB with action " + dbaction + ": " + ioe.getMessage());
|
708 |
|
|
} catch (PermOrderException poe) {
|
709 |
|
|
logMetacat.error("ReplicationService.handleForceReplicateRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);
|
710 |
|
|
logReplication.error("ReplicationService.handleForceReplicateRequest - Permissions order error when adding doc " + docid +
|
711 |
|
|
" to DB with action " + dbaction + ": " + poe.getMessage());
|
712 |
|
|
} catch (AccessControlException ace) {
|
713 |
|
|
logMetacat.error("ReplicationService.handleForceReplicateRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);
|
714 |
|
|
logReplication.error("ReplicationService.handleForceReplicateRequest - Permissions order error when adding doc " + docid +
|
715 |
|
|
" to DB with action " + dbaction + ": " + ace.getMessage());
|
716 |
|
|
} catch (Exception e) {
|
717 |
|
|
logMetacat.error("ReplicationService.handleForceReplicateRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);
|
718 |
|
|
logReplication.error("ReplicationService.handleForceReplicateRequest - General error when adding doc " + docid +
|
719 |
|
|
" to DB with action " + dbaction + ": " + e.getMessage());
|
720 |
|
|
} finally {
|
721 |
5014
|
daigle
|
// Return the checked out DBConnection
|
722 |
|
|
DBConnectionPool.returnDBConnection(dbConn, serialNumber);
|
723 |
|
|
}//finally
|
724 |
|
|
}
|
725 |
2298
|
tao
|
|
726 |
5014
|
daigle
|
/*
|
727 |
|
|
* when a forcereplication delete request comes in, local host will delete this
|
728 |
|
|
* document
|
729 |
|
|
*/
|
730 |
5755
|
leinfelder
|
protected static void handleForceReplicateDeleteRequest(
|
731 |
5014
|
daigle
|
Hashtable<String, String[]> params, HttpServletResponse response,
|
732 |
|
|
HttpServletRequest request) {
|
733 |
|
|
String server = ((String[]) params.get("server"))[0]; // the server that
|
734 |
|
|
String docid = ((String[]) params.get("docid"))[0]; // sent the document
|
735 |
|
|
try {
|
736 |
|
|
logReplication.info("ReplicationService.handleForceReplicateDeleteRequest - force replication delete request from " + server);
|
737 |
|
|
logReplication.info("ReplicationService.handleForceReplicateDeleteRequest - force replication delete docid " + docid);
|
738 |
|
|
logReplication.info("ReplicationService.handleForceReplicateDeleteRequest - Force replication delete request from: " + server);
|
739 |
|
|
logReplication.info("ReplicationService.handleForceReplicateDeleteRequest - Force replication delete docid: " + docid);
|
740 |
|
|
DocumentImpl.delete(docid, null, null, server);
|
741 |
|
|
logReplication.info("ReplicationService.handleForceReplicateDeleteRequest - document " + docid + " was successfully deleted ");
|
742 |
|
|
EventLog.getInstance().log(request.getRemoteAddr(), REPLICATIONUSER, docid,
|
743 |
|
|
"delete");
|
744 |
|
|
logReplication.info("ReplicationService.handleForceReplicateDeleteRequest - document " + docid + " was successfully deleted ");
|
745 |
6001
|
cjones
|
} catch (McdbDocNotFoundException e) {
|
746 |
|
|
logMetacat.error("ReplicationService.handleForceReplicateDeleteRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);
|
747 |
|
|
logReplication.error("document " + docid
|
748 |
|
|
+ " failed to delete because " + e.getMessage());
|
749 |
|
|
logReplication.error("ReplicationService.handleForceReplicateDeleteRequest - error: " + e.getMessage());
|
750 |
|
|
} catch (InsufficientKarmaException e) {
|
751 |
|
|
logMetacat.error("ReplicationService.handleForceReplicateDeleteRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);
|
752 |
|
|
logReplication.error("document " + docid
|
753 |
|
|
+ " failed to delete because " + e.getMessage());
|
754 |
|
|
logReplication.error("ReplicationService.handleForceReplicateDeleteRequest - error: " + e.getMessage());
|
755 |
|
|
} catch (SQLException e) {
|
756 |
|
|
logMetacat.error("ReplicationService.handleForceReplicateDeleteRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);
|
757 |
|
|
logReplication.error("document " + docid
|
758 |
|
|
+ " failed to delete because " + e.getMessage());
|
759 |
|
|
logReplication.error("ReplicationService.handleForceReplicateDeleteRequest - error: " + e.getMessage());
|
760 |
5014
|
daigle
|
} catch (Exception e) {
|
761 |
|
|
logMetacat.error("ReplicationService.handleForceReplicateDeleteRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);
|
762 |
|
|
logReplication.error("document " + docid
|
763 |
|
|
+ " failed to delete because " + e.getMessage());
|
764 |
|
|
logReplication.error("ReplicationService.handleForceReplicateDeleteRequest - error: " + e.getMessage());
|
765 |
2298
|
tao
|
|
766 |
5014
|
daigle
|
}//catch
|
767 |
2298
|
tao
|
|
768 |
5014
|
daigle
|
}
|
769 |
2286
|
tao
|
|
770 |
5014
|
daigle
|
/**
|
771 |
|
|
* when a forcereplication data file request comes in, local host sends a
|
772 |
|
|
* readdata request to the requesting server (remote server) for the specified
|
773 |
|
|
* docid. Then store it in local database and file system
|
774 |
|
|
*/
|
775 |
|
|
protected static void handleForceReplicateDataFileRequest(Hashtable<String, String[]> params,
|
776 |
|
|
HttpServletRequest request) {
|
777 |
2286
|
tao
|
|
778 |
5014
|
daigle
|
//make sure there is some parameters
|
779 |
|
|
if (params.isEmpty()) {
|
780 |
|
|
return;
|
781 |
|
|
}
|
782 |
|
|
// Get remote server
|
783 |
|
|
String server = ((String[]) params.get("server"))[0];
|
784 |
|
|
// the docid should include rev number
|
785 |
|
|
String docid = ((String[]) params.get("docid"))[0];
|
786 |
|
|
// Make sure there is a docid and server
|
787 |
|
|
if (docid == null || server == null || server.equals("")) {
|
788 |
|
|
logMetacat.error("ReplicationService.handleForceReplicateDataFileRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);
|
789 |
|
|
logReplication.error("ReplicationService.handleForceReplicateDataFileRequest - Didn't specify docid or server for replication");
|
790 |
|
|
return;
|
791 |
|
|
}
|
792 |
2286
|
tao
|
|
793 |
5014
|
daigle
|
// Overide or not
|
794 |
|
|
// boolean override = false;
|
795 |
|
|
// dbaction - update or insert
|
796 |
|
|
String dbaction = null;
|
797 |
2286
|
tao
|
|
798 |
5014
|
daigle
|
try {
|
799 |
|
|
//docid was switch to two parts uinque code and rev
|
800 |
|
|
//String uniqueCode=MetacatUtil.getDocIdFromString(docid);
|
801 |
|
|
//int rev=MetacatUtil.getVersionFromString(docid);
|
802 |
|
|
if (params.containsKey("dbaction")) {
|
803 |
|
|
dbaction = ((String[]) params.get("dbaction"))[0];
|
804 |
|
|
} else//default value is update
|
805 |
|
|
{
|
806 |
|
|
dbaction = "update";
|
807 |
|
|
}
|
808 |
2286
|
tao
|
|
809 |
5014
|
daigle
|
logReplication.info("ReplicationService.handleForceReplicateDataFileRequest - force replication request from " + server);
|
810 |
|
|
logReplication.info("ReplicationService.handleForceReplicateDataFileRequest - Force replication request from: " + server);
|
811 |
|
|
logReplication.info("ReplicationService.handleForceReplicateDataFileRequest - Force replication docid: " + docid);
|
812 |
|
|
logReplication.info("ReplicationService.handleForceReplicateDataFileRequest - Force replication action: " + dbaction);
|
813 |
|
|
// get the document info from server
|
814 |
|
|
URL docinfourl = new URL("https://" + server + "?server="
|
815 |
|
|
+ MetacatUtil.getLocalReplicationServerName()
|
816 |
|
|
+ "&action=getdocumentinfo&docid=" + docid);
|
817 |
1023
|
tao
|
|
818 |
5014
|
daigle
|
String docInfoStr = ReplicationService.getURLContent(docinfourl);
|
819 |
2286
|
tao
|
|
820 |
5014
|
daigle
|
//dih is the parser for the docinfo xml format
|
821 |
|
|
DocInfoHandler dih = new DocInfoHandler();
|
822 |
|
|
XMLReader docinfoParser = ReplicationHandler.initParser(dih);
|
823 |
|
|
docinfoParser.parse(new InputSource(new StringReader(docInfoStr)));
|
824 |
|
|
Hashtable<String, String> docinfoHash = dih.getDocInfo();
|
825 |
6015
|
leinfelder
|
|
826 |
5014
|
daigle
|
String docName = (String) docinfoHash.get("docname");
|
827 |
2286
|
tao
|
|
828 |
5014
|
daigle
|
String docType = (String) docinfoHash.get("doctype");
|
829 |
2286
|
tao
|
|
830 |
5014
|
daigle
|
String docHomeServer = (String) docinfoHash.get("home_server");
|
831 |
2286
|
tao
|
|
832 |
5014
|
daigle
|
String createdDate = (String) docinfoHash.get("date_created");
|
833 |
2286
|
tao
|
|
834 |
5014
|
daigle
|
String updatedDate = (String) docinfoHash.get("date_updated");
|
835 |
|
|
logReplication.info("ReplicationService.handleForceReplicateDataFileRequest - docHomeServer of datafile: " + docHomeServer);
|
836 |
|
|
|
837 |
|
|
//if action is delete, we don't delete the data file. Just archieve
|
838 |
|
|
//the xml_documents
|
839 |
|
|
/*if (dbaction.equals("delete"))
|
840 |
|
|
{
|
841 |
|
|
//conn = util.getConnection();
|
842 |
|
|
DocumentImpl.delete(docid,user,null);
|
843 |
|
|
//util.returnConnection(conn);
|
844 |
|
|
}*/
|
845 |
|
|
//To data file insert or update is same
|
846 |
|
|
if (dbaction.equals("insert") || dbaction.equals("update")) {
|
847 |
|
|
//Get data file and store it into local file system.
|
848 |
|
|
// sending back readdata request to server
|
849 |
|
|
URL url = new URL("https://" + server + "?server="
|
850 |
|
|
+ MetacatUtil.getLocalReplicationServerName()
|
851 |
|
|
+ "&action=readdata&docid=" + docid);
|
852 |
|
|
String datafilePath = PropertyService
|
853 |
|
|
.getProperty("application.datafilepath");
|
854 |
|
|
|
855 |
|
|
Exception writeException = null;
|
856 |
|
|
//register data file into xml_documents table and wite data file
|
857 |
|
|
//into file system
|
858 |
|
|
try {
|
859 |
|
|
DocumentImpl.writeDataFileInReplication(url.openStream(),
|
860 |
6015
|
leinfelder
|
datafilePath, docName, docType, docid, null, docHomeServer,
|
861 |
5014
|
daigle
|
server, DocumentImpl.DOCUMENTTABLE, false, createdDate,
|
862 |
|
|
updatedDate);
|
863 |
|
|
} catch (Exception e) {
|
864 |
|
|
writeException = e;
|
865 |
|
|
}
|
866 |
5098
|
daigle
|
|
867 |
6015
|
leinfelder
|
// process the real owner and updater
|
868 |
|
|
DBConnection dbConn = DBConnectionPool.getDBConnection("ReplicationService.handleForceDataFileRequest");
|
869 |
|
|
int serialNumber = dbConn.getCheckOutSerialNumber();
|
870 |
|
|
dbConn.setAutoCommit(false);
|
871 |
|
|
String user = (String) docinfoHash.get("user_owner");
|
872 |
|
|
String updated = (String) docinfoHash.get("user_updated");
|
873 |
|
|
updateUserOwner(dbConn, docid, user, updated);
|
874 |
|
|
DBConnectionPool.returnDBConnection(dbConn, serialNumber);
|
875 |
|
|
|
876 |
5098
|
daigle
|
Vector<XMLAccessDAO> accessControlList = dih.getAccessControlList();
|
877 |
|
|
if (accessControlList != null) {
|
878 |
|
|
AccessControlForSingleFile acfsf = new AccessControlForSingleFile(docid);
|
879 |
|
|
for (XMLAccessDAO xmlAccessDAO : accessControlList) {
|
880 |
|
|
if (!acfsf.accessControlExists(xmlAccessDAO)) {
|
881 |
|
|
acfsf.insertPermissions(xmlAccessDAO);
|
882 |
|
|
logReplication.info("ReplicationService.handleForceReplicateRequest - document " + docid
|
883 |
5014
|
daigle
|
+ " permissions added to DB");
|
884 |
5098
|
daigle
|
}
|
885 |
|
|
}
|
886 |
|
|
}
|
887 |
5014
|
daigle
|
|
888 |
|
|
if (writeException != null) {
|
889 |
|
|
throw writeException;
|
890 |
|
|
}
|
891 |
|
|
|
892 |
|
|
//false means non-timed replication
|
893 |
|
|
logReplication.info("ReplicationService.handleForceReplicateDataFileRequest - datafile " + docid + " added to DB with "
|
894 |
|
|
+ "action " + dbaction);
|
895 |
|
|
EventLog.getInstance().log(request.getRemoteAddr(), REPLICATIONUSER,
|
896 |
|
|
docid, dbaction);
|
897 |
|
|
}
|
898 |
|
|
|
899 |
|
|
} catch (Exception e) {
|
900 |
|
|
logMetacat.error("ReplicationService.handleForceReplicateDataFileRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);
|
901 |
|
|
logReplication.error("ReplicationService.handleForceReplicateDataFileRequest - Datafile " + docid
|
902 |
|
|
+ " failed to added to DB with " + "action " + dbaction + " because "
|
903 |
|
|
+ e.getMessage());
|
904 |
|
|
logReplication.error("ReplicationService.handleForceReplicateDataFileRequest - ERROR in MetacatReplication.handleForceDataFileReplicate"
|
905 |
|
|
+ "Request(): " + e.getMessage());
|
906 |
4449
|
leinfelder
|
}
|
907 |
5014
|
daigle
|
}
|
908 |
2286
|
tao
|
|
909 |
5014
|
daigle
|
/**
|
910 |
|
|
* Grants or denies a lock to a requesting host.
|
911 |
|
|
* The servlet parameters of interrest are:
|
912 |
|
|
* docid: the docid of the file the lock is being requested for
|
913 |
|
|
* currentdate: the timestamp of the document on the remote server
|
914 |
|
|
*
|
915 |
|
|
*/
|
916 |
5755
|
leinfelder
|
protected static void handleGetLockRequest(
|
917 |
5014
|
daigle
|
Hashtable<String, String[]> params, HttpServletResponse response) {
|
918 |
1292
|
tao
|
|
919 |
5014
|
daigle
|
try {
|
920 |
2286
|
tao
|
|
921 |
5014
|
daigle
|
String docid = ((String[]) params.get("docid"))[0];
|
922 |
|
|
String remoteRev = ((String[]) params.get("updaterev"))[0];
|
923 |
|
|
DocumentImpl requestDoc = new DocumentImpl(docid);
|
924 |
|
|
logReplication.info("ReplicationService.handleGetLockRequest - lock request for " + docid);
|
925 |
|
|
int localRevInt = requestDoc.getRev();
|
926 |
|
|
int remoteRevInt = Integer.parseInt(remoteRev);
|
927 |
2286
|
tao
|
|
928 |
5755
|
leinfelder
|
// get a writer for sending back to response
|
929 |
|
|
response.setContentType("text/xml");
|
930 |
|
|
Writer out = response.getWriter();
|
931 |
|
|
|
932 |
5014
|
daigle
|
if (remoteRevInt >= localRevInt) {
|
933 |
|
|
if (!fileLocks.contains(docid)) { //grant the lock if it is not already locked
|
934 |
|
|
fileLocks.add(0, docid); //insert at the beginning of the queue Vector
|
935 |
|
|
//send a message back to the the remote host authorizing the insert
|
936 |
5755
|
leinfelder
|
out.write("<lockgranted><docid>" + docid
|
937 |
5014
|
daigle
|
+ "</docid></lockgranted>");
|
938 |
|
|
// lockThread = new Thread(this);
|
939 |
|
|
// lockThread.setPriority(Thread.MIN_PRIORITY);
|
940 |
|
|
// lockThread.start();
|
941 |
|
|
logReplication.info("ReplicationService.handleGetLockRequest - lock granted for " + docid);
|
942 |
|
|
} else { //deny the lock
|
943 |
5755
|
leinfelder
|
out.write("<filelocked><docid>" + docid + "</docid></filelocked>");
|
944 |
5014
|
daigle
|
logReplication.info("ReplicationService.handleGetLockRequest - lock denied for " + docid
|
945 |
|
|
+ "reason: file already locked");
|
946 |
|
|
}
|
947 |
|
|
} else {//deny the lock.
|
948 |
5755
|
leinfelder
|
out.write("<outdatedfile><docid>" + docid + "</docid></filelocked>");
|
949 |
5014
|
daigle
|
logReplication.info("ReplicationService.handleGetLockRequest - lock denied for " + docid
|
950 |
|
|
+ "reason: client has outdated file");
|
951 |
|
|
}
|
952 |
5755
|
leinfelder
|
out.close();
|
953 |
5014
|
daigle
|
//conn.close();
|
954 |
|
|
} catch (Exception e) {
|
955 |
|
|
logMetacat.error("ReplicationService.handleGetLockRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);
|
956 |
|
|
logReplication.error("ReplicationService.handleGetLockRequest - error requesting file lock from MetacatReplication."
|
957 |
|
|
+ "handleGetLockRequest: " + e.getMessage());
|
958 |
|
|
e.printStackTrace(System.out);
|
959 |
|
|
}
|
960 |
|
|
}
|
961 |
2286
|
tao
|
|
962 |
5014
|
daigle
|
/**
|
963 |
|
|
* Sends all of the xml_documents information encoded in xml to a requestor
|
964 |
|
|
* the format is:
|
965 |
|
|
* <!ELEMENT documentinfo (docid, docname, doctype, doctitle, user_owner,
|
966 |
|
|
* user_updated, home_server, public_access, rev)/>
|
967 |
|
|
* all of the subelements of document info are #PCDATA
|
968 |
|
|
*/
|
969 |
5755
|
leinfelder
|
protected static void handleGetDocumentInfoRequest(
|
970 |
5014
|
daigle
|
Hashtable<String, String[]> params, HttpServletResponse response) {
|
971 |
|
|
String docid = ((String[]) (params.get("docid")))[0];
|
972 |
|
|
StringBuffer sb = new StringBuffer();
|
973 |
2286
|
tao
|
|
974 |
5014
|
daigle
|
try {
|
975 |
5322
|
berkley
|
IdentifierManager idman = IdentifierManager.getInstance();
|
976 |
2286
|
tao
|
|
977 |
5014
|
daigle
|
DocumentImpl doc = new DocumentImpl(docid);
|
978 |
|
|
sb.append("<documentinfo><docid>").append(docid);
|
979 |
5322
|
berkley
|
sb.append("</docid>");
|
980 |
|
|
try
|
981 |
|
|
{
|
982 |
5323
|
berkley
|
String guid = idman.getGUID(doc.getDocID(), doc.getRev());
|
983 |
|
|
sb.append("<guid>").append(guid).append("</guid>");
|
984 |
5454
|
berkley
|
String smLocalId = idman.getSystemMetadataLocalId(guid);
|
985 |
5895
|
berkley
|
Hashtable<String, String> sysmetaInfo = idman.getSystemMetadataInfo(smLocalId);
|
986 |
5454
|
berkley
|
if(smLocalId != null && !smLocalId.trim().equals(""))
|
987 |
|
|
{
|
988 |
|
|
sb.append("<systemmetadatalocalid>").append(smLocalId).append("</systemmetadatalocalid>");
|
989 |
|
|
}
|
990 |
5895
|
berkley
|
|
991 |
|
|
Enumeration<String> sysmetaKeys = sysmetaInfo.keys();
|
992 |
|
|
while(sysmetaKeys.hasMoreElements())
|
993 |
|
|
{
|
994 |
|
|
String key = sysmetaKeys.nextElement();
|
995 |
|
|
sb.append("<" + key + ">" + sysmetaInfo.get(key).toString() + "</" + key + ">");
|
996 |
|
|
}
|
997 |
|
|
|
998 |
5322
|
berkley
|
}
|
999 |
|
|
catch(McdbDocNotFoundException e)
|
1000 |
|
|
{
|
1001 |
|
|
//do nothing, there was no guid for this document
|
1002 |
|
|
}
|
1003 |
|
|
sb.append("<docname>").append(doc.getDocname());
|
1004 |
5014
|
daigle
|
sb.append("</docname><doctype>").append(doc.getDoctype());
|
1005 |
|
|
sb.append("</doctype>");
|
1006 |
|
|
sb.append("<user_owner>").append(doc.getUserowner());
|
1007 |
|
|
sb.append("</user_owner><user_updated>").append(doc.getUserupdated());
|
1008 |
|
|
sb.append("</user_updated>");
|
1009 |
|
|
sb.append("<date_created>");
|
1010 |
|
|
sb.append(doc.getCreateDate());
|
1011 |
|
|
sb.append("</date_created>");
|
1012 |
|
|
sb.append("<date_updated>");
|
1013 |
|
|
sb.append(doc.getUpdateDate());
|
1014 |
|
|
sb.append("</date_updated>");
|
1015 |
|
|
sb.append("<home_server>");
|
1016 |
|
|
sb.append(doc.getDocHomeServer());
|
1017 |
|
|
sb.append("</home_server>");
|
1018 |
|
|
sb.append("<public_access>").append(doc.getPublicaccess());
|
1019 |
|
|
sb.append("</public_access><rev>").append(doc.getRev());
|
1020 |
|
|
sb.append("</rev>");
|
1021 |
2286
|
tao
|
|
1022 |
5014
|
daigle
|
sb.append("<accessControl>");
|
1023 |
5098
|
daigle
|
|
1024 |
|
|
AccessControlForSingleFile acfsf = new AccessControlForSingleFile(docid);
|
1025 |
|
|
sb.append(acfsf.getAccessString());
|
1026 |
|
|
|
1027 |
5014
|
daigle
|
sb.append("</accessControl>");
|
1028 |
2286
|
tao
|
|
1029 |
5014
|
daigle
|
sb.append("</documentinfo>");
|
1030 |
5755
|
leinfelder
|
// get a writer for sending back to response
|
1031 |
5014
|
daigle
|
response.setContentType("text/xml");
|
1032 |
5755
|
leinfelder
|
Writer out = response.getWriter();
|
1033 |
|
|
out.write(sb.toString());
|
1034 |
|
|
out.close();
|
1035 |
2286
|
tao
|
|
1036 |
5014
|
daigle
|
} catch (Exception e) {
|
1037 |
|
|
logMetacat.error("ReplicationService.handleGetDocumentInfoRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);
|
1038 |
|
|
logReplication.error("ReplicationService.handleGetDocumentInfoRequest - error in metacatReplication.handlegetdocumentinforequest "
|
1039 |
|
|
+ "for doc: " + docid + " : " + e.getMessage());
|
1040 |
|
|
}
|
1041 |
2286
|
tao
|
|
1042 |
5014
|
daigle
|
}
|
1043 |
2286
|
tao
|
|
1044 |
5014
|
daigle
|
/**
|
1045 |
|
|
* Sends a datafile to a remote host
|
1046 |
|
|
*/
|
1047 |
|
|
protected static void handleGetDataFileRequest(OutputStream outPut,
|
1048 |
|
|
Hashtable<String, String[]> params, HttpServletResponse response)
|
1049 |
2286
|
tao
|
|
1050 |
5014
|
daigle
|
{
|
1051 |
|
|
// File path for data file
|
1052 |
|
|
String filepath;
|
1053 |
|
|
// Request docid
|
1054 |
|
|
String docId = ((String[]) (params.get("docid")))[0];
|
1055 |
|
|
//check if the doicd is null
|
1056 |
|
|
if (docId == null) {
|
1057 |
|
|
logMetacat.error("ReplicationService.handleGetDataFileRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);
|
1058 |
|
|
logReplication.error("ReplicationService.handleGetDataFileRequest - Didn't specify docid for replication");
|
1059 |
|
|
return;
|
1060 |
|
|
}
|
1061 |
2286
|
tao
|
|
1062 |
5014
|
daigle
|
//try to open a https stream to test if the request server's public key
|
1063 |
|
|
//in the key store, this is security issue
|
1064 |
|
|
try {
|
1065 |
|
|
filepath = PropertyService.getProperty("application.datafilepath");
|
1066 |
|
|
String server = params.get("server")[0];
|
1067 |
|
|
URL u = new URL("https://" + server + "?server="
|
1068 |
|
|
+ MetacatUtil.getLocalReplicationServerName() + "&action=test");
|
1069 |
|
|
String test = ReplicationService.getURLContent(u);
|
1070 |
|
|
//couldn't pass the test
|
1071 |
|
|
if (test.indexOf("successfully") == -1) {
|
1072 |
|
|
//response.setContentType("text/xml");
|
1073 |
|
|
//outPut.println("<error>Couldn't pass the trust test</error>");
|
1074 |
|
|
logMetacat.error("ReplicationService.handleGetDataFileRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);
|
1075 |
|
|
logReplication.error("ReplicationService.handleGetDataFileRequest - Couldn't pass the trust test");
|
1076 |
|
|
return;
|
1077 |
|
|
}
|
1078 |
|
|
}//try
|
1079 |
|
|
catch (Exception ee) {
|
1080 |
|
|
return;
|
1081 |
|
|
}//catch
|
1082 |
2286
|
tao
|
|
1083 |
5014
|
daigle
|
if (!filepath.endsWith("/")) {
|
1084 |
|
|
filepath += "/";
|
1085 |
|
|
}
|
1086 |
|
|
// Get file aboslute file name
|
1087 |
|
|
String filename = filepath + docId;
|
1088 |
2286
|
tao
|
|
1089 |
5014
|
daigle
|
//MIME type
|
1090 |
|
|
String contentType = null;
|
1091 |
|
|
if (filename.endsWith(".xml")) {
|
1092 |
|
|
contentType = "text/xml";
|
1093 |
|
|
} else if (filename.endsWith(".css")) {
|
1094 |
|
|
contentType = "text/css";
|
1095 |
|
|
} else if (filename.endsWith(".dtd")) {
|
1096 |
|
|
contentType = "text/plain";
|
1097 |
|
|
} else if (filename.endsWith(".xsd")) {
|
1098 |
|
|
contentType = "text/xml";
|
1099 |
|
|
} else if (filename.endsWith("/")) {
|
1100 |
|
|
contentType = "text/html";
|
1101 |
|
|
} else {
|
1102 |
|
|
File f = new File(filename);
|
1103 |
|
|
if (f.isDirectory()) {
|
1104 |
|
|
contentType = "text/html";
|
1105 |
|
|
} else {
|
1106 |
|
|
contentType = "application/octet-stream";
|
1107 |
|
|
}
|
1108 |
|
|
}
|
1109 |
2286
|
tao
|
|
1110 |
5014
|
daigle
|
// Set the mime type
|
1111 |
|
|
response.setContentType(contentType);
|
1112 |
2286
|
tao
|
|
1113 |
5014
|
daigle
|
// Get the content of the file
|
1114 |
|
|
FileInputStream fin = null;
|
1115 |
|
|
try {
|
1116 |
|
|
// FileInputStream to metacat
|
1117 |
|
|
fin = new FileInputStream(filename);
|
1118 |
|
|
// 4K buffer
|
1119 |
|
|
byte[] buf = new byte[4 * 1024];
|
1120 |
|
|
// Read data from file input stream to byte array
|
1121 |
|
|
int b = fin.read(buf);
|
1122 |
|
|
// Write to outStream from byte array
|
1123 |
|
|
while (b != -1) {
|
1124 |
|
|
outPut.write(buf, 0, b);
|
1125 |
|
|
b = fin.read(buf);
|
1126 |
|
|
}
|
1127 |
|
|
// close file input stream
|
1128 |
|
|
fin.close();
|
1129 |
2286
|
tao
|
|
1130 |
5014
|
daigle
|
}//try
|
1131 |
|
|
catch (Exception e) {
|
1132 |
|
|
logMetacat.error("ReplicationService.handleGetDataFileRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);
|
1133 |
|
|
logReplication.error("ReplicationService.handleGetDataFileRequest - error getting data file from MetacatReplication."
|
1134 |
|
|
+ "handlGetDataFileRequest " + e.getMessage());
|
1135 |
|
|
e.printStackTrace(System.out);
|
1136 |
|
|
}//catch
|
1137 |
2286
|
tao
|
|
1138 |
5014
|
daigle
|
}
|
1139 |
|
|
|
1140 |
|
|
/**
|
1141 |
4854
|
daigle
|
* Sends a document to a remote host
|
1142 |
|
|
*/
|
1143 |
5755
|
leinfelder
|
protected static void handleGetDocumentRequest(
|
1144 |
4854
|
daigle
|
Hashtable<String, String[]> params, HttpServletResponse response) {
|
1145 |
2286
|
tao
|
|
1146 |
4854
|
daigle
|
String urlString = null;
|
1147 |
|
|
String documentPath = null;
|
1148 |
5755
|
leinfelder
|
String errorMsg = null;
|
1149 |
4854
|
daigle
|
try {
|
1150 |
|
|
// try to open a https stream to test if the request server's public
|
1151 |
|
|
// key
|
1152 |
|
|
// in the key store, this is security issue
|
1153 |
|
|
String server = params.get("server")[0];
|
1154 |
|
|
urlString = "https://" + server + "?server="
|
1155 |
|
|
+ MetacatUtil.getLocalReplicationServerName() + "&action=test";
|
1156 |
|
|
URL u = new URL(urlString);
|
1157 |
5014
|
daigle
|
String test = ReplicationService.getURLContent(u);
|
1158 |
4854
|
daigle
|
// couldn't pass the test
|
1159 |
|
|
if (test.indexOf("successfully") == -1) {
|
1160 |
|
|
response.setContentType("text/xml");
|
1161 |
5755
|
leinfelder
|
Writer out = response.getWriter();
|
1162 |
|
|
out.write("<error>Couldn't pass the trust test " + test + " </error>");
|
1163 |
4854
|
daigle
|
out.close();
|
1164 |
|
|
return;
|
1165 |
|
|
}
|
1166 |
2286
|
tao
|
|
1167 |
4854
|
daigle
|
String docid = params.get("docid")[0];
|
1168 |
5014
|
daigle
|
logReplication.debug("ReplicationService.handleGetDocumentRequest - MetacatReplication.handleGetDocumentRequest for docid: "
|
1169 |
|
|
+ docid);
|
1170 |
4854
|
daigle
|
DocumentImpl di = new DocumentImpl(docid);
|
1171 |
2286
|
tao
|
|
1172 |
5014
|
daigle
|
String documentDir = PropertyService
|
1173 |
|
|
.getProperty("application.documentfilepath");
|
1174 |
4854
|
daigle
|
documentPath = documentDir + FileUtil.getFS() + docid;
|
1175 |
4488
|
daigle
|
|
1176 |
4854
|
daigle
|
// if the document does not exist on disk, read it from db and write
|
1177 |
|
|
// it to disk.
|
1178 |
|
|
if (FileUtil.getFileStatus(documentPath) == FileUtil.DOES_NOT_EXIST
|
1179 |
|
|
|| FileUtil.getFileSize(documentPath) == 0) {
|
1180 |
5752
|
leinfelder
|
FileOutputStream fos = new FileOutputStream(documentPath);
|
1181 |
|
|
di.toXml(fos, null, null, true);
|
1182 |
4854
|
daigle
|
}
|
1183 |
2286
|
tao
|
|
1184 |
5752
|
leinfelder
|
// read the file from disk and send it to outputstream
|
1185 |
|
|
OutputStream outputStream = response.getOutputStream();
|
1186 |
|
|
di.readFromFileSystem(outputStream, null, null, documentPath);
|
1187 |
2286
|
tao
|
|
1188 |
5014
|
daigle
|
logReplication.info("ReplicationService.handleGetDocumentRequest - document " + docid + " sent");
|
1189 |
4854
|
daigle
|
|
1190 |
5755
|
leinfelder
|
// return to avoid continuing to the error reporting section at the end
|
1191 |
|
|
return;
|
1192 |
|
|
|
1193 |
4854
|
daigle
|
} catch (MalformedURLException mue) {
|
1194 |
5014
|
daigle
|
logMetacat.error("ReplicationService.handleGetDocumentRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);
|
1195 |
|
|
logReplication.error("ReplicationService.handleGetDocumentRequest - Url error when getting document from MetacatReplication."
|
1196 |
4854
|
daigle
|
+ "handlGetDocumentRequest for url: " + urlString + " : "
|
1197 |
|
|
+ mue.getMessage());
|
1198 |
|
|
// e.printStackTrace(System.out);
|
1199 |
5755
|
leinfelder
|
|
1200 |
4854
|
daigle
|
} catch (IOException ioe) {
|
1201 |
5014
|
daigle
|
logMetacat.error("ReplicationService.handleGetDocumentRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);
|
1202 |
|
|
logReplication.error("ReplicationService.handleGetDocumentRequest - I/O error when getting document from MetacatReplication."
|
1203 |
|
|
+ "handlGetDocumentRequest for file: " + documentPath + " : "
|
1204 |
|
|
+ ioe.getMessage());
|
1205 |
5755
|
leinfelder
|
errorMsg = ioe.getMessage();
|
1206 |
4854
|
daigle
|
} catch (PropertyNotFoundException pnfe) {
|
1207 |
5014
|
daigle
|
logMetacat.error("ReplicationService.handleGetDocumentRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);
|
1208 |
|
|
logReplication
|
1209 |
|
|
.error("ReplicationService.handleGetDocumentRequest - Error getting property when getting document from MetacatReplication."
|
1210 |
|
|
+ "handlGetDocumentRequest for file: "
|
1211 |
|
|
+ documentPath
|
1212 |
|
|
+ " : "
|
1213 |
|
|
+ pnfe.getMessage());
|
1214 |
4854
|
daigle
|
// e.printStackTrace(System.out);
|
1215 |
5755
|
leinfelder
|
errorMsg = pnfe.getMessage();
|
1216 |
4854
|
daigle
|
} catch (McdbException me) {
|
1217 |
5014
|
daigle
|
logReplication
|
1218 |
|
|
.error("ReplicationService.handleGetDocumentRequest - Document implementation error getting property when getting document from MetacatReplication."
|
1219 |
|
|
+ "handlGetDocumentRequest for file: "
|
1220 |
|
|
+ documentPath
|
1221 |
|
|
+ " : "
|
1222 |
|
|
+ me.getMessage());
|
1223 |
4854
|
daigle
|
// e.printStackTrace(System.out);
|
1224 |
5755
|
leinfelder
|
errorMsg = me.getMessage();
|
1225 |
4854
|
daigle
|
}
|
1226 |
5755
|
leinfelder
|
|
1227 |
|
|
// report any errors if we got here
|
1228 |
|
|
response.setContentType("text/xml");
|
1229 |
|
|
Writer out = null;
|
1230 |
|
|
try {
|
1231 |
|
|
response.getWriter();
|
1232 |
|
|
out = response.getWriter();
|
1233 |
|
|
out.write("<error>" + errorMsg + "</error>");
|
1234 |
|
|
} catch (Exception e) {
|
1235 |
|
|
logMetacat.error(e.getMessage(), e);
|
1236 |
|
|
} finally {
|
1237 |
|
|
try {
|
1238 |
|
|
out.close();
|
1239 |
|
|
} catch (IOException e) {
|
1240 |
|
|
logMetacat.error(e.getMessage(), e);
|
1241 |
|
|
}
|
1242 |
|
|
}
|
1243 |
|
|
|
1244 |
4854
|
daigle
|
|
1245 |
|
|
}
|
1246 |
|
|
|
1247 |
5014
|
daigle
|
/**
|
1248 |
4854
|
daigle
|
* Sends a list of all of the documents on this sever along with their
|
1249 |
|
|
* revision numbers. The format is: <!ELEMENT replication (server, updates)>
|
1250 |
|
|
* <!ELEMENT server (#PCDATA)> <!ELEMENT updates ((updatedDocument |
|
1251 |
|
|
* deleteDocument | revisionDocument)*)> <!ELEMENT updatedDocument (docid,
|
1252 |
|
|
* rev, datafile*)> <!ELEMENT deletedDocument (docid, rev)> <!ELEMENT
|
1253 |
|
|
* revisionDocument (docid, rev, datafile*)> <!ELEMENT docid (#PCDATA)>
|
1254 |
|
|
* <!ELEMENT rev (#PCDATA)> <!ELEMENT datafile (#PCDATA)> note that the rev
|
1255 |
|
|
* in deletedDocument is always empty. I just left it in there to make the
|
1256 |
|
|
* parser implementation easier.
|
1257 |
|
|
*/
|
1258 |
5755
|
leinfelder
|
protected static void handleUpdateRequest(Hashtable<String, String[]> params,
|
1259 |
5014
|
daigle
|
HttpServletResponse response) {
|
1260 |
|
|
// Checked out DBConnection
|
1261 |
|
|
DBConnection dbConn = null;
|
1262 |
|
|
// DBConenction serial number when checked it out
|
1263 |
|
|
int serialNumber = -1;
|
1264 |
|
|
PreparedStatement pstmt = null;
|
1265 |
|
|
// Server list to store server info of xml_replication table
|
1266 |
|
|
ReplicationServerList serverList = null;
|
1267 |
5755
|
leinfelder
|
|
1268 |
|
|
// a writer for response
|
1269 |
|
|
Writer out = null;
|
1270 |
2286
|
tao
|
|
1271 |
5014
|
daigle
|
try {
|
1272 |
5755
|
leinfelder
|
// get writer, TODO: encoding?
|
1273 |
|
|
response.setContentType("text/xml");
|
1274 |
|
|
out = response.getWriter();
|
1275 |
|
|
|
1276 |
5014
|
daigle
|
// Check out a DBConnection from pool
|
1277 |
|
|
dbConn = DBConnectionPool
|
1278 |
|
|
.getDBConnection("MetacatReplication.handleUpdateRequest");
|
1279 |
|
|
serialNumber = dbConn.getCheckOutSerialNumber();
|
1280 |
|
|
// Create a server list from xml_replication table
|
1281 |
|
|
serverList = new ReplicationServerList();
|
1282 |
2286
|
tao
|
|
1283 |
5014
|
daigle
|
// Get remote server name from param
|
1284 |
|
|
String server = ((String[]) params.get("server"))[0];
|
1285 |
|
|
// If no servr name in param, return a error
|
1286 |
|
|
if (server == null || server.equals("")) {
|
1287 |
5755
|
leinfelder
|
out.write("<error>Request didn't specify server name</error>");
|
1288 |
5014
|
daigle
|
out.close();
|
1289 |
|
|
return;
|
1290 |
|
|
}//if
|
1291 |
2286
|
tao
|
|
1292 |
5014
|
daigle
|
//try to open a https stream to test if the request server's public key
|
1293 |
|
|
//in the key store, this is security issue
|
1294 |
5440
|
berkley
|
String testUrl = "https://" + server + "?server="
|
1295 |
5441
|
berkley
|
+ MetacatUtil.getLocalReplicationServerName() + "&action=test";
|
1296 |
5440
|
berkley
|
logReplication.info("Running trust test: " + testUrl);
|
1297 |
|
|
URL u = new URL(testUrl);
|
1298 |
5014
|
daigle
|
String test = ReplicationService.getURLContent(u);
|
1299 |
5441
|
berkley
|
logReplication.info("Ouput from test is '" + test + "'");
|
1300 |
5014
|
daigle
|
//couldn't pass the test
|
1301 |
|
|
if (test.indexOf("successfully") == -1) {
|
1302 |
5441
|
berkley
|
logReplication.error("Trust test failed.");
|
1303 |
5755
|
leinfelder
|
out.write("<error>Couldn't pass the trust test</error>");
|
1304 |
5014
|
daigle
|
out.close();
|
1305 |
|
|
return;
|
1306 |
|
|
}
|
1307 |
5441
|
berkley
|
logReplication.info("Trust test succeeded.");
|
1308 |
2286
|
tao
|
|
1309 |
5014
|
daigle
|
// Check if local host configure to replicate xml documents to remote
|
1310 |
|
|
// server. If not send back a error message
|
1311 |
|
|
if (!serverList.getReplicationValue(server)) {
|
1312 |
5755
|
leinfelder
|
out.write("<error>Configuration not allow to replicate document to you</error>");
|
1313 |
5014
|
daigle
|
out.close();
|
1314 |
|
|
return;
|
1315 |
|
|
}//if
|
1316 |
2286
|
tao
|
|
1317 |
5014
|
daigle
|
// Store the sql command
|
1318 |
|
|
StringBuffer docsql = new StringBuffer();
|
1319 |
|
|
StringBuffer revisionSql = new StringBuffer();
|
1320 |
|
|
// Stroe the docid list
|
1321 |
|
|
StringBuffer doclist = new StringBuffer();
|
1322 |
|
|
// Store the deleted docid list
|
1323 |
|
|
StringBuffer delsql = new StringBuffer();
|
1324 |
|
|
// Store the data set file
|
1325 |
|
|
Vector<Vector<String>> packageFiles = new Vector<Vector<String>>();
|
1326 |
2286
|
tao
|
|
1327 |
5014
|
daigle
|
// Append local server's name and replication servlet to doclist
|
1328 |
|
|
doclist.append("<?xml version=\"1.0\"?><replication>");
|
1329 |
|
|
doclist.append("<server>")
|
1330 |
|
|
.append(MetacatUtil.getLocalReplicationServerName());
|
1331 |
|
|
//doclist.append(util.getProperty("replicationpath"));
|
1332 |
|
|
doclist.append("</server><updates>");
|
1333 |
2286
|
tao
|
|
1334 |
5014
|
daigle
|
// Get correct docid that reside on this server according the requesting
|
1335 |
|
|
// server's replicate and data replicate value in xml_replication table
|
1336 |
5319
|
jones
|
docsql.append(DatabaseService.getInstance().getDBAdapter().getReplicationDocumentListSQL());
|
1337 |
5014
|
daigle
|
//docsql.append("select docid, rev, doctype from xml_documents where (docid not in (select a.docid from xml_documents a, xml_revisions b where a.docid=b.docid and a.rev<=b.rev)) ");
|
1338 |
|
|
revisionSql.append("select docid, rev, doctype from xml_revisions ");
|
1339 |
|
|
// If the localhost is not a hub to the remote server, only replicate
|
1340 |
|
|
// the docid' which home server is local host (server_location =1)
|
1341 |
|
|
if (!serverList.getHubValue(server)) {
|
1342 |
|
|
String serverLocationDoc = " and a.server_location = 1";
|
1343 |
|
|
String serverLocationRev = "where server_location = 1";
|
1344 |
|
|
docsql.append(serverLocationDoc);
|
1345 |
|
|
revisionSql.append(serverLocationRev);
|
1346 |
|
|
}
|
1347 |
|
|
logReplication.info("ReplicationService.handleUpdateRequest - Doc sql: " + docsql.toString());
|
1348 |
2286
|
tao
|
|
1349 |
5014
|
daigle
|
// Get any deleted documents
|
1350 |
|
|
delsql.append("select distinct docid from ");
|
1351 |
|
|
delsql.append("xml_revisions where docid not in (select docid from ");
|
1352 |
|
|
delsql.append("xml_documents) ");
|
1353 |
|
|
// If the localhost is not a hub to the remote server, only replicate
|
1354 |
|
|
// the docid' which home server is local host (server_location =1)
|
1355 |
|
|
if (!serverList.getHubValue(server)) {
|
1356 |
|
|
delsql.append("and server_location = 1");
|
1357 |
|
|
}
|
1358 |
|
|
logReplication.info("ReplicationService.handleUpdateRequest - Deleted sql: " + delsql.toString());
|
1359 |
2286
|
tao
|
|
1360 |
5014
|
daigle
|
// Get docid list of local host
|
1361 |
|
|
pstmt = dbConn.prepareStatement(docsql.toString());
|
1362 |
|
|
pstmt.execute();
|
1363 |
|
|
ResultSet rs = pstmt.getResultSet();
|
1364 |
|
|
boolean tablehasrows = rs.next();
|
1365 |
|
|
//If metacat configed to replicate data file
|
1366 |
|
|
//if ((util.getProperty("replicationsenddata")).equals("on"))
|
1367 |
|
|
boolean replicateData = serverList.getDataReplicationValue(server);
|
1368 |
|
|
if (replicateData) {
|
1369 |
|
|
while (tablehasrows) {
|
1370 |
|
|
String recordDoctype = rs.getString(3);
|
1371 |
|
|
Vector<String> packagedoctypes = MetacatUtil
|
1372 |
|
|
.getOptionList(PropertyService
|
1373 |
|
|
.getProperty("xml.packagedoctype"));
|
1374 |
|
|
//if this is a package file, put it at the end
|
1375 |
|
|
//because if a package file is read before all of the files it
|
1376 |
|
|
//refers to are loaded then there is an error
|
1377 |
|
|
if (recordDoctype != null && !packagedoctypes.contains(recordDoctype)) {
|
1378 |
|
|
//If this is not data file
|
1379 |
|
|
if (!recordDoctype.equals("BIN")) {
|
1380 |
|
|
//for non-data file document
|
1381 |
|
|
doclist.append("<updatedDocument>");
|
1382 |
|
|
doclist.append("<docid>").append(rs.getString(1));
|
1383 |
|
|
doclist.append("</docid><rev>").append(rs.getInt(2));
|
1384 |
|
|
doclist.append("</rev>");
|
1385 |
|
|
doclist.append("</updatedDocument>");
|
1386 |
|
|
}//if
|
1387 |
|
|
else {
|
1388 |
|
|
//for data file document, in datafile attributes
|
1389 |
|
|
//we put "datafile" value there
|
1390 |
|
|
doclist.append("<updatedDocument>");
|
1391 |
|
|
doclist.append("<docid>").append(rs.getString(1));
|
1392 |
|
|
doclist.append("</docid><rev>").append(rs.getInt(2));
|
1393 |
|
|
doclist.append("</rev>");
|
1394 |
|
|
doclist.append("<datafile>");
|
1395 |
|
|
doclist.append(PropertyService
|
1396 |
|
|
.getProperty("replication.datafileflag"));
|
1397 |
|
|
doclist.append("</datafile>");
|
1398 |
|
|
doclist.append("</updatedDocument>");
|
1399 |
|
|
}//else
|
1400 |
|
|
}//if packagedoctpes
|
1401 |
|
|
else { //the package files are saved to be put into the xml later.
|
1402 |
|
|
Vector<String> v = new Vector<String>();
|
1403 |
|
|
v.add(rs.getString(1));
|
1404 |
|
|
v.add(String.valueOf(rs.getInt(2)));
|
1405 |
|
|
packageFiles.add(v);
|
1406 |
|
|
}//esle
|
1407 |
|
|
tablehasrows = rs.next();
|
1408 |
|
|
}//while
|
1409 |
|
|
}//if
|
1410 |
|
|
else //metacat was configured not to send data file
|
1411 |
|
|
{
|
1412 |
|
|
while (tablehasrows) {
|
1413 |
|
|
String recordDoctype = rs.getString(3);
|
1414 |
|
|
if (!recordDoctype.equals("BIN")) { //don't replicate data files
|
1415 |
|
|
Vector<String> packagedoctypes = MetacatUtil
|
1416 |
|
|
.getOptionList(PropertyService
|
1417 |
|
|
.getProperty("xml.packagedoctype"));
|
1418 |
|
|
if (recordDoctype != null
|
1419 |
|
|
&& !packagedoctypes.contains(recordDoctype)) { //if this is a package file, put it at the end
|
1420 |
|
|
//because if a package file is read before all of the files it
|
1421 |
|
|
//refers to are loaded then there is an error
|
1422 |
|
|
doclist.append("<updatedDocument>");
|
1423 |
|
|
doclist.append("<docid>").append(rs.getString(1));
|
1424 |
|
|
doclist.append("</docid><rev>").append(rs.getInt(2));
|
1425 |
|
|
doclist.append("</rev>");
|
1426 |
|
|
doclist.append("</updatedDocument>");
|
1427 |
|
|
} else { //the package files are saved to be put into the xml later.
|
1428 |
|
|
Vector<String> v = new Vector<String>();
|
1429 |
|
|
v.add(rs.getString(1));
|
1430 |
|
|
v.add(String.valueOf(rs.getInt(2)));
|
1431 |
|
|
packageFiles.add(v);
|
1432 |
|
|
}
|
1433 |
|
|
}//if
|
1434 |
|
|
tablehasrows = rs.next();
|
1435 |
|
|
}//while
|
1436 |
|
|
}//else
|
1437 |
2286
|
tao
|
|
1438 |
5014
|
daigle
|
pstmt = dbConn.prepareStatement(delsql.toString());
|
1439 |
|
|
//usage count should increas 1
|
1440 |
|
|
dbConn.increaseUsageCount(1);
|
1441 |
2286
|
tao
|
|
1442 |
5014
|
daigle
|
pstmt.execute();
|
1443 |
|
|
rs = pstmt.getResultSet();
|
1444 |
|
|
tablehasrows = rs.next();
|
1445 |
|
|
while (tablehasrows) { //handle the deleted documents
|
1446 |
|
|
doclist.append("<deletedDocument><docid>").append(rs.getString(1));
|
1447 |
|
|
doclist.append("</docid><rev></rev></deletedDocument>");
|
1448 |
|
|
//note that rev is always empty for deleted docs
|
1449 |
|
|
tablehasrows = rs.next();
|
1450 |
|
|
}
|
1451 |
2286
|
tao
|
|
1452 |
5014
|
daigle
|
//now we can put the package files into the xml results
|
1453 |
|
|
for (int i = 0; i < packageFiles.size(); i++) {
|
1454 |
|
|
Vector<String> v = packageFiles.elementAt(i);
|
1455 |
|
|
doclist.append("<updatedDocument>");
|
1456 |
|
|
doclist.append("<docid>").append(v.elementAt(0));
|
1457 |
|
|
doclist.append("</docid><rev>");
|
1458 |
|
|
doclist.append(v.elementAt(1));
|
1459 |
|
|
doclist.append("</rev>");
|
1460 |
|
|
doclist.append("</updatedDocument>");
|
1461 |
|
|
}
|
1462 |
|
|
// add revision doc list
|
1463 |
|
|
doclist.append(prepareRevisionDoc(dbConn, revisionSql.toString(),
|
1464 |
|
|
replicateData));
|
1465 |
2286
|
tao
|
|
1466 |
5014
|
daigle
|
doclist.append("</updates></replication>");
|
1467 |
|
|
logReplication.info("ReplicationService.handleUpdateRequest - doclist: " + doclist.toString());
|
1468 |
|
|
pstmt.close();
|
1469 |
|
|
//conn.close();
|
1470 |
5755
|
leinfelder
|
out.write(doclist.toString());
|
1471 |
2286
|
tao
|
|
1472 |
5014
|
daigle
|
} catch (Exception e) {
|
1473 |
|
|
logMetacat.error("ReplicationService.handleUpdateRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);
|
1474 |
|
|
logReplication.error("ReplicationService.handleUpdateRequest - error in MetacatReplication." + "handleupdaterequest: "
|
1475 |
|
|
+ e.getMessage());
|
1476 |
|
|
//e.printStackTrace(System.out);
|
1477 |
5755
|
leinfelder
|
try {
|
1478 |
|
|
out.write("<error>" + e.getMessage() + "</error>");
|
1479 |
|
|
} catch (IOException e1) {
|
1480 |
|
|
logMetacat.error(e1.getMessage(), e1);
|
1481 |
|
|
}
|
1482 |
5014
|
daigle
|
} finally {
|
1483 |
|
|
try {
|
1484 |
|
|
pstmt.close();
|
1485 |
|
|
}//try
|
1486 |
|
|
catch (SQLException ee) {
|
1487 |
|
|
logMetacat.error("ReplicationService.handleUpdateRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);
|
1488 |
|
|
logReplication.error("ReplicationService.handleUpdateRequest - Error in MetacatReplication."
|
1489 |
|
|
+ "handleUpdaterequest to close pstmt: " + ee.getMessage());
|
1490 |
|
|
}//catch
|
1491 |
|
|
finally {
|
1492 |
|
|
DBConnectionPool.returnDBConnection(dbConn, serialNumber);
|
1493 |
|
|
}//finally
|
1494 |
5755
|
leinfelder
|
try {
|
1495 |
|
|
out.close();
|
1496 |
|
|
} catch (IOException e) {
|
1497 |
|
|
logMetacat.error(e.getMessage(), e);
|
1498 |
|
|
}
|
1499 |
5014
|
daigle
|
}//finally
|
1500 |
2286
|
tao
|
|
1501 |
5014
|
daigle
|
}//handlUpdateRequest
|
1502 |
2286
|
tao
|
|
1503 |
6015
|
leinfelder
|
/**
|
1504 |
|
|
*
|
1505 |
|
|
* @param dbConn connection for doing the update
|
1506 |
|
|
* @param docid the document id to update
|
1507 |
|
|
* @param owner the user_owner
|
1508 |
|
|
* @param updater the user_updated
|
1509 |
|
|
* @throws SQLException
|
1510 |
|
|
*/
|
1511 |
|
|
public static void updateUserOwner(DBConnection dbConn, String docid, String owner, String updater) throws SQLException {
|
1512 |
|
|
|
1513 |
|
|
String sql =
|
1514 |
|
|
"UPDATE xml_documents " +
|
1515 |
|
|
"SET user_owner = ?, " +
|
1516 |
|
|
"user_updated = ? " +
|
1517 |
|
|
"WHERE docid = ?;";
|
1518 |
|
|
PreparedStatement pstmt = dbConn.prepareStatement(sql);
|
1519 |
|
|
//usage count should increas 1
|
1520 |
|
|
dbConn.increaseUsageCount(1);
|
1521 |
|
|
|
1522 |
|
|
docid = DocumentUtil.getSmartDocId(docid);
|
1523 |
|
|
pstmt.setString(1, owner);
|
1524 |
|
|
pstmt.setString(2, updater);
|
1525 |
|
|
pstmt.setString(3, docid);
|
1526 |
|
|
pstmt.execute();
|
1527 |
|
|
pstmt.close();
|
1528 |
|
|
|
1529 |
|
|
dbConn.commit();
|
1530 |
|
|
}
|
1531 |
|
|
|
1532 |
5014
|
daigle
|
/*
|
1533 |
|
|
* This method will get the xml string for document in xml_revision
|
1534 |
|
|
* The schema look like <!ELEMENT revisionDocument (docid, rev, datafile*)>
|
1535 |
|
|
*/
|
1536 |
|
|
private static String prepareRevisionDoc(DBConnection dbConn, String revSql,
|
1537 |
|
|
boolean replicateData) throws Exception {
|
1538 |
|
|
logReplication.warn("ReplicationService.prepareRevisionDoc - The revision document sql is " + revSql);
|
1539 |
|
|
StringBuffer revDocList = new StringBuffer();
|
1540 |
|
|
PreparedStatement pstmt = dbConn.prepareStatement(revSql);
|
1541 |
|
|
//usage count should increas 1
|
1542 |
|
|
dbConn.increaseUsageCount(1);
|
1543 |
2286
|
tao
|
|
1544 |
5014
|
daigle
|
pstmt.execute();
|
1545 |
|
|
ResultSet rs = pstmt.getResultSet();
|
1546 |
|
|
boolean tablehasrows = rs.next();
|
1547 |
|
|
while (tablehasrows) {
|
1548 |
|
|
String recordDoctype = rs.getString(3);
|
1549 |
2286
|
tao
|
|
1550 |
5014
|
daigle
|
//If this is data file and it isn't configured to replicate data
|
1551 |
|
|
if (recordDoctype.equals("BIN") && !replicateData) {
|
1552 |
|
|
// do nothing
|
1553 |
|
|
continue;
|
1554 |
|
|
} else {
|
1555 |
2597
|
tao
|
|
1556 |
5014
|
daigle
|
revDocList.append("<revisionDocument>");
|
1557 |
|
|
revDocList.append("<docid>").append(rs.getString(1));
|
1558 |
|
|
revDocList.append("</docid><rev>").append(rs.getInt(2));
|
1559 |
|
|
revDocList.append("</rev>");
|
1560 |
|
|
// data file
|
1561 |
|
|
if (recordDoctype.equals("BIN")) {
|
1562 |
|
|
revDocList.append("<datafile>");
|
1563 |
|
|
revDocList.append(PropertyService
|
1564 |
|
|
.getProperty("replication.datafileflag"));
|
1565 |
|
|
revDocList.append("</datafile>");
|
1566 |
|
|
}
|
1567 |
|
|
revDocList.append("</revisionDocument>");
|
1568 |
2286
|
tao
|
|
1569 |
5014
|
daigle
|
}//else
|
1570 |
|
|
tablehasrows = rs.next();
|
1571 |
|
|
}
|
1572 |
|
|
//System.out.println("The revision list is"+ revDocList.toString());
|
1573 |
|
|
return revDocList.toString();
|
1574 |
|
|
}
|
1575 |
2286
|
tao
|
|
1576 |
5014
|
daigle
|
/**
|
1577 |
|
|
* Returns the xml_catalog table encoded in xml
|
1578 |
|
|
*/
|
1579 |
|
|
public static String getCatalogXML() {
|
1580 |
5755
|
leinfelder
|
return handleGetCatalogRequest(null, null, false);
|
1581 |
5014
|
daigle
|
}
|
1582 |
2286
|
tao
|
|
1583 |
5014
|
daigle
|
/**
|
1584 |
|
|
* Sends the contents of the xml_catalog table encoded in xml
|
1585 |
|
|
* The xml format is:
|
1586 |
|
|
* <!ELEMENT xml_catalog (row*)>
|
1587 |
|
|
* <!ELEMENT row (entry_type, source_doctype, target_doctype, public_id,
|
1588 |
|
|
* system_id)>
|
1589 |
|
|
* All of the sub elements of row are #PCDATA
|
1590 |
2286
|
tao
|
|
1591 |
5014
|
daigle
|
* If printFlag == false then do not print to out.
|
1592 |
|
|
*/
|
1593 |
5755
|
leinfelder
|
protected static String handleGetCatalogRequest(
|
1594 |
5014
|
daigle
|
Hashtable<String, String[]> params, HttpServletResponse response,
|
1595 |
|
|
boolean printFlag) {
|
1596 |
|
|
DBConnection dbConn = null;
|
1597 |
|
|
int serialNumber = -1;
|
1598 |
|
|
PreparedStatement pstmt = null;
|
1599 |
5755
|
leinfelder
|
Writer out = null;
|
1600 |
5014
|
daigle
|
try {
|
1601 |
5755
|
leinfelder
|
// get writer, TODO: encoding?
|
1602 |
5944
|
berkley
|
if(printFlag)
|
1603 |
|
|
{
|
1604 |
|
|
response.setContentType("text/xml");
|
1605 |
|
|
out = response.getWriter();
|
1606 |
|
|
}
|
1607 |
5014
|
daigle
|
/*conn = MetacatReplication.getDBConnection("MetacatReplication." +
|
1608 |
|
|
"handleGetCatalogRequest");*/
|
1609 |
|
|
dbConn = DBConnectionPool
|
1610 |
|
|
.getDBConnection("MetacatReplication.handleGetCatalogRequest");
|
1611 |
|
|
serialNumber = dbConn.getCheckOutSerialNumber();
|
1612 |
|
|
pstmt = dbConn.prepareStatement("select entry_type, "
|
1613 |
|
|
+ "source_doctype, target_doctype, public_id, "
|
1614 |
|
|
+ "system_id from xml_catalog");
|
1615 |
|
|
pstmt.execute();
|
1616 |
|
|
ResultSet rs = pstmt.getResultSet();
|
1617 |
|
|
boolean tablehasrows = rs.next();
|
1618 |
|
|
StringBuffer sb = new StringBuffer();
|
1619 |
|
|
sb.append("<?xml version=\"1.0\"?><xml_catalog>");
|
1620 |
|
|
while (tablehasrows) {
|
1621 |
|
|
sb.append("<row><entry_type>").append(rs.getString(1));
|
1622 |
|
|
sb.append("</entry_type><source_doctype>").append(rs.getString(2));
|
1623 |
|
|
sb.append("</source_doctype><target_doctype>").append(rs.getString(3));
|
1624 |
|
|
sb.append("</target_doctype><public_id>").append(rs.getString(4));
|
1625 |
|
|
// system id may not have server url on front. Add it if not.
|
1626 |
|
|
String systemID = rs.getString(5);
|
1627 |
|
|
if (!systemID.startsWith("http://")) {
|
1628 |
|
|
systemID = SystemUtil.getContextURL() + systemID;
|
1629 |
|
|
}
|
1630 |
|
|
sb.append("</public_id><system_id>").append(systemID);
|
1631 |
|
|
sb.append("</system_id></row>");
|
1632 |
2286
|
tao
|
|
1633 |
5014
|
daigle
|
tablehasrows = rs.next();
|
1634 |
|
|
}
|
1635 |
|
|
sb.append("</xml_catalog>");
|
1636 |
|
|
//conn.close();
|
1637 |
|
|
if (printFlag) {
|
1638 |
|
|
response.setContentType("text/xml");
|
1639 |
5755
|
leinfelder
|
out.write(sb.toString());
|
1640 |
5014
|
daigle
|
}
|
1641 |
|
|
pstmt.close();
|
1642 |
|
|
return sb.toString();
|
1643 |
|
|
} catch (Exception e) {
|
1644 |
|
|
logMetacat.error("ReplicationService.handleGetCatalogRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);
|
1645 |
|
|
logReplication.error("ReplicationService.handleGetCatalogRequest - error in MetacatReplication.handleGetCatalogRequest:"
|
1646 |
|
|
+ e.getMessage());
|
1647 |
|
|
e.printStackTrace(System.out);
|
1648 |
|
|
if (printFlag) {
|
1649 |
5755
|
leinfelder
|
try {
|
1650 |
|
|
out.write("<error>" + e.getMessage() + "</error>");
|
1651 |
|
|
} catch (IOException e1) {
|
1652 |
|
|
logMetacat.error(e1.getMessage(), e1);
|
1653 |
|
|
}
|
1654 |
5014
|
daigle
|
}
|
1655 |
|
|
} finally {
|
1656 |
|
|
try {
|
1657 |
|
|
pstmt.close();
|
1658 |
|
|
}//try
|
1659 |
|
|
catch (SQLException ee) {
|
1660 |
|
|
logMetacat.error("ReplicationService.handleGetCatalogRequest - " + ReplicationService.METACAT_REPL_ERROR_MSG);
|
1661 |
|
|
logReplication.error("ReplicationService.handleGetCatalogRequest - Error in MetacatReplication.handleGetCatalogRequest: "
|
1662 |
|
|
+ ee.getMessage());
|
1663 |
|
|
}//catch
|
1664 |
|
|
finally {
|
1665 |
|
|
DBConnectionPool.returnDBConnection(dbConn, serialNumber);
|
1666 |
|
|
}//finally
|
1667 |
5755
|
leinfelder
|
if (out != null) {
|
1668 |
|
|
try {
|
1669 |
|
|
out.close();
|
1670 |
|
|
} catch (IOException e1) {
|
1671 |
|
|
logMetacat.error(e1.getMessage(), e1);
|
1672 |
|
|
}
|
1673 |
|
|
}
|
1674 |
5014
|
daigle
|
}//finally
|
1675 |
2286
|
tao
|
|
1676 |
5014
|
daigle
|
return null;
|
1677 |
|
|
}
|
1678 |
2286
|
tao
|
|
1679 |
5014
|
daigle
|
/**
|
1680 |
|
|
* Sends the current system date to the remote server. Using this action
|
1681 |
|
|
* for replication gets rid of any problems with syncronizing clocks
|
1682 |
|
|
* because a time specific to a document is always kept on its home server.
|
1683 |
|
|
*/
|
1684 |
5755
|
leinfelder
|
protected static void handleGetTimeRequest(
|
1685 |
5014
|
daigle
|
Hashtable<String, String[]> params, HttpServletResponse response) {
|
1686 |
|
|
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yy HH:mm:ss");
|
1687 |
|
|
java.util.Date localtime = new java.util.Date();
|
1688 |
|
|
String dateString = formatter.format(localtime);
|
1689 |
5755
|
leinfelder
|
|
1690 |
|
|
// get a writer for sending back to response
|
1691 |
5014
|
daigle
|
response.setContentType("text/xml");
|
1692 |
5755
|
leinfelder
|
Writer out = null;
|
1693 |
|
|
try {
|
1694 |
|
|
out = response.getWriter();
|
1695 |
|
|
out.write("<timestamp>" + dateString + "</timestamp>");
|
1696 |
|
|
out.close();
|
1697 |
|
|
} catch (IOException e) {
|
1698 |
|
|
logMetacat.error(e.getMessage(), e);
|
1699 |
|
|
}
|
1700 |
|
|
|
1701 |
5014
|
daigle
|
}
|
1702 |
2286
|
tao
|
|
1703 |
5014
|
daigle
|
/**
|
1704 |
|
|
* this method handles the timeout for a file lock. when a lock is
|
1705 |
|
|
* granted it is granted for 30 seconds. When this thread runs out
|
1706 |
|
|
* it deletes the docid from the queue, thus eliminating the lock.
|
1707 |
|
|
*/
|
1708 |
|
|
public void run() {
|
1709 |
|
|
try {
|
1710 |
|
|
logReplication.info("ReplicationService.run - thread started for docid: "
|
1711 |
|
|
+ (String) fileLocks.elementAt(0));
|
1712 |
2286
|
tao
|
|
1713 |
5014
|
daigle
|
Thread.sleep(30000); //the lock will expire in 30 seconds
|
1714 |
|
|
logReplication.info("thread for docid: "
|
1715 |
|
|
+ (String) fileLocks.elementAt(fileLocks.size() - 1) + " exiting.");
|
1716 |
2286
|
tao
|
|
1717 |
5014
|
daigle
|
fileLocks.remove(fileLocks.size() - 1);
|
1718 |
|
|
//fileLocks is treated as a FIFO queue. If there are more than one lock
|
1719 |
|
|
//in the vector, the first one inserted will be removed.
|
1720 |
|
|
} catch (Exception e) {
|
1721 |
|
|
logMetacat.error("ReplicationService.run - " + ReplicationService.METACAT_REPL_ERROR_MSG);
|
1722 |
|
|
logReplication.error("ReplicationService.run - error in file lock thread from "
|
1723 |
|
|
+ "MetacatReplication.run: " + e.getMessage());
|
1724 |
|
|
}
|
1725 |
|
|
}
|
1726 |
2286
|
tao
|
|
1727 |
5014
|
daigle
|
/**
|
1728 |
|
|
* Returns the name of a server given a serverCode
|
1729 |
|
|
* @param serverCode the serverid of the server
|
1730 |
|
|
* @return the servername or null if the specified serverCode does not
|
1731 |
|
|
* exist.
|
1732 |
|
|
*/
|
1733 |
|
|
public static String getServerNameForServerCode(int serverCode) {
|
1734 |
|
|
//System.out.println("serverid: " + serverCode);
|
1735 |
|
|
DBConnection dbConn = null;
|
1736 |
|
|
int serialNumber = -1;
|
1737 |
|
|
PreparedStatement pstmt = null;
|
1738 |
|
|
try {
|
1739 |
|
|
dbConn = DBConnectionPool.getDBConnection("MetacatReplication.getServer");
|
1740 |
|
|
serialNumber = dbConn.getCheckOutSerialNumber();
|
1741 |
|
|
String sql = new String("select server from "
|
1742 |
|
|
+ "xml_replication where serverid = " + serverCode);
|
1743 |
|
|
pstmt = dbConn.prepareStatement(sql);
|
1744 |
|
|
//System.out.println("getserver sql: " + sql);
|
1745 |
|
|
pstmt.execute();
|
1746 |
|
|
ResultSet rs = pstmt.getResultSet();
|
1747 |
|
|
boolean tablehasrows = rs.next();
|
1748 |
|
|
if (tablehasrows) {
|
1749 |
|
|
//System.out.println("server: " + rs.getString(1));
|
1750 |
|
|
return rs.getString(1);
|
1751 |
|
|
}
|
1752 |
2286
|
tao
|
|
1753 |
5014
|
daigle
|
//conn.close();
|
1754 |
|
|
} catch (Exception e) {
|
1755 |
|
|
logMetacat.error("ReplicationService.getServerNameForServerCode - " + ReplicationService.METACAT_REPL_ERROR_MSG);
|
1756 |
|
|
logReplication.error("ReplicationService.getServerNameForServerCode - Error in MetacatReplication.getServer: " + e.getMessage());
|
1757 |
|
|
} finally {
|
1758 |
|
|
try {
|
1759 |
|
|
pstmt.close();
|
1760 |
|
|
}//try
|
1761 |
|
|
catch (SQLException ee) {
|
1762 |
|
|
logMetacat.error("ReplicationService.getServerNameForServerCode - " + ReplicationService.METACAT_REPL_ERROR_MSG);
|
1763 |
|
|
logReplication.error("ReplicationService.getServerNameForServerCode - Error in MetacactReplication.getserver: "
|
1764 |
|
|
+ ee.getMessage());
|
1765 |
|
|
}//catch
|
1766 |
|
|
finally {
|
1767 |
|
|
DBConnectionPool.returnDBConnection(dbConn, serialNumber);
|
1768 |
|
|
}//fianlly
|
1769 |
|
|
}//finally
|
1770 |
2286
|
tao
|
|
1771 |
5014
|
daigle
|
return null;
|
1772 |
|
|
//return null if the server does not exist
|
1773 |
|
|
}
|
1774 |
2286
|
tao
|
|
1775 |
5014
|
daigle
|
/**
|
1776 |
|
|
* Returns a server code given a server name
|
1777 |
|
|
* @param server the name of the server
|
1778 |
|
|
* @return integer > 0 representing the code of the server, 0 if the server
|
1779 |
|
|
* does not exist.
|
1780 |
|
|
*/
|
1781 |
|
|
public static int getServerCodeForServerName(String server) throws ServiceException {
|
1782 |
|
|
DBConnection dbConn = null;
|
1783 |
|
|
int serialNumber = -1;
|
1784 |
|
|
PreparedStatement pstmt = null;
|
1785 |
|
|
int serverCode = 0;
|
1786 |
2286
|
tao
|
|
1787 |
5014
|
daigle
|
try {
|
1788 |
837
|
bojilova
|
|
1789 |
5014
|
daigle
|
//conn = util.openDBConnection();
|
1790 |
|
|
dbConn = DBConnectionPool.getDBConnection("MetacatReplication.getServerCode");
|
1791 |
|
|
serialNumber = dbConn.getCheckOutSerialNumber();
|
1792 |
|
|
pstmt = dbConn.prepareStatement("SELECT serverid FROM xml_replication "
|
1793 |
|
|
+ "WHERE server LIKE '" + server + "'");
|
1794 |
|
|
pstmt.execute();
|
1795 |
|
|
ResultSet rs = pstmt.getResultSet();
|
1796 |
|
|
boolean tablehasrows = rs.next();
|
1797 |
|
|
if (tablehasrows) {
|
1798 |
|
|
serverCode = rs.getInt(1);
|
1799 |
|
|
pstmt.close();
|
1800 |
|
|
//conn.close();
|
1801 |
|
|
return serverCode;
|
1802 |
|
|
}
|
1803 |
837
|
bojilova
|
|
1804 |
5014
|
daigle
|
} catch (SQLException sqle) {
|
1805 |
|
|
throw new ServiceException("ReplicationService.getServerCodeForServerName - "
|
1806 |
|
|
+ "SQL error when getting server code: " + sqle.getMessage());
|
1807 |
2286
|
tao
|
|
1808 |
5014
|
daigle
|
} finally {
|
1809 |
|
|
try {
|
1810 |
|
|
pstmt.close();
|
1811 |
|
|
//conn.close();
|
1812 |
|
|
}//try
|
1813 |
|
|
catch (Exception ee) {
|
1814 |
|
|
logMetacat.error("ReplicationService.getServerCodeForServerName - " + ReplicationService.METACAT_REPL_ERROR_MSG);
|
1815 |
|
|
logReplication.error("ReplicationService.getServerNameForServerCode - Error in MetacatReplicatio.getServerCode: "
|
1816 |
|
|
+ ee.getMessage());
|
1817 |
837
|
bojilova
|
|
1818 |
5014
|
daigle
|
}//catch
|
1819 |
|
|
finally {
|
1820 |
|
|
DBConnectionPool.returnDBConnection(dbConn, serialNumber);
|
1821 |
|
|
}//finally
|
1822 |
|
|
}//finally
|
1823 |
2286
|
tao
|
|
1824 |
5014
|
daigle
|
return serverCode;
|
1825 |
|
|
}
|
1826 |
2286
|
tao
|
|
1827 |
5014
|
daigle
|
/**
|
1828 |
|
|
* Method to get a host server information for given docid
|
1829 |
|
|
* @param conn a connection to the database
|
1830 |
|
|
*/
|
1831 |
|
|
public static Hashtable<String, String> getHomeServerInfoForDocId(String docId) {
|
1832 |
|
|
Hashtable<String, String> sl = new Hashtable<String, String>();
|
1833 |
|
|
DBConnection dbConn = null;
|
1834 |
|
|
int serialNumber = -1;
|
1835 |
5027
|
daigle
|
docId = DocumentUtil.getDocIdFromString(docId);
|
1836 |
5014
|
daigle
|
PreparedStatement pstmt = null;
|
1837 |
|
|
int serverLocation;
|
1838 |
|
|
try {
|
1839 |
|
|
//get conection
|
1840 |
|
|
dbConn = DBConnectionPool.getDBConnection("ReplicationHandler.getHomeServer");
|
1841 |
|
|
serialNumber = dbConn.getCheckOutSerialNumber();
|
1842 |
|
|
//get a server location from xml_document table
|
1843 |
|
|
pstmt = dbConn.prepareStatement("select server_location from xml_documents "
|
1844 |
|
|
+ "where docid = ?");
|
1845 |
|
|
pstmt.setString(1, docId);
|
1846 |
|
|
pstmt.execute();
|
1847 |
|
|
ResultSet serverName = pstmt.getResultSet();
|
1848 |
|
|
//get a server location
|
1849 |
|
|
if (serverName.next()) {
|
1850 |
|
|
serverLocation = serverName.getInt(1);
|
1851 |
|
|
pstmt.close();
|
1852 |
|
|
} else {
|
1853 |
|
|
pstmt.close();
|
1854 |
|
|
//ut.returnConnection(conn);
|
1855 |
|
|
return null;
|
1856 |
|
|
}
|
1857 |
|
|
pstmt = dbConn.prepareStatement("select server, last_checked, replicate "
|
1858 |
|
|
+ "from xml_replication where serverid = ?");
|
1859 |
|
|
//increase usage count
|
1860 |
|
|
dbConn.increaseUsageCount(1);
|
1861 |
|
|
pstmt.setInt(1, serverLocation);
|
1862 |
|
|
pstmt.execute();
|
1863 |
|
|
ResultSet rs = pstmt.getResultSet();
|
1864 |
|
|
boolean tableHasRows = rs.next();
|
1865 |
|
|
if (tableHasRows) {
|
1866 |
2286
|
tao
|
|
1867 |
5014
|
daigle
|
String server = rs.getString(1);
|
1868 |
|
|
String last_checked = rs.getString(2);
|
1869 |
|
|
if (!server.equals("localhost")) {
|
1870 |
|
|
sl.put(server, last_checked);
|
1871 |
|
|
}
|
1872 |
2286
|
tao
|
|
1873 |
5014
|
daigle
|
} else {
|
1874 |
|
|
pstmt.close();
|
1875 |
|
|
//ut.returnConnection(conn);
|
1876 |
|
|
return null;
|
1877 |
|
|
}
|
1878 |
|
|
pstmt.close();
|
1879 |
|
|
} catch (Exception e) {
|
1880 |
|
|
logMetacat.error("ReplicationService.getHomeServerInfoForDocId - " + ReplicationService.METACAT_REPL_ERROR_MSG);
|
1881 |
|
|
logReplication.error("ReplicationService.getHomeServerInfoForDocId - error in replicationHandler.getHomeServer(): "
|
1882 |
|
|
+ e.getMessage());
|
1883 |
|
|
} finally {
|
1884 |
|
|
try {
|
1885 |
|
|
pstmt.close();
|
1886 |
|
|
//ut.returnConnection(conn);
|
1887 |
|
|
} catch (Exception ee) {
|
1888 |
|
|
logMetacat.error("ReplicationService.getHomeServerInfoForDocId - " + ReplicationService.METACAT_REPL_ERROR_MSG);
|
1889 |
|
|
logReplication.error("ReplicationService.getHomeServerInfoForDocId - Eror irn rplicationHandler.getHomeServer() "
|
1890 |
|
|
+ "to close pstmt: " + ee.getMessage());
|
1891 |
|
|
} finally {
|
1892 |
|
|
DBConnectionPool.returnDBConnection(dbConn, serialNumber);
|
1893 |
|
|
}
|
1894 |
2286
|
tao
|
|
1895 |
5014
|
daigle
|
}//finally
|
1896 |
|
|
return sl;
|
1897 |
|
|
}
|
1898 |
2286
|
tao
|
|
1899 |
5014
|
daigle
|
/**
|
1900 |
|
|
* Returns a home server location given a accnum
|
1901 |
|
|
* @param accNum , given accNum for a document
|
1902 |
|
|
*
|
1903 |
|
|
*/
|
1904 |
|
|
public static int getHomeServerCodeForDocId(String accNum) throws ServiceException {
|
1905 |
|
|
DBConnection dbConn = null;
|
1906 |
|
|
int serialNumber = -1;
|
1907 |
|
|
PreparedStatement pstmt = null;
|
1908 |
|
|
int serverCode = 1;
|
1909 |
5027
|
daigle
|
String docId = DocumentUtil.getDocIdFromString(accNum);
|
1910 |
2286
|
tao
|
|
1911 |
5014
|
daigle
|
try {
|
1912 |
1292
|
tao
|
|
1913 |
5014
|
daigle
|
// Get DBConnection
|
1914 |
|
|
dbConn = DBConnectionPool
|
1915 |
|
|
.getDBConnection("ReplicationHandler.getServerLocation");
|
1916 |
|
|
serialNumber = dbConn.getCheckOutSerialNumber();
|
1917 |
|
|
pstmt = dbConn.prepareStatement("SELECT server_location FROM xml_documents "
|
1918 |
|
|
+ "WHERE docid LIKE '" + docId + "'");
|
1919 |
|
|
pstmt.execute();
|
1920 |
|
|
ResultSet rs = pstmt.getResultSet();
|
1921 |
|
|
boolean tablehasrows = rs.next();
|
1922 |
|
|
//If a document is find, return the server location for it
|
1923 |
|
|
if (tablehasrows) {
|
1924 |
|
|
serverCode = rs.getInt(1);
|
1925 |
|
|
pstmt.close();
|
1926 |
|
|
//conn.close();
|
1927 |
|
|
return serverCode;
|
1928 |
|
|
}
|
1929 |
|
|
//if couldn't find in xml_documents table, we think server code is 1
|
1930 |
|
|
//(this is new document)
|
1931 |
|
|
else {
|
1932 |
|
|
pstmt.close();
|
1933 |
|
|
//conn.close();
|
1934 |
|
|
return serverCode;
|
1935 |
|
|
}
|
1936 |
1292
|
tao
|
|
1937 |
5014
|
daigle
|
} catch (SQLException sqle) {
|
1938 |
|
|
throw new ServiceException("ReplicationService.getHomeServerCodeForDocId - "
|
1939 |
|
|
+ "SQL error when getting home server code for docid: " + docId + " : "
|
1940 |
|
|
+ sqle.getMessage());
|
1941 |
2286
|
tao
|
|
1942 |
5014
|
daigle
|
} finally {
|
1943 |
|
|
try {
|
1944 |
|
|
pstmt.close();
|
1945 |
|
|
//conn.close();
|
1946 |
2286
|
tao
|
|
1947 |
5014
|
daigle
|
} catch (SQLException sqle) {
|
1948 |
|
|
logMetacat.error("ReplicationService.getHomeServerCodeForDocId - " + ReplicationService.METACAT_REPL_ERROR_MSG);
|
1949 |
|
|
logReplication.error("ReplicationService.getHomeServerCodeForDocId - ReplicationService.getHomeServerCodeForDocId - "
|
1950 |
|
|
+ "SQL error when getting home server code for docid: " + docId + " : "
|
1951 |
|
|
+ sqle.getMessage());
|
1952 |
|
|
} finally {
|
1953 |
|
|
DBConnectionPool.returnDBConnection(dbConn, serialNumber);
|
1954 |
|
|
}//finally
|
1955 |
|
|
}//finally
|
1956 |
|
|
//return serverCode;
|
1957 |
|
|
}
|
1958 |
1292
|
tao
|
|
1959 |
5014
|
daigle
|
/**
|
1960 |
|
|
* This method returns the content of a url
|
1961 |
|
|
* @param u the url to return the content from
|
1962 |
|
|
* @return a string representing the content of the url
|
1963 |
|
|
* @throws java.io.IOException
|
1964 |
|
|
*/
|
1965 |
|
|
public static String getURLContent(URL u) throws java.io.IOException {
|
1966 |
5441
|
berkley
|
logReplication.info("Getting url content from " + u.toString());
|
1967 |
5014
|
daigle
|
char istreamChar;
|
1968 |
|
|
int istreamInt;
|
1969 |
5440
|
berkley
|
logReplication.info("ReplicationService.getURLContent - Before open the stream" + u.toString());
|
1970 |
5014
|
daigle
|
InputStream input = u.openStream();
|
1971 |
5440
|
berkley
|
logReplication.info("ReplicationService.getURLContent - After open the stream" + u.toString());
|
1972 |
5014
|
daigle
|
InputStreamReader istream = new InputStreamReader(input);
|
1973 |
|
|
StringBuffer serverResponse = new StringBuffer();
|
1974 |
|
|
while ((istreamInt = istream.read()) != -1) {
|
1975 |
|
|
istreamChar = (char) istreamInt;
|
1976 |
|
|
serverResponse.append(istreamChar);
|
1977 |
|
|
}
|
1978 |
|
|
istream.close();
|
1979 |
|
|
input.close();
|
1980 |
2286
|
tao
|
|
1981 |
5014
|
daigle
|
return serverResponse.toString();
|
1982 |
|
|
}
|
1983 |
2286
|
tao
|
|
1984 |
5014
|
daigle
|
// /**
|
1985 |
|
|
// * Method for writing replication messages to a log file specified in
|
1986 |
|
|
// * metacat.properties
|
1987 |
|
|
// */
|
1988 |
|
|
// public static void replLog(String message) {
|
1989 |
|
|
// try {
|
1990 |
|
|
// FileOutputStream fos = new FileOutputStream(PropertyService
|
1991 |
|
|
// .getProperty("replication.logdir")
|
1992 |
|
|
// + "/metacatreplication.log", true);
|
1993 |
|
|
// PrintWriter pw = new PrintWriter(fos);
|
1994 |
|
|
// SimpleDateFormat formatter = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
|
1995 |
|
|
// java.util.Date localtime = new java.util.Date();
|
1996 |
|
|
// String dateString = formatter.format(localtime);
|
1997 |
|
|
// dateString += " :: " + message;
|
1998 |
|
|
// // time stamp each entry
|
1999 |
|
|
// pw.println(dateString);
|
2000 |
|
|
// pw.flush();
|
2001 |
|
|
// } catch (Exception e) {
|
2002 |
|
|
// logReplication.error("error writing to replication log from "
|
2003 |
|
|
// + "MetacatReplication.replLog: " + e.getMessage());
|
2004 |
|
|
// // e.printStackTrace(System.out);
|
2005 |
|
|
// }
|
2006 |
|
|
// }
|
2007 |
2286
|
tao
|
|
2008 |
5014
|
daigle
|
// /**
|
2009 |
|
|
// * Method for writing replication messages to a log file specified in
|
2010 |
|
|
// * metacat.properties
|
2011 |
|
|
// */
|
2012 |
|
|
// public static void replErrorLog(String message) {
|
2013 |
|
|
// try {
|
2014 |
|
|
// FileOutputStream fos = new FileOutputStream(PropertyService
|
2015 |
|
|
// .getProperty("replication.logdir")
|
2016 |
|
|
// + "/metacatreplicationerror.log", true);
|
2017 |
|
|
// PrintWriter pw = new PrintWriter(fos);
|
2018 |
|
|
// SimpleDateFormat formatter = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
|
2019 |
|
|
// java.util.Date localtime = new java.util.Date();
|
2020 |
|
|
// String dateString = formatter.format(localtime);
|
2021 |
|
|
// dateString += " :: " + message;
|
2022 |
|
|
// //time stamp each entry
|
2023 |
|
|
// pw.println(dateString);
|
2024 |
|
|
// pw.flush();
|
2025 |
|
|
// } catch (Exception e) {
|
2026 |
|
|
// logReplication.error("error writing to replication error log from "
|
2027 |
|
|
// + "MetacatReplication.replErrorLog: " + e.getMessage());
|
2028 |
|
|
// //e.printStackTrace(System.out);
|
2029 |
|
|
// }
|
2030 |
|
|
// }
|
2031 |
2286
|
tao
|
|
2032 |
5014
|
daigle
|
/**
|
2033 |
|
|
* Returns true if the replicate field for server in xml_replication is 1.
|
2034 |
|
|
* Returns false otherwise
|
2035 |
4080
|
daigle
|
*/
|
2036 |
5014
|
daigle
|
public static boolean replToServer(String server) {
|
2037 |
|
|
DBConnection dbConn = null;
|
2038 |
|
|
int serialNumber = -1;
|
2039 |
|
|
PreparedStatement pstmt = null;
|
2040 |
4080
|
daigle
|
try {
|
2041 |
5014
|
daigle
|
dbConn = DBConnectionPool.getDBConnection("MetacatReplication.repltoServer");
|
2042 |
|
|
serialNumber = dbConn.getCheckOutSerialNumber();
|
2043 |
|
|
pstmt = dbConn.prepareStatement("select replicate from "
|
2044 |
|
|
+ "xml_replication where server like '" + server + "'");
|
2045 |
|
|
pstmt.execute();
|
2046 |
|
|
ResultSet rs = pstmt.getResultSet();
|
2047 |
|
|
boolean tablehasrows = rs.next();
|
2048 |
|
|
if (tablehasrows) {
|
2049 |
|
|
int i = rs.getInt(1);
|
2050 |
|
|
if (i == 1) {
|
2051 |
|
|
pstmt.close();
|
2052 |
|
|
//conn.close();
|
2053 |
|
|
return true;
|
2054 |
|
|
} else {
|
2055 |
|
|
pstmt.close();
|
2056 |
|
|
//conn.close();
|
2057 |
|
|
return false;
|
2058 |
|
|
}
|
2059 |
|
|
}
|
2060 |
|
|
} catch (SQLException sqle) {
|
2061 |
|
|
logMetacat.error("ReplicationService.replToServer - " + ReplicationService.METACAT_REPL_ERROR_MSG);
|
2062 |
|
|
logReplication.error("ReplicationService.replToServer - SQL error in MetacatReplication.replToServer: "
|
2063 |
|
|
+ sqle.getMessage());
|
2064 |
|
|
} finally {
|
2065 |
|
|
try {
|
2066 |
|
|
pstmt.close();
|
2067 |
|
|
//conn.close();
|
2068 |
|
|
}//try
|
2069 |
|
|
catch (Exception ee) {
|
2070 |
|
|
logMetacat.error("ReplicationService.replToServer - " + ReplicationService.METACAT_REPL_ERROR_MSG);
|
2071 |
|
|
logReplication.error("ReplicationService.replToServer - Error in MetacatReplication.replToServer: "
|
2072 |
|
|
+ ee.getMessage());
|
2073 |
|
|
}//catch
|
2074 |
|
|
finally {
|
2075 |
|
|
DBConnectionPool.returnDBConnection(dbConn, serialNumber);
|
2076 |
|
|
}//finally
|
2077 |
|
|
}//finally
|
2078 |
|
|
return false;
|
2079 |
|
|
//the default if this server does not exist is to not replicate to it.
|
2080 |
4080
|
daigle
|
}
|
2081 |
2286
|
tao
|
|
2082 |
522
|
berkley
|
}
|