Project

General

Profile

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-03-19 15:26:44 -0800 (Wed, 19 Mar 2003) $'
12
 * '$Revision: 1496 $'
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 java.util.Vector;
32
import java.sql.*;
33

    
34
/**
35
 * A class represent a replication server list in xml_replcation table
36
 */
37
 
38
public class ReplicationServerList
39
{
40
  private static Vector serverList = null; //Vector to store server list
41
  
42
  /**
43
   * constructor of ReplicationServerList
44
   * It will build server list. If only local host exists, ther server list
45
   * will be null
46
   */
47
  public ReplicationServerList()
48
  {
49
    
50
    serverList = new Vector();
51
    //Build serverList
52
    buildServerList();
53
  }//ReplicationServerList
54
  
55
  /**
56
   * Method to create server list from xml_replication table
57
   * If in xml_replication table, only local host exists, server list will be
58
   * empty
59
   */
60
  private void buildServerList()
61
  {
62
    ReplicationServer server = null; //element in server list
63
    DBConnection dbConn = null;//DBConnection
64
    int serialNumber = -1;//DBConnection serial number
65
    PreparedStatement pstmt = null;
66
    ResultSet rs = null;
67
    boolean hasReplication = false;//Replication xml or not
68
    boolean hasDataReplication = false;//Replication data file or not
69
    boolean isHub = false;//local host is the hub for this server or not
70
    
71
    try
72
    {
73
      //Get DBConnection
74
      dbConn=DBConnectionPool.
75
                  getDBConnection("ReplicationHandler.buildServerList");
76
      serialNumber=dbConn.getCheckOutSerialNumber();
77
      //Select fields from xml_replication table
78
      pstmt = dbConn.prepareStatement("select server, last_checked, replicate,"+
79
                    " datareplicate, hub from xml_replication");
80
      //Execute prepare statement
81
      pstmt.execute();
82
      //Get result set
83
      rs = pstmt.getResultSet();
84
      //Iterate over the result set
85
      boolean tableHasRows = rs.next();
86
      while(tableHasRows)
87
      {
88
        //Server name
89
        String serverName = rs.getString(1);
90
        MetaCatUtil.debugMessage("ServerName: "+serverName, 40);
91
        //Last check date
92
        String lastChecked = rs.getString(2);
93
        MetaCatUtil.debugMessage("Last checked time: "+lastChecked, 40);
94
        //Replication value
95
        int replication = rs.getInt(3);
96
        MetaCatUtil.debugMessage("Replication value: "+replication, 40);
97
        //Data replication value
98
        int dataReplication = rs.getInt(4);
99
        MetaCatUtil.debugMessage("DataReplication value: "+dataReplication, 40);
100
        //Hub value
101
        int hubValue = rs.getInt(5);
102
        MetaCatUtil.debugMessage("Hub value: "+hubValue, 40);
103
        //Get rid of local host
104
        if(!serverName.equals("localhost"))
105
        {
106
          
107
          //create a new object of Replication
108
          server = new ReplicationServer();
109
         
110
          //Set server name
111
          server.setServerName(serverName);
112
     
113
          //Set last check date
114
          server.setLastCheckedDate(lastChecked);
115
          //From replication value to determine hasReplication valuse
116
          if (replication ==1)
117
          {
118
            //If replication equals 1, it can replicate xml documents
119
            hasReplication = true;
120
          }//if
121
          else
122
          {
123
            //if replication is NOT 1, it can't replicate xml documents
124
            hasReplication = false;
125
          }//else
126
          //Set replication value
127
          server.setReplication(hasReplication);
128
           
129
          //From both replication and data replication value to determine
130
          //hasDataReplication value
131
          if (hasReplication && dataReplication ==1)
132
          { 
133
            //Only server can replicate xml (hasRplication == true) and
134
            // dataReplication is 1, local host can replicate data file
135
            hasDataReplication = true;
136
          }//if
137
          else
138
          {
139
            hasDataReplication = false;
140
          }//else
141
          //Set data replciation value
142
          server.setDataReplication(hasDataReplication);
143
          
144
          //Detemine isHub by hubValue
145
          if (hubValue ==1)
146
          {
147
            isHub = true;
148
          }//if
149
          else
150
          {
151
            isHub = false;
152
          }//else
153
          //Set hub value
154
          server.setHub(isHub);
155
          
156
          //Add this server into server list
157
          serverList.add(server);
158
        }//if
159
        //Go to new row      
160
        tableHasRows = rs.next();   
161
      }//while
162
     
163
    }//try
164
    catch(Exception e)
165
    {
166
      MetaCatUtil.debugMessage("Error in ReplicationServerList."
167
                                    +"buildServerList(): "+e.getMessage(), 30);
168
    }//catch
169
    finally
170
    {
171
      try
172
      {
173
        //close result set
174
        rs.close();
175
        //close prepareed statement
176
        pstmt.close();
177
      }//try
178
      catch (SQLException sqlE)
179
      {
180
        MetaCatUtil.debugMessage("Error in ReplicationHandler.buildServerList: "
181
                                +sqlE.getMessage(), 30);
182
      }//catch
183
      finally
184
      {
185
        //Return dbconnection too pool
186
        DBConnectionPool.returnDBConnection(dbConn, serialNumber);
187
      }//finally
188
    }//finally
189
  
190
  }//buildServerList
191
  
192
  /**
193
   * Method to determine the server list is empty or not
194
   */
195
  public boolean isEmpty()
196
  {
197
    return serverList.isEmpty();
198
  }//isEmpty
199
  
200
  /**
201
   * Method to get the size of serverList
202
   */
203
  public int size()
204
  {
205
    return serverList.size();
206
  }//size()
207
  
208
  /**
209
   * Method to add a new replciation server object to serverList
210
   * @Param newReplciationServer, the object need to be add
211
   */
212
  private synchronized void addNewReplicationServer
213
                                      (ReplicationServer newReplicationServer)
214
  {
215
    serverList.add(newReplicationServer);
216
  }//addNewReplicationServer
217
  
218
  /**
219
   * Method to get a server object given a index number
220
   * @param index, the given index number
221
   */
222
  public ReplicationServer serverAt(int index)
223
  {
224
    return (ReplicationServer)serverList.elementAt(index);
225
  }//serverAt
226
   
227

    
228
  /**
229
   * Method to determine if a given server in the replication server list
230
   * @param givenServerName, a server name will be check.
231
   */
232
  public boolean isGivenServerInList(String givenServerName)
233
  {
234
    boolean result = false;//Variable store the return value
235
    ReplicationServer server = null; //Element in vetor
236
    int size = 0;//Variable to store the size of vector serverList
237
    //Get the size of vector
238
    size = serverList.size();
239
    
240
    //If server list is empty
241
    if (size == 0||givenServerName == null||givenServerName.equals(""))
242
    {
243
      result = false;
244
      return result;
245
    }
246
    
247
    //Check every element in the vector
248
    for (int i = 0; i < size; i++)
249
    {
250
      //Get the ReplicationServer object indexed i in vector
251
      server = (ReplicationServer) serverList.elementAt(i);
252
      //If given name match a server's name, return true
253
      if ( givenServerName.equalsIgnoreCase(server.getServerName()))
254
      {
255
        result = true;
256
        return result;
257
      }//if
258
      
259
    }//for
260
    
261
    //If reach here, there is no server's name match the given name
262
    //flase will return
263
    return result;
264
  }//isGinvenServerInList
265
  
266
  /**
267
   * Method to determine its index in server list for a given server.
268
   * If couldn't find it, -1 will be returned
269
   * @param givenServerName, a server name will be check.
270
   */
271
  private synchronized int findIndexInServerList(String givenServerName)
272
  {
273
    int index = -1;//Variable store the return value
274
    ReplicationServer server = null; //Element in vetor
275
    int size = 0;//Variable to store the size of vector serverList
276
    //Get the size of vector
277
    size = serverList.size();
278
    
279
    //If server list is empty, -1 will be returned
280
    if (size == 0 || givenServerName ==null ||givenServerName.equals(""))
281
    {
282
      return index;
283
    }
284
    
285
    //Check every element in the vector
286
    for (int i = 0; i < size; i++)
287
    {
288
      //Get the ReplicationServer object indexed i in vector
289
      server = (ReplicationServer) serverList.elementAt(i);
290
      //If given name match a server's name, return true
291
      if ( givenServerName.equalsIgnoreCase(server.getServerName()))
292
      {
293
        index = i;
294
        return index;
295
      }//if
296
      
297
    }//for
298
    
299
    //If reach here, there is no server's name match the given name
300
    //-1 will return
301
    return index;
302
  }//isGinvenServerInList
303
  
304
  /**
305
   * To a given server name, try to get its lastcheck date.
306
   * If couldn't find the server in the server list, null will return
307
   * @param givenServerName, the server's name which we want to get last checked
308
   * out date
309
   */
310
  public synchronized String getLastCheckedDate(String givenServerName)
311
  {
312
    int index = -1;//Variable to store the index
313
    ReplicationServer server = null;//Variable for replication server
314
    
315
    //Get the server's index in server list
316
    index = findIndexInServerList(givenServerName);
317
    //If index = -1, couldn't find this server, null will return
318
    if (index == -1)
319
    {
320
      return null;
321
    }//if
322
    //Get Replication server object
323
    server = (ReplicationServer) serverList.elementAt(index);
324
    //return its lastcheckeddate attributes in this object
325
    return server.getLastCheckedDate();
326
  }//getLastCehckedDate
327
   
328
  /**
329
   * To a given server name, try to get its xml replciation option
330
   * If couldn't find the server in the server list, false will return
331
   * @param givenServerName, the server's name which we want to get replication
332
   * value
333
   */
334
  public synchronized boolean getReplicationValue(String givenServerName)
335
  {
336
    int index = -1;//Variable to store the index
337
    ReplicationServer server = null;//Variable for replication server
338
    
339
    //Get the server's index in server list
340
    index = findIndexInServerList(givenServerName);
341
    //If index = -1, couldn't find this server, null will return
342
    if (index == -1)
343
    {
344
      return false;
345
    }//if
346
    //Get Replication server object
347
    server = (ReplicationServer) serverList.elementAt(index);
348
    //return this object's replication value
349
    return server.getReplication();
350
  }//getReplicationValue
351
  
352
  /**
353
   * To a given server name, try to get its data file replciation option
354
   * If couldn't find the server in the server list, false will return
355
   * @param givenServerName, the server's name which we want to get data file 
356
   * replication value
357
   */
358
  public synchronized boolean getDataReplicationValue(String givenServerName)
359
  {
360
    int index = -1;//Variable to store the index
361
    ReplicationServer server = null;//Variable for replication server
362
    
363
    //Get the server's index in server list
364
    index = findIndexInServerList(givenServerName);
365
    //If index = -1, couldn't find this server, null will return
366
    if (index == -1)
367
    {
368
      return false;
369
    }//if
370
    //Get Replication server object
371
    server = (ReplicationServer) serverList.elementAt(index);
372
    //Return this object's data replication value
373
    return server.getDataReplication();
374
  }//getDataReplicationValue
375

    
376
  /**
377
   * To a given server name, try to get its hub option
378
   * If couldn't find the server in the server list, false will return
379
   * @param givenServerName, the server's name which we want to get hub
380
   */
381
  public synchronized boolean getHubValue(String givenServerName)
382
  {
383
    int index = -1;//Variable to store the index
384
    ReplicationServer server = null;//Variable for replication server
385
    
386
    //Get the server's index in server list
387
    index = findIndexInServerList(givenServerName);
388
    //If index = -1, couldn't find this server, null will return
389
    if (index == -1)
390
    {
391
      return false;
392
    }//if
393
    //Get Replication server object
394
    server = (ReplicationServer) serverList.elementAt(index);
395
    //Return this object's hub value
396
    return server.getHub();
397
  }//getHubValue  
398
  
399
  /**
400
   * To a given server name, to check if it is in the server list.
401
   * If it is not, add it to server list and the xml_replication table
402
   * This method is for a document was replicated by a hub and the document's
403
   * home server is not in the replication table. The value for replicate,
404
   * datareplicate and hub are all false
405
   * @param givenServerName, the server's name which want to check
406
   */
407
  public synchronized void addToServerListIfItIsNot(String givenServerName)
408
  {
409
    // Variable to store the index for the givenServerName
410
    int index = -1;
411
    // Variable to store the new replciation server object
412
    ReplicationServer newServer = null;
413
    // For sql command
414
    PreparedStatement pStmt=null;
415
    // For check out DBConnection
416
    DBConnection dbConn = null;
417
    int serialNumber = -1;
418
    // Field value for xml_replication table
419
    int replicate = 0;
420
    int dataReplicate = 0;
421
    int hub = 0;
422
    
423
    // Get the the index
424
    index = findIndexInServerList(givenServerName);
425
    // if index ==-1, it not in the server list
426
    // Add the server to server list
427
    // Add the server to xml_replication table
428
    if (index ==-1)
429
    {
430
      // Create a new replication server object it's replicate, datareplicate
431
      // and hub values are default - false
432
      newServer = new ReplicationServer();
433
      // Set newServer's name as givenServerName
434
      newServer.setServerName(givenServerName);
435
      // Add newServer to serverList
436
      this.addNewReplicationServer(newServer);
437
      // Add this server to xml_table too
438
     try
439
      {
440
        // Checkout DBConnection     
441
        dbConn=DBConnectionPool.
442
             getDBConnection("ReplicationSErverList.addToServerListIfItIsNot");
443
        serialNumber=dbConn.getCheckOutSerialNumber();
444
        // Inser it into xml_replication table
445
        pStmt = dbConn.prepareStatement("INSERT INTO xml_replication " +
446
                      "(server, last_checked, replicate, datareplicate, hub) "+
447
                       "VALUES ('" + givenServerName + "', to_date(" +
448
                       "'01/01/00', 'MM/DD/YY'), '" +
449
                       replicate +"', '"+dataReplicate+"','"+ hub + "')");
450
        pStmt.execute();
451
        pStmt.close();
452
      }//try
453
      catch (Exception e)
454
      {
455
        MetaCatUtil.debugMessage("Error in ReplicationServerList."+
456
                            "addToServerListIfItIsNot: " + e.getMessage(), 30);
457
      }//catch
458
      finally
459
      { 
460
        try
461
        {
462
          pStmt.close();
463
        }//try
464
        catch (Exception ee)
465
        { 
466
          MetaCatUtil.debugMessage("Error in ReplicationServerList."+
467
                            "addToServerListIfItIsNot: " + ee.getMessage(), 50);
468
        }//catch
469
        finally
470
        {
471
          DBConnectionPool.returnDBConnection(dbConn, serialNumber);
472
        }//finally
473
      }//finally
474
    }//if
475
  }//addToServerListIfItIsNot
476
  
477
}//ReplicationServerList
(51-51/54)