001: /**********************************************************************************
002: * $URL$
003: * $Id$
004: ***********************************************************************************
005: *
006: * Copyright (c) 2004, 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the"License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.tool.assessment.ui.queue.delivery;
021:
022: import org.sakaiproject.tool.assessment.ui.model.delivery.TimedAssessmentGradingModel;
023: import org.sakaiproject.tool.assessment.ui.queue.delivery.SubmitTimedAssessmentThread;
024: import org.apache.commons.logging.Log;
025: import org.apache.commons.logging.LogFactory;
026:
027: import java.util.Collection;
028: import java.util.HashMap;
029: import java.util.Iterator;
030: import java.util.Timer;
031:
032: /**
033: * <p>Title: </p>
034: * <p>Description: </p>
035: * <p>Copyright: Copyright (c) 2004</p>
036: * <p>Company: </p>
037: * @author not attributable
038: * @version 1.0
039: */
040:
041: public class TimedAssessmentQueue {
042: private static TimedAssessmentQueue instance;
043: private HashMap queue;
044: // private SubmitTimedAssessmentThread thread;
045: private Timer timer;
046:
047: private static Log log = LogFactory
048: .getLog(TimedAssessmentQueue.class);
049:
050: private TimedAssessmentQueue() {
051: queue = new HashMap();
052: }
053:
054: public static synchronized TimedAssessmentQueue getInstance() {
055: if (instance == null) {
056: instance = new TimedAssessmentQueue();
057: }
058: return instance;
059: }
060:
061: public void add(TimedAssessmentGradingModel timedAG) {
062: queue.put(timedAG.getAssessmentGradingId(), timedAG);
063: log
064: .debug("***1. TimedAssessmentQueue.add, before schedule timer="
065: + timer);
066: scheduleTask();
067: log
068: .debug("***2. TimedAssessmentQueue.add, after schedule timer="
069: + timer);
070:
071: }
072:
073: private void scheduleTask() {
074: if (timer == null) {
075: timer = new Timer();
076: timer.scheduleAtFixedRate(
077: new SubmitTimedAssessmentThread(), 0, 5 * 1000); //delay=0s, every 5s
078: }
079: }
080:
081: public void remove(TimedAssessmentGradingModel timedAG) {
082: queue.remove(timedAG.getAssessmentGradingId());
083: if (isEmpty()) {
084: log.debug("*** before destroy, timer=" + timer);
085: timer.cancel();
086: timer = null;
087: log.debug("*** after destroy, timer=" + timer);
088: }
089: }
090:
091: public TimedAssessmentGradingModel get(Long assessmentGradingId) {
092: return (TimedAssessmentGradingModel) queue
093: .get(assessmentGradingId);
094: }
095:
096: public void emptyQueue() {
097: queue.clear();
098: }
099:
100: public boolean isEmpty() {
101: return queue.isEmpty();
102: }
103:
104: public int size() {
105: return queue.size();
106: }
107:
108: public Iterator iterator() {
109: Collection c = queue.values();
110: return c.iterator();
111: }
112:
113: }
|