KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jmx > JobManager


1 /*
2  * Copyright (c) 1998-2002 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.jmx;
30
31 import java.lang.reflect.Method;
32 import java.lang.reflect.InvocationTargetException;
33
34 import java.util.TimerTask;
35
36 /**
37  * Abstract implementation for an MBeanServer factory.
38  */

39 public class JobManager {
40   private static Method _queueAbsolute;
41   private static Method _queueRelative;
42   private static Method _dequeue;
43   private static Method _getCurrentTime;
44   
45   private JobManager() {}
46
47   /**
48    * Returns the current time.
49    */

50   public static long getCurrentTime()
51   {
52     try {
53       Long value = (Long) _getCurrentTime.invoke(null, null);
54
55       if (value == null)
56     return 0;
57       else
58     return value.longValue();
59     } catch (IllegalAccessException e) {
60       throw new RuntimeException(e);
61     } catch (InvocationTargetException e) {
62       throw (RuntimeException) e.getCause();
63     }
64   }
65
66   /**
67    * Queues a job.
68    */

69   public static void queueAbsolute(TimerTask job, long time)
70   {
71     try {
72       _queueAbsolute.invoke(null, new Object[] { job, new Long(time) });
73     } catch (IllegalAccessException e) {
74       throw new RuntimeException(e);
75     } catch (InvocationTargetException e) {
76       throw (RuntimeException) e.getCause();
77     }
78   }
79
80   /**
81    * Queues a job.
82    */

83   public static void queueRelative(TimerTask job, long time)
84   {
85     try {
86       _queueRelative.invoke(null, new Object[] { job, new Long(time) });
87     } catch (IllegalAccessException e) {
88       throw new RuntimeException(e);
89     } catch (InvocationTargetException e) {
90       throw (RuntimeException) e.getCause();
91     }
92   }
93
94   /**
95    * Queues a job.
96    */

97   public static void dequeue(TimerTask job)
98   {
99     try {
100       _dequeue.invoke(null, new Object[] { job });
101     } catch (IllegalAccessException e) {
102       throw new RuntimeException(e);
103     } catch (InvocationTargetException e) {
104       throw (RuntimeException) e.getCause();
105     }
106   }
107
108   static {
109     try {
110       Class jmx = Class.forName("com.caucho.jmx.Jmx");
111
112       _queueAbsolute = jmx.getMethod("queueAbsolute",
113                      new Class[] { TimerTask.class,
114                            long.class });
115       _queueRelative = jmx.getMethod("queueRelative",
116                      new Class[] { TimerTask.class,
117                            long.class });
118       _dequeue = jmx.getMethod("dequeue", new Class[] { TimerTask.class });
119       
120       Class alarm = Class.forName("com.caucho.util.Alarm");
121       
122       _getCurrentTime = alarm.getMethod("getCurrentTime", new Class[] { });
123     } catch (Throwable e) {
124       e.printStackTrace();
125     }
126   }
127 }
128
Popular Tags