1 /*
2 * Copyright (C) The Apache Software Foundation. All rights reserved.
3 *
4 * This software is published under the terms of the Apache Software License
5 * version 1.1, a copy of which has been included with this distribution in
6 * the LICENSE file.
7 */
8 package org.apache.james.util.watchdog;
9
10 import org.apache.avalon.cornerstone.services.scheduler.PeriodicTimeTrigger;
11 import org.apache.avalon.cornerstone.services.scheduler.Target;
12 import org.apache.avalon.cornerstone.services.scheduler.TimeScheduler;
13
14 /***
15 * This class is a factory to produce Watchdogs, each of which is associated
16 * with a single TimeScheduler Target and a TimeScheduler object.
17 *
18 * This could be used in James by adding a server configuration
19 * parameter:
20 *
21 * schedulerWatchdogs = conf.getChild("useSchedulerWatchdogs").getValueAsBoolean(false);
22 *
23 * getting the TimeScheduler component:
24 *
25 * scheduler = (TimeScheduler) compMgr.lookup(TimeScheduler.ROLE);
26 *
27 * and changing AbstractJamesService.getWatchdogFactory to look
28 * something like:
29 *
30 * protected WatchdogFactory getWatchdogFactory() {
31 * WatchdogFactory theWatchdogFactory = null;
32 * if (schedulerWatchdogs) {
33 * theWatchdogFactory = new SchedulerWatchdogFactory(scheduler, timeout);
34 * } else {
35 * theWatchdogFactory = new ThreadPerWatchdogFactory(threadPool, timeout);
36 * }
37 * if (theWatchdogFactory instanceof LogEnabled) {
38 * ((LogEnabled)theWatchdogFactory).enableLogging(getLogger());
39 * }
40 * return theWatchdogFactory;
41 * }
42 *
43 * @author Peter M. Goldstein <farsight@alum.mit.edu>
44 */
45 public class SchedulerWatchdogFactory implements WatchdogFactory {
46
47 /***
48 * The thread pool used to generate InaccurateTimeoutWatchdogs
49 */
50 private TimeScheduler myTimeScheduler;
51
52 private long timeout = -1;
53
54 /***
55 * Creates the factory and sets the TimeScheduler used to implement
56 * the watchdogs.
57 *
58 * @param theTimeScheduler the scheduler that manages Watchdog triggering
59 * for Watchdogs produced by this factory
60 * @param timeout the timeout for Watchdogs produced by this factory
61 */
62 public SchedulerWatchdogFactory(TimeScheduler theTimeScheduler, long timeout) {
63 this.timeout = timeout;
64 myTimeScheduler = theTimeScheduler;
65 }
66
67 /***
68 * @see org.apache.james.util.watchdog.WatchdogFactory#getWatchdog(WatchdogTarget)
69 */
70 public Watchdog getWatchdog(WatchdogTarget theTarget) {
71 return new SchedulerWatchdog(theTarget);
72 }
73
74 /***
75 * An inner class that acts as an adaptor between the Watchdog
76 * interface and the TimeScheduler interface.
77 */
78 private class SchedulerWatchdog implements Watchdog {
79
80 /***
81 * The in-scheduler identifier for this trigger.
82 */
83 private String triggerID = null;
84
85 /***
86 * The WatchdogTarget that is passed in when this
87 * SchedulerWatchdog is initialized
88 */
89 private WatchdogTarget theWatchdogTarget;
90
91 /***
92 * Constructor for the SchedulerWatchdog
93 *
94 * @param theTarget the target triggered by this Watchdog
95 */
96 SchedulerWatchdog(WatchdogTarget theTarget) {
97 // TODO: This should be made more robust then just
98 // using toString()
99 triggerID = this.toString();
100 theWatchdogTarget = theTarget;
101 }
102
103 /***
104 * Start this Watchdog, causing it to begin monitoring. The Watchdog can
105 * be stopped and restarted.
106 */
107 public void start() {
108 PeriodicTimeTrigger theTrigger = new PeriodicTimeTrigger((int)SchedulerWatchdogFactory.this.timeout, -1);
109 Target theTarget = new Target() {
110 public void targetTriggered(String targetID) {
111 theWatchdogTarget.execute();
112 }
113 };
114 SchedulerWatchdogFactory.this.myTimeScheduler.addTrigger(triggerID, theTrigger, theTarget);
115 }
116
117 /***
118 * Reset this Watchdog. Resets any conditions in the implementations
119 * (time to expiration, etc.) to their original values
120 */
121 public void reset() {
122 SchedulerWatchdogFactory.this.myTimeScheduler.resetTrigger(triggerID);
123 }
124
125 /***
126 * Stop this Watchdog, terminating the monitoring condition. The monitor
127 * can be restarted with a call to startWatchdog.
128 */
129 public void stop() {
130 SchedulerWatchdogFactory.this.myTimeScheduler.removeTrigger(triggerID);
131 }
132 }
133
134 }
This page was automatically generated by Maven