KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ControlApplet


1 /*
2  * $Id: ControlApplet.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2003 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24
25 import java.applet.Applet JavaDoc;
26 import java.applet.AppletContext JavaDoc;
27 import java.io.BufferedReader JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.InputStreamReader JavaDoc;
30 import java.io.PrintWriter JavaDoc;
31 import java.io.UnsupportedEncodingException JavaDoc;
32 import java.net.MalformedURLException JavaDoc;
33 import java.net.URL JavaDoc;
34 import java.net.URLConnection JavaDoc;
35 import java.net.URLEncoder JavaDoc;
36 import java.util.HashMap JavaDoc;
37 import java.util.Iterator JavaDoc;
38 import java.util.Map JavaDoc;
39
40 /**
41  * Control Applet - Client applet for page pushing and (future) chat
42  *
43  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
44  * @version $Rev: 5462 $
45  * @since 3.0
46  */

47 public class ControlApplet extends Applet JavaDoc implements Runnable JavaDoc {
48     
49     private static String JavaDoc pushUrl = "/commonapp/control/pushapplet";
50     private static String JavaDoc pullUrl = "/commonapp/control/pullapplet";
51     
52     protected AppletContext JavaDoc ctx = null;
53     
54     protected String JavaDoc sessionId = null;
55     protected String JavaDoc visitId = null;
56     protected String JavaDoc serverUrl = null;
57     protected String JavaDoc timeoutUrl = null;
58     protected String JavaDoc currentPage = null;
59     protected String JavaDoc debug = null;
60     
61     protected int waitTime = 1500;
62     protected int timeout = 300000;
63     
64     protected boolean isRunning = false;
65     protected boolean stopped = true;
66     
67     protected Thread JavaDoc thread = null;
68     
69     public void init() {
70         ctx = this.getAppletContext();
71         this.sessionId = this.getParameter("sessionId");
72         this.visitId = this.getParameter("visitId");
73         this.serverUrl = this.getParameter("serverUrl");
74         this.timeoutUrl = this.getParameter("timeoutUrl");
75         this.currentPage = this.getParameter("currentPage");
76         this.debug = this.getParameter("debug");
77         
78         // see if we override the integer values
79
try {
80             int waitInt = Integer.parseInt(this.getParameter("waitTime"));
81             if (waitInt > 0)
82                 waitTime = waitInt;
83         } catch (NumberFormatException JavaDoc e) {
84         }
85         try {
86             int outInt = Integer.parseInt(this.getParameter("timeout"));
87             if (outInt > 0)
88                 timeout = outInt;
89         } catch (NumberFormatException JavaDoc e) {
90         }
91                 
92         if (serverUrl != null) {
93             boolean sessionOkay = false;
94             boolean visitOkay = false;
95             boolean pageOkay = false;
96             
97             if (sessionId != null && sessionId.length() > 0)
98                 sessionOkay = true;
99             if (visitId != null && visitId.length() > 0)
100                 visitOkay = true;
101             if (currentPage != null && currentPage.length() > 0)
102                 pageOkay = true;
103              
104             if (sessionOkay && visitOkay && pageOkay) {
105                 // tell the host about our current page (mainly for followers)
106
this.push();
107             }
108         
109             // start the polling thread
110
this.isRunning = true;
111             this.stopped = false;
112             thread = new Thread JavaDoc(this);
113             thread.setDaemon(false);
114             thread.start();
115         }
116     }
117     
118     public void destroy() {
119         this.stopped = true;
120     }
121     
122     // poll the servlet for page request
123
public void run() {
124         while (isRunning && !stopped) {
125             this.pull();
126             try {
127                 Thread.sleep(waitTime);
128             } catch (InterruptedException JavaDoc e) {
129                 this.stopped = true;
130                 this.isRunning = false;
131             }
132         }
133         if (debug != null && debug.equalsIgnoreCase("true"))
134             System.out.println("Polling finished.");
135     }
136     
137     protected void pull() {
138         Map JavaDoc params = new HashMap JavaDoc();
139         params.put("sessionId", this.sessionId.trim());
140         params.put("visitId", this.visitId.trim());
141                             
142         String JavaDoc pullResp = null;
143         URL JavaDoc callPullUrl = null;
144         try {
145             callPullUrl = new URL JavaDoc(serverUrl + pullUrl);
146         } catch (MalformedURLException JavaDoc e) {
147         }
148         
149         if (callPullUrl != null) {
150             try {
151                 pullResp = callServer(callPullUrl, params);
152             } catch (IOException JavaDoc e) {
153             }
154         }
155         
156         if (pullResp != null && pullResp.length() > 0) {
157             URL JavaDoc docUrl = null;
158             try {
159                 docUrl = new URL JavaDoc(pullResp);
160             } catch (MalformedURLException JavaDoc e) {
161             }
162             if (docUrl != null)
163                 ctx.showDocument(docUrl, "appletWindow");
164         }
165     }
166     
167     protected void push() {
168         Map JavaDoc params = new HashMap JavaDoc();
169         params.put("sessionId", this.sessionId.trim());
170         params.put("visitId", this.visitId.trim());
171         params.put("currentPage", this.currentPage.trim());
172         
173         String JavaDoc pushResp = null;
174         URL JavaDoc callPushUrl = null;
175         try {
176             callPushUrl = new URL JavaDoc(serverUrl + pushUrl);
177         } catch (MalformedURLException JavaDoc e) {
178         }
179         
180         if (callPushUrl != null) {
181             try {
182                 pushResp = callServer(callPushUrl, params);
183             } catch (IOException JavaDoc e) {
184             }
185         }
186     }
187     
188     private String JavaDoc callServer(URL JavaDoc serverUrl, Map JavaDoc parms) throws IOException JavaDoc {
189         // send the request
190
String JavaDoc parameters = this.encodeArgs(parms);
191         if (debug != null && debug.equalsIgnoreCase("true"))
192             System.out.println("Sending parameters: " + parameters);
193         URLConnection JavaDoc uc = serverUrl.openConnection();
194         uc.setDoOutput(true);
195         uc.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
196         PrintWriter JavaDoc pw = new PrintWriter JavaDoc(uc.getOutputStream());
197         pw.println(parameters);
198         pw.close();
199         
200         // read the response
201
BufferedReader JavaDoc in = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(uc.getInputStream()));
202         String JavaDoc responseStr = in.readLine();
203         in.close();
204         if (responseStr != null)
205             responseStr.trim();
206         if (debug != null && debug.equalsIgnoreCase("true"))
207             System.out.println("Receive response: " + responseStr);
208         return responseStr;
209     }
210     
211     public String JavaDoc encodeArgs(Map JavaDoc args) {
212         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
213         if (args != null) {
214             Iterator JavaDoc i = args.entrySet().iterator();
215             while (i.hasNext()) {
216                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) i.next();
217                 String JavaDoc name = (String JavaDoc) entry.getKey();
218                 Object JavaDoc value = entry.getValue();
219                 String JavaDoc valueStr = null;
220                 if (name != null && value != null) {
221                     if (value instanceof String JavaDoc) {
222                         valueStr = (String JavaDoc) value;
223                     } else {
224                         valueStr = value.toString();
225                     }
226                     
227                     if (valueStr != null && valueStr.length() > 0) {
228                         if (buf.length() > 0) buf.append('&');
229                         try {
230                             buf.append(URLEncoder.encode(name, "UTF-8"));
231                         } catch (UnsupportedEncodingException JavaDoc e) {
232                             e.printStackTrace();
233                         }
234                         buf.append('=');
235                         try {
236                             buf.append(URLEncoder.encode(valueStr, "UTF-8"));
237                         } catch (UnsupportedEncodingException JavaDoc e) {
238                             e.printStackTrace();
239                         }
240                     }
241                 }
242             }
243         }
244         return buf.toString();
245     }
246 }
247
Popular Tags