View Javadoc

1   /*
2    * Copyright 2002,2004 The Apache Software Foundation.
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  package org.apache.commons.jelly.tags.threads;
18  
19  /***
20   * Represents the status of {@link JellyThread}.
21   *
22   * @author <a href="mailto:jason@jhorman.org">Jason Horman</a>
23   */
24  
25  public class RunnableStatus {
26      public static final int NONE = 0;
27      public static final int SUCCESS = 1;
28      public static final int FAILURE = 2;
29      public static final int AVOIDED = 3;
30      public static final int TIMED_OUT = 4;
31      public static final int KILLED = 5;
32  
33      private int status = NONE;
34  
35      /*** On a status change to FAILURE an exception can be set */
36      private Exception exception = null;
37  
38      public RunnableStatus() {
39  
40      }
41  
42      public RunnableStatus(int status) {
43          set(status);
44      }
45  
46      public synchronized void set(int status) {
47          set(status, null);
48      }
49  
50      public synchronized void set(int status, Exception e) {
51          // this check is important since I may call setStatus(BLAH) again
52          // to trigger the callback
53          if (this.status != status) {
54              this.status = status;
55  
56              // store the exception if one was set
57              if (e != null)
58                  this.exception = e;
59          }
60      }
61  
62      public synchronized int get() {
63          return status;
64      }
65  
66      public synchronized boolean isSuccess() {
67          return (status == SUCCESS);
68      }
69  
70      public synchronized boolean isFailure() {
71          return (status == FAILURE);
72      }
73  
74      public synchronized boolean isAvoided() {
75          return (status == AVOIDED);
76      }
77  
78      public synchronized boolean isTimedOut() {
79          return (status == TIMED_OUT);
80      }
81  
82      public synchronized boolean isKilled() {
83          return (status == KILLED);
84      }
85  
86      public synchronized Exception getException() {
87          return exception;
88      }
89  
90      public synchronized boolean equals(RunnableStatus status) {
91          return status.get() == this.status;
92      }
93  
94      public synchronized boolean equals(int status) {
95          return this.status == status;
96      }
97  
98      /***
99       * Used to get the status code from a string representation. Mainly used for
100      * xml parsing.
101      * @param status The status string rep.
102      * @return The status enum value
103      */
104     public static int getStatusCode(String status) {
105         if (status.equalsIgnoreCase("SUCCESS")) {
106             return SUCCESS;
107         } else if (status.equalsIgnoreCase("FAILURE")) {
108             return FAILURE;
109         } else if (status.equalsIgnoreCase("TIMED_OUT")) {
110             return TIMED_OUT;
111         } else if (status.equalsIgnoreCase("AVOIDED")) {
112             return AVOIDED;
113         } else if (status.equalsIgnoreCase("KILLED")) {
114             return KILLED;
115         } else {
116             throw new IllegalArgumentException(status + " is invalid status");
117         }
118     }
119 
120     /***
121      * The reverse of getStatusCode
122      */
123     public static String getStatusString(int status) {
124         if (status == SUCCESS) {
125             return "SUCCESS";
126         } else if (status == FAILURE) {
127             return "FAILURE";
128         } else if (status == TIMED_OUT) {
129             return "TIMED_OUT";
130         } else if (status == AVOIDED) {
131             return "AVOIDED";
132         } else if (status == KILLED) {
133             return "KILLED";
134         } else {
135             throw new IllegalArgumentException(status + " is invalid status");
136         }
137     }
138 
139     public static boolean isValidStatus(int status) {
140         if (status == SUCCESS) {
141             return true;
142         } else if (status == FAILURE) {
143             return true;
144         } else if (status == TIMED_OUT) {
145             return true;
146         } else if (status == AVOIDED) {
147             return true;
148         } else if (status == KILLED) {
149             return true;
150         } else {
151             return false;
152         }
153     }
154 
155     public String toString() {
156         return getStatusString(status);
157     }
158 }