KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > ejb > containers > RuntimeTimerState


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.ejb.containers;
24
25 import java.util.logging.Logger JavaDoc;
26 import java.util.logging.Level JavaDoc;
27
28 import com.sun.logging.LogDomains;
29 import java.io.Serializable JavaDoc;
30 import java.util.Date JavaDoc;
31
32 import com.sun.enterprise.deployment.EjbDescriptor;
33 import com.sun.enterprise.deployment.Application;
34
35 /**
36  * RuntimeTimerState holds all runtime state of an EJB
37  * timer, including what fine-grained state it's in,
38  * stats about the number of expirations and failed
39  * deliveries that have occurred, and any existing JDK
40  * timer task that is currently scheduled for this timer.
41  * It also caches read-only state of a timer to improve
42  * performance.
43  *
44  * @author Kenneth Saks
45  */

46 class RuntimeTimerState {
47
48     private static Logger JavaDoc logger = LogDomains.getLogger(LogDomains.EJB_LOGGER);
49
50     //
51
// Fine-grained timer states
52
//
53

54     // Timer has been created but not committed.
55
private static final int CREATED = 0;
56
57     // There is a scheduled JDK timer task for this timer.
58
private static final int SCHEDULED = 1;
59
60     // The JDK timer task has expired and the timer's ejbTimeout method
61
// is being called.
62
private static final int BEING_DELIVERED = 2;
63
64     // The timer has been cancelled but the cancellation can still be
65
// rolled back.
66
private static final int CANCELLED = 3;
67     
68     private int state_;
69
70     //
71
// Immutable timer state.
72
//
73
private TimerPrimaryKey timerId_;
74
75     private Date JavaDoc initialExpiration_;
76     private long intervalDuration_;
77     private long containerId_;
78     private Object JavaDoc timedObjectPrimaryKey_;
79
80     //
81
private BaseContainer container_;
82
83     // Handle to scheduled timer task. This is only set when timer is SCHEDULED
84
private EJBTimerTask currentTask_;
85
86     //
87
// stats
88
//
89

90     private int numExpirations_;
91     private int numFailedDeliveries_;
92     
93     RuntimeTimerState(TimerPrimaryKey timerId,
94                       Date JavaDoc initialExpiration, long intervalDuration,
95                       BaseContainer container,
96                       Object JavaDoc timedObjectPkey) {
97
98         state_ = CREATED;
99         currentTask_ = null;
100
101         timerId_ = timerId;
102         initialExpiration_ = initialExpiration;
103         intervalDuration_ = intervalDuration;
104         timedObjectPrimaryKey_ = timedObjectPkey;
105         container_ = container;
106
107         containerId_ = container.getContainerId();
108
109         if( logger.isLoggable(Level.FINE) ) {
110             logger.log(Level.FINE, "RuntimeTimerState " + timerId_ +
111                        " created");
112         }
113     }
114
115     TimerPrimaryKey getTimerId() {
116         return timerId_;
117     }
118
119     Date JavaDoc getInitialExpiration() {
120         return initialExpiration_;
121     }
122
123     BaseContainer getContainer() {
124         return container_;
125     }
126
127     long getContainerId() {
128         return containerId_;
129     }
130
131     Object JavaDoc getTimedObjectPrimaryKey() {
132         return timedObjectPrimaryKey_;
133     }
134
135     long getIntervalDuration() {
136         return intervalDuration_;
137     }
138
139    
140     //
141
// Operations for performing state transitions.
142
//
143

144     void scheduled(EJBTimerTask timerTask) {
145         if( logger.isLoggable(Level.FINER) ) {
146             printStateTransition(state_, SCHEDULED);
147         }
148         currentTask_ = timerTask;
149         state_ = SCHEDULED;
150         numFailedDeliveries_ = 0;
151     }
152
153     void rescheduled(EJBTimerTask timerTask) {
154         if( logger.isLoggable(Level.FINER) ) {
155             printStateTransition(state_, SCHEDULED);
156         }
157         currentTask_ = timerTask;
158         state_ = SCHEDULED;
159         numFailedDeliveries_++;
160     }
161     
162     /**
163      * Transition from CANCELLED to DELIVERED when ejbTimeout calls
164      * cancel and then rolls back. Don't reset numFailedDeliveries.
165      */

166     void restoredToDelivered() {
167         if( logger.isLoggable(Level.FINER) ) {
168             printStateTransition(state_, BEING_DELIVERED);
169         }
170
171         currentTask_ = null;
172         state_ = BEING_DELIVERED;
173     }
174
175     void delivered() {
176         if( logger.isLoggable(Level.FINER) ) {
177             printStateTransition(state_, BEING_DELIVERED);
178         }
179
180         currentTask_ = null;
181
182         if( numFailedDeliveries_ == 0 ) {
183             numExpirations_++;
184         }
185         
186         state_ = BEING_DELIVERED;
187     }
188     
189     void cancelled() {
190         if( logger.isLoggable(Level.FINER) ) {
191             printStateTransition(state_, CANCELLED);
192         }
193
194         currentTask_ = null;
195         state_ = CANCELLED;
196     }
197
198     String JavaDoc stateToString() {
199         return stateToString(state_);
200     }
201
202
203     private String JavaDoc stateToString(int state) {
204         switch(state) {
205         case CREATED :
206             return "CREATED";
207         case SCHEDULED :
208             return "SCHEDULED";
209         case BEING_DELIVERED :
210             return "BEING_DELIVERED";
211         case CANCELLED :
212             return "CANCELLED";
213         }
214         return state + " NOT FOUND";
215     }
216
217     private void printStateTransition(int fromState, int toState) {
218         logger.log(Level.FINER, timerId_ + ": " + stateToString(fromState) +
219                    " to " + stateToString(toState));
220     }
221     
222     int getNumExpirations() {
223         return numExpirations_;
224     }
225
226     /**
227      * Number of failed deliveries since timer last transitioned to
228      * the SCHEDULED state.
229      */

230     int getNumFailedDeliveries() {
231         return numFailedDeliveries_;
232     }
233     
234     EJBTimerTask getCurrentTimerTask() {
235         return currentTask_;
236     }
237
238     String JavaDoc getTimedObjectEjbName() {
239         return container_.getEjbDescriptor().getName();
240     }
241
242     String JavaDoc getTimedObjectApplicationName() {
243         EjbDescriptor ejbDesc = container_.getEjbDescriptor();
244         Application app = ejbDesc.getApplication();
245         return (app != null) ? app.getRegistrationName() : "";
246     }
247
248     boolean timedObjectIsEntity() {
249         return (timedObjectPrimaryKey_ != null);
250     }
251
252     //
253
// State-testing accessors.
254
//
255

256     boolean isActive() {
257         return (state_ != CANCELLED);
258     }
259     
260     boolean isCancelled() {
261         return (state_ == CANCELLED);
262     }
263
264     boolean isCreated() {
265         return (state_ == CREATED);
266     }
267
268     boolean isBeingDelivered() {
269         return (state_ == BEING_DELIVERED);
270     }
271
272     boolean isScheduled() {
273         return (state_ == SCHEDULED);
274     }
275
276     boolean isRescheduled() {
277         return (isScheduled() && (numFailedDeliveries_ > 0));
278     }
279
280     Date JavaDoc getNextTimeout() {
281         if( !isScheduled() && !isRescheduled() ) {
282             throw new IllegalStateException JavaDoc();
283         }
284         return currentTask_.getTimeout();
285     }
286
287     long getTimeRemaining() {
288         Date JavaDoc timeout = getNextTimeout();
289         Date JavaDoc now = new Date JavaDoc();
290         return (timeout.getTime() - now.getTime());
291     }
292
293     /**
294      * @return true if interval timer and false otherwise
295      */

296     boolean isPeriodic() {
297         return (intervalDuration_ > 0);
298     }
299
300     //
301
// java.lang.Object methods.
302
//
303

304     public int hashCode() {
305         return timerId_.hashCode();
306     }
307
308     public boolean equals(Object JavaDoc other) {
309         boolean equal = false;
310         if( other instanceof RuntimeTimerState ) {
311             equal = timerId_.equals(((RuntimeTimerState) other).timerId_);
312         }
313         return equal;
314     }
315
316     public String JavaDoc toString() {
317         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
318         buffer.append("'" + getTimerId() + "' ");
319         buffer.append("'TimedObject = " + getTimedObjectEjbName() + "' ");
320         buffer.append("'Application = " + getTimedObjectApplicationName()
321                       + "' ");
322         buffer.append("'" + stateToString() + "' ");
323         buffer.append("'" + (isPeriodic() ? "PERIODIC' " : "SINGLE-ACTION' "));
324         buffer.append("'Container ID = " + containerId_ + "' ");
325         buffer.append("'" + getInitialExpiration() + "' ");
326         buffer.append("'" + getIntervalDuration() + "' ");
327         Object JavaDoc pk = getTimedObjectPrimaryKey();
328         if( pk != null ) {
329             buffer.append("'" + pk + "' ");
330         }
331         return buffer.toString();
332     }
333 }
334
Popular Tags