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