KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jtests > beans > folder > FileEC


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: FileEC.java,v 1.7 2004/12/17 15:08:35 joaninh Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.jtests.beans.folder;
27
28 import java.io.Serializable JavaDoc;
29 import java.rmi.RemoteException JavaDoc;
30 import javax.ejb.EJBException JavaDoc;
31 import javax.ejb.EJBHome JavaDoc;
32 import javax.ejb.EJBLocalHome JavaDoc;
33 import javax.ejb.EJBMetaData JavaDoc;
34 import javax.ejb.EntityBean JavaDoc;
35 import javax.ejb.EntityContext JavaDoc;
36 import javax.ejb.FinderException JavaDoc;
37 import javax.ejb.RemoveException JavaDoc;
38 import javax.ejb.CreateException JavaDoc;
39 import javax.ejb.TimedObject JavaDoc;
40 import javax.ejb.Timer JavaDoc;
41 import javax.ejb.TimerHandle JavaDoc;
42 import javax.ejb.TimerService JavaDoc;
43 import javax.naming.Context JavaDoc;
44 import javax.naming.InitialContext JavaDoc;
45 import javax.naming.NamingException JavaDoc;
46 import javax.rmi.PortableRemoteObject JavaDoc;
47
48 import org.objectweb.jonas.common.Log;
49 import org.objectweb.util.monolog.api.Logger;
50 import org.objectweb.util.monolog.api.BasicLevel;
51
52 /**
53  * Implementation for the bean FileEC.
54  * @author Philippe Durieux
55  */

56 public class FileEC implements EntityBean JavaDoc, TimedObject JavaDoc {
57
58     protected static Logger logger = null;
59     EntityContext JavaDoc ejbContext;
60     InitialContext JavaDoc ictx;
61     Context JavaDoc myEnv;
62     PaperLocalHome phome;
63     PaperLocal p1;
64     PaperLocal p2;
65
66     // ------------------------------------------------------------------
67
// State of the bean.
68
// They must be public for Container Managed Persistance.
69
// ------------------------------------------------------------------
70
public String JavaDoc name;
71     public String JavaDoc p1pk;
72     public String JavaDoc p2pk;
73     public int count;
74
75     /**
76      * Check environment variables
77      */

78     void checkEnv(String JavaDoc method) {
79
80         // Check directly in my context
81
logger.log(BasicLevel.DEBUG, "Check directly in my context");
82         try {
83             String JavaDoc value = (String JavaDoc) myEnv.lookup("myname");
84             if (!value.equals("myentity")) {
85                 logger.log(BasicLevel.ERROR, ": myEnv.lookup failed: myname=" + value);
86                 throw new EJBException JavaDoc("FileEC 1: " + method);
87             }
88         } catch (NamingException JavaDoc e) {
89             logger.log(BasicLevel.ERROR, ": myEnv.lookup raised exception:\n" + e);
90             throw new EJBException JavaDoc("FileEC 2: " + method);
91         }
92         // Check from initial Context
93
logger.log(BasicLevel.DEBUG, "Check from initial Context");
94         try {
95             String JavaDoc value = (String JavaDoc) ictx.lookup("java:comp/env/myname");
96             if (!value.equals("myentity")) {
97                 logger.log(BasicLevel.ERROR, ": ictx.lookup failed: myname=" + value);
98                 throw new EJBException JavaDoc("FileEC 6: " + method);
99             }
100         } catch (NamingException JavaDoc e) {
101             logger.log(BasicLevel.ERROR, ": ictx.lookup raised exception:\n" + e);
102             throw new EJBException JavaDoc("FileEC 7: " + method);
103         }
104         logger.log(BasicLevel.DEBUG, ": checkEnv OK");
105     }
106
107     // ------------------------------------------------------------------
108
// TimedObject implementation
109
// ------------------------------------------------------------------
110

111     /*
112      * @param timer the Timer object
113      * @see javax.ejb.TimedObject#ejbTimeout(javax.ejb.Timer)
114      */

115     public void ejbTimeout(Timer JavaDoc timer) {
116         logger.log(BasicLevel.DEBUG, "");
117         Serializable JavaDoc sz = timer.getInfo();
118         if (!(sz instanceof Integer JavaDoc)) {
119             logger.log(BasicLevel.ERROR, "Bad Info");
120             return;
121         }
122         int action = ((Integer JavaDoc)sz).intValue();
123         boolean ok = true;
124     }
125     
126     // ------------------------------------------------------------------
127
// EntityBean implementation
128
// ------------------------------------------------------------------
129

130     /**
131      * Called by the container after the instance has been created.
132      */

133     public void setEntityContext(EntityContext JavaDoc ctx) {
134         if (logger == null) {
135             logger = Log.getLogger(Log.JONAS_TESTS_PREFIX);
136         }
137         logger.log(BasicLevel.DEBUG, "");
138         ejbContext = ctx;
139         try {
140             // Get initial Context
141
ictx = new InitialContext JavaDoc();
142             myEnv = (Context JavaDoc) ictx.lookup("java:comp/env");
143         } catch (NamingException JavaDoc e) {
144             throw new EJBException JavaDoc("FileEC: Cannot get filehome:" + e);
145         }
146         checkEnv("setEntityContext");
147
148         // Check that we can do "getEJBHome"
149
EJBHome JavaDoc home = ctx.getEJBHome();
150         if (home == null) {
151             throw new EJBException JavaDoc("FileEC: setEntityContext cannot get EJBHome");
152         }
153         // Check that we can do "getEJBLocalHome"
154
EJBLocalHome JavaDoc homel = ctx.getEJBLocalHome();
155         if (homel == null) {
156             throw new EJBException JavaDoc("FileEC: setEntityContext cannot get EJBLocalHome");
157         }
158         // Check that we can do "getEJBMetaData"
159
try {
160             EJBMetaData JavaDoc md = home.getEJBMetaData();
161         } catch (RemoteException JavaDoc e) {
162             throw new EJBException JavaDoc("FileEC: setEntityContext cannot get EJBMetaData");
163         }
164     }
165
166     public void unsetEntityContext() {
167         logger.log(BasicLevel.DEBUG, "");
168         ejbContext = null;
169     }
170
171     public void ejbActivate() {
172         logger.log(BasicLevel.DEBUG, "");
173         try {
174             stateUpdate();
175         } catch (NamingException JavaDoc e) {
176             logger.log(BasicLevel.ERROR, "FileEC ejbActivate raised exception " + e);
177             throw new EJBException JavaDoc("Error in ejbActivate:" + e);
178         }
179     }
180
181     public void ejbPassivate() {
182         logger.log(BasicLevel.DEBUG, "");
183         // raz values to verify that activation is ok.
184
phome = null;
185         p1 = null;
186         p2 = null;
187     }
188
189     /**
190      * Persistent state has been loaded just before this method is invoked
191      * by the container.
192      * Must reinit here non persistent data.
193      */

194     public void ejbLoad() {
195         logger.log(BasicLevel.DEBUG, "");
196         p1 = null;
197         if (p1pk.length() > 0) {
198             try {
199                 p1 = phome.findByPrimaryKey(p1pk);
200             } catch (FinderException JavaDoc e) {
201             }
202         }
203         p2 = null;
204         if (p2pk.length() > 0) {
205             try {
206                 p2 = phome.findByPrimaryKey(p2pk);
207             } catch (FinderException JavaDoc e) {
208             }
209         }
210     }
211
212     public void ejbStore() {
213         logger.log(BasicLevel.DEBUG, "");
214     }
215   
216     public void ejbRemove() throws RemoveException JavaDoc {
217         logger.log(BasicLevel.DEBUG, "");
218     }
219
220     // ------------------------------------------------------------------
221
// ejbCreate methods
222
// ------------------------------------------------------------------
223

224     public String JavaDoc ejbCreate(String JavaDoc name) throws CreateException JavaDoc {
225         logger.log(BasicLevel.DEBUG, "");
226         this.name = name;
227         return null; // In CMP, should return null.
228
}
229
230     public String JavaDoc ejbPostCreate(String JavaDoc name) throws CreateException JavaDoc {
231         logger.log(BasicLevel.DEBUG, "");
232         try {
233             stateUpdate();
234         } catch (NamingException JavaDoc e) {
235             logger.log(BasicLevel.ERROR, "FileEC ejbPostCreate raised exception " + e);
236             throw new CreateException JavaDoc("Error in ejbPostCreate:" + e);
237         }
238         return null; // In CMP, should return null.
239
}
240
241     // ------------------------------------------------------------------
242
// File / FileLocal implementation
243
// ------------------------------------------------------------------
244

245     public TimerHandle JavaDoc getTimerHandle() throws RemoteException JavaDoc {
246         logger.log(BasicLevel.DEBUG, "");
247         TimerService JavaDoc timerservice = ejbContext.getTimerService();
248         Timer JavaDoc mt = timerservice.createTimer(300 * 1000, new Integer JavaDoc(1));
249         TimerHandle JavaDoc th = mt.getHandle();
250         return th;
251     }
252
253     public void cancelTimer(TimerHandle JavaDoc th) throws RemoteException JavaDoc {
254         logger.log(BasicLevel.DEBUG, "");
255         Timer JavaDoc mt = th.getTimer();
256         mt.cancel();
257     }
258     
259     public int getP1Value() {
260         logger.log(BasicLevel.DEBUG, "");
261         int ret = p1.getValue();
262         return ret;
263     }
264
265     public int getP2Value() {
266         logger.log(BasicLevel.DEBUG, "");
267         int ret = p2.getValue();
268         return ret;
269     }
270
271     public String JavaDoc getName() {
272         logger.log(BasicLevel.DEBUG, "");
273         return this.name;
274     }
275
276     public int getCount() {
277         logger.log(BasicLevel.DEBUG, "");
278         return this.count;
279     }
280
281     public PaperLocal getP1() {
282         logger.log(BasicLevel.DEBUG, "");
283         return p1;
284     }
285
286     public PaperLocal getP2() {
287         logger.log(BasicLevel.DEBUG, "");
288         return p2;
289     }
290
291     // ------------------------------------------------------------------
292
// private methods
293
// ------------------------------------------------------------------
294

295     /**
296      * init non persistent bean data.
297      * This should be called when instance is created or activated.
298      */

299     private void stateUpdate() throws NamingException JavaDoc {
300         // lookup paperhome in JNDI
301
phome = (PaperLocalHome) ictx.lookup("java:comp/env/ejb/paper");
302     }
303
304 }
305
306
Popular Tags