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: cjones $'
10
 *     '$Date: 2011-06-07 09:53:46 -0700 (Tue, 07 Jun 2011) $'
11
 * '$Revision: 6124 $'
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.replication;
29

    
30
import java.sql.PreparedStatement;
31
import java.sql.ResultSet;
32
import java.sql.SQLException;
33
import java.util.Date;
34
import java.util.Vector;
35

    
36
import org.apache.log4j.Logger;
37

    
38
import edu.ucsb.nceas.metacat.database.DBConnection;
39
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
40
import edu.ucsb.nceas.metacat.database.DatabaseService;
41

    
42
/**
43
 * A class represent a replication server list in xml_replcation table
44
 */
45
 
46
public class ReplicationServerList
47
{
48
  private static Vector<ReplicationServer> serverList = null; //Vector to store server list
49
  private static Logger logMetacat = Logger.getLogger(ReplicationServer.class);
50

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

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

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