KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > common > concurrent > Valve


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9 package org.jboss.portal.common.concurrent;
10
11 /**
12  * Start in closed mode and adds an open() method to keep the same valve.
13  *
14  * @author <a HREF="mailto:adrian@jboss.org">Adrian Brock</a>
15  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
16  * @version $Revision: 1.1.1.1 $
17  */

18 public class Valve
19 {
20
21    // Constants -----------------------------------------------------
22

23    /**
24     * The valve is open.
25     */

26    public static final int OPEN = 0;
27
28    /**
29     * The valve is in holding state.
30     */

31    public static final int CLOSING = 1;
32
33    /**
34     * The valve is in hold state.
35     */

36    public static final int CLOSED = 2;
37
38    /**
39     * User friendly names.
40     */

41    private static final String JavaDoc[] STATE_NAMES = {"OPEN","CLOSING","CLOSED"};
42
43    // Attributes ----------------------------------------------------
44

45    /**
46     * The state lock.
47     */

48    protected Object JavaDoc stateLock = new Object JavaDoc();
49
50    /**
51     * The state.
52     */

53    protected int state;
54
55    /**
56     * The invocation count.
57     */

58    protected int invocations = 0;
59
60    // Static --------------------------------------------------------
61

62    // Constructors --------------------------------------------------
63

64    /**
65     * Create a valve in the closed state
66     */

67    public Valve()
68    {
69       this(CLOSED);
70    }
71
72    /**
73     * Create a valve with the give initial state
74     *
75     * @param state the initial state
76     */

77    protected Valve(int state)
78    {
79       this.state = state;
80    }
81
82    // Public --------------------------------------------------------
83

84    /**
85     * Are we closed?
86     *
87     * @return true when closing or closed, false otherwise
88     */

89    public boolean isClosed()
90    {
91       synchronized (stateLock)
92       {
93          return state != OPEN;
94       }
95    }
96
97    /**
98     * Invoked before an invocation
99     *
100     * @return true if allowed entry, false otherwise
101     */

102    public boolean beforeInvocation()
103    {
104       synchronized (stateLock)
105       {
106          if (state != OPEN)
107          {
108             return false;
109          }
110          ++invocations;
111       }
112       return true;
113    }
114
115    /**
116     * Invoked after an invocation
117     */

118    public void afterInvocation()
119    {
120       synchronized (stateLock)
121       {
122          --invocations;
123          stateLock.notifyAll();
124       }
125    }
126
127    /**
128     * Return the state.
129     */

130    public int getState()
131    {
132       return state;
133    }
134
135    /**
136     * How many invocations are held in the valve.
137     */

138    public int getInvocations()
139    {
140       return invocations;
141    }
142
143    /**
144     * Open the valve.
145     *
146     * @throws IllegalStateException if the valve is not closed
147     */

148    public void open() throws IllegalStateException JavaDoc
149    {
150       synchronized (stateLock)
151       {
152          if (state != CLOSED)
153          {
154             throw new IllegalStateException JavaDoc("Cannot invoke open() valve in state " + STATE_NAMES[state]);
155          }
156          state = OPEN;
157       }
158    }
159
160    /**
161     * Invoked before closing.
162     *
163     * @throws IllegalStateException if the valve is closed
164     */

165    public void closing() throws IllegalStateException JavaDoc
166    {
167       closing(0);
168    }
169
170    /**
171     * Invoked before closing.
172     *
173     * @throws IllegalStateException if the valve is closed
174     */

175    public boolean closing(long millis) throws IllegalStateException JavaDoc
176    {
177       boolean interrupted = false;
178       boolean empty = false;
179       synchronized (stateLock)
180       {
181          if (state == CLOSED)
182          {
183             throw new IllegalStateException JavaDoc("Cannot invoke closing() valve in state " + STATE_NAMES[state]);
184          }
185
186          //
187
state = CLOSING;
188
189          //
190
long finished = -1;
191          if (millis > 0)
192          {
193             finished = System.currentTimeMillis() + millis;
194          }
195
196          while (invocations > 0)
197          {
198             try
199             {
200                if (finished == -1)
201                {
202                   stateLock.wait();
203                }
204                else
205                {
206                   long time = finished - System.currentTimeMillis();
207                   if (time > 0)
208                   {
209                      stateLock.wait(time);
210                   }
211                   else
212                   {
213                      break;
214                   }
215                }
216             }
217             catch (InterruptedException JavaDoc e)
218             {
219                interrupted = true;
220             }
221          }
222
223          empty = invocations == 0;
224       }
225
226       if (interrupted)
227       {
228          Thread.currentThread().interrupt();
229       }
230
231       return empty;
232    }
233
234    /**
235     * Invoked after closing.
236     *
237     * @throws IllegalStateException if the valve is not closing
238     */

239    public void closed() throws IllegalStateException JavaDoc
240    {
241       synchronized (stateLock)
242       {
243          if (state != CLOSING)
244          {
245             throw new IllegalStateException JavaDoc("Cannot invoke close() valve in state " + STATE_NAMES[state]);
246          }
247          state = CLOSED;
248       }
249    }
250
251    // Protected -----------------------------------------------------
252

253    // Package Private -----------------------------------------------
254

255    // Private -------------------------------------------------------
256

257    // Inner classes -------------------------------------------------
258

259 }
260
Popular Tags