KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_ejb > container > JTimerHandle


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: JTimerHandle.java,v 1.15 2005/04/28 16:52:59 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas_ejb.container;
27
28 import java.io.Serializable JavaDoc;
29
30 import javax.ejb.EJBException JavaDoc;
31 import javax.ejb.NoSuchObjectLocalException JavaDoc;
32 import javax.ejb.Timer JavaDoc;
33 import javax.ejb.TimerHandle JavaDoc;
34 import javax.naming.Context JavaDoc;
35 import javax.naming.InitialContext JavaDoc;
36 import javax.naming.NamingException JavaDoc;
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 /**
47  * TimerHandle implementation
48  */

49 public class JTimerHandle implements TimerHandle JavaDoc {
50
51     private long period;
52     private long starttime;
53     private Serializable JavaDoc info;
54     private String JavaDoc beanname;
55     private String JavaDoc container;
56
57     /**
58      * Encoded PK. If CMP2 is not used, no encoding is done.
59      */

60     private Serializable JavaDoc pk;
61
62     /**
63      * Constructor
64      */

65     public JTimerHandle(long starttime, long period, Serializable JavaDoc info, String JavaDoc beanname, String JavaDoc container, Serializable JavaDoc 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     /**
78      * @return a reference to the timer represented by this handle.
79      * @throws java.lang.IllegalStateException If this method is invoked while the instance
80      * is in a state that does not allow access to this method.
81      * @throws NoSuchObjectLocalException If invoked on a handle whose associated timer
82      * has expired or has been cancelled.
83      * @throws EJBException If this method could not complete due to a system-level failure.
84      */

85     public Timer JavaDoc getTimer() throws IllegalStateException JavaDoc, NoSuchObjectLocalException JavaDoc, EJBException JavaDoc {
86
87         // Must reject Timer operations according to the EJB spec
88
// See EJB 2.1 page 87 (Stateful session beans)
89
try {
90             ContainerNaming naming = NamingManager.getInstance();
91             Context JavaDoc ctx = naming.getComponentContext();
92             JContext sc = (JContext) ctx.lookup("MY_SF_CONTEXT");
93             if (sc.getState() != 2) {
94                 throw new IllegalStateException JavaDoc("This operation is not allowed here");
95             }
96         } catch (NamingException JavaDoc ne) {
97             // No context registered. Assume it's OK.
98
// It's the case for Entity or MDB.
99
}
100
101         EJBServiceImpl ejbserv = null;
102         try {
103             ejbserv = (EJBServiceImpl) ServiceManager.getInstance().getEjbService();
104         } catch (Exception JavaDoc e) {
105             throw new IllegalStateException JavaDoc("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 JavaDoc("Cannot get container");
111         }
112         JFactory bf = (JFactory) cont.getBeanFactory(beanname);
113         JTimerService timerservice = null;
114         if (bf instanceof JEntityFactory) {
115             // entity bean
116
JEntityFactory ef = (JEntityFactory) bf;
117             Serializable JavaDoc 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 JavaDoc("No entity for this pk");
125             }
126             timerservice = (JTimerService) es.getEntityTimerService();
127         } else {
128             // stateless session or message driven bean
129
timerservice = (JTimerService) bf.getTimerService();
130         }
131         if (timerservice == null) {
132             throw new IllegalStateException JavaDoc("Cannot retrieve TimerService");
133         }
134         // Get the Timer from the list. If not found, it may have been cancelled.
135
Timer JavaDoc ret = timerservice.getTimer(starttime, period, info);
136         if (ret == null) {
137             throw new NoSuchObjectLocalException JavaDoc("Timer does not exist any longer");
138         }
139         return ret;
140     }
141
142 }
143
Popular Tags