1
|
/**
|
2
|
* '$RCSfile$'
|
3
|
* Purpose: A class represent a replication server list in
|
4
|
xml_replcation table
|
5
|
* Copyright: 2000 Regents of the University of California and the
|
6
|
* National Center for Ecological Analysis and Synthesis
|
7
|
* Authors: Jing Tao
|
8
|
* Release: @release@
|
9
|
*
|
10
|
* '$Author: tao $'
|
11
|
* '$Date: 2003-07-25 08:39:08 -0700 (Fri, 25 Jul 2003) $'
|
12
|
* '$Revision: 1758 $'
|
13
|
*
|
14
|
* This program is free software; you can redistribute it and/or modify
|
15
|
* it under the terms of the GNU General Public License as published by
|
16
|
* the Free Software Foundation; either version 2 of the License, or
|
17
|
* (at your option) any later version.
|
18
|
*
|
19
|
* This program is distributed in the hope that it will be useful,
|
20
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
21
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
22
|
* GNU General Public License for more details.
|
23
|
*
|
24
|
* You should have received a copy of the GNU General Public License
|
25
|
* along with this program; if not, write to the Free Software
|
26
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
27
|
*/
|
28
|
|
29
|
package edu.ucsb.nceas.metacat;
|
30
|
|
31
|
import edu.ucsb.nceas.dbadapter.AbstractDatabase;
|
32
|
import java.util.Vector;
|
33
|
import java.sql.*;
|
34
|
|
35
|
/**
|
36
|
* A class represent a replication server list in xml_replcation table
|
37
|
*/
|
38
|
|
39
|
public class ReplicationServerList
|
40
|
{
|
41
|
private static Vector serverList = null; //Vector to store server list
|
42
|
private static final AbstractDatabase dbAdapter = MetaCatUtil.dbAdapter;
|
43
|
|
44
|
/**
|
45
|
* constructor of ReplicationServerList
|
46
|
* It will build server list. If only local host exists, ther server list
|
47
|
* will be null
|
48
|
*/
|
49
|
public ReplicationServerList()
|
50
|
{
|
51
|
|
52
|
serverList = new Vector();
|
53
|
//Build serverList
|
54
|
buildServerList();
|
55
|
}//ReplicationServerList
|
56
|
|
57
|
/**
|
58
|
* Method to create server list from xml_replication table
|
59
|
* If in xml_replication table, only local host exists, server list will be
|
60
|
* empty
|
61
|
*/
|
62
|
private void buildServerList()
|
63
|
{
|
64
|
ReplicationServer server = null; //element in server list
|
65
|
DBConnection dbConn = null;//DBConnection
|
66
|
int serialNumber = -1;//DBConnection serial number
|
67
|
PreparedStatement pstmt = null;
|
68
|
ResultSet rs = null;
|
69
|
boolean hasReplication = false;//Replication xml or not
|
70
|
boolean hasDataReplication = false;//Replication data file or not
|
71
|
boolean isHub = false;//local host is the hub for this server or not
|
72
|
|
73
|
try
|
74
|
{
|
75
|
//Get DBConnection
|
76
|
dbConn=DBConnectionPool.
|
77
|
getDBConnection("ReplicationHandler.buildServerList");
|
78
|
serialNumber=dbConn.getCheckOutSerialNumber();
|
79
|
//Select fields from xml_replication table
|
80
|
pstmt = dbConn.prepareStatement("select server, last_checked, replicate,"+
|
81
|
" datareplicate, hub from xml_replication");
|
82
|
//Execute prepare statement
|
83
|
pstmt.execute();
|
84
|
//Get result set
|
85
|
rs = pstmt.getResultSet();
|
86
|
//Iterate over the result set
|
87
|
boolean tableHasRows = rs.next();
|
88
|
while(tableHasRows)
|
89
|
{
|
90
|
//Server name
|
91
|
String serverName = rs.getString(1);
|
92
|
MetaCatUtil.debugMessage("ServerName: "+serverName, 40);
|
93
|
//Last check date
|
94
|
String lastChecked = rs.getString(2);
|
95
|
MetaCatUtil.debugMessage("Last checked time: "+lastChecked, 40);
|
96
|
//Replication value
|
97
|
int replication = rs.getInt(3);
|
98
|
MetaCatUtil.debugMessage("Replication value: "+replication, 40);
|
99
|
//Data replication value
|
100
|
int dataReplication = rs.getInt(4);
|
101
|
MetaCatUtil.debugMessage("DataReplication value: "+dataReplication, 40);
|
102
|
//Hub value
|
103
|
int hubValue = rs.getInt(5);
|
104
|
MetaCatUtil.debugMessage("Hub value: "+hubValue, 40);
|
105
|
//Get rid of local host
|
106
|
if(!serverName.equals("localhost"))
|
107
|
{
|
108
|
|
109
|
//create a new object of Replication
|
110
|
server = new ReplicationServer();
|
111
|
|
112
|
//Set server name
|
113
|
server.setServerName(serverName);
|
114
|
|
115
|
//Set last check date
|
116
|
server.setLastCheckedDate(lastChecked);
|
117
|
//From replication value to determine hasReplication valuse
|
118
|
if (replication ==1)
|
119
|
{
|
120
|
//If replication equals 1, it can replicate xml documents
|
121
|
hasReplication = true;
|
122
|
}//if
|
123
|
else
|
124
|
{
|
125
|
//if replication is NOT 1, it can't replicate xml documents
|
126
|
hasReplication = false;
|
127
|
}//else
|
128
|
//Set replication value
|
129
|
server.setReplication(hasReplication);
|
130
|
|
131
|
//From both replication and data replication value to determine
|
132
|
//hasDataReplication value
|
133
|
if (hasReplication && dataReplication ==1)
|
134
|
{
|
135
|
//Only server can replicate xml (hasRplication == true) and
|
136
|
// dataReplication is 1, local host can replicate data file
|
137
|
hasDataReplication = true;
|
138
|
}//if
|
139
|
else
|
140
|
{
|
141
|
hasDataReplication = false;
|
142
|
}//else
|
143
|
//Set data replciation value
|
144
|
server.setDataReplication(hasDataReplication);
|
145
|
|
146
|
//Detemine isHub by hubValue
|
147
|
if (hubValue ==1)
|
148
|
{
|
149
|
isHub = true;
|
150
|
}//if
|
151
|
else
|
152
|
{
|
153
|
isHub = false;
|
154
|
}//else
|
155
|
//Set hub value
|
156
|
server.setHub(isHub);
|
157
|
|
158
|
//Add this server into server list
|
159
|
serverList.add(server);
|
160
|
}//if
|
161
|
//Go to new row
|
162
|
tableHasRows = rs.next();
|
163
|
}//while
|
164
|
|
165
|
}//try
|
166
|
catch(Exception e)
|
167
|
{
|
168
|
MetaCatUtil.debugMessage("Error in ReplicationServerList."
|
169
|
+"buildServerList(): "+e.getMessage(), 30);
|
170
|
}//catch
|
171
|
finally
|
172
|
{
|
173
|
try
|
174
|
{
|
175
|
//close result set
|
176
|
rs.close();
|
177
|
//close prepareed statement
|
178
|
pstmt.close();
|
179
|
}//try
|
180
|
catch (SQLException sqlE)
|
181
|
{
|
182
|
MetaCatUtil.debugMessage("Error in ReplicationHandler.buildServerList: "
|
183
|
+sqlE.getMessage(), 30);
|
184
|
}//catch
|
185
|
finally
|
186
|
{
|
187
|
//Return dbconnection too pool
|
188
|
DBConnectionPool.returnDBConnection(dbConn, serialNumber);
|
189
|
}//finally
|
190
|
}//finally
|
191
|
|
192
|
}//buildServerList
|
193
|
|
194
|
/**
|
195
|
* Method to determine the server list is empty or not
|
196
|
*/
|
197
|
public boolean isEmpty()
|
198
|
{
|
199
|
return serverList.isEmpty();
|
200
|
}//isEmpty
|
201
|
|
202
|
/**
|
203
|
* Method to get the size of serverList
|
204
|
*/
|
205
|
public int size()
|
206
|
{
|
207
|
return serverList.size();
|
208
|
}//size()
|
209
|
|
210
|
/**
|
211
|
* Method to add a new replciation server object to serverList
|
212
|
* @Param newReplciationServer, the object need to be add
|
213
|
*/
|
214
|
private synchronized void addNewReplicationServer
|
215
|
(ReplicationServer newReplicationServer)
|
216
|
{
|
217
|
serverList.add(newReplicationServer);
|
218
|
}//addNewReplicationServer
|
219
|
|
220
|
/**
|
221
|
* Method to get a server object given a index number
|
222
|
* @param index, the given index number
|
223
|
*/
|
224
|
public ReplicationServer serverAt(int index)
|
225
|
{
|
226
|
return (ReplicationServer)serverList.elementAt(index);
|
227
|
}//serverAt
|
228
|
|
229
|
|
230
|
/**
|
231
|
* Method to determine if a given server in the replication server list
|
232
|
* @param givenServerName, a server name will be check.
|
233
|
*/
|
234
|
public boolean isGivenServerInList(String givenServerName)
|
235
|
{
|
236
|
boolean result = false;//Variable store the return value
|
237
|
ReplicationServer server = null; //Element in vetor
|
238
|
int size = 0;//Variable to store the size of vector serverList
|
239
|
//Get the size of vector
|
240
|
size = serverList.size();
|
241
|
|
242
|
//If server list is empty
|
243
|
if (size == 0||givenServerName == null||givenServerName.equals(""))
|
244
|
{
|
245
|
result = false;
|
246
|
return result;
|
247
|
}
|
248
|
|
249
|
//Check every element in the vector
|
250
|
for (int i = 0; i < size; i++)
|
251
|
{
|
252
|
//Get the ReplicationServer object indexed i in vector
|
253
|
server = (ReplicationServer) serverList.elementAt(i);
|
254
|
//If given name match a server's name, return true
|
255
|
if ( givenServerName.equalsIgnoreCase(server.getServerName()))
|
256
|
{
|
257
|
result = true;
|
258
|
return result;
|
259
|
}//if
|
260
|
|
261
|
}//for
|
262
|
|
263
|
//If reach here, there is no server's name match the given name
|
264
|
//flase will return
|
265
|
return result;
|
266
|
}//isGinvenServerInList
|
267
|
|
268
|
/**
|
269
|
* Method to determine its index in server list for a given server.
|
270
|
* If couldn't find it, -1 will be returned
|
271
|
* @param givenServerName, a server name will be check.
|
272
|
*/
|
273
|
private synchronized int findIndexInServerList(String givenServerName)
|
274
|
{
|
275
|
int index = -1;//Variable store the return value
|
276
|
ReplicationServer server = null; //Element in vetor
|
277
|
int size = 0;//Variable to store the size of vector serverList
|
278
|
//Get the size of vector
|
279
|
size = serverList.size();
|
280
|
|
281
|
//If server list is empty, -1 will be returned
|
282
|
if (size == 0 || givenServerName ==null ||givenServerName.equals(""))
|
283
|
{
|
284
|
return index;
|
285
|
}
|
286
|
|
287
|
//Check every element in the vector
|
288
|
for (int i = 0; i < size; i++)
|
289
|
{
|
290
|
//Get the ReplicationServer object indexed i in vector
|
291
|
server = (ReplicationServer) serverList.elementAt(i);
|
292
|
//If given name match a server's name, return true
|
293
|
if ( givenServerName.equalsIgnoreCase(server.getServerName()))
|
294
|
{
|
295
|
index = i;
|
296
|
return index;
|
297
|
}//if
|
298
|
|
299
|
}//for
|
300
|
|
301
|
//If reach here, there is no server's name match the given name
|
302
|
//-1 will return
|
303
|
return index;
|
304
|
}//isGinvenServerInList
|
305
|
|
306
|
/**
|
307
|
* To a given server name, try to get its lastcheck date.
|
308
|
* If couldn't find the server in the server list, null will return
|
309
|
* @param givenServerName, the server's name which we want to get last checked
|
310
|
* out date
|
311
|
*/
|
312
|
public synchronized String getLastCheckedDate(String givenServerName)
|
313
|
{
|
314
|
int index = -1;//Variable to store the index
|
315
|
ReplicationServer server = null;//Variable for replication server
|
316
|
|
317
|
//Get the server's index in server list
|
318
|
index = findIndexInServerList(givenServerName);
|
319
|
//If index = -1, couldn't find this server, null will return
|
320
|
if (index == -1)
|
321
|
{
|
322
|
return null;
|
323
|
}//if
|
324
|
//Get Replication server object
|
325
|
server = (ReplicationServer) serverList.elementAt(index);
|
326
|
//return its lastcheckeddate attributes in this object
|
327
|
return server.getLastCheckedDate();
|
328
|
}//getLastCehckedDate
|
329
|
|
330
|
/**
|
331
|
* To a given server name, try to get its xml replciation option
|
332
|
* If couldn't find the server in the server list, false will return
|
333
|
* @param givenServerName, the server's name which we want to get replication
|
334
|
* value
|
335
|
*/
|
336
|
public synchronized boolean getReplicationValue(String givenServerName)
|
337
|
{
|
338
|
int index = -1;//Variable to store the index
|
339
|
ReplicationServer server = null;//Variable for replication server
|
340
|
|
341
|
//Get the server's index in server list
|
342
|
index = findIndexInServerList(givenServerName);
|
343
|
//If index = -1, couldn't find this server, null will return
|
344
|
if (index == -1)
|
345
|
{
|
346
|
return false;
|
347
|
}//if
|
348
|
//Get Replication server object
|
349
|
server = (ReplicationServer) serverList.elementAt(index);
|
350
|
//return this object's replication value
|
351
|
return server.getReplication();
|
352
|
}//getReplicationValue
|
353
|
|
354
|
/**
|
355
|
* To a given server name, try to get its data file replciation option
|
356
|
* If couldn't find the server in the server list, false will return
|
357
|
* @param givenServerName, the server's name which we want to get data file
|
358
|
* replication value
|
359
|
*/
|
360
|
public synchronized boolean getDataReplicationValue(String givenServerName)
|
361
|
{
|
362
|
int index = -1;//Variable to store the index
|
363
|
ReplicationServer server = null;//Variable for replication server
|
364
|
|
365
|
//Get the server's index in server list
|
366
|
index = findIndexInServerList(givenServerName);
|
367
|
//If index = -1, couldn't find this server, null will return
|
368
|
if (index == -1)
|
369
|
{
|
370
|
return false;
|
371
|
}//if
|
372
|
//Get Replication server object
|
373
|
server = (ReplicationServer) serverList.elementAt(index);
|
374
|
//Return this object's data replication value
|
375
|
return server.getDataReplication();
|
376
|
}//getDataReplicationValue
|
377
|
|
378
|
/**
|
379
|
* To a given server name, try to get its hub option
|
380
|
* If couldn't find the server in the server list, false will return
|
381
|
* @param givenServerName, the server's name which we want to get hub
|
382
|
*/
|
383
|
public synchronized boolean getHubValue(String givenServerName)
|
384
|
{
|
385
|
int index = -1;//Variable to store the index
|
386
|
ReplicationServer server = null;//Variable for replication server
|
387
|
|
388
|
//Get the server's index in server list
|
389
|
index = findIndexInServerList(givenServerName);
|
390
|
//If index = -1, couldn't find this server, null will return
|
391
|
if (index == -1)
|
392
|
{
|
393
|
return false;
|
394
|
}//if
|
395
|
//Get Replication server object
|
396
|
server = (ReplicationServer) serverList.elementAt(index);
|
397
|
//Return this object's hub value
|
398
|
return server.getHub();
|
399
|
}//getHubValue
|
400
|
|
401
|
/**
|
402
|
* To a given server name, to check if it is in the server list.
|
403
|
* If it is not, add it to server list and the xml_replication table
|
404
|
* This method is for a document was replicated by a hub and the document's
|
405
|
* home server is not in the replication table. The value for replicate,
|
406
|
* datareplicate and hub are all false
|
407
|
* @param givenServerName, the server's name which want to check
|
408
|
*/
|
409
|
public synchronized void addToServerListIfItIsNot(String givenServerName)
|
410
|
{
|
411
|
// Variable to store the index for the givenServerName
|
412
|
int index = -1;
|
413
|
// Variable to store the new replciation server object
|
414
|
ReplicationServer newServer = null;
|
415
|
// For sql command
|
416
|
PreparedStatement pStmt=null;
|
417
|
// For check out DBConnection
|
418
|
DBConnection dbConn = null;
|
419
|
int serialNumber = -1;
|
420
|
// Field value for xml_replication table
|
421
|
int replicate = 0;
|
422
|
int dataReplicate = 0;
|
423
|
int hub = 0;
|
424
|
|
425
|
// Get the the index
|
426
|
index = findIndexInServerList(givenServerName);
|
427
|
// if index ==-1, it not in the server list
|
428
|
// Add the server to server list
|
429
|
// Add the server to xml_replication table
|
430
|
if (index ==-1)
|
431
|
{
|
432
|
// Create a new replication server object it's replicate, datareplicate
|
433
|
// and hub values are default - false
|
434
|
newServer = new ReplicationServer();
|
435
|
// Set newServer's name as givenServerName
|
436
|
newServer.setServerName(givenServerName);
|
437
|
// Add newServer to serverList
|
438
|
this.addNewReplicationServer(newServer);
|
439
|
// Add this server to xml_table too
|
440
|
try
|
441
|
{
|
442
|
// Checkout DBConnection
|
443
|
dbConn=DBConnectionPool.
|
444
|
getDBConnection("ReplicationSErverList.addToServerListIfItIsNot");
|
445
|
serialNumber=dbConn.getCheckOutSerialNumber();
|
446
|
// Inser it into xml_replication table
|
447
|
/*pStmt = dbConn.prepareStatement("INSERT INTO xml_replication " +
|
448
|
"(server, last_checked, replicate, datareplicate, hub) "+
|
449
|
"VALUES ('" + givenServerName + "', to_date(" +
|
450
|
"'01/01/00', 'MM/DD/YY'), '" +
|
451
|
replicate +"', '"+dataReplicate+"','"+ hub + "')");*/
|
452
|
pStmt = dbConn.prepareStatement("INSERT INTO xml_replication " +
|
453
|
"(server, last_checked, replicate, datareplicate, hub) "+
|
454
|
"VALUES ('" + givenServerName + "', "+
|
455
|
dbAdapter.toDate("01/01/1980","MM/DD/YYYY") + ", '" +
|
456
|
replicate +"', '"+dataReplicate+"','"+ hub + "')");
|
457
|
pStmt.execute();
|
458
|
|
459
|
pStmt.close();
|
460
|
}//try
|
461
|
catch (Exception e)
|
462
|
{
|
463
|
MetaCatUtil.debugMessage("Error in ReplicationServerList."+
|
464
|
"addToServerListIfItIsNot: " + e.getMessage(), 30);
|
465
|
}//catch
|
466
|
finally
|
467
|
{
|
468
|
try
|
469
|
{
|
470
|
pStmt.close();
|
471
|
}//try
|
472
|
catch (Exception ee)
|
473
|
{
|
474
|
MetaCatUtil.debugMessage("Error in ReplicationServerList."+
|
475
|
"addToServerListIfItIsNot: " + ee.getMessage(), 50);
|
476
|
}//catch
|
477
|
finally
|
478
|
{
|
479
|
DBConnectionPool.returnDBConnection(dbConn, serialNumber);
|
480
|
}//finally
|
481
|
}//finally
|
482
|
}//if
|
483
|
}//addToServerListIfItIsNot
|
484
|
|
485
|
}//ReplicationServerList
|