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.jmx;
030:
031: import com.caucho.loader.ClassLoaderListener;
032: import com.caucho.loader.DynamicClassLoader;
033: import com.caucho.loader.Environment;
034: import com.caucho.log.Log;
035: import com.caucho.util.Alarm;
036: import com.caucho.util.L10N;
037:
038: import java.util.ArrayList;
039: import java.util.TimerTask;
040: import java.util.logging.Level;
041: import java.util.logging.Logger;
042:
043: /**
044: * Static convenience methods.
045: */
046: class JobThread implements Runnable {
047: private static final L10N L = new L10N(JobThread.class);
048: private static final Logger log = Log.open(JobThread.class);
049:
050: private static JobThread _job = new JobThread();
051:
052: private ArrayList<Job> _jobs = new ArrayList<Job>();
053: private ArrayList<Job> _runJobs = new ArrayList<Job>();
054:
055: private JobThread() {
056: }
057:
058: /**
059: * Queues a task.
060: */
061: public static void queue(TimerTask task, long time) {
062: synchronized (_job._jobs) {
063: ArrayList<Job> jobs = _job._jobs;
064:
065: for (int i = jobs.size() - 1; i >= 0; i--) {
066: Job oldJob = jobs.get(i);
067:
068: if (oldJob.getTask() == task) {
069: if (time < oldJob.getTime())
070: oldJob.setTime(time);
071:
072: return;
073: }
074: }
075:
076: Job job = new Job(task);
077: job.setTime(time);
078:
079: _job._jobs.add(job);
080: }
081: }
082:
083: /**
084: * Dequeues a task.
085: */
086: public static void dequeue(TimerTask job) {
087: _job.remove(job);
088: }
089:
090: void remove(TimerTask task) {
091: synchronized (_jobs) {
092: for (int i = _jobs.size() - 1; i >= 0; i--) {
093: Job job = _jobs.get(i);
094:
095: if (job.getTask() == task)
096: _jobs.remove(i);
097: }
098: }
099: }
100:
101: public void run() {
102: Thread thread = Thread.currentThread();
103:
104: while (true) {
105: long now = Alarm.getCurrentTime();
106:
107: _runJobs.clear();
108:
109: synchronized (_jobs) {
110: for (int i = _jobs.size() - 1; i >= 0; i--) {
111: Job job = _jobs.get(i);
112:
113: if (job.getTime() <= now) {
114: _runJobs.add(job);
115: _jobs.remove(i);
116: }
117: }
118: }
119:
120: for (int i = _runJobs.size() - 1; i >= 0; i--) {
121: Job job = _runJobs.get(i);
122:
123: try {
124: job.run();
125: } catch (Throwable e) {
126: log.log(Level.WARNING, e.toString(), e);
127: }
128: }
129:
130: try {
131: thread.sleep(500);
132: } catch (Throwable e) {
133: }
134: }
135: }
136:
137: static class Job implements ClassLoaderListener {
138: private TimerTask _task;
139: private ClassLoader _loader;
140: private long _time;
141: private boolean _isDead;
142:
143: Job(TimerTask task) {
144: _task = task;
145:
146: _loader = Thread.currentThread().getContextClassLoader();
147:
148: Environment.addClassLoaderListener(this );
149: }
150:
151: public TimerTask getTask() {
152: return _task;
153: }
154:
155: /**
156: * Sets the time the job should execute.
157: */
158: public void setTime(long time) {
159: _time = time;
160: }
161:
162: /**
163: * Gets the time the job should execute.
164: */
165: public long getTime() {
166: return _time;
167: }
168:
169: /**
170: * Handles the case where a class loader has completed initialization
171: */
172: public void classLoaderInit(DynamicClassLoader loader) {
173: }
174:
175: /**
176: * Handles the case where a class loader is dropped.
177: */
178: public void classLoaderDestroy(DynamicClassLoader loader) {
179: _isDead = true;
180: JobThread.dequeue(_task);
181: }
182:
183: public void run() {
184: if (!_isDead) {
185: Thread thread = Thread.currentThread();
186: ClassLoader oldLoader = thread.getContextClassLoader();
187:
188: try {
189: thread.setContextClassLoader(_loader);
190: _task.run();
191: } finally {
192: thread.setContextClassLoader(oldLoader);
193: }
194: }
195: }
196: }
197:
198: static {
199: try {
200: Thread thread = new Thread(_job, "jmx-job-thread");
201: thread.setDaemon(true);
202: thread.start();
203: } catch (Throwable e) {
204: log.log(Level.WARNING, e.toString(), e);
205: }
206: }
207: }
|