View Javadoc
1   /**
2    * Copyright 2014 Internet2
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  /*
17   * $Header: /home/hagleyj/i2mi/grouper-misc/grouperClient/src/ext/edu/internet2/middleware/grouperClientExt/org/apache/commons/httpclient/util/TimeoutController.java,v 1.1 2008-11-30 10:57:27 mchyzer Exp $
18   * $Revision: 1.1 $
19   * $Date: 2008-11-30 10:57:27 $
20   *
21   * ====================================================================
22   *
23   *  Licensed to the Apache Software Foundation (ASF) under one or more
24   *  contributor license agreements.  See the NOTICE file distributed with
25   *  this work for additional information regarding copyright ownership.
26   *  The ASF licenses this file to You under the Apache License, Version 2.0
27   *  (the "License"); you may not use this file except in compliance with
28   *  the License.  You may obtain a copy of the License at
29   *
30   *      http://www.apache.org/licenses/LICENSE-2.0
31   *
32   *  Unless required by applicable law or agreed to in writing, software
33   *  distributed under the License is distributed on an "AS IS" BASIS,
34   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
35   *  See the License for the specific language governing permissions and
36   *  limitations under the License.
37   * ====================================================================
38   *
39   * This software consists of voluntary contributions made by many
40   * individuals on behalf of the Apache Software Foundation.  For more
41   * information on the Apache Software Foundation, please see
42   * <http://www.apache.org/>.
43   *
44   */
45  
46  package edu.internet2.middleware.grouperClientExt.org.apache.commons.httpclient.util;
47  
48  /**
49   * <p>
50   * Executes a task with a specified timeout.
51   * </p>
52   * @author Ortwin Glueck
53   * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
54   * @version $Revision: 1.1 $
55   * @since 2.0
56   */
57  public final class TimeoutController {
58  
59      /**
60       * Do not instantiate objects of this class. Methods are static.
61       */
62      private TimeoutController() {
63      }
64  
65      /**
66       * Executes <code>task</code>. Waits for <code>timeout</code>
67       * milliseconds for the task to end and returns. If the task does not return
68       * in time, the thread is interrupted and an Exception is thrown.
69       * The caller should override the Thread.interrupt() method to something that
70       * quickly makes the thread die or use Thread.isInterrupted().
71       * @param task The thread to execute
72       * @param timeout The timeout in milliseconds. 0 means to wait forever.
73       * @throws TimeoutException if the timeout passes and the thread does not return.
74       */
75      public static void execute(Thread task, long timeout) throws TimeoutException {
76          task.start();
77          try {
78              task.join(timeout);
79          } catch (InterruptedException e) {
80              /* if somebody interrupts us he knows what he is doing */
81          }
82          if (task.isAlive()) {
83              task.interrupt();
84              throw new TimeoutException();
85          }
86      }
87  
88      /**
89       * Executes <code>task</code> in a new deamon Thread and waits for the timeout.
90       * @param task The task to execute
91       * @param timeout The timeout in milliseconds. 0 means to wait forever.
92       * @throws TimeoutException if the timeout passes and the thread does not return.
93       */
94      public static void execute(Runnable task, long timeout) throws TimeoutException {
95          Thread t = new Thread(task, "Timeout guard");
96          t.setDaemon(true);
97          execute(t, timeout);
98      }
99  
100     /**
101      * Signals that the task timed out.
102      */
103     public static class TimeoutException extends Exception {
104         /** Create an instance */
105         public TimeoutException() {
106         }
107     }
108 }