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-11-02 20:40:12 -0700 (Wed, 02 Nov 2011) $'
11
 * '$Revision: 6595 $'
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.sql.Timestamp;
34
import java.util.Calendar;
35
import java.util.Date;
36
import java.util.Vector;
37

    
38
import org.apache.log4j.Logger;
39

    
40
import edu.ucsb.nceas.metacat.database.DBConnection;
41
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
42
import edu.ucsb.nceas.metacat.database.DatabaseService;
43

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

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

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

    
419
  /**
420
   * To a given server name, try to get its hub option
421
   * If couldn't find the server in the server list, false will return
422
   * @param givenServerName, the server's name which we want to get hub
423
   */
424
  public synchronized boolean getHubValue(String givenServerName)
425
  {
426
    int index = -1;//Variable to store the index
427
    ReplicationServer server = null;//Variable for replication server
428
    
429
    //Get the server's index in server list
430
    index = findIndexInServerList(givenServerName);
431
    //If index = -1, couldn't find this server, null will return
432
    if (index == -1)
433
    {
434
      return false;
435
    }//if
436
    //Get Replication server object
437
    server = (ReplicationServer) serverList.elementAt(index);
438
    //Return this object's hub value
439
    return server.getHub();
440
  }//getHubValue  
441
  
442
  /**
443
   * To a given server name, to check if it is in the server list.
444
   * If it is not, add it to server list and the xml_replication table
445
   * This method is for a document was replicated by a hub and the document's
446
   * home server is not in the replication table. The value for replicate,
447
   * datareplicate and hub are all false
448
   * @param givenServerName, the server's name which want to check
449
   */
450
  public synchronized void addToServerListIfItIsNot(String givenServerName)
451
  {
452
    // Variable to store the index for the givenServerName
453
    int index = -1;
454
    // Variable to store the new replciation server object
455
    ReplicationServer newServer = null;
456
    // For sql command
457
    PreparedStatement pStmt=null;
458
    // For check out DBConnection
459
    DBConnection dbConn = null;
460
    int serialNumber = -1;
461
    // Field value for xml_replication table
462
    int replicate = 0;
463
    int dataReplicate = 0;
464
    int hub = 0;
465
    
466
    // Get the the index
467
    index = findIndexInServerList(givenServerName);
468
    // if index ==-1, it not in the server list
469
    // Add the server to server list
470
    // Add the server to xml_replication table
471
    if (index ==-1)
472
    {
473
      // Create a new replication server object it's replicate, datareplicate
474
      // and hub values are default - false
475
      newServer = new ReplicationServer();
476
      // Set newServer's name as givenServerName
477
      newServer.setServerName(givenServerName);
478
      // Add newServer to serverList
479
      this.addNewReplicationServer(newServer);
480
      // Add this server to xml_table too
481
     try
482
      {
483
    	 Calendar cal = Calendar.getInstance();
484
         cal.set(1980, 1, 1);
485
        // Checkout DBConnection     
486
        dbConn=DBConnectionPool.
487
             getDBConnection("ReplicationSErverList.addToServerListIfItIsNot");
488
        serialNumber=dbConn.getCheckOutSerialNumber();
489
        // Inser it into xml_replication table
490
        /*pStmt = dbConn.prepareStatement("INSERT INTO xml_replication " +
491
                      "(server, last_checked, replicate, datareplicate, hub) "+
492
                       "VALUES ('" + givenServerName + "', to_date(" +
493
                       "'01/01/00', 'MM/DD/YY'), '" +
494
                       replicate +"', '"+dataReplicate+"','"+ hub + "')");*/
495
        pStmt = dbConn.prepareStatement("INSERT INTO xml_replication " +
496
                      "(server, last_checked, replicate, datareplicate, hub) "+
497
                       "VALUES (?, ?, ?, ?, ?, ?)");
498
        pStmt.setString(1, givenServerName);
499
		pStmt.setTimestamp(2, new Timestamp(cal.getTimeInMillis()) );
500
        pStmt.setInt(3, replicate);
501
        pStmt.setInt(4, dataReplicate);
502
        pStmt.setInt(5, hub);
503

    
504
        pStmt.execute();
505
       
506
        pStmt.close();
507
      }//try
508
      catch (Exception e)
509
      {
510
        logMetacat.error("Error in ReplicationServerList."+
511
                            "addToServerListIfItIsNot: " + e.getMessage());
512
      }//catch
513
      finally
514
      { 
515
        try
516
        {
517
          pStmt.close();
518
        }//try
519
        catch (Exception ee)
520
        { 
521
          logMetacat.error("Error in ReplicationServerList."+
522
                            "addToServerListIfItIsNot: " + ee.getMessage());
523
        }//catch
524
        finally
525
        {
526
          DBConnectionPool.returnDBConnection(dbConn, serialNumber);
527
        }//finally
528
      }//finally
529
    }//if
530
  }//addToServerListIfItIsNot
531
  
532
}//ReplicationServerList
(6-6/8)