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
21 package org.apache.james.util.watchdog;
22
23 import org.apache.avalon.framework.container.ContainerUtil;
24 import org.apache.avalon.framework.logger.AbstractLogEnabled;
25 import org.apache.excalibur.thread.ThreadPool;
26
27 /**
28 * This class is a factory to produce Watchdogs, each of which is associated
29 * with a single thread.
30 *
31 */
32 public class ThreadPerWatchdogFactory
33 extends AbstractLogEnabled
34 implements WatchdogFactory {
35
36 /**
37 * The thread pool used to generate InaccurateTimeoutWatchdogs
38 */
39 private ThreadPool myThreadPool;
40
41 /**
42 * The watchdog timeout for Watchdogs generated by this factory
43 */
44 private long timeout;
45
46 /**
47 * Creates the factory and sets the thread pool used to generate
48 * InaccurateTimeoutWatchdogs.
49 */
50 public ThreadPerWatchdogFactory(ThreadPool theThreadPool, long timeout) {
51 if (theThreadPool == null) {
52 throw new IllegalArgumentException("The thread pool for the ThreadPerWatchdogFactory cannot be null.");
53 }
54 myThreadPool = theThreadPool;
55 this.timeout = timeout;
56 }
57
58 /**
59 * @see org.apache.james.util.watchdog.WatchdogFactory#getWatchdog(WatchdogTarget)
60 */
61 public Watchdog getWatchdog(WatchdogTarget theTarget)
62 throws Exception {
63 InaccurateTimeoutWatchdog watchdog = new InaccurateTimeoutWatchdog(timeout, theTarget, myThreadPool);
64 ContainerUtil.enableLogging(watchdog, getLogger());
65 return watchdog;
66 }
67 }