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
 *    Release: @release@
12
 *
13
 *   '$Author: tao $'
14
 *     '$Date: 2002-04-30 08:21:10 -0700 (Tue, 30 Apr 2002) $'
15
 * '$Revision: 1040 $'
16
 *
17
 * This program is free software; you can redistribute it and/or modify
18
 * it under the terms of the GNU General Public License as published by
19
 * the Free Software Foundation; either version 2 of the License, or
20
 * (at your option) any later version.
21
 *
22
 * This program is distributed in the hope that it will be useful,
23
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25
 * GNU General Public License for more details.
26
 *
27
 * You should have received a copy of the GNU General Public License
28
 * along with this program; if not, write to the Free Software
29
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
30
 */
31
 
32
package edu.ucsb.nceas.metacat;
33

    
34
import java.util.*;
35
import java.io.*;
36
import java.sql.*;
37
import java.net.*;
38
import java.lang.*;
39
import java.text.*;
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
 * solicites a read of an updated or newly inserted document 
46
 * with a certain docid.
47
 */
48
public class ForceReplicationHandler implements Runnable
49
{
50
  private Thread btThread;
51
  private Connection conn;
52
  private MetaCatUtil util = new MetaCatUtil();
53
  private String docid;
54
  private String action;
55
  private boolean xmlDocument;
56
  private boolean dbactionFlag = true;
57
  private Hashtable serverList;
58
  
59
  
60
  /**
61
   * @param docid the docid to force replicate
62
   * @param the action that is being performed on the document (either 
63
   *        INSERT or UPDATE)
64
   */
65
  public ForceReplicationHandler(String docid, String action, boolean xml, 
66
                                                          Hashtable server )
67
  {
68
    this.docid = docid;
69
    this.action = action;
70
    this.xmlDocument =xml;
71
    this.serverList = server;
72
    
73
    if(this.action.equals(""))
74
    {
75
      dbactionFlag = false;
76
    }
77
    
78
    btThread = new Thread(this);
79
    btThread.setPriority(Thread.MIN_PRIORITY);
80
    btThread.start();
81
  }
82
  
83
  /**
84
   * Use this constructor when the action is implied.
85
   */
86
  public ForceReplicationHandler(String docid, boolean xml, Hashtable server)
87
  {
88
    this.docid = docid;
89
    this.xmlDocument = xml;
90
    dbactionFlag = false;
91
    this.serverList = server;
92
    btThread = new Thread(this);
93
    btThread.setPriority(Thread.MIN_PRIORITY);
94
    btThread.start();
95
  }
96
  
97
  public void run()
98
  {
99
    //System.out.println("in ForceReplicationHandler Thread");
100
    try
101
    {
102
      //check if the metacat was configured to hanldle replication
103
      if (!(util.getOption("replication")).equals("on"))
104
      {
105
         return;
106
      }
107
      URL comeAndGetIt = null;
108
      conn = util.getConnection();
109
      // If no server in xml_replication table, metacat don't need do anything
110
      if (ReplicationHandler.buildServerList(conn)==null)
111
      {
112
        return;
113
      }
114
      //Enumeration keys = (ReplicationHandler.buildServerList(conn)).keys();
115
      //Make sure that server list is not empty
116
      if (serverList==null || serverList.isEmpty())
117
      {
118
        return;
119
      }
120
      
121
      Enumeration keys = serverList.keys();
122
      //get the list of servers
123
      while(keys.hasMoreElements())
124
      {
125
        String server = (String)(keys.nextElement());
126
        if(dbactionFlag)
127
        {
128
            if (xmlDocument)// xml documents
129
            {
130
              MetacatReplication.replLog("force xml replicating to " + server);
131
              comeAndGetIt = new URL("https://" + server + 
132
                                 "?action=forcereplicate&server=" + 
133
                                 util.getOption("server") + 
134
                                 util.getOption("replicationpath") +
135
                                 "&docid=" + docid + "&dbaction=" +
136
                                  action);
137
            }
138
            //It is data file and configured to handle data replication
139
            else if ((util.getOption("replicationdata")).equals("on"))
140
            {
141
              MetacatReplication.replLog("force data replicating to " + server);
142
              comeAndGetIt = new URL("https://" + server + 
143
                                 "?action=forcereplicatedatafile&server=" + 
144
                                 util.getOption("server") + 
145
                                 util.getOption("replicationpath") +
146
                                 "&docid=" + docid + "&dbaction=" +
147
                                  action);
148
                                
149
            }//else if
150
    
151
        }//if
152
        else
153
        {
154
          MetacatReplication.replLog("force replicating (default action) to )" + 
155
                                      server);
156
          if (xmlDocument)
157
          {
158
            comeAndGetIt = new URL("https://" + server + 
159
                                 "?action=forcereplicate&server=" + 
160
                                 util.getOption("server") + 
161
                                 util.getOption("replicationpath") +
162
                                 "&docid=" + docid);
163
          }
164
          //It is datafile and metacat is configured to handle data file
165
          else if ((util.getOption("replicationdata")).equals("on"))
166
          {
167
            comeAndGetIt = new URL("https://" + server + 
168
                                 "?action=forcereplicatedatafile&server=" + 
169
                                 util.getOption("server") + 
170
                                 util.getOption("replicationpath") +
171
                                 "&docid=" + docid);
172
          }//else if
173
        }//else
174
        MetaCatUtil.debugMessage("sending message: " + comeAndGetIt.toString());
175
        String message = MetacatReplication.getURLContent(comeAndGetIt);
176
        //send out the url.  message is a dummy variable as the target of
177
        //the URL never directly replies to the request.  this simply
178
        //invoces a read request from the server to this local machine.
179
      }//while
180
      util.returnConnection(conn);
181
    }//try
182
    catch(Exception e)
183
    {
184
      System.out.println("error in ForceReplicationHandler.run: " + 
185
                          e.getMessage());
186
      e.printStackTrace(System.out);
187
    }
188
    MetaCatUtil.debugMessage("exiting ForceReplicationHandler Thread");
189
  }
190
}
(26-26/41)