KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > util > watchdog > SchedulerWatchdogFactory


1 /***********************************************************************
2  * Copyright (c) 2000-2004 The Apache Software Foundation. *
3  * All rights reserved. *
4  * ------------------------------------------------------------------- *
5  * Licensed under the Apache License, Version 2.0 (the "License"); you *
6  * may not use this file except in compliance with the License. You *
7  * may obtain a copy of the License at: *
8  * *
9  * http://www.apache.org/licenses/LICENSE-2.0 *
10  * *
11  * Unless required by applicable law or agreed to in writing, software *
12  * distributed under the License is distributed on an "AS IS" BASIS, *
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or *
14  * implied. See the License for the specific language governing *
15  * permissions and limitations under the License. *
16  ***********************************************************************/

17
18 package org.apache.james.util.watchdog;
19
20 import org.apache.avalon.cornerstone.services.scheduler.PeriodicTimeTrigger;
21 import org.apache.avalon.cornerstone.services.scheduler.Target;
22 import org.apache.avalon.cornerstone.services.scheduler.TimeScheduler;
23
24 /**
25  * This class is a factory to produce Watchdogs, each of which is associated
26  * with a single TimeScheduler Target and a TimeScheduler object.
27  *
28  * This could be used in James by adding a server configuration
29  * parameter:
30  *
31  * schedulerWatchdogs = conf.getChild("useSchedulerWatchdogs").getValueAsBoolean(false);
32  *
33  * getting the TimeScheduler component:
34  *
35  * scheduler = (TimeScheduler) compMgr.lookup(TimeScheduler.ROLE);
36  *
37  * and changing AbstractJamesService.getWatchdogFactory to look
38  * something like:
39  *
40  * protected WatchdogFactory getWatchdogFactory() {
41  * WatchdogFactory theWatchdogFactory = null;
42  * if (schedulerWatchdogs) {
43  * theWatchdogFactory = new SchedulerWatchdogFactory(scheduler, timeout);
44  * } else {
45  * theWatchdogFactory = new ThreadPerWatchdogFactory(threadPool, timeout);
46  * }
47  * if (theWatchdogFactory instanceof LogEnabled) {
48  * ((LogEnabled)theWatchdogFactory).enableLogging(getLogger());
49  * }
50  * return theWatchdogFactory;
51  * }
52  *
53  */

54 public class SchedulerWatchdogFactory implements WatchdogFactory {
55
56     /**
57      * The thread pool used to generate InaccurateTimeoutWatchdogs
58      */

59     private TimeScheduler myTimeScheduler;
60
61     private long timeout = -1;
62
63     /**
64      * Creates the factory and sets the TimeScheduler used to implement
65      * the watchdogs.
66      *
67      * @param theTimeScheduler the scheduler that manages Watchdog triggering
68      * for Watchdogs produced by this factory
69      * @param timeout the timeout for Watchdogs produced by this factory
70      */

71     public SchedulerWatchdogFactory(TimeScheduler theTimeScheduler, long timeout) {
72         this.timeout = timeout;
73         myTimeScheduler = theTimeScheduler;
74     }
75
76     /**
77      * @see org.apache.james.util.watchdog.WatchdogFactory#getWatchdog(WatchdogTarget)
78      */

79     public Watchdog getWatchdog(WatchdogTarget theTarget) {
80         return new SchedulerWatchdog(theTarget);
81     }
82
83     /**
84      * An inner class that acts as an adaptor between the Watchdog
85      * interface and the TimeScheduler interface.
86      */

87     private class SchedulerWatchdog implements Watchdog {
88
89         /**
90          * The in-scheduler identifier for this trigger.
91          */

92         private String JavaDoc triggerID = null;
93
94         /**
95          * The WatchdogTarget that is passed in when this
96          * SchedulerWatchdog is initialized
97          */

98         private WatchdogTarget theWatchdogTarget;
99
100         /**
101          * Constructor for the SchedulerWatchdog
102          *
103          * @param theTarget the target triggered by this Watchdog
104          */

105         SchedulerWatchdog(WatchdogTarget theTarget) {
106             // TODO: This should be made more robust then just
107
// using toString()
108
triggerID = this.toString();
109             theWatchdogTarget = theTarget;
110         }
111
112         /**
113          * Start this Watchdog, causing it to begin monitoring. The Watchdog can
114          * be stopped and restarted.
115          */

116         public void start() {
117             PeriodicTimeTrigger theTrigger = new PeriodicTimeTrigger((int)SchedulerWatchdogFactory.this.timeout, -1);
118             Target theTarget = new Target() {
119                                     public void targetTriggered(String JavaDoc targetID) {
120                                         theWatchdogTarget.execute();
121                                     }
122                                };
123             SchedulerWatchdogFactory.this.myTimeScheduler.addTrigger(triggerID, theTrigger, theTarget);
124         }
125
126         /**
127          * Reset this Watchdog. Resets any conditions in the implementations
128          * (time to expiration, etc.) to their original values
129          */

130         public void reset() {
131             SchedulerWatchdogFactory.this.myTimeScheduler.resetTrigger(triggerID);
132         }
133
134         /**
135          * Stop this Watchdog, terminating the monitoring condition. The monitor
136          * can be restarted with a call to startWatchdog.
137          */

138         public void stop() {
139             SchedulerWatchdogFactory.this.myTimeScheduler.removeTrigger(triggerID);
140         }
141     }
142
143 }
144
Popular Tags