KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Collection JavaDoc;
27 import java.util.Set JavaDoc;
28 import java.util.HashSet JavaDoc;
29 import java.util.Iterator JavaDoc;
30
31 import java.io.Serializable JavaDoc;
32
33 import javax.ejb.EJBLocalObject JavaDoc;
34 import javax.ejb.TimerService JavaDoc;
35 import javax.ejb.Timer JavaDoc;
36 import javax.ejb.EJBException JavaDoc;
37 import javax.ejb.FinderException JavaDoc;
38 import javax.ejb.CreateException JavaDoc;
39
40 import com.sun.enterprise.Switch;
41
42 /*
43  * EJBTimerServiceWrappers is the application-level representation
44  * of the EJB timer service.
45  *
46  * @author Kenneth Saks
47  */

48 public class EJBTimerServiceWrapper implements TimerService JavaDoc {
49
50     private EJBTimerService timerService_;
51     private EJBContextImpl ejbContext_;
52     private long containerId_;
53
54     private boolean entity_;
55
56     // Only used for entity beans
57
private Object JavaDoc timedObjectPrimaryKey_;
58
59     public EJBTimerServiceWrapper(EJBTimerService timerService,
60                                   EJBContextImpl ejbContext)
61     {
62         timerService_ = timerService;
63         ejbContext_ = ejbContext;
64         BaseContainer container = (BaseContainer) ejbContext.getContainer();
65         containerId_ = container.getEjbDescriptor().getUniqueId();
66         entity_ = false;
67         timedObjectPrimaryKey_ = null;
68     }
69
70     public EJBTimerServiceWrapper(EJBTimerService timerService,
71                                   EntityContextImpl entityContext)
72     {
73         this(timerService, ((EJBContextImpl)entityContext));
74         entity_ = true;
75         // Delay access of primary key since this might have been called
76
// from ejbCreate
77
timedObjectPrimaryKey_ = null;
78     }
79
80     public Timer JavaDoc createTimer(long duration, Serializable JavaDoc info)
81         throws IllegalArgumentException JavaDoc, IllegalStateException JavaDoc, EJBException JavaDoc {
82
83         checkCreateTimerCallPermission();
84
85         if( duration < 0 ) {
86             throw new IllegalArgumentException JavaDoc("invalid duration=" + duration);
87         }
88                              
89         TimerPrimaryKey timerId = null;
90
91         try {
92             timerId = timerService_.createTimer
93                 (containerId_, getTimedObjectPrimaryKey(), duration, 0, info);
94         } catch(CreateException JavaDoc ce) {
95             EJBException JavaDoc ejbEx = new EJBException JavaDoc();
96             ejbEx.initCause(ce);
97             throw ejbEx;
98         }
99
100         return new TimerWrapper(timerId, timerService_);
101     }
102
103     public Timer JavaDoc createTimer(long initialDuration, long intervalDuration,
104                              Serializable JavaDoc info)
105         throws IllegalArgumentException JavaDoc, IllegalStateException JavaDoc, EJBException JavaDoc {
106
107         checkCreateTimerCallPermission();
108
109         if( initialDuration < 0 ) {
110             throw new IllegalArgumentException JavaDoc("invalid initial duration = " +
111                                                initialDuration);
112         } else if( intervalDuration < 0 ) {
113             throw new IllegalArgumentException JavaDoc("invalid interval duration = " +
114                                                intervalDuration);
115         }
116                              
117         TimerPrimaryKey timerId = null;
118
119         try {
120             timerId = timerService_.createTimer
121                 (containerId_, getTimedObjectPrimaryKey(), initialDuration,
122                  intervalDuration, info);
123         } catch(CreateException JavaDoc ce) {
124             EJBException JavaDoc ejbEx = new EJBException JavaDoc();
125             ejbEx.initCause(ce);
126             throw ejbEx;
127         }
128
129         return new TimerWrapper(timerId, timerService_);
130     }
131
132     public Timer JavaDoc createTimer(Date JavaDoc expiration, Serializable JavaDoc info)
133         throws IllegalArgumentException JavaDoc, IllegalStateException JavaDoc, EJBException JavaDoc {
134                              
135         checkCreateTimerCallPermission();
136
137         if( expiration == null ) {
138             throw new IllegalArgumentException JavaDoc("null expiration");
139         }
140
141         TimerPrimaryKey timerId = null;
142
143         try {
144             timerId = timerService_.createTimer(containerId_,
145                                                 getTimedObjectPrimaryKey(),
146                                                 expiration, 0, info);
147         } catch(CreateException JavaDoc ce) {
148             EJBException JavaDoc ejbEx = new EJBException JavaDoc();
149             ejbEx.initCause(ce);
150             throw ejbEx;
151         }
152
153         return new TimerWrapper(timerId, timerService_);
154     }
155
156     public Timer JavaDoc createTimer(Date JavaDoc initialExpiration, long intervalDuration,
157                              Serializable JavaDoc info)
158         throws IllegalArgumentException JavaDoc, IllegalStateException JavaDoc, EJBException JavaDoc {
159
160         checkCreateTimerCallPermission();
161
162         if( initialExpiration == null ) {
163             throw new IllegalArgumentException JavaDoc("null expiration");
164         } else if ( intervalDuration < 0 ) {
165             throw new IllegalArgumentException JavaDoc("invalid interval duration = " +
166                                                intervalDuration);
167         }
168
169         TimerPrimaryKey timerId = null;
170         try {
171             timerId = timerService_.createTimer(containerId_,
172                 getTimedObjectPrimaryKey(), initialExpiration,
173                 intervalDuration, info);
174         } catch(CreateException JavaDoc e) {
175             EJBException JavaDoc ejbEx = new EJBException JavaDoc();
176             ejbEx.initCause(e);
177             throw ejbEx;
178         }
179
180         return new TimerWrapper(timerId, timerService_);
181     }
182
183     public Collection JavaDoc getTimers() throws IllegalStateException JavaDoc, EJBException JavaDoc {
184         
185         checkCallPermission();
186         
187         Collection JavaDoc timerIds = new HashSet JavaDoc();
188
189         if( ejbContext_.isTimedObject() ) {
190             try {
191                 timerIds = timerService_.getTimerIds
192                     (containerId_, getTimedObjectPrimaryKey());
193             } catch(FinderException JavaDoc fe) {
194                 EJBException JavaDoc ejbEx = new EJBException JavaDoc();
195                 ejbEx.initCause(fe);
196                 throw ejbEx;
197             }
198         }
199                                                         
200         Collection JavaDoc timerWrappers = new HashSet JavaDoc();
201
202         for(Iterator JavaDoc iter = timerIds.iterator(); iter.hasNext();) {
203             TimerPrimaryKey next = (TimerPrimaryKey) iter.next();
204             timerWrappers.add( new TimerWrapper(next, timerService_) );
205         }
206
207         return timerWrappers;
208     }
209
210     private Object JavaDoc getTimedObjectPrimaryKey() {
211         if( !entity_ ) {
212             return null;
213         } else {
214             synchronized(this) {
215                 if( timedObjectPrimaryKey_ == null ) {
216                     timedObjectPrimaryKey_ =
217                         ((EntityContextImpl) ejbContext_).getPrimaryKey();
218                 }
219             }
220         }
221         return timedObjectPrimaryKey_;
222     }
223
224     private void checkCreateTimerCallPermission()
225         throws IllegalStateException JavaDoc {
226         if( ejbContext_.isTimedObject() ) {
227             checkCallPermission();
228         } else {
229             throw new IllegalStateException JavaDoc("EJBTimerService.createTimer can "
230                 + "only be called from a timed object. This EJB does not "
231                 + "implement javax.ejb.TimedObject");
232         }
233     }
234
235     private void checkCallPermission()
236         throws IllegalStateException JavaDoc {
237         ejbContext_.checkTimerServiceMethodAccess();
238     }
239
240 }
241
Popular Tags