1 23 package com.sun.ejb.containers; 24 25 import java.util.logging.Logger ; 26 import java.util.logging.Level ; 27 28 import com.sun.logging.LogDomains; 29 import java.io.Serializable ; 30 import java.util.Date ; 31 32 import com.sun.enterprise.deployment.EjbDescriptor; 33 import com.sun.enterprise.deployment.Application; 34 35 46 class RuntimeTimerState { 47 48 private static Logger logger = LogDomains.getLogger(LogDomains.EJB_LOGGER); 49 50 54 private static final int CREATED = 0; 56 57 private static final int SCHEDULED = 1; 59 60 private static final int BEING_DELIVERED = 2; 63 64 private static final int CANCELLED = 3; 67 68 private int state_; 69 70 private TimerPrimaryKey timerId_; 74 75 private Date initialExpiration_; 76 private long intervalDuration_; 77 private long containerId_; 78 private Object timedObjectPrimaryKey_; 79 80 private BaseContainer container_; 82 83 private EJBTimerTask currentTask_; 85 86 90 private int numExpirations_; 91 private int numFailedDeliveries_; 92 93 RuntimeTimerState(TimerPrimaryKey timerId, 94 Date initialExpiration, long intervalDuration, 95 BaseContainer container, 96 Object 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 getInitialExpiration() { 120 return initialExpiration_; 121 } 122 123 BaseContainer getContainer() { 124 return container_; 125 } 126 127 long getContainerId() { 128 return containerId_; 129 } 130 131 Object getTimedObjectPrimaryKey() { 132 return timedObjectPrimaryKey_; 133 } 134 135 long getIntervalDuration() { 136 return intervalDuration_; 137 } 138 139 140 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 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 stateToString() { 199 return stateToString(state_); 200 } 201 202 203 private String 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 230 int getNumFailedDeliveries() { 231 return numFailedDeliveries_; 232 } 233 234 EJBTimerTask getCurrentTimerTask() { 235 return currentTask_; 236 } 237 238 String getTimedObjectEjbName() { 239 return container_.getEjbDescriptor().getName(); 240 } 241 242 String 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 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 getNextTimeout() { 281 if( !isScheduled() && !isRescheduled() ) { 282 throw new IllegalStateException (); 283 } 284 return currentTask_.getTimeout(); 285 } 286 287 long getTimeRemaining() { 288 Date timeout = getNextTimeout(); 289 Date now = new Date (); 290 return (timeout.getTime() - now.getTime()); 291 } 292 293 296 boolean isPeriodic() { 297 return (intervalDuration_ > 0); 298 } 299 300 304 public int hashCode() { 305 return timerId_.hashCode(); 306 } 307 308 public boolean equals(Object 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 toString() { 317 StringBuffer buffer = new StringBuffer (); 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 pk = getTimedObjectPrimaryKey(); 328 if( pk != null ) { 329 buffer.append("'" + pk + "' "); 330 } 331 return buffer.toString(); 332 } 333 } 334 | Popular Tags |