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-23 18:07:12 -0700 (Tue, 23 Apr 2002) $'
15
 * '$Revision: 1022 $'
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.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
            if (xmlDocument)// xml documents
113
            {
114
              MetacatReplication.replLog("force xml replicating to " + server);
115
              comeAndGetIt = new URL("https://" + server + 
116
                                 "?action=forcereplicate&server=" + 
117
                                 util.getOption("server") + 
118
                                 util.getOption("replicationpath") +
119
                                 "&docid=" + docid + "&dbaction=" +
120
                                  action);
121
            }
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
        else
137
        {
138
          MetacatReplication.replLog("force replicating (default action) to )" + 
139
                                      server);
140
          if (xmlDocument)
141
          {
142
            comeAndGetIt = new URL("https://" + server + 
143
                                 "?action=forcereplicate&server=" + 
144
                                 util.getOption("server") + 
145
                                 util.getOption("replicationpath") +
146
                                 "&docid=" + docid);
147
          }
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
        MetaCatUtil.debugMessage("sending message: " + comeAndGetIt.toString());
159
        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
      }//while
164
      conn.close();
165
    }//try
166
    catch(Exception e)
167
    {
168
      System.out.println("error in ForceReplicationHandler.run: " + 
169
                          e.getMessage());
170
      e.printStackTrace(System.out);
171
    }
172
    MetaCatUtil.debugMessage("exiting ForceReplicationHandler Thread");
173
  }
174
}
(26-26/41)