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.utils;
019:
020: import java.util.Iterator;
021: import java.util.Vector;
022:
023: import vqwiki.servlets.LongLastingOperationServlet;
024:
025: /**
026: * Singleton class, which manages long lasting operations
027: *
028: * This class was created on 00:25:22 23.07.2003
029: *
030: * @author $Author: wrh2 $
031: */
032: public class LongLastingOperationsManager {
033:
034: /** instance of this class */
035: private static LongLastingOperationsManager instance = new LongLastingOperationsManager();
036:
037: /** a vector of all ongoing threads */
038: Vector onGoingThreads = new Vector();
039:
040: /**
041: * Constructor; private. Use getInstance() instead
042: *
043: * @see getInstance()
044: */
045: private LongLastingOperationsManager() {
046: }
047:
048: /**
049: *
050: */
051: public static LongLastingOperationsManager getInstance() {
052: return instance;
053: }
054:
055: /**
056: * Register a new thread to the manager
057: * @param t The thread to register
058: * @return the id of this thread
059: */
060: public synchronized int registerNewThread(Runnable r) {
061: // go through vector if threads already ended can be removed:
062: boolean somethingremoved = false;
063: do {
064: somethingremoved = false;
065: for (Iterator iter = onGoingThreads.iterator(); !somethingremoved
066: && iter.hasNext();) {
067: LongLastingOperationServlet aThread = (LongLastingOperationServlet) iter
068: .next();
069: if (aThread.getProgress() >= LongLastingOperationServlet.PROGRESS_DONE) {
070: iter.remove();
071: somethingremoved = true;
072: }
073: }
074: } while (somethingremoved);
075: // register new thread
076: int id = onGoingThreads.size();
077: onGoingThreads.add(id, r);
078: return id;
079: }
080:
081: /**
082: * Get a thread for a certain identifier
083: * @param string The identifier of this thred
084: * @return the thread or null, if the thread cannot be found
085: */
086: public Runnable getThreadForId(String idStr) {
087: try {
088: int id = Integer.parseInt(idStr);
089: return (Runnable) onGoingThreads.get(id);
090: } catch (Exception e) {
091: // in case of error:
092: return null;
093: }
094: }
095:
096: /**
097: * Get a thread for a certain identifier
098: * @param int The identifier of this thred
099: * @return the thread or null, if the thread cannot be found
100: */
101: public Runnable getThreadForId(int id) {
102: try {
103: return (Runnable) onGoingThreads.get(id);
104: } catch (Exception e) {
105: // in case of error:
106: return null;
107: }
108: }
109:
110: /**
111: * Remove a thread by its identifier
112: * @param id The id if the thread to be removed
113: */
114: public void removeThreadById(int id) {
115: try {
116: onGoingThreads.remove(id);
117: } catch (Exception e) {
118: ;
119: }
120: }
121: }
122:
123: /*
124: * Log:
125: *
126: * $Log$
127: * Revision 1.4 2006/04/23 04:45:45 wrh2
128: * Coding style updates (VQW-73).
129: *
130: * Revision 1.3 2003/10/05 05:07:32 garethc
131: * fixes and admin file encoding option + merge with contributions
132: *
133: * Revision 1.2 2003/07/23 09:50:54 mrgadget4711
134: * Fixes
135: *
136: * Revision 1.1 2003/07/23 00:34:26 mrgadget4711
137: * ADD: Long lasting operations
138: *
139: * ------------END------------
140: */
|