1 /*****************************************************************
2 * Licensed to the Apache Software Foundation (ASF) under one *
3 * or more contributor license agreements. See the NOTICE file *
4 * distributed with this work for additional information *
5 * regarding copyright ownership. The ASF licenses this file *
6 * to you under the Apache License, Version 2.0 (the *
7 * "License"); you may not use this file except in compliance *
8 * with the License. You may obtain a copy of the License at *
9 * *
10 * http://www.apache.org/licenses/LICENSE-2.0 *
11 * *
12 * Unless required by applicable law or agreed to in writing, *
13 * software distributed under the License is distributed on an *
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15 * KIND, either express or implied. See the License for the *
16 * specific language governing permissions and limitations *
17 * under the License. *
18 ****************************************************************/
19
20 package org.apache.james.util.watchdog;
21
22 import org.apache.avalon.cornerstone.services.scheduler.PeriodicTimeTrigger;
23 import org.apache.avalon.cornerstone.services.scheduler.Target;
24 import org.apache.avalon.cornerstone.services.scheduler.TimeScheduler;
25
26 /***
27 * This class is a factory to produce Watchdogs, each of which is associated
28 * with a single TimeScheduler Target and a TimeScheduler object.
29 *
30 * This could be used in James by adding a server configuration
31 * parameter:
32 *
33 * schedulerWatchdogs = conf.getChild("useSchedulerWatchdogs").getValueAsBoolean(false);
34 *
35 * getting the TimeScheduler component:
36 *
37 * scheduler = (TimeScheduler) compMgr.lookup(TimeScheduler.ROLE);
38 *
39 * and changing AbstractJamesService.getWatchdogFactory to look
40 * something like:
41 *
42 * protected WatchdogFactory getWatchdogFactory() {
43 * WatchdogFactory theWatchdogFactory = null;
44 * if (schedulerWatchdogs) {
45 * theWatchdogFactory = new SchedulerWatchdogFactory(scheduler, timeout);
46 * } else {
47 * theWatchdogFactory = new ThreadPerWatchdogFactory(threadPool, timeout);
48 * }
49 * if (theWatchdogFactory instanceof LogEnabled) {
50 * ((LogEnabled)theWatchdogFactory).enableLogging(getLogger());
51 * }
52 * return theWatchdogFactory;
53 * }
54 *
55 */
56 public class SchedulerWatchdogFactory implements WatchdogFactory {
57
58 /***
59 * The thread pool used to generate InaccurateTimeoutWatchdogs
60 */
61 private TimeScheduler myTimeScheduler;
62
63 private long timeout = -1;
64
65 /***
66 * Creates the factory and sets the TimeScheduler used to implement
67 * the watchdogs.
68 *
69 * @param theTimeScheduler the scheduler that manages Watchdog triggering
70 * for Watchdogs produced by this factory
71 * @param timeout the timeout for Watchdogs produced by this factory
72 */
73 public SchedulerWatchdogFactory(TimeScheduler theTimeScheduler, long timeout) {
74 this.timeout = timeout;
75 myTimeScheduler = theTimeScheduler;
76 }
77
78 /***
79 * @see org.apache.james.util.watchdog.WatchdogFactory#getWatchdog(WatchdogTarget)
80 */
81 public Watchdog getWatchdog(WatchdogTarget theTarget) {
82 return new SchedulerWatchdog(theTarget);
83 }
84
85 /***
86 * An inner class that acts as an adaptor between the Watchdog
87 * interface and the TimeScheduler interface.
88 */
89 private class SchedulerWatchdog implements Watchdog {
90
91 /***
92 * The in-scheduler identifier for this trigger.
93 */
94 private String triggerID = null;
95
96 /***
97 * The WatchdogTarget that is passed in when this
98 * SchedulerWatchdog is initialized
99 */
100 private WatchdogTarget theWatchdogTarget;
101
102 /***
103 * Constructor for the SchedulerWatchdog
104 *
105 * @param theTarget the target triggered by this Watchdog
106 */
107 SchedulerWatchdog(WatchdogTarget theTarget) {
108
109
110 triggerID = this.toString();
111 theWatchdogTarget = theTarget;
112 }
113
114 /***
115 * Start this Watchdog, causing it to begin monitoring. The Watchdog can
116 * be stopped and restarted.
117 */
118 public void start() {
119 PeriodicTimeTrigger theTrigger = new PeriodicTimeTrigger((int)SchedulerWatchdogFactory.this.timeout, -1);
120 Target theTarget = new Target() {
121 public void targetTriggered(String targetID) {
122 theWatchdogTarget.execute();
123 }
124 };
125 SchedulerWatchdogFactory.this.myTimeScheduler.addTrigger(triggerID, theTrigger, theTarget);
126 }
127
128 /***
129 * Reset this Watchdog. Resets any conditions in the implementations
130 * (time to expiration, etc.) to their original values
131 */
132 public void reset() {
133 SchedulerWatchdogFactory.this.myTimeScheduler.resetTrigger(triggerID);
134 }
135
136 /***
137 * Stop this Watchdog, terminating the monitoring condition. The monitor
138 * can be restarted with a call to startWatchdog.
139 */
140 public void stop() {
141 SchedulerWatchdogFactory.this.myTimeScheduler.removeTrigger(triggerID);
142 }
143 }
144
145 }