1 22 package org.jboss.ejb.txtimer; 23 24 26 import javax.ejb.EJBException ; 27 import javax.ejb.NoSuchObjectLocalException ; 28 import javax.ejb.Timer ; 29 import javax.ejb.TimerHandle ; 30 import javax.management.ObjectName ; 31 import java.io.Serializable ; 32 import java.text.ParseException ; 33 import java.text.SimpleDateFormat ; 34 import java.util.Date ; 35 import java.util.StringTokenizer ; 36 37 44 public class TimerHandleImpl implements TimerHandle 45 { 46 49 public static final String DATE_PATTERN = "dd-MMM-yyyy HH:mm:ss.SSS"; 50 51 private String timerId; 53 private TimedObjectId timedObjectId; 54 private Date firstTime; 55 private long periode; 56 private Serializable info; 57 private int hashCode; 58 59 62 TimerHandleImpl(TimerImpl timer) 63 { 64 timerId = timer.getTimerId(); 65 timedObjectId = timer.getTimedObjectId(); 66 firstTime = timer.getFirstTime(); 67 periode = timer.getPeriode(); 68 info = timer.getInfoInternal(); 69 } 70 71 74 TimerHandleImpl(String timerId, TimedObjectId timedObjectId, Date firstTime, long periode, Serializable info) 75 { 76 this.timerId = timerId; 77 this.timedObjectId = timedObjectId; 78 this.firstTime = firstTime; 79 this.periode = periode; 80 this.info = info; 81 } 82 83 86 private TimerHandleImpl(String externalForm) 87 { 88 if (externalForm.startsWith("[") == false || externalForm.endsWith("]") == false) 89 throw new IllegalArgumentException ("Square brackets expected arround: " + externalForm); 90 91 try 92 { 93 String inStr = externalForm.substring(1, externalForm.length() - 1); 95 96 if (inStr.startsWith("id=") == false) 97 throw new IllegalArgumentException ("Cannot parse: " + externalForm); 98 99 int targetIndex = inStr.indexOf(",target="); 100 int firstIndex = inStr.indexOf(",first="); 101 102 String idStr = inStr.substring(3, targetIndex); 103 String targetStr = inStr.substring(targetIndex + 8, firstIndex); 104 String restStr = inStr.substring(firstIndex + 1); 105 106 timerId = idStr; 107 timedObjectId = TimedObjectId.parse(targetStr); 108 109 StringTokenizer st = new StringTokenizer (restStr, ",="); 110 if (st.countTokens() % 2 != 0) 111 throw new IllegalArgumentException ("Cannot parse: " + externalForm); 112 113 SimpleDateFormat sdf = new SimpleDateFormat (DATE_PATTERN); 114 115 periode = -1; 116 117 while (st.hasMoreTokens()) 118 { 119 String key = st.nextToken(); 120 String value = st.nextToken(); 121 if (key.equals("first")) 122 firstTime = sdf.parse(value); 123 if (key.equals("periode")) 124 periode = new Long (value).longValue(); 125 } 126 127 if (firstTime == null || periode < 0) 128 throw new IllegalArgumentException ("Cannot parse: " + externalForm); 129 } 130 catch (ParseException e) 131 { 132 throw new IllegalArgumentException ("Cannot parse date/time in: " + externalForm); 133 } 134 } 135 136 140 public static TimerHandleImpl parse(String externalForm) 141 { 142 return new TimerHandleImpl(externalForm); 143 } 144 145 149 public String toExternalForm() 150 { 151 SimpleDateFormat sdf = new SimpleDateFormat (DATE_PATTERN); 152 String firstEvent = sdf.format(firstTime); 153 return "[id=" + timerId + ",target=" + timedObjectId + ",first=" + firstEvent + ",periode=" + periode + "]"; 154 } 155 156 public String getTimerId() 157 { 158 return timerId; 159 } 160 161 public TimedObjectId getTimedObjectId() 162 { 163 return timedObjectId; 164 } 165 166 public Date getFirstTime() 167 { 168 return firstTime; 169 } 170 171 public long getPeriode() 172 { 173 return periode; 174 } 175 176 public Serializable getInfo() 177 { 178 return info; 179 } 180 181 191 public Timer getTimer() throws IllegalStateException , NoSuchObjectLocalException , EJBException 192 { 193 194 EJBTimerService ejbTimerService = EJBTimerServiceLocator.getEjbTimerService(); 195 ObjectName containerId = timedObjectId.getContainerId(); 196 Object instancePk = timedObjectId.getInstancePk(); 197 TimerServiceImpl timerService = (TimerServiceImpl)ejbTimerService.getTimerService(containerId, instancePk); 198 if (timerService == null) 199 throw new NoSuchObjectLocalException ("TimerService not available: " + timedObjectId); 200 201 TimerImpl timer = (TimerImpl)timerService.getTimer(this); 202 if (timer == null || timer.isActive() == false) 203 throw new NoSuchObjectLocalException ("Timer not available: " + timedObjectId); 204 205 return timer; 206 } 207 208 211 public boolean equals(Object obj) 212 { 213 if (obj == this) return true; 214 if (obj instanceof TimerHandleImpl) 215 { 216 TimerHandleImpl other = (TimerHandleImpl)obj; 217 return hashCode() == other.hashCode(); 218 } 219 return false; 220 } 221 222 225 public int hashCode() 226 { 227 if (hashCode == 0) 228 { 229 hashCode = toExternalForm().hashCode(); 230 } 231 return hashCode; 232 } 233 234 237 public String toString() 238 { 239 return toExternalForm(); 240 } 241 } 242 | Popular Tags |