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: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.util;
030:
031: import java.lang.ref.WeakReference;
032:
033: /**
034: * The alarm class provides a lightweight event scheduler. This allows
035: * an objects to schedule a timeout without creating a new thread.
036: *
037: * <p>A separate thread periodically tests the queue for alarms ready.
038: *
039: * <p>You should use Cron for slow requests. Alarm is only
040: * appropriate for very short jobs.
041: */
042: public class WeakAlarm extends Alarm {
043: private WeakReference<AlarmListener> _listenerRef;
044: private WeakReference<ClassLoader> _loaderRef;
045:
046: /**
047: * Create a new wakeup alarm with a designated listener as a callback.
048: * The alarm is not scheduled.
049: */
050: public WeakAlarm(AlarmListener listener) {
051: super (listener);
052: }
053:
054: /**
055: * Create a new wakeup alarm with a designated listener as a callback.
056: * The alarm is not scheduled.
057: */
058: public WeakAlarm(String name, AlarmListener listener) {
059: super (name, listener);
060: }
061:
062: /**
063: * Creates a named alarm and schedules its wakeup.
064: *
065: * @param name the object prepared to receive the callback
066: * @param listener the object prepared to receive the callback
067: * @param delta the time in milliseconds to wake up
068: */
069: public WeakAlarm(String name, AlarmListener listener, long delta) {
070: super (name, listener, delta);
071: }
072:
073: /**
074: * Creates a new alarm and schedules its wakeup.
075: *
076: * @param listener the object prepared to receive the callback
077: * @param delta the time in milliseconds to wake up
078: */
079: public WeakAlarm(AlarmListener listener, long delta) {
080: this (listener);
081:
082: queue(delta);
083: }
084:
085: /**
086: * Return the alarm's listener.
087: */
088: public AlarmListener getListener() {
089: return _listenerRef.get();
090: }
091:
092: /**
093: * Sets the alarm's listener.
094: */
095: public void setListener(AlarmListener listener) {
096: _listenerRef = new WeakReference<AlarmListener>(listener);
097: }
098:
099: /**
100: * Sets the class loader.
101: */
102: public void setContextLoader(ClassLoader loader) {
103: if (loader != null)
104: _loaderRef = new WeakReference<ClassLoader>(loader);
105: else
106: _loaderRef = null;
107: }
108: }
|