KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > util > threadpool > BlockingMode


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.util.threadpool;
23
24 import java.io.Serializable JavaDoc;
25 import java.io.ObjectStreamException JavaDoc;
26
27 /** A type-safe enum for the BasicThreadPool blocking mode.
28  *
29  * @author Scott.Stark@jboss.org
30  * @version $Revision: 1958 $
31  */

32 public class BlockingMode implements Serializable JavaDoc
33 {
34    /** @since 1.0 */
35    private static final long serialVersionUID = -9102277941374138830L;
36
37    public static final int RUN_TYPE = 0;
38    public static final int WAIT_TYPE = 1;
39    public static final int DISCARD_TYPE = 2;
40    public static final int DISCARD_OLDEST_TYPE = 3;
41    public static final int ABORT_TYPE = 4;
42
43    /** Set the policy for blocked execution to be that the current thread
44       executes the command if there are no available threads in the pool.
45     */

46    public static final BlockingMode RUN = new BlockingMode("run", RUN_TYPE);
47    /** Set the policy for blocked execution to be to wait until a thread
48     * is available, unless the pool has been shut down, in which case
49     * the action is discarded.
50     */

51    public static final BlockingMode WAIT = new BlockingMode("wait", WAIT_TYPE);
52    /** Set the policy for blocked execution to be to return without
53     * executing the request.
54     */

55    public static final BlockingMode DISCARD = new BlockingMode("discard", DISCARD_TYPE);
56    /** Set the policy for blocked execution to be to discard the oldest
57     * unhandled request
58     */

59    public static final BlockingMode DISCARD_OLDEST =
60       new BlockingMode("discardOldest", DISCARD_OLDEST_TYPE);
61    /** Set the policy for blocked execution to be to throw an AbortWhenBlocked
62     * (a subclass of RuntimeException).
63     */

64    public static final BlockingMode ABORT = new BlockingMode("abort", ABORT_TYPE);
65
66    /** The string form of the enum */
67    private final transient String JavaDoc name;
68    /** The enum manifest constant */
69    private final int type;
70
71    /** A utility method to convert a string name to a BlockingMode
72     * @param name
73     * @return The associated BlockingMode constant if name is valid, null otherwise
74     */

75    public static final BlockingMode toBlockingMode(String JavaDoc name)
76    {
77       BlockingMode mode = null;
78       if( name == null )
79       {
80          mode = null;
81       }
82       else if( name.equalsIgnoreCase("run") )
83       {
84          mode = RUN;
85       }
86       else if( name.equalsIgnoreCase("wait") )
87       {
88          mode = WAIT;
89       }
90       else if( name.equalsIgnoreCase("discard") )
91       {
92          mode = DISCARD;
93       }
94       else if( name.equalsIgnoreCase("discardOldest") )
95       {
96          mode = DISCARD_OLDEST;
97       }
98       else if( name.equalsIgnoreCase("abort") )
99       {
100          mode = ABORT;
101       }
102       return mode;
103    }
104
105    private BlockingMode(String JavaDoc name, int type)
106    {
107       this.name = name;
108       this.type = type;
109    }
110
111    public String JavaDoc toString()
112    {
113       return name;
114    }
115
116    /**
117     * Overriden to return the indentity instance of BlockingMode based on the
118     * stream type int value. This ensures that BlockingMode enums can be
119     * compared using ==.
120     *
121     * @return The BlockingMode instance for the XXX_TYPE int.
122     * @throws ObjectStreamException
123     */

124    Object JavaDoc readResolve() throws ObjectStreamException JavaDoc
125    {
126       // Replace the marshalled instance type with the local instance
127
BlockingMode mode = ABORT;
128       switch( type )
129       {
130          case RUN_TYPE:
131             mode = RUN;
132             break;
133          case WAIT_TYPE:
134             mode = RUN;
135             break;
136          case DISCARD_TYPE:
137             mode = RUN;
138             break;
139          case DISCARD_OLDEST_TYPE:
140             mode = RUN;
141             break;
142          case ABORT_TYPE:
143             mode = RUN;
144             break;
145       }
146       return mode;
147    }
148 }
149
Popular Tags