1 25 26 package org.objectweb.jonas_ejb.container; 27 28 import java.io.Serializable ; 29 30 import javax.ejb.EJBException ; 31 import javax.ejb.NoSuchObjectLocalException ; 32 import javax.ejb.Timer ; 33 import javax.ejb.TimerHandle ; 34 import javax.naming.Context ; 35 import javax.naming.InitialContext ; 36 import javax.naming.NamingException ; 37 38 import org.objectweb.jonas.naming.NamingManager; 39 import org.objectweb.jonas_ejb.container.JContext; 40 import org.objectweb.jonas_lib.naming.ContainerNaming; 41 import org.objectweb.jonas.container.EJBServiceImpl; 42 import org.objectweb.jonas.service.ServiceManager; 43 import org.objectweb.jonas_timer.TraceTimer; 44 import org.objectweb.util.monolog.api.BasicLevel; 45 46 49 public class JTimerHandle implements TimerHandle { 50 51 private long period; 52 private long starttime; 53 private Serializable info; 54 private String beanname; 55 private String container; 56 57 60 private Serializable pk; 61 62 65 public JTimerHandle(long starttime, long period, Serializable info, String beanname, String container, Serializable pk) { 66 if (TraceTimer.isDebug()) { 67 TraceTimer.logger.log(BasicLevel.DEBUG, "New JTimerHandle initial = " + starttime + ", period = " + period); 68 } 69 this.starttime = starttime; 70 this.period = period; 71 this.info = info; 72 this.beanname = beanname; 73 this.container = container; 74 this.pk = pk; 75 } 76 77 85 public Timer getTimer() throws IllegalStateException , NoSuchObjectLocalException , EJBException { 86 87 try { 90 ContainerNaming naming = NamingManager.getInstance(); 91 Context ctx = naming.getComponentContext(); 92 JContext sc = (JContext) ctx.lookup("MY_SF_CONTEXT"); 93 if (sc.getState() != 2) { 94 throw new IllegalStateException ("This operation is not allowed here"); 95 } 96 } catch (NamingException ne) { 97 } 100 101 EJBServiceImpl ejbserv = null; 102 try { 103 ejbserv = (EJBServiceImpl) ServiceManager.getInstance().getEjbService(); 104 } catch (Exception e) { 105 throw new IllegalStateException ("Cannot use Timer outside jonas server"); 106 } 107 JContainer cont = (JContainer) ejbserv.getContainer(container); 108 if (cont == null) { 109 TraceTimer.logger.log(BasicLevel.ERROR, "Cannot get container =" + container); 110 throw new IllegalStateException ("Cannot get container"); 111 } 112 JFactory bf = (JFactory) cont.getBeanFactory(beanname); 113 JTimerService timerservice = null; 114 if (bf instanceof JEntityFactory) { 115 JEntityFactory ef = (JEntityFactory) bf; 117 Serializable pks = ef.decodePK(pk); 118 if (TraceEjb.isDebugIc()) { 119 TraceEjb.interp.log(BasicLevel.DEBUG, "encoded PK=" + pk); 120 TraceEjb.interp.log(BasicLevel.DEBUG, "decoded PK=" + pks); 121 } 122 JEntitySwitch es = ef.existEJB(pks, null); 123 if (es == null) { 124 throw new NoSuchObjectLocalException ("No entity for this pk"); 125 } 126 timerservice = (JTimerService) es.getEntityTimerService(); 127 } else { 128 timerservice = (JTimerService) bf.getTimerService(); 130 } 131 if (timerservice == null) { 132 throw new IllegalStateException ("Cannot retrieve TimerService"); 133 } 134 Timer ret = timerservice.getTimer(starttime, period, info); 136 if (ret == null) { 137 throw new NoSuchObjectLocalException ("Timer does not exist any longer"); 138 } 139 return ret; 140 } 141 142 } 143 | Popular Tags |