001: /*
002: Very Quick Wiki - WikiWikiWeb clone
003: Copyright (C) 2001-2002 Gareth Cronin
004:
005: This program is free software; you can redistribute it and/or modify
006: it under the terms of the latest version of the GNU Lesser General
007: Public License as published by the Free Software Foundation;
008:
009: This program is distributed in the hope that it will be useful,
010: but WITHOUT ANY WARRANTY; without even the implied warranty of
011: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: GNU Lesser General Public License for more details.
013:
014: You should have received a copy of the GNU Lesser General Public License
015: along with this program (gpl.txt); if not, write to the Free Software
016: Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: */
018: package vqwiki.servlets;
019:
020: import java.io.IOException;
021: import java.util.Date;
022: import java.util.Locale;
023:
024: import javax.servlet.ServletException;
025: import javax.servlet.http.HttpServletRequest;
026: import javax.servlet.http.HttpServletResponse;
027:
028: import vqwiki.WikiException;
029: import vqwiki.utils.LongLastingOperationsManager;
030:
031: /**
032: * Abstract class to support long lasting operations
033: *
034: * This class was created on 00:24:36 23.07.2003
035: *
036: * @author $Author: wrh2 $
037: */
038: public abstract class LongLastingOperationServlet extends VQWikiServlet
039: implements Runnable {
040:
041: /** time, when everything starts */
042: protected Date startingTime = null;
043: /** Maximum time in seconds to wait until the page is refreshed */
044: protected int MAX_TIME_TO_REFRESH = 3;
045: /** progress done */
046: public static final int PROGRESS_DONE = 1000;
047: /** the url, which is used to call this servlet */
048: protected String url;
049: /** progress made */
050: protected int progress = 0;
051: /** The request, which is used during the operation */
052: protected Locale locale = null;
053:
054: /**
055: * Handle post request.
056: * Generate a long lasting operation and execute it.
057: *
058: * @param request The current http request
059: * @param response What the servlet will send back as response
060: *
061: * @throws ServletException If something goes wrong during servlet execution
062: * @throws IOException If the output stream cannot be accessed
063: *
064: */
065: protected void doPost(HttpServletRequest httpRequest,
066: HttpServletResponse response) throws ServletException,
067: IOException {
068: if (httpRequest.getParameter("norefresh") != null) {
069: startingTime = new Date();
070: locale = httpRequest.getLocale();
071: url = httpRequest.getRequestURL().toString() + "?"
072: + httpRequest.getQueryString();
073: // execute task not in a thread
074: run();
075: dispatchDone(httpRequest, response);
076: } else {
077: LongLastingOperationsManager mgr = LongLastingOperationsManager
078: .getInstance();
079: int id;
080: // check, if we are coming here again
081: if (httpRequest.getParameter("id") == null) {
082: // first time visitor: create new thread
083: startingTime = new Date();
084: locale = httpRequest.getLocale();
085: url = httpRequest.getRequestURL().toString() + "?"
086: + httpRequest.getQueryString();
087: Thread t = new Thread(this );
088: id = mgr.registerNewThread(this );
089: t.setPriority(Thread.MIN_PRIORITY);
090: t.start();
091: } else {
092: try {
093: id = Integer.parseInt((String) httpRequest
094: .getParameter("id"));
095: } catch (NumberFormatException e) {
096: id = 0;
097: }
098: }
099: // get information on the thread
100: httpRequest.setAttribute("id", new Integer(id));
101: Runnable myThread = mgr.getThreadForId(id);
102: if (myThread != null) {
103: LongLastingOperationServlet myServlet = (LongLastingOperationServlet) myThread;
104: httpRequest.setAttribute("progress", new Integer(
105: myServlet.getProgress()));
106: httpRequest.setAttribute("nextRefresh", new Integer(
107: myServlet.getNextRefresh()));
108: httpRequest.setAttribute("url", myServlet.getUrl());
109: if (myServlet.getProgress() >= 100) {
110: myServlet.dispatchDone(httpRequest, response);
111: mgr.removeThreadById(id);
112: return;
113: }
114: } else {
115: error(httpRequest, response, new WikiException(
116: "Operation lost in /dev/null"));
117: return;
118: }
119: dispatch("/jsp/longlastingoperation.jsp", httpRequest,
120: response);
121: }
122: }
123:
124: /**
125: * Handle get request.
126: * The request is handled the same way as the post request.
127: *
128: * @see doPost()
129: *
130: * @param httpServletRequest The current http request
131: * @param httpServletResponse What the servlet will send back as response
132: *
133: * @throws ServletException If something goes wrong during servlet execution
134: * @throws IOException If the output stream cannot be accessed
135: *
136: */
137: protected void doGet(HttpServletRequest httpServletRequest,
138: HttpServletResponse httpServletResponse)
139: throws ServletException, IOException {
140: this .doPost(httpServletRequest, httpServletResponse);
141: }
142:
143: /**
144: * What is the progress of the ongoing operation?
145: * @return int giving the percent (0 - 100), how much is finished.
146: */
147: public int getProgress() {
148: return progress;
149: }
150:
151: /**
152: * How many seconds shall we wait until the next refresh?
153: * @return int giving the number of seconds to wait for the next refresh
154: */
155: protected int getNextRefresh() {
156: if (getProgress() < 50) {
157: return MAX_TIME_TO_REFRESH;
158: } else {
159: long alreadyWorking = new Date().getTime()
160: - startingTime.getTime();
161: int timeToFinish = (int) ((double) alreadyWorking / ((double) getProgress() * 10.0));
162: if (timeToFinish > MAX_TIME_TO_REFRESH) {
163: return MAX_TIME_TO_REFRESH;
164: } else {
165: return timeToFinish;
166: }
167: }
168: }
169:
170: /**
171: * Go to the done page
172: * @param request The current servlet request
173: * @param response The current servlet response
174: */
175: protected abstract void dispatchDone(
176: HttpServletRequest httpServletRequest,
177: HttpServletResponse httpServletResponse);
178:
179: /**
180: * Get the url, how this servlet is called
181: * @return
182: */
183: protected String getUrl() {
184: return url;
185: }
186: }
187:
188: /*
189: * Log:
190: *
191: * $Log$
192: * Revision 1.5 2006/04/23 06:36:55 wrh2
193: * Coding style updates (VQW-73).
194: *
195: * Revision 1.4 2003/10/05 05:07:32 garethc
196: * fixes and admin file encoding option + merge with contributions
197: *
198: * Revision 1.3 2003/08/20 20:41:41 mrgadget4711
199: * ADD: Override refresh with norefresh=true
200: *
201: * Revision 1.2 2003/07/23 09:50:50 mrgadget4711
202: * Fixes
203: *
204: * Revision 1.1 2003/07/23 00:34:26 mrgadget4711
205: * ADD: Long lasting operations
206: *
207: * ------------END------------
208: */
|