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: jones $'
10
 *     '$Date: 2010-04-19 16:51:13 -0700 (Mon, 19 Apr 2010) $'
11
 * '$Revision: 5319 $'
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 edu.ucsb.nceas.metacat.database.DBConnection;
31
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
32
import edu.ucsb.nceas.metacat.database.DatabaseService;
33

    
34
import java.util.Vector;
35
import java.sql.*;
36

    
37
import org.apache.log4j.Logger;
38

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

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

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

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