Project

General

Profile

1 581 berkley
/**
2
 *  '$RCSfile$'
3 669 jones
 *    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 581 berkley
 *  Copyright: 2000 Regents of the University of California and the
9
 *             National Center for Ecological Analysis and Synthesis
10 669 jones
 *    Authors: Chad Berkley
11
 *    Release: @release@
12 581 berkley
 *
13 669 jones
 *   '$Author$'
14
 *     '$Date$'
15
 * '$Revision$'
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 581 berkley
 */
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 669 jones
/**
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 581 berkley
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 1022 tao
  private boolean xmlDocument;
56 581 berkley
  private boolean dbactionFlag = true;
57
58 1022 tao
59 581 berkley
  /**
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 1022 tao
  public ForceReplicationHandler(String docid, String action, boolean xml )
65 581 berkley
  {
66
    this.docid = docid;
67
    this.action = action;
68 1022 tao
    this.xmlDocument =xml;
69
70 581 berkley
    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 1022 tao
  public ForceReplicationHandler(String docid, boolean xml)
84 581 berkley
  {
85
    this.docid = docid;
86 1022 tao
    this.xmlDocument = xml;
87 581 berkley
    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 1022 tao
      //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 581 berkley
      conn = util.openDBConnection();
105
      Enumeration keys = (ReplicationHandler.buildServerList(conn)).keys();
106
      //get the list of servers
107
      while(keys.hasMoreElements())
108
      {
109
        String server = (String)(keys.nextElement());
110
        if(dbactionFlag)
111
        {
112 1022 tao
            if (xmlDocument)// xml documents
113
            {
114
              MetacatReplication.replLog("force xml replicating to " + server);
115
              comeAndGetIt = new URL("https://" + server +
116 581 berkley
                                 "?action=forcereplicate&server=" +
117
                                 util.getOption("server") +
118
                                 util.getOption("replicationpath") +
119
                                 "&docid=" + docid + "&dbaction=" +
120
                                  action);
121 1022 tao
            }
122
            //It is data file and configured to handle data replication
123
            else if ((util.getOption("replication")).equals("on"))
124
            {
125
              MetacatReplication.replLog("force data replicating to " + server);
126
              comeAndGetIt = new URL("https://" + server +
127
                                 "?action=forcereplicatedatafile&server=" +
128
                                 util.getOption("server") +
129
                                 util.getOption("replicationpath") +
130
                                 "&docid=" + docid + "&dbaction=" +
131
                                  action);
132
133
            }//else if
134
135
        }//if
136 581 berkley
        else
137
        {
138 584 berkley
          MetacatReplication.replLog("force replicating (default action) to )" +
139
                                      server);
140 1022 tao
          if (xmlDocument)
141
          {
142
            comeAndGetIt = new URL("https://" + server +
143 581 berkley
                                 "?action=forcereplicate&server=" +
144
                                 util.getOption("server") +
145
                                 util.getOption("replicationpath") +
146
                                 "&docid=" + docid);
147 1022 tao
          }
148
          //It is datafile and metacat is configured to handle data file
149
          else if ((util.getOption("replication")).equals("on"))
150
          {
151
            comeAndGetIt = new URL("https://" + server +
152
                                 "?action=forcereplicatedatafile&server=" +
153
                                 util.getOption("server") +
154
                                 util.getOption("replicationpath") +
155
                                 "&docid=" + docid);
156
          }//else if
157
        }//else
158 590 berkley
        MetaCatUtil.debugMessage("sending message: " + comeAndGetIt.toString());
159 581 berkley
        String message = MetacatReplication.getURLContent(comeAndGetIt);
160
        //send out the url.  message is a dummy variable as the target of
161
        //the URL never directly replies to the request.  this simply
162
        //invoces a read request from the server to this local machine.
163 1022 tao
      }//while
164 581 berkley
      conn.close();
165 1022 tao
    }//try
166 581 berkley
    catch(Exception e)
167
    {
168 675 berkley
      System.out.println("error in ForceReplicationHandler.run: " +
169
                          e.getMessage());
170 581 berkley
      e.printStackTrace(System.out);
171
    }
172 590 berkley
    MetaCatUtil.debugMessage("exiting ForceReplicationHandler Thread");
173 581 berkley
  }
174
}