View Javadoc

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