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
 *
9
 *   '$Author: daigle $'
10
 *     '$Date: 2008-08-29 10:20:19 -0700 (Fri, 29 Aug 2008) $'
11
 * '$Revision: 4335 $'
12
 *
13
 * This program is free software; you can redistribute it and/or modify
14
 * it under the terms of the GNU General Public License as published by
15
 * the Free Software Foundation; either version 2 of the License, or
16
 * (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 * GNU General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU General Public License
24
 * along with this program; if not, write to the Free Software
25
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26
 */
27

    
28
package edu.ucsb.nceas.metacat;
29

    
30
import edu.ucsb.nceas.metacat.service.DatabaseService;
31

    
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 Logger logMetacat = Logger.getLogger(ReplicationServer.class);
45

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

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

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