package org.jboss.ejb.txtimer;
import javax.ejb.EJBException;
import javax.ejb.NoSuchObjectLocalException;
import javax.ejb.Timer;
import javax.ejb.TimerHandle;
import javax.management.ObjectName;
import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.StringTokenizer;
public class TimerHandleImpl implements TimerHandle
{
public static final String DATE_PATTERN = "dd-MMM-yyyy HH:mm:ss.SSS";
private String timerId;
private TimedObjectId timedObjectId;
private Date firstTime;
private long periode;
private Serializable info;
private int hashCode;
TimerHandleImpl(TimerImpl timer)
{
timerId = timer.getTimerId();
timedObjectId = timer.getTimedObjectId();
firstTime = timer.getFirstTime();
periode = timer.getPeriode();
info = timer.getInfoInternal();
}
TimerHandleImpl(String timerId, TimedObjectId timedObjectId, Date firstTime, long periode, Serializable info)
{
this.timerId = timerId;
this.timedObjectId = timedObjectId;
this.firstTime = firstTime;
this.periode = periode;
this.info = info;
}
private TimerHandleImpl(String externalForm)
{
if (externalForm.startsWith("[") == false || externalForm.endsWith("]") == false)
throw new IllegalArgumentException("Square brackets expected arround: " + externalForm);
try
{
String inStr = externalForm.substring(1, externalForm.length() - 1);
if (inStr.startsWith("id=") == false)
throw new IllegalArgumentException("Cannot parse: " + externalForm);
int targetIndex = inStr.indexOf(",target=");
int firstIndex = inStr.indexOf(",first=");
String idStr = inStr.substring(3, targetIndex);
String targetStr = inStr.substring(targetIndex + 8, firstIndex);
String restStr = inStr.substring(firstIndex + 1);
timerId = idStr;
timedObjectId = TimedObjectId.parse(targetStr);
StringTokenizer st = new StringTokenizer(restStr, ",=");
if (st.countTokens() % 2 != 0)
throw new IllegalArgumentException("Cannot parse: " + externalForm);
SimpleDateFormat sdf = new SimpleDateFormat(DATE_PATTERN);
periode = -1;
while (st.hasMoreTokens())
{
String key = st.nextToken();
String value = st.nextToken();
if (key.equals("first"))
firstTime = sdf.parse(value);
if (key.equals("periode"))
periode = new Long(value).longValue();
}
if (firstTime == null || periode < 0)
throw new IllegalArgumentException("Cannot parse: " + externalForm);
}
catch (ParseException e)
{
throw new IllegalArgumentException("Cannot parse date/time in: " + externalForm);
}
}
public static TimerHandleImpl parse(String externalForm)
{
return new TimerHandleImpl(externalForm);
}
public String toExternalForm()
{
SimpleDateFormat sdf = new SimpleDateFormat(DATE_PATTERN);
String firstEvent = sdf.format(firstTime);
return "[id=" + timerId + ",target=" + timedObjectId + ",first=" + firstEvent + ",periode=" + periode + "]";
}
public String getTimerId()
{
return timerId;
}
public TimedObjectId getTimedObjectId()
{
return timedObjectId;
}
public Date getFirstTime()
{
return firstTime;
}
public long getPeriode()
{
return periode;
}
public Serializable getInfo()
{
return info;
}
public Timer getTimer() throws IllegalStateException, NoSuchObjectLocalException, EJBException
{
EJBTimerService ejbTimerService = EJBTimerServiceLocator.getEjbTimerService();
ObjectName containerId = timedObjectId.getContainerId();
Object instancePk = timedObjectId.getInstancePk();
TimerServiceImpl timerService = (TimerServiceImpl)ejbTimerService.getTimerService(containerId, instancePk);
if (timerService == null)
throw new NoSuchObjectLocalException("TimerService not available: " + timedObjectId);
TimerImpl timer = (TimerImpl)timerService.getTimer(this);
if (timer == null || timer.isActive() == false)
throw new NoSuchObjectLocalException("Timer not available: " + timedObjectId);
return timer;
}
public boolean equals(Object obj)
{
if (obj == this) return true;
if (obj instanceof TimerHandleImpl)
{
TimerHandleImpl other = (TimerHandleImpl)obj;
return hashCode() == other.hashCode();
}
return false;
}
public int hashCode()
{
if (hashCode == 0)
{
hashCode = toExternalForm().hashCode();
}
return hashCode;
}
public String toString()
{
return toExternalForm();
}
}