KickJava   Java API By Example, From Geeks To Geeks.

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


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.Date JavaDoc;
26 import java.io.Serializable JavaDoc;
27
28 import javax.ejb.EJBLocalObject JavaDoc;
29 import javax.ejb.NoSuchObjectLocalException JavaDoc;
30 import javax.ejb.EJBException JavaDoc;
31 import javax.ejb.Timer JavaDoc;
32 import javax.ejb.TimerHandle JavaDoc;
33 import javax.ejb.EJBException JavaDoc;
34 import javax.ejb.FinderException JavaDoc;
35
36
37 import com.sun.enterprise.Switch;
38 import com.sun.enterprise.InvocationManager;
39 import com.sun.enterprise.InvocationException;
40 import com.sun.enterprise.ComponentInvocation;
41
42 import com.sun.ejb.Invocation;
43 import com.sun.ejb.ComponentContext;
44
45 import java.io.IOException JavaDoc;
46
47 import com.sun.ejb.spi.io.IndirectlySerializable;
48 import com.sun.ejb.spi.io.SerializableObjectFactory;
49
50 /*
51  * TimerWrapper is the application-level representation
52  * of a timer. Note that this class is not Serializable.
53  * Timer instances are not intended to be directly persisted
54  * by the application. TimerHandle should be used instead.
55  *
56  * @author Kenneth Saks
57  */

58 public class TimerWrapper
59     implements Timer JavaDoc, IndirectlySerializable
60 {
61     
62     private TimerPrimaryKey timerId_;
63     private EJBTimerService timerService_;
64
65     TimerWrapper(TimerPrimaryKey timerId, EJBTimerService timerService) {
66         timerId_ = timerId;
67         timerService_ = timerService; //TimerService passed in could be null
68
}
69
70     /*
71      * Implementations of javax.ejb.Timer methods
72      */

73     public void cancel()
74         throws IllegalStateException JavaDoc, NoSuchObjectLocalException JavaDoc, EJBException JavaDoc {
75
76         checkCallPermission();
77
78         try {
79             timerService_.cancelTimer(timerId_);
80         } catch(FinderException JavaDoc fe) {
81             throw new NoSuchObjectLocalException JavaDoc("timer no longer exists", fe);
82         } catch(Exception JavaDoc e) {
83             throw new EJBException JavaDoc(e);
84         }
85
86     }
87     
88     public long getTimeRemaining()
89         throws IllegalStateException JavaDoc, NoSuchObjectLocalException JavaDoc {
90
91         Date JavaDoc nextTimeout = getNextTimeout();
92
93         Date JavaDoc now = new Date JavaDoc();
94
95         long timeRemaining = nextTimeout.getTime() - now.getTime();
96
97         return (timeRemaining > 0) ? timeRemaining : 0;
98     }
99     
100     public Date JavaDoc getNextTimeout()
101         throws IllegalStateException JavaDoc, NoSuchObjectLocalException JavaDoc {
102
103         checkCallPermission();
104
105         Date JavaDoc nextTimeout;
106
107         try {
108             nextTimeout = timerService_.getNextTimeout(timerId_);
109         } catch(FinderException JavaDoc fe) {
110             throw new NoSuchObjectLocalException JavaDoc("timer no longer exists", fe);
111         }
112
113         return nextTimeout;
114     }
115     
116     public Serializable JavaDoc getInfo()
117         throws IllegalStateException JavaDoc, NoSuchObjectLocalException JavaDoc {
118
119         checkCallPermission();
120
121         Serializable JavaDoc info;
122
123         try {
124             info = timerService_.getInfo(timerId_);
125         } catch(FinderException JavaDoc fe) {
126             throw new NoSuchObjectLocalException JavaDoc("timer no longer exists", fe);
127         }
128
129         return info;
130     }
131     
132     public TimerHandle JavaDoc getHandle()
133         throws IllegalStateException JavaDoc, NoSuchObjectLocalException JavaDoc {
134
135         checkCallPermission();
136
137         if( !timerService_.timerExists(timerId_) ) {
138             throw new NoSuchObjectLocalException JavaDoc("timer no longer exists");
139         }
140
141         return new TimerHandleImpl(timerId_);
142     }
143
144     public boolean equals(Object JavaDoc o) {
145         boolean equal = false;
146         if(o instanceof TimerWrapper) {
147             TimerWrapper other = (TimerWrapper) o;
148             equal = other.timerId_.equals(this.timerId_);
149         }
150         return equal;
151     }
152
153     public int hashCode() {
154         return timerId_.hashCode();
155     }
156
157     public String JavaDoc toString() {
158         return "Timer " + timerId_;
159     }
160
161     /**
162      * Verify that Timer method access is allowed from this context.
163      * This method is static so that TimerHandle can call it even
164      * before it has created a TimerWrapper instance.
165      */

166     private static void checkCallPermission() throws IllegalStateException JavaDoc {
167
168         boolean allowed = false;
169
170         ContainerFactoryImpl cf = (ContainerFactoryImpl)
171             Switch.getSwitch().getContainerFactory();
172         EJBTimerService timerService = cf.getEJBTimerService();
173         if( timerService == null ) {
174             throw new IllegalStateException JavaDoc
175                 ("EJBTimerService is not available");
176         }
177
178         try {
179             InvocationManager invManager =
180                 Switch.getSwitch().getInvocationManager();
181
182             ComponentInvocation inv = invManager.getCurrentInvocation();
183             if (inv == null)
184                 throw new IllegalStateException JavaDoc
185                     ("Invocation cannot be null");
186
187             int invType = inv.getInvocationType();
188             if( invType == ComponentInvocation.EJB_INVOCATION ) {
189                 if ( inv instanceof Invocation ) {
190                     ComponentContext context = ((Invocation) inv).context;
191                     // Delegate check to EJB context. Let any
192
// IllegalStateException bubble up.
193
context.checkTimerServiceMethodAccess();
194                     allowed = true;
195                 } else {
196                     // NOTE : There shouldn't be any cases where an EJB
197
// container uses com.sun.enterprise.ComponentInvocation
198
// instead of com.sun.ejb.Invocation and timer method
199
// access is allowed. No EJBContextImpl is available
200
// to perform checks.
201
allowed = false;
202                 }
203             } else if( invType == ComponentInvocation.SERVLET_INVOCATION ) {
204                 throw new IllegalStateException JavaDoc
205                     ("Web tier access to EJB timers through local " +
206                      "interfaces is not portable");
207             }
208         } catch(InvocationException ie) {
209             IllegalStateException JavaDoc ise = new IllegalStateException JavaDoc
210                 ("Operation not allowed");
211             ise.initCause(ie);
212             throw ise;
213         }
214
215         if( !allowed ) {
216             throw new IllegalStateException JavaDoc("Operation not allowed");
217         }
218         
219     }
220
221     public SerializableObjectFactory getSerializableObjectFactory() {
222         return new SerializedTimerWrapper(timerId_);
223     }
224
225     /**
226      * Used by serialization code to serialize a TimerWrapper. We
227      * need a separate type that TimerHandle so that on deserialization
228      * we know it started as a TimerWrapper instead of a TimerHandle.
229      */

230     public static class SerializedTimerWrapper
231         implements SerializableObjectFactory
232     {
233         private TimerPrimaryKey timerId_;
234
235         SerializedTimerWrapper() {}
236
237         SerializedTimerWrapper(TimerPrimaryKey timerId) {
238             timerId_ = timerId;
239         }
240
241         /**
242          * When deserializing the timer wrapper create a TimerWrapper object.
243          * Check if the record is valid only when making calls on the object.
244          */

245         public Object JavaDoc createObject()
246             throws EJBException JavaDoc
247         {
248             ContainerFactoryImpl cf = (ContainerFactoryImpl)
249                 Switch.getSwitch().getContainerFactory();
250             EJBTimerService timerService = cf.getEJBTimerService();
251             TimerWrapper timer = new TimerWrapper(timerId_, timerService);
252
253             return timer;
254         }
255     }
256
257     private static class TimerHandleImpl implements TimerHandle JavaDoc {
258
259         private TimerPrimaryKey timerId_;
260         
261         public TimerHandleImpl(TimerPrimaryKey timerId) {
262             timerId_ = timerId;
263         }
264
265         /**
266          * Materialize Timer from handle. This must be coded
267          * very defensively, since handle use might be attempted from
268          * outside of EJB container.
269          */

270         public Timer JavaDoc getTimer() throws IllegalStateException JavaDoc,
271             NoSuchObjectLocalException JavaDoc, EJBException JavaDoc {
272             TimerWrapper timer = null;
273             Switch theSwitch = Switch.getSwitch();
274             if( theSwitch != null ) {
275                 
276                 // Make sure use of timer service methods are allowed
277
TimerWrapper.checkCallPermission();
278
279                 timer = getTimerInternal(timerId_);
280
281             } else {
282                 throw new IllegalStateException JavaDoc
283                     ("Attempt to use EJB timer from outside a container");
284             }
285
286             return timer;
287         }
288
289         public static TimerWrapper getTimerInternal(TimerPrimaryKey timerId)
290             throws NoSuchObjectLocalException JavaDoc, EJBException JavaDoc {
291
292             TimerWrapper timer = null;
293             ContainerFactoryImpl cf = (ContainerFactoryImpl)
294                 Switch.getSwitch().getContainerFactory();
295             EJBTimerService timerService = cf.getEJBTimerService();
296
297             if( timerService != null ) {
298                 if( timerService.timerExists(timerId) ) {
299                     timer = new TimerWrapper(timerId, timerService);
300                 } else {
301                     throw new NoSuchObjectLocalException JavaDoc
302                         ("timer is no longer active");
303                 }
304             } else {
305                 throw new EJBException JavaDoc("EJB Timer Service not available");
306             }
307             
308             return timer;
309         }
310     }
311 }
312
Popular Tags