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

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

    
417
  /**
418
   * To a given server name, try to get its hub option
419
   * If couldn't find the server in the server list, false will return
420
   * @param givenServerName, the server's name which we want to get hub
421
   */
422
  public synchronized boolean getHubValue(String givenServerName)
423
  {
424
    int index = -1;//Variable to store the index
425
    ReplicationServer server = null;//Variable for replication server
426
    
427
    //Get the server's index in server list
428
    index = findIndexInServerList(givenServerName);
429
    //If index = -1, couldn't find this server, null will return
430
    if (index == -1)
431
    {
432
      return false;
433
    }//if
434
    //Get Replication server object
435
    server = (ReplicationServer) serverList.elementAt(index);
436
    //Return this object's hub value
437
    return server.getHub();
438
  }//getHubValue  
439
  
440
  /**
441
   * To a given server name, to check if it is in the server list.
442
   * If it is not, add it to server list and the xml_replication table
443
   * This method is for a document was replicated by a hub and the document's
444
   * home server is not in the replication table. The value for replicate,
445
   * datareplicate and hub are all false
446
   * @param givenServerName, the server's name which want to check
447
   */
448
  public synchronized void addToServerListIfItIsNot(String givenServerName)
449
  {
450
    // Variable to store the index for the givenServerName
451
    int index = -1;
452
    // Variable to store the new replciation server object
453
    ReplicationServer newServer = null;
454
    // For sql command
455
    PreparedStatement pStmt=null;
456
    // For check out DBConnection
457
    DBConnection dbConn = null;
458
    int serialNumber = -1;
459
    // Field value for xml_replication table
460
    int replicate = 0;
461
    int dataReplicate = 0;
462
    int hub = 0;
463
    
464
    // Get the the index
465
    index = findIndexInServerList(givenServerName);
466
    // if index ==-1, it not in the server list
467
    // Add the server to server list
468
    // Add the server to xml_replication table
469
    if (index ==-1)
470
    {
471
      // Create a new replication server object it's replicate, datareplicate
472
      // and hub values are default - false
473
      newServer = new ReplicationServer();
474
      // Set newServer's name as givenServerName
475
      newServer.setServerName(givenServerName);
476
      // Add newServer to serverList
477
      this.addNewReplicationServer(newServer);
478
      // Add this server to xml_table too
479
     try
480
      {
481
        // Checkout DBConnection     
482
        dbConn=DBConnectionPool.
483
             getDBConnection("ReplicationSErverList.addToServerListIfItIsNot");
484
        serialNumber=dbConn.getCheckOutSerialNumber();
485
        // Inser it into xml_replication table
486
        /*pStmt = dbConn.prepareStatement("INSERT INTO xml_replication " +
487
                      "(server, last_checked, replicate, datareplicate, hub) "+
488
                       "VALUES ('" + givenServerName + "', to_date(" +
489
                       "'01/01/00', 'MM/DD/YY'), '" +
490
                       replicate +"', '"+dataReplicate+"','"+ hub + "')");*/
491
        pStmt = dbConn.prepareStatement("INSERT INTO xml_replication " +
492
                      "(server, last_checked, replicate, datareplicate, hub) "+
493
                       "VALUES ('" + givenServerName + "', "+
494
                        DatabaseService.getInstance().getDBAdapter().toDate("01/01/1980","MM/DD/YYYY") + ", '" +
495
                       replicate +"', '"+dataReplicate+"','"+ hub + "')");
496
        pStmt.execute();
497
       
498
        pStmt.close();
499
      }//try
500
      catch (Exception e)
501
      {
502
        logMetacat.error("Error in ReplicationServerList."+
503
                            "addToServerListIfItIsNot: " + e.getMessage());
504
      }//catch
505
      finally
506
      { 
507
        try
508
        {
509
          pStmt.close();
510
        }//try
511
        catch (Exception ee)
512
        { 
513
          logMetacat.error("Error in ReplicationServerList."+
514
                            "addToServerListIfItIsNot: " + ee.getMessage());
515
        }//catch
516
        finally
517
        {
518
          DBConnectionPool.returnDBConnection(dbConn, serialNumber);
519
        }//finally
520
      }//finally
521
    }//if
522
  }//addToServerListIfItIsNot
523
  
524
}//ReplicationServerList
(6-6/8)