00001
00026 package org.objectweb.jonas_ejb.container;
00027
00028 import java.io.Serializable;
00029
00030 import javax.ejb.EJBException;
00031 import javax.ejb.NoSuchObjectLocalException;
00032 import javax.ejb.Timer;
00033 import javax.ejb.TimerHandle;
00034 import javax.naming.Context;
00035 import javax.naming.InitialContext;
00036 import javax.naming.NamingException;
00037
00038 import org.objectweb.jonas.naming.NamingManager;
00039 import org.objectweb.jonas_ejb.container.JContext;
00040 import org.objectweb.jonas_lib.naming.ContainerNaming;
00041 import org.objectweb.jonas.container.EJBServiceImpl;
00042 import org.objectweb.jonas.service.ServiceManager;
00043 import org.objectweb.jonas_timer.TraceTimer;
00044 import org.objectweb.util.monolog.api.BasicLevel;
00045
00049 public class JTimerHandle implements TimerHandle {
00050
00051 private long period;
00052 private long starttime;
00053 private Serializable info;
00054 private String beanname;
00055 private String container;
00056
00060 private Serializable pk;
00061
00065 public JTimerHandle(long starttime, long period, Serializable info, String beanname, String container, Serializable pk) {
00066 TraceTimer.logger.log(BasicLevel.DEBUG, "New JTimerHandle initial = " + starttime + ", period = " + period);
00067 this.starttime = starttime;
00068 this.period = period;
00069 this.info = info;
00070 this.beanname = beanname;
00071 this.container = container;
00072 this.pk = pk;
00073 }
00074
00083 public Timer getTimer() throws IllegalStateException, NoSuchObjectLocalException, EJBException {
00084
00085
00086
00087 try {
00088 ContainerNaming naming = NamingManager.getInstance();
00089 Context ctx = naming.getComponentContext();
00090 JContext sc = (JContext) ctx.lookup("MY_SF_CONTEXT");
00091 if (sc.getState() != 2) {
00092 throw new IllegalStateException("This operation is not allowed here");
00093 }
00094 } catch (NamingException ne) {
00095
00096
00097 }
00098
00099 EJBServiceImpl ejbserv = null;
00100 try {
00101 ejbserv = (EJBServiceImpl) ServiceManager.getInstance().getEjbService();
00102 } catch (Exception e) {
00103 throw new IllegalStateException("Cannot use Timer outside jonas server");
00104 }
00105 JContainer cont = (JContainer) ejbserv.getContainer(container);
00106 if (cont == null) {
00107 TraceTimer.logger.log(BasicLevel.ERROR, "Cannot get container =" + container);
00108 throw new IllegalStateException("Cannot get container");
00109 }
00110 JFactory bf = (JFactory) cont.getBeanFactory(beanname);
00111 JTimerService timerservice = null;
00112 if (bf instanceof JEntityFactory) {
00113
00114 JEntityFactory ef = (JEntityFactory) bf;
00115 Serializable pks = ef.decodePK(pk);
00116 TraceEjb.interp.log(BasicLevel.DEBUG, "encoded PK=" + pk);
00117 TraceEjb.interp.log(BasicLevel.DEBUG, "decoded PK=" + pks);
00118 JEntitySwitch es = ef.existEJB(pks);
00119 if (es == null) {
00120 throw new NoSuchObjectLocalException("No entity for this pk");
00121 }
00122 timerservice = (JTimerService) es.getEntityTimerService();
00123 } else {
00124
00125 timerservice = (JTimerService) bf.getTimerService();
00126 }
00127 if (timerservice == null) {
00128 throw new IllegalStateException("Cannot retrieve TimerService");
00129 }
00130
00131 Timer ret = timerservice.getTimer(starttime, period, info);
00132 if (ret == null) {
00133 throw new NoSuchObjectLocalException("Timer does not exist any longer");
00134 }
00135 return ret;
00136 }
00137
00138 }