001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.timer.vm;
017:
018: import java.util.Collections;
019: import java.util.Iterator;
020: import java.util.LinkedHashMap;
021: import java.util.Map;
022: import java.util.Collection;
023: import java.util.ArrayList;
024:
025: import java.util.concurrent.atomic.AtomicLong;
026:
027: import org.apache.geronimo.timer.PersistenceException;
028: import org.apache.geronimo.timer.Playback;
029: import org.apache.geronimo.timer.WorkInfo;
030: import org.apache.geronimo.timer.WorkerPersistence;
031:
032: /**
033: *
034: *
035: * @version $Rev: 520573 $ $Date: 2007-03-20 13:48:26 -0700 (Tue, 20 Mar 2007) $
036: *
037: * */
038: public class VMWorkerPersistence implements WorkerPersistence {
039:
040: private final Map tasks = Collections
041: .synchronizedMap(new LinkedHashMap());
042:
043: private final AtomicLong counter = new AtomicLong(0);
044:
045: public void save(WorkInfo workInfo) throws PersistenceException {
046: long id = counter.incrementAndGet();
047: workInfo.setId(id);
048: tasks.put(new Long(id), workInfo);
049: }
050:
051: public void cancel(long id) throws PersistenceException {
052: tasks.remove(new Long(id));
053: }
054:
055: public void playback(String key, Playback playback)
056: throws PersistenceException {
057: synchronized (tasks) {
058: for (Iterator iterator = tasks.entrySet().iterator(); iterator
059: .hasNext();) {
060: Map.Entry entry = (Map.Entry) iterator.next();
061: WorkInfo workInfo = (WorkInfo) entry.getValue();
062: playback.schedule(workInfo);
063: }
064: }
065: }
066:
067: public void fixedRateWorkPerformed(long id)
068: throws PersistenceException {
069: //don't do anything, we are sharing the object with NonTransactionalWork, which is incrementing the time itself.
070: // Long key = new Long(id);
071: // synchronized (tasks) {
072: // WorkInfo task = (WorkInfo) tasks.get(key);
073: // task.nextTime();
074: // //see if task was cancelled while we executed.
075: // if (task != null) {
076: // tasks.put(key, TaskWrapper.nextTask(task));
077: // }
078: // }
079: }
080:
081: public void intervalWorkPerformed(long id, long period)
082: throws PersistenceException {
083: //dont do anything... sharing data with WorkInfo.
084: }
085:
086: public Collection getIdsByKey(String key, Object userId)
087: throws PersistenceException {
088: Collection ids = new ArrayList();
089: synchronized (tasks) {
090: for (Iterator iterator = tasks.values().iterator(); iterator
091: .hasNext();) {
092: WorkInfo workInfo = (WorkInfo) iterator.next();
093: if (key.equals(workInfo.getKey())
094: && (userId == null || userId.equals(workInfo
095: .getUserId()))) {
096: ids.add(new Long(workInfo.getId()));
097: }
098: }
099: }
100: return ids;
101: }
102:
103: }
|