001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.ejb.timer;
031:
032: import javax.ejb.*;
033:
034: import java.io.Serializable;
035: import java.util.ArrayList;
036: import java.util.Collection;
037: import java.util.Date;
038: import java.util.logging.*;
039:
040: import com.caucho.ejb.AbstractContext;
041: import com.caucho.util.Alarm;
042: import com.caucho.util.L10N;
043: import com.caucho.loader.EnvironmentLocal;
044:
045: /**
046: * Implements the timer service
047: */
048: public class EjbTimerService implements TimerService {
049: private static final L10N L = new L10N(EjbTimerService.class);
050: protected static final Logger log = Logger
051: .getLogger(EjbTimerService.class.getName());
052:
053: private static final EnvironmentLocal<EjbTimerService> _localTimer = new EnvironmentLocal<EjbTimerService>();
054:
055: private AbstractContext _context;
056: private ArrayList<EjbTimer> _timers = new ArrayList<EjbTimer>();
057:
058: EjbTimerService(AbstractContext context) {
059: _context = context;
060: }
061:
062: public static EjbTimerService getLocal(ClassLoader loader,
063: AbstractContext context) {
064: synchronized (_localTimer) {
065: EjbTimerService timer = _localTimer.get(loader);
066:
067: if (timer == null) {
068: timer = new EjbTimerService(context);
069:
070: _localTimer.set(timer, loader);
071: }
072:
073: return timer;
074: }
075: }
076:
077: public static EjbTimerService getCurrent() {
078: return getCurrent(Thread.currentThread()
079: .getContextClassLoader());
080: }
081:
082: public static EjbTimerService getCurrent(ClassLoader loader) {
083: return _localTimer.get(loader);
084: }
085:
086: /**
087: * Creates a timer for a duration.
088: */
089: public Timer createTimer(long duration, Serializable info)
090: throws EJBException {
091: if (duration < 0)
092: throw new IllegalArgumentException(
093: "Timer duration must not be negative");
094:
095: Date expiration = new Date(Alarm.getCurrentTime() + duration);
096:
097: return createTimer(expiration, info);
098: }
099:
100: /**
101: * Creates an interval timer
102: */
103: public Timer createTimer(long initialDuration,
104: long intervalDuration, Serializable info)
105: throws EJBException {
106: if (initialDuration < 0)
107: throw new IllegalArgumentException(
108: "Timer initial duration must not be negative");
109:
110: if (intervalDuration < 0)
111: throw new IllegalArgumentException(
112: "Timer interval duration must not be negative");
113:
114: Date initialExpiration = new Date(Alarm.getCurrentTime()
115: + initialDuration);
116:
117: return createTimer(initialExpiration, intervalDuration, info);
118: }
119:
120: /**
121: * Creates a timer
122: */
123: public Timer createTimer(Date expiration, Serializable info)
124: throws EJBException {
125: EjbTimer timer = new EjbTimer(expiration, info, _context);
126:
127: _timers.add(timer);
128:
129: return timer;
130: }
131:
132: /**
133: * Creates a timer
134: */
135: public Timer createTimer(Date expiration, long interval,
136: Serializable info) throws EJBException {
137: if (interval < 0)
138: throw new IllegalArgumentException(
139: "Timer interval must not be negative");
140:
141: EjbTimer timer = new EjbTimer(expiration, interval, info,
142: _context);
143:
144: _timers.add(timer);
145:
146: return timer;
147: }
148:
149: /**
150: * Returns the timers
151: */
152: public Collection getTimers() throws EJBException {
153: return _timers;
154: }
155:
156: /**
157: * Finds a timer by id
158: */
159: EjbTimer __caucho_find(long timerId) {
160: for (EjbTimer timer : _timers) {
161: if (timer.__caucho_getId() == timerId)
162: return timer;
163: }
164:
165: return null;
166: }
167:
168: public String toString() {
169: return "EjbTimerService[]";
170: }
171: }
|