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-12-21 13:00:40 -0800 (Wed, 21 Dec 2011) $'
11
 * '$Revision: 6814 $'
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, 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
        //Hub value
113
        int hubValue = rs.getInt(5);
114
        logMetacat.info("Hub value: "+hubValue);
115
        //Get rid of local host
116
        if(!serverName.equals("localhost"))
117
        {
118
          
119
          //create a new object of Replication
120
          server = new ReplicationServer();
121
         
122
          //Set server name
123
          server.setServerName(serverName);
124
     
125
          //Set last check date
126
          server.setLastCheckedDate(lastChecked);
127
          //From replication value to determine hasReplication valuse
128
          if (replication ==1)
129
          {
130
            //If replication equals 1, it can replicate xml documents
131
            hasReplication = true;
132
          }//if
133
          else
134
          {
135
            //if replication is NOT 1, it can't replicate xml documents
136
            hasReplication = false;
137
          }//else
138
          //Set replication value
139
          server.setReplication(hasReplication);
140
           
141
          //From both replication and data replication value to determine
142
          //hasDataReplication value
143
          if (hasReplication && dataReplication ==1)
144
          { 
145
            //Only server can replicate xml (hasRplication == true) and
146
            // dataReplication is 1, local host can replicate data file
147
            hasDataReplication = true;
148
          }//if
149
          else
150
          {
151
            hasDataReplication = false;
152
          }//else
153
          //Set data replciation value
154
          server.setDataReplication(hasDataReplication);
155
          
156
          //Detemine isHub by hubValue
157
          if (hubValue ==1)
158
          {
159
            isHub = true;
160
          }//if
161
          else
162
          {
163
            isHub = false;
164
          }//else
165
          //Set hub value
166
          server.setHub(isHub);
167
          
168
          //Add this server into server list
169
          serverList.add(server);
170
        }//if
171
        //Go to new row      
172
        tableHasRows = rs.next();   
173
      }//while
174
     
175
    }//try
176
    catch(Exception e)
177
    {
178
      logMetacat.error("Error in ReplicationServerList."
179
                                    +"buildServerList(): "+e.getMessage());
180
    }//catch
181
    finally
182
    {
183
      try
184
      {
185
        //close result set
186
        rs.close();
187
        //close prepareed statement
188
        pstmt.close();
189
      }//try
190
      catch (SQLException sqlE)
191
      {
192
        logMetacat.error("Error in ReplicationHandler.buildServerList: "
193
                                +sqlE.getMessage());
194
      }//catch
195
      finally
196
      {
197
        //Return dbconnection too pool
198
        DBConnectionPool.returnDBConnection(dbConn, serialNumber);
199
      }//finally
200
    }//finally
201
  
202
  }//buildServerList
203
  
204
  /**
205
   * Method to determine the server list is empty or not
206
   */
207
  public boolean isEmpty()
208
  {
209
    return serverList.isEmpty();
210
  }//isEmpty
211
  
212
  /**
213
   * Method to get the size of serverList
214
   */
215
  public int size()
216
  {
217
    return serverList.size();
218
  }//size()
219
  
220
  /**
221
   * Method to add a new replciation server object to serverList
222
   * @Param newReplciationServer, the object need to be add
223
   */
224
  private synchronized void addNewReplicationServer
225
                                      (ReplicationServer newReplicationServer)
226
  {
227
    serverList.add(newReplicationServer);
228
  }//addNewReplicationServer
229
  
230
  /**
231
   * Method to get a server object given a index number
232
   * @param index, the given index number
233
   */
234
  public ReplicationServer serverAt(int index)
235
  {
236
    return (ReplicationServer)serverList.elementAt(index);
237
  }//serverAt
238
   
239

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

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

    
474
        pStmt.execute();
475
       
476
        pStmt.close();
477
      }//try
478
      catch (Exception e)
479
      {
480
        logMetacat.error("Error in ReplicationServerList."+
481
                            "addToServerListIfItIsNot: " + e.getMessage());
482
      }//catch
483
      finally
484
      { 
485
        try
486
        {
487
          pStmt.close();
488
        }//try
489
        catch (Exception ee)
490
        { 
491
          logMetacat.error("Error in ReplicationServerList."+
492
                            "addToServerListIfItIsNot: " + ee.getMessage());
493
        }//catch
494
        finally
495
        {
496
          DBConnectionPool.returnDBConnection(dbConn, serialNumber);
497
        }//finally
498
      }//finally
499
    }//if
500
  }//addToServerListIfItIsNot
501
  
502
}//ReplicationServerList
(5-5/7)