1 23 package com.sun.ejb.containers; 24 25 import java.util.Date ; 26 import java.io.Serializable ; 27 28 import javax.ejb.EJBLocalObject ; 29 import javax.ejb.NoSuchObjectLocalException ; 30 import javax.ejb.EJBException ; 31 import javax.ejb.Timer ; 32 import javax.ejb.TimerHandle ; 33 import javax.ejb.EJBException ; 34 import javax.ejb.FinderException ; 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 ; 46 47 import com.sun.ejb.spi.io.IndirectlySerializable; 48 import com.sun.ejb.spi.io.SerializableObjectFactory; 49 50 58 public class TimerWrapper 59 implements Timer , IndirectlySerializable 60 { 61 62 private TimerPrimaryKey timerId_; 63 private EJBTimerService timerService_; 64 65 TimerWrapper(TimerPrimaryKey timerId, EJBTimerService timerService) { 66 timerId_ = timerId; 67 timerService_ = timerService; } 69 70 73 public void cancel() 74 throws IllegalStateException , NoSuchObjectLocalException , EJBException { 75 76 checkCallPermission(); 77 78 try { 79 timerService_.cancelTimer(timerId_); 80 } catch(FinderException fe) { 81 throw new NoSuchObjectLocalException ("timer no longer exists", fe); 82 } catch(Exception e) { 83 throw new EJBException (e); 84 } 85 86 } 87 88 public long getTimeRemaining() 89 throws IllegalStateException , NoSuchObjectLocalException { 90 91 Date nextTimeout = getNextTimeout(); 92 93 Date now = new Date (); 94 95 long timeRemaining = nextTimeout.getTime() - now.getTime(); 96 97 return (timeRemaining > 0) ? timeRemaining : 0; 98 } 99 100 public Date getNextTimeout() 101 throws IllegalStateException , NoSuchObjectLocalException { 102 103 checkCallPermission(); 104 105 Date nextTimeout; 106 107 try { 108 nextTimeout = timerService_.getNextTimeout(timerId_); 109 } catch(FinderException fe) { 110 throw new NoSuchObjectLocalException ("timer no longer exists", fe); 111 } 112 113 return nextTimeout; 114 } 115 116 public Serializable getInfo() 117 throws IllegalStateException , NoSuchObjectLocalException { 118 119 checkCallPermission(); 120 121 Serializable info; 122 123 try { 124 info = timerService_.getInfo(timerId_); 125 } catch(FinderException fe) { 126 throw new NoSuchObjectLocalException ("timer no longer exists", fe); 127 } 128 129 return info; 130 } 131 132 public TimerHandle getHandle() 133 throws IllegalStateException , NoSuchObjectLocalException { 134 135 checkCallPermission(); 136 137 if( !timerService_.timerExists(timerId_) ) { 138 throw new NoSuchObjectLocalException ("timer no longer exists"); 139 } 140 141 return new TimerHandleImpl(timerId_); 142 } 143 144 public boolean equals(Object 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 toString() { 158 return "Timer " + timerId_; 159 } 160 161 166 private static void checkCallPermission() throws IllegalStateException { 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 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 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 context.checkTimerServiceMethodAccess(); 194 allowed = true; 195 } else { 196 allowed = false; 202 } 203 } else if( invType == ComponentInvocation.SERVLET_INVOCATION ) { 204 throw new IllegalStateException 205 ("Web tier access to EJB timers through local " + 206 "interfaces is not portable"); 207 } 208 } catch(InvocationException ie) { 209 IllegalStateException ise = new IllegalStateException 210 ("Operation not allowed"); 211 ise.initCause(ie); 212 throw ise; 213 } 214 215 if( !allowed ) { 216 throw new IllegalStateException ("Operation not allowed"); 217 } 218 219 } 220 221 public SerializableObjectFactory getSerializableObjectFactory() { 222 return new SerializedTimerWrapper(timerId_); 223 } 224 225 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 245 public Object createObject() 246 throws EJBException 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 { 258 259 private TimerPrimaryKey timerId_; 260 261 public TimerHandleImpl(TimerPrimaryKey timerId) { 262 timerId_ = timerId; 263 } 264 265 270 public Timer getTimer() throws IllegalStateException , 271 NoSuchObjectLocalException , EJBException { 272 TimerWrapper timer = null; 273 Switch theSwitch = Switch.getSwitch(); 274 if( theSwitch != null ) { 275 276 TimerWrapper.checkCallPermission(); 278 279 timer = getTimerInternal(timerId_); 280 281 } else { 282 throw new IllegalStateException 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 , EJBException { 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 302 ("timer is no longer active"); 303 } 304 } else { 305 throw new EJBException ("EJB Timer Service not available"); 306 } 307 308 return timer; 309 } 310 } 311 } 312 | Popular Tags |