View Javadoc

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