Source Code Cross Referenced for LongLastingOperationServlet.java in  » Wiki-Engine » VeryQuickWiki » vqwiki » servlets » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Wiki Engine » VeryQuickWiki » vqwiki.servlets 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:         */
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.