Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A class to asyncronously force the replication of each server
4
 *             that has an entry in the xml_replication table.  When run,
5
 *             this thread communicates with each server in the list and
6
 *             solicites a read of an updated or newly inserted document
7
 *             with a certain docid.
8
 *  Copyright: 2000 Regents of the University of California and the
9
 *             National Center for Ecological Analysis and Synthesis
10
 *    Authors: Chad Berkley
11
 *
12
 *   '$Author: daigle $'
13
 *     '$Date: 2009-08-24 13:34:17 -0800 (Mon, 24 Aug 2009) $'
14
 * '$Revision: 5030 $'
15
 *
16
 * This program is free software; you can redistribute it and/or modify
17
 * it under the terms of the GNU General Public License as published by
18
 * the Free Software Foundation; either version 2 of the License, or
19
 * (at your option) any later version.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 * GNU General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU General Public License
27
 * along with this program; if not, write to the Free Software
28
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
29
 */
30

    
31
package edu.ucsb.nceas.metacat.replication;
32

    
33
import java.net.URL;
34

    
35
import org.apache.log4j.Logger;
36

    
37
import edu.ucsb.nceas.metacat.properties.PropertyService;
38
import edu.ucsb.nceas.metacat.util.MetacatUtil;
39
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
40

    
41
/**
42
 * A class to asyncronously force the replication of each server
43
 * that has an entry in the xml_replication table.  When run,
44
 * this thread communicates with each server in the list and
45
 * forces replication of system metadata
46
 */
47
public class ForceReplicationSystemMetadataHandler implements Runnable
48
{
49
  private Thread btThread;
50
  private String guid;
51
    
52
  private ReplicationServerList serverLists = null;
53

    
54
  private String notificationServer = null;
55
  private static Logger logReplication = Logger.getLogger("ReplicationLogging");
56
  private static Logger logMetacat = Logger.getLogger(ForceReplicationSystemMetadataHandler.class);
57
 
58
  /**
59
   * Use this constructor when the action is implied.
60
   */
61
  public ForceReplicationSystemMetadataHandler(String docid, boolean xml,
62
                                                String myNotificationServer )
63
  {
64
    this.guid = docid;
65
    // Build a severLists from xml_replication table
66
    this.serverLists = new ReplicationServerList();
67
   
68
    // Get notification server
69
    this.notificationServer = myNotificationServer;
70
    btThread = new Thread(this);
71
    btThread.setPriority(Thread.MIN_PRIORITY);
72
    btThread.start();
73
  }
74

    
75
  /**
76
   * Method to send force replication command to other server to get
77
   * a new or updated docid
78
   */
79
  public void run()
80
  {
81

    
82
    
83
	    // URL for notification
84
	    URL comeAndGetIt = null;
85
		// If no server in xml_replication table we are done
86
		if (serverLists.isEmpty()) {
87
			return;
88
		}
89

    
90
		// Thread sleeping for some seconds to make sure the document was insert
91
		// into the database before we send force replication request
92
		int sleepTime;
93
		try {
94
			sleepTime = Integer.parseInt(PropertyService.getProperty("replication.forcereplicationwaitingtime"));
95
			Thread.sleep(sleepTime);
96
		} catch (PropertyNotFoundException pnfe) {
97
	    	logMetacat.error(ReplicationService.METACAT_REPL_ERROR_MSG);
98
			logReplication.error("Property error: " + pnfe.getMessage());
99
		} catch (InterruptedException ie) {
100
	    	logMetacat.error(ReplicationService.METACAT_REPL_ERROR_MSG);
101
			logReplication.error("Thread sleep error: " + ie.getMessage());
102
		}
103
		logReplication.info("Notification server:" + notificationServer);
104
		// Check every server in the serverlists
105
		for (int i = 0; i < serverLists.size(); i++) {
106
			//Set comeAndGetIt null
107
			comeAndGetIt = null;
108
			// Get ReplicationServer object in index i
109
			ReplicationServer replicationServer = serverLists.serverAt(i);
110
			// Get this ReplicationServer 's name
111
			String server = replicationServer.getServerName();
112
			try {
113

    
114
				// If the server is the notification server, we don't notify it back
115
				// again, if server is null we don't replicate to
116
				if (server != null && !server.equals(notificationServer)) {
117

    
118
					comeAndGetIt = new URL("https://" + server
119
							+ "?action=forcereplicatesystemmetadata"
120
							+ "&guid=" + guid + "&server="
121
							+ MetacatUtil.getLocalReplicationServerName());
122

    
123
					// Make sure comeAndGetIt is not empty
124
					if (comeAndGetIt != null && !comeAndGetIt.equals("")) {
125
						logReplication.warn("Sending message: " + comeAndGetIt.toString());
126
						String message = ReplicationService.getURLContent(comeAndGetIt);
127
					}
128
				}
129
			} catch (Exception e) {
130
		    	logMetacat.error(ReplicationService.METACAT_REPL_ERROR_MSG, e);
131
				logReplication.error("Error forcing System Metadata replication");
132
			}
133
			
134
		}
135

    
136
		logReplication.warn("Exiting Forced System Metadata Thread");
137
	}
138
}
(2-2/8)