KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > winstone > cluster > ClusterSessionSearch


1 /*
2  * Copyright 2003-2006 Rick Knowles <winstone-devel at lists sourceforge net>
3  * Distributed under the terms of either:
4  * - the common development and distribution license (CDDL), v1.0; or
5  * - the GNU Lesser General Public License, v2.1 or later
6  */

7 package winstone.cluster;
8
9 import java.net.*;
10 import java.io.*;
11
12 import winstone.Logger;
13 import winstone.WinstoneSession;
14
15 /**
16  * Contains all the logic for reading in sessions
17  *
18  * @author <a HREF="mailto:rick_knowles@hotmail.com">Rick Knowles</a>
19  * @version $Id: ClusterSessionSearch.java,v 1.6 2006/03/24 17:24:18 rickknowles Exp $
20  */

21 public class ClusterSessionSearch implements Runnable JavaDoc {
22     final int TIMEOUT = 2000;
23     public static final byte SESSION_CHECK_TYPE = (byte) '1';
24     public static final String JavaDoc SESSION_NOT_FOUND = "NOTFOUND";
25     public static final String JavaDoc SESSION_FOUND = "FOUND";
26     public static final String JavaDoc SESSION_RECEIVED = "OK";
27     private boolean isFinished;
28 // private boolean interrupted;
29
private WinstoneSession result;
30     private String JavaDoc searchWebAppHostname;
31     private String JavaDoc searchWebAppPrefix;
32     private String JavaDoc searchId;
33     private String JavaDoc searchAddressPort;
34     private int controlPort;
35
36     /**
37      * Sets up for a threaded search
38      */

39     public ClusterSessionSearch(String JavaDoc webAppPrefix, String JavaDoc hostName, String JavaDoc sessionId,
40             String JavaDoc ipPort, int controlPort) {
41         this.isFinished = false;
42         this.searchWebAppHostname = hostName;
43         this.searchWebAppPrefix = webAppPrefix;
44 // this.interrupted = false;
45
this.searchId = sessionId;
46         this.searchAddressPort = ipPort;
47         this.result = null;
48         this.controlPort = controlPort;
49
50         // Start the search thread
51
Thread JavaDoc searchThread = new Thread JavaDoc(this);
52         searchThread.setDaemon(true);
53         searchThread.start();
54     }
55
56     /**
57      * Actually implements the search
58      */

59     public void run() {
60         try {
61             int colonPos = this.searchAddressPort.indexOf(':');
62             String JavaDoc ipAddress = this.searchAddressPort.substring(0, colonPos);
63             String JavaDoc port = this.searchAddressPort.substring(colonPos + 1);
64
65             Socket controlConnection = new Socket(ipAddress, Integer.parseInt(port));
66             controlConnection.setSoTimeout(TIMEOUT);
67             OutputStream out = controlConnection.getOutputStream();
68             out.write(SESSION_CHECK_TYPE);
69             out.flush();
70
71             ObjectOutputStream outControl = new ObjectOutputStream(out);
72             outControl.writeInt(this.controlPort);
73             outControl.writeUTF(this.searchId);
74             outControl.writeUTF(this.searchWebAppHostname);
75             outControl.writeUTF(this.searchWebAppPrefix);
76             outControl.flush();
77             InputStream in = controlConnection.getInputStream();
78             ObjectInputStream inSession = new ObjectInputStream(in);
79             String JavaDoc reply = inSession.readUTF();
80             if ((reply != null) && reply.equals(SESSION_FOUND)) {
81                 WinstoneSession session = (WinstoneSession) inSession
82                         .readObject();
83                 outControl.writeUTF(SESSION_RECEIVED);
84                 this.result = session;
85             }
86             outControl.close();
87             inSession.close();
88             out.close();
89             in.close();
90             controlConnection.close();
91         } catch (Throwable JavaDoc err) {
92             Logger.log(Logger.WARNING, SimpleCluster.CLUSTER_RESOURCES,
93                     "ClusterSessionSearch.Error", err);
94         }
95         this.isFinished = true;
96     }
97
98     public boolean isFinished() {
99         return this.isFinished;
100     }
101
102     public WinstoneSession getResult() {
103         return this.result;
104     }
105
106     public void destroy() {
107 // this.interrupted = true;
108
}
109
110     public String JavaDoc getAddressPort() {
111         return this.searchAddressPort;
112     }
113 }
114
Popular Tags