001: /*
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: FileEC.java 5995 2004-12-17 15:08:36Z joaninh $
023: * --------------------------------------------------------------------------
024: */
025:
026: package org.objectweb.jonas.jtests.beans.folder;
027:
028: import java.io.Serializable;
029: import java.rmi.RemoteException;
030: import javax.ejb.EJBException;
031: import javax.ejb.EJBHome;
032: import javax.ejb.EJBLocalHome;
033: import javax.ejb.EJBMetaData;
034: import javax.ejb.EntityBean;
035: import javax.ejb.EntityContext;
036: import javax.ejb.FinderException;
037: import javax.ejb.RemoveException;
038: import javax.ejb.CreateException;
039: import javax.ejb.TimedObject;
040: import javax.ejb.Timer;
041: import javax.ejb.TimerHandle;
042: import javax.ejb.TimerService;
043: import javax.naming.Context;
044: import javax.naming.InitialContext;
045: import javax.naming.NamingException;
046: import javax.rmi.PortableRemoteObject;
047:
048: import org.objectweb.jonas.common.Log;
049: import org.objectweb.util.monolog.api.Logger;
050: import org.objectweb.util.monolog.api.BasicLevel;
051:
052: /**
053: * Implementation for the bean FileEC.
054: * @author Philippe Durieux
055: */
056: public class FileEC implements EntityBean, TimedObject {
057:
058: protected static Logger logger = null;
059: EntityContext ejbContext;
060: InitialContext ictx;
061: Context myEnv;
062: PaperLocalHome phome;
063: PaperLocal p1;
064: PaperLocal p2;
065:
066: // ------------------------------------------------------------------
067: // State of the bean.
068: // They must be public for Container Managed Persistance.
069: // ------------------------------------------------------------------
070: public String name;
071: public String p1pk;
072: public String p2pk;
073: public int count;
074:
075: /**
076: * Check environment variables
077: */
078: void checkEnv(String method) {
079:
080: // Check directly in my context
081: logger.log(BasicLevel.DEBUG, "Check directly in my context");
082: try {
083: String value = (String) myEnv.lookup("myname");
084: if (!value.equals("myentity")) {
085: logger.log(BasicLevel.ERROR,
086: ": myEnv.lookup failed: myname=" + value);
087: throw new EJBException("FileEC 1: " + method);
088: }
089: } catch (NamingException e) {
090: logger.log(BasicLevel.ERROR,
091: ": myEnv.lookup raised exception:\n" + e);
092: throw new EJBException("FileEC 2: " + method);
093: }
094: // Check from initial Context
095: logger.log(BasicLevel.DEBUG, "Check from initial Context");
096: try {
097: String value = (String) ictx.lookup("java:comp/env/myname");
098: if (!value.equals("myentity")) {
099: logger.log(BasicLevel.ERROR,
100: ": ictx.lookup failed: myname=" + value);
101: throw new EJBException("FileEC 6: " + method);
102: }
103: } catch (NamingException e) {
104: logger.log(BasicLevel.ERROR,
105: ": ictx.lookup raised exception:\n" + e);
106: throw new EJBException("FileEC 7: " + method);
107: }
108: logger.log(BasicLevel.DEBUG, ": checkEnv OK");
109: }
110:
111: // ------------------------------------------------------------------
112: // TimedObject implementation
113: // ------------------------------------------------------------------
114:
115: /*
116: * @param timer the Timer object
117: * @see javax.ejb.TimedObject#ejbTimeout(javax.ejb.Timer)
118: */
119: public void ejbTimeout(Timer timer) {
120: logger.log(BasicLevel.DEBUG, "");
121: Serializable sz = timer.getInfo();
122: if (!(sz instanceof Integer)) {
123: logger.log(BasicLevel.ERROR, "Bad Info");
124: return;
125: }
126: int action = ((Integer) sz).intValue();
127: boolean ok = true;
128: }
129:
130: // ------------------------------------------------------------------
131: // EntityBean implementation
132: // ------------------------------------------------------------------
133:
134: /**
135: * Called by the container after the instance has been created.
136: */
137: public void setEntityContext(EntityContext ctx) {
138: if (logger == null) {
139: logger = Log.getLogger(Log.JONAS_TESTS_PREFIX);
140: }
141: logger.log(BasicLevel.DEBUG, "");
142: ejbContext = ctx;
143: try {
144: // Get initial Context
145: ictx = new InitialContext();
146: myEnv = (Context) ictx.lookup("java:comp/env");
147: } catch (NamingException e) {
148: throw new EJBException("FileEC: Cannot get filehome:" + e);
149: }
150: checkEnv("setEntityContext");
151:
152: // Check that we can do "getEJBHome"
153: EJBHome home = ctx.getEJBHome();
154: if (home == null) {
155: throw new EJBException(
156: "FileEC: setEntityContext cannot get EJBHome");
157: }
158: // Check that we can do "getEJBLocalHome"
159: EJBLocalHome homel = ctx.getEJBLocalHome();
160: if (homel == null) {
161: throw new EJBException(
162: "FileEC: setEntityContext cannot get EJBLocalHome");
163: }
164: // Check that we can do "getEJBMetaData"
165: try {
166: EJBMetaData md = home.getEJBMetaData();
167: } catch (RemoteException e) {
168: throw new EJBException(
169: "FileEC: setEntityContext cannot get EJBMetaData");
170: }
171: }
172:
173: public void unsetEntityContext() {
174: logger.log(BasicLevel.DEBUG, "");
175: ejbContext = null;
176: }
177:
178: public void ejbActivate() {
179: logger.log(BasicLevel.DEBUG, "");
180: try {
181: stateUpdate();
182: } catch (NamingException e) {
183: logger.log(BasicLevel.ERROR,
184: "FileEC ejbActivate raised exception " + e);
185: throw new EJBException("Error in ejbActivate:" + e);
186: }
187: }
188:
189: public void ejbPassivate() {
190: logger.log(BasicLevel.DEBUG, "");
191: // raz values to verify that activation is ok.
192: phome = null;
193: p1 = null;
194: p2 = null;
195: }
196:
197: /**
198: * Persistent state has been loaded just before this method is invoked
199: * by the container.
200: * Must reinit here non persistent data.
201: */
202: public void ejbLoad() {
203: logger.log(BasicLevel.DEBUG, "");
204: p1 = null;
205: if (p1pk.length() > 0) {
206: try {
207: p1 = phome.findByPrimaryKey(p1pk);
208: } catch (FinderException e) {
209: }
210: }
211: p2 = null;
212: if (p2pk.length() > 0) {
213: try {
214: p2 = phome.findByPrimaryKey(p2pk);
215: } catch (FinderException e) {
216: }
217: }
218: }
219:
220: public void ejbStore() {
221: logger.log(BasicLevel.DEBUG, "");
222: }
223:
224: public void ejbRemove() throws RemoveException {
225: logger.log(BasicLevel.DEBUG, "");
226: }
227:
228: // ------------------------------------------------------------------
229: // ejbCreate methods
230: // ------------------------------------------------------------------
231:
232: public String ejbCreate(String name) throws CreateException {
233: logger.log(BasicLevel.DEBUG, "");
234: this .name = name;
235: return null; // In CMP, should return null.
236: }
237:
238: public String ejbPostCreate(String name) throws CreateException {
239: logger.log(BasicLevel.DEBUG, "");
240: try {
241: stateUpdate();
242: } catch (NamingException e) {
243: logger.log(BasicLevel.ERROR,
244: "FileEC ejbPostCreate raised exception " + e);
245: throw new CreateException("Error in ejbPostCreate:" + e);
246: }
247: return null; // In CMP, should return null.
248: }
249:
250: // ------------------------------------------------------------------
251: // File / FileLocal implementation
252: // ------------------------------------------------------------------
253:
254: public TimerHandle getTimerHandle() throws RemoteException {
255: logger.log(BasicLevel.DEBUG, "");
256: TimerService timerservice = ejbContext.getTimerService();
257: Timer mt = timerservice.createTimer(300 * 1000, new Integer(1));
258: TimerHandle th = mt.getHandle();
259: return th;
260: }
261:
262: public void cancelTimer(TimerHandle th) throws RemoteException {
263: logger.log(BasicLevel.DEBUG, "");
264: Timer mt = th.getTimer();
265: mt.cancel();
266: }
267:
268: public int getP1Value() {
269: logger.log(BasicLevel.DEBUG, "");
270: int ret = p1.getValue();
271: return ret;
272: }
273:
274: public int getP2Value() {
275: logger.log(BasicLevel.DEBUG, "");
276: int ret = p2.getValue();
277: return ret;
278: }
279:
280: public String getName() {
281: logger.log(BasicLevel.DEBUG, "");
282: return this .name;
283: }
284:
285: public int getCount() {
286: logger.log(BasicLevel.DEBUG, "");
287: return this .count;
288: }
289:
290: public PaperLocal getP1() {
291: logger.log(BasicLevel.DEBUG, "");
292: return p1;
293: }
294:
295: public PaperLocal getP2() {
296: logger.log(BasicLevel.DEBUG, "");
297: return p2;
298: }
299:
300: // ------------------------------------------------------------------
301: // private methods
302: // ------------------------------------------------------------------
303:
304: /**
305: * init non persistent bean data.
306: * This should be called when instance is created or activated.
307: */
308: private void stateUpdate() throws NamingException {
309: // lookup paperhome in JNDI
310: phome = (PaperLocalHome) ictx.lookup("java:comp/env/ejb/paper");
311: }
312:
313: }
|