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: sgarg $'
11
 *     '$Date: 2005-10-10 11:06:55 -0700 (Mon, 10 Oct 2005) $'
12
 * '$Revision: 2663 $'
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
import org.apache.log4j.Logger;
36

    
37
/**
38
 * A class represent a replication server list in xml_replcation table
39
 */
40
 
41
public class ReplicationServerList
42
{
43
  private static Vector serverList = null; //Vector to store server list
44
  private static final AbstractDatabase dbAdapter = MetaCatUtil.dbAdapter;
45
  private static Logger logMetacat = Logger.getLogger(ReplicationServer.class);
46

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

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

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