1 23 package com.sun.ejb.containers; 24 25 import java.util.Date ; 26 import java.util.Collection ; 27 import java.util.Set ; 28 import java.util.HashSet ; 29 import java.util.Iterator ; 30 31 import java.io.Serializable ; 32 33 import javax.ejb.EJBLocalObject ; 34 import javax.ejb.TimerService ; 35 import javax.ejb.Timer ; 36 import javax.ejb.EJBException ; 37 import javax.ejb.FinderException ; 38 import javax.ejb.CreateException ; 39 40 import com.sun.enterprise.Switch; 41 42 48 public class EJBTimerServiceWrapper implements TimerService { 49 50 private EJBTimerService timerService_; 51 private EJBContextImpl ejbContext_; 52 private long containerId_; 53 54 private boolean entity_; 55 56 private Object 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 timedObjectPrimaryKey_ = null; 78 } 79 80 public Timer createTimer(long duration, Serializable info) 81 throws IllegalArgumentException , IllegalStateException , EJBException { 82 83 checkCreateTimerCallPermission(); 84 85 if( duration < 0 ) { 86 throw new IllegalArgumentException ("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 ce) { 95 EJBException ejbEx = new EJBException (); 96 ejbEx.initCause(ce); 97 throw ejbEx; 98 } 99 100 return new TimerWrapper(timerId, timerService_); 101 } 102 103 public Timer createTimer(long initialDuration, long intervalDuration, 104 Serializable info) 105 throws IllegalArgumentException , IllegalStateException , EJBException { 106 107 checkCreateTimerCallPermission(); 108 109 if( initialDuration < 0 ) { 110 throw new IllegalArgumentException ("invalid initial duration = " + 111 initialDuration); 112 } else if( intervalDuration < 0 ) { 113 throw new IllegalArgumentException ("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 ce) { 124 EJBException ejbEx = new EJBException (); 125 ejbEx.initCause(ce); 126 throw ejbEx; 127 } 128 129 return new TimerWrapper(timerId, timerService_); 130 } 131 132 public Timer createTimer(Date expiration, Serializable info) 133 throws IllegalArgumentException , IllegalStateException , EJBException { 134 135 checkCreateTimerCallPermission(); 136 137 if( expiration == null ) { 138 throw new IllegalArgumentException ("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 ce) { 148 EJBException ejbEx = new EJBException (); 149 ejbEx.initCause(ce); 150 throw ejbEx; 151 } 152 153 return new TimerWrapper(timerId, timerService_); 154 } 155 156 public Timer createTimer(Date initialExpiration, long intervalDuration, 157 Serializable info) 158 throws IllegalArgumentException , IllegalStateException , EJBException { 159 160 checkCreateTimerCallPermission(); 161 162 if( initialExpiration == null ) { 163 throw new IllegalArgumentException ("null expiration"); 164 } else if ( intervalDuration < 0 ) { 165 throw new IllegalArgumentException ("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 e) { 175 EJBException ejbEx = new EJBException (); 176 ejbEx.initCause(e); 177 throw ejbEx; 178 } 179 180 return new TimerWrapper(timerId, timerService_); 181 } 182 183 public Collection getTimers() throws IllegalStateException , EJBException { 184 185 checkCallPermission(); 186 187 Collection timerIds = new HashSet (); 188 189 if( ejbContext_.isTimedObject() ) { 190 try { 191 timerIds = timerService_.getTimerIds 192 (containerId_, getTimedObjectPrimaryKey()); 193 } catch(FinderException fe) { 194 EJBException ejbEx = new EJBException (); 195 ejbEx.initCause(fe); 196 throw ejbEx; 197 } 198 } 199 200 Collection timerWrappers = new HashSet (); 201 202 for(Iterator 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 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 { 226 if( ejbContext_.isTimedObject() ) { 227 checkCallPermission(); 228 } else { 229 throw new IllegalStateException ("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 { 237 ejbContext_.checkTimerServiceMethodAccess(); 238 } 239 240 } 241 | Popular Tags |