001: /*
002: * Copyright 2005 jWic group (http://www.jwic.de)
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: *
016: * de.jwic.controls.ClientNotifierControl
017: * Created on 05.12.2005
018: * $Id: ClientNotifierControl.java,v 1.2 2006/08/14 10:56:37 lordsam Exp $
019: */
020: package de.jwic.controls;
021:
022: import java.io.IOException;
023: import java.io.PrintWriter;
024:
025: import javax.servlet.http.HttpServletRequest;
026: import javax.servlet.http.HttpServletResponse;
027:
028: import de.jwic.base.Control;
029: import de.jwic.base.Field;
030: import de.jwic.base.IControlContainer;
031: import de.jwic.base.IResourceControl;
032:
033: /**
034: * Creates a JavaScript on the client that polls the control if a 'submit' is
035: * required. This enables the application to force a refresh of the controls on a
036: * users page from another thread (i.e. a background process).
037: *
038: * This control works ONLY if the users browser supports XmlHttpRequest (ajax).
039: *
040: * @author Florian Lippisch
041: * @version $Revision: 1.2 $
042: */
043: public class ClientNotifierControl extends Control implements
044: IResourceControl {
045:
046: private static final long serialVersionUID = 1L;
047:
048: private int interval = 2000; // intervall in milliseconds
049: private int sleepTime = 200;
050:
051: private int stateValue = 0;
052: private Thread waitThread = null;
053:
054: private Field fldStateValue;
055:
056: /**
057: * @param container
058: * @param name
059: */
060: public ClientNotifierControl(IControlContainer container) {
061: this (container, null);
062: }
063:
064: /**
065: * @param container
066: * @param name
067: */
068: public ClientNotifierControl(IControlContainer container,
069: String name) {
070: super (container, name);
071: fldStateValue = new Field(this , "stateValue");
072: fldStateValue.setValue(Integer.toString(stateValue));
073: }
074:
075: /* (non-Javadoc)
076: * @see de.jwic.base.Control#actionPerformed(java.lang.String, java.lang.String)
077: */
078: public void actionPerformed(String actionId, String parameter) {
079: if (actionId.equals("refresh")) {
080: requireRedraw(); // force redraw
081: }
082:
083: }
084:
085: /* (non-Javadoc)
086: * @see de.jwic.base.IResourceControl#attachResource(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
087: */
088: public void attachResource(HttpServletRequest req,
089: HttpServletResponse res) throws IOException {
090:
091: log.debug("checkStateMonitor IN");
092:
093: try {
094: String value = req.getParameter(fldStateValue.getId());
095: if (value == null || value.equals(fldStateValue.getValue())) {
096: if (waitThread != null) {
097: log.warn("A thread is already waiting!");
098: }
099: waitThread = Thread.currentThread();
100: Thread.sleep(sleepTime);
101: waitThread = null;
102: }
103: } catch (InterruptedException e) {
104: // as expected
105: waitThread = null;
106: }
107:
108: res.setContentType("text/plain");
109: PrintWriter pw = res.getWriter();
110: pw.write(Integer.toString(stateValue));
111: pw.flush();
112:
113: log.debug("checkStateMonitor OUT");
114:
115: }
116:
117: /**
118: * @return Returns the interval.
119: */
120: public int getInterval() {
121: return interval;
122: }
123:
124: /**
125: * @param interval The interval to set.
126: */
127: public void setInterval(int interval) {
128: this .interval = interval;
129: }
130:
131: /**
132: * @return Returns the sleepTime.
133: */
134: public int getSleepTime() {
135: return sleepTime;
136: }
137:
138: /**
139: * @param sleepTime The sleepTime to set.
140: */
141: public void setSleepTime(int sleepTime) {
142: this .sleepTime = sleepTime;
143: }
144:
145: /**
146: *
147: */
148: public void notifyClient() {
149: stateValue++;
150: requireRedraw();
151: fldStateValue.setValue(Integer.toString(stateValue));
152: if (waitThread != null) {
153: waitThread.interrupt();
154: }
155: }
156:
157: /**
158: * Returns a unique ID based upon the control ID that is used by the template to
159: * render unique javascript functions.
160: * @return
161: */
162: public String getUniqueID() {
163: return getControlID().replace('.', '_');
164: }
165:
166: /**
167: * @return Returns the stateValue.
168: */
169: public int getStateValue() {
170: return stateValue;
171: }
172:
173: /**
174: * @param stateValue The stateValue to set.
175: */
176: public void setStateValue(int stateValue) {
177: this.stateValue = stateValue;
178: fldStateValue.setValue(Integer.toString(stateValue));
179: }
180:
181: }
|