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 5996 2004-12-17 15:09:45Z joaninh $
023: * --------------------------------------------------------------------------
024: */
025:
026: package org.objectweb.jonas.jtests.beans.ffolder;
027:
028: import java.rmi.RemoteException;
029: import javax.ejb.EJBException;
030: import javax.ejb.EJBHome;
031: import javax.ejb.EJBLocalHome;
032: import javax.ejb.EJBMetaData;
033: import javax.ejb.EntityBean;
034: import javax.ejb.EntityContext;
035: import javax.ejb.FinderException;
036: import javax.ejb.RemoveException;
037: import javax.ejb.CreateException;
038: import javax.naming.Context;
039: import javax.naming.InitialContext;
040: import javax.naming.NamingException;
041: import javax.rmi.PortableRemoteObject;
042:
043: /**
044: * Implementation for the bean FileEC.
045: * @author Philippe Durieux
046: */
047: public class FileEC implements EntityBean {
048:
049: // protected static Logger logger = null;
050: EntityContext ejbContext;
051: InitialContext ictx;
052: Context myEnv;
053: PaperLocalHome phome;
054: PaperLocal p1;
055: PaperLocal p2;
056:
057: // ------------------------------------------------------------------
058: // State of the bean.
059: // They must be public for Container Managed Persistance.
060: // ------------------------------------------------------------------
061: public String name;
062: public String p1pk;
063: public String p2pk;
064: public int count;
065:
066: /**
067: * Check environment variables
068: */
069: void checkEnv(String method) {
070:
071: // Check directly in my context
072: //logger.log(BasicLevel.DEBUG, "Check directly in my context");
073: try {
074: String value = (String) myEnv.lookup("myname");
075: if (!value.equals("myentity")) {
076: //logger.log(BasicLevel.ERROR, ": myEnv.lookup failed: myname=" + value);
077: throw new EJBException("FileEC 1: " + method);
078: }
079: } catch (NamingException e) {
080: // logger.log(BasicLevel.ERROR, ": myEnv.lookup raised exception:\n" + e);
081: throw new EJBException("FileEC 2: " + method);
082: }
083: // Check from initial Context
084: //logger.log(BasicLevel.DEBUG, "Check from initial Context");
085: try {
086: String value = (String) ictx.lookup("java:comp/env/myname");
087: if (!value.equals("myentity")) {
088: //logger.log(BasicLevel.ERROR, ": ictx.lookup failed: myname=" + value);
089: throw new EJBException("FileEC 6: " + method);
090: }
091: } catch (NamingException e) {
092: //logger.log(BasicLevel.ERROR, ": ictx.lookup raised exception:\n" + e);
093: throw new EJBException("FileEC 7: " + method);
094: }
095: //logger.log(BasicLevel.DEBUG, ": checkEnv OK");
096: }
097:
098: // ------------------------------------------------------------------
099: // EntityBean implementation
100: // ------------------------------------------------------------------
101:
102: /**
103: * Called by the container after the instance has been created.
104: */
105: public void setEntityContext(EntityContext ctx) {
106: //if (logger == null) {
107: //logger = Log.getLogger(Log.JONAS_TESTS_PREFIX);
108: // }
109: //logger.log(BasicLevel.DEBUG, "");
110: ejbContext = ctx;
111: try {
112: // Get initial Context
113: ictx = new InitialContext();
114: myEnv = (Context) ictx.lookup("java:comp/env");
115: } catch (NamingException e) {
116: throw new EJBException("FileEC: Cannot get filehome:" + e);
117: }
118: checkEnv("setEntityContext");
119:
120: // Check that we can do "getEJBHome"
121: EJBHome home = ctx.getEJBHome();
122: if (home == null) {
123: throw new EJBException(
124: "FileEC: setEntityContext cannot get EJBHome");
125: }
126: // Check that we can do "getEJBLocalHome"
127: EJBLocalHome homel = ctx.getEJBLocalHome();
128: if (homel == null) {
129: throw new EJBException(
130: "FileEC: setEntityContext cannot get EJBLocalHome");
131: }
132: // Check that we can do "getEJBMetaData"
133: try {
134: EJBMetaData md = home.getEJBMetaData();
135: } catch (RemoteException e) {
136: throw new EJBException(
137: "FileEC: setEntityContext cannot get EJBMetaData");
138: }
139: }
140:
141: public void unsetEntityContext() {
142: //logger.log(BasicLevel.DEBUG, "");
143: ejbContext = null;
144: }
145:
146: public void ejbActivate() {
147: //logger.log(BasicLevel.DEBUG, "");
148: try {
149: stateUpdate();
150: } catch (NamingException e) {
151: //logger.log(BasicLevel.ERROR, "FileEC ejbActivate raised exception " + e);
152: throw new EJBException("Error in ejbActivate:" + e);
153: }
154: }
155:
156: public void ejbPassivate() {
157: //logger.log(BasicLevel.DEBUG, "");
158: // raz values to verify that activation is ok.
159: phome = null;
160: p1 = null;
161: p2 = null;
162: }
163:
164: /**
165: * Persistent state has been loaded just before this method is invoked
166: * by the container.
167: * Must reinit here non persistent data.
168: */
169: public void ejbLoad() {
170: //logger.log(BasicLevel.DEBUG, "");
171: p1 = null;
172: if (p1pk.length() > 0) {
173: try {
174: p1 = phome.findByPrimaryKey(p1pk);
175: } catch (FinderException e) {
176: }
177: }
178: p2 = null;
179: if (p2pk.length() > 0) {
180: try {
181: p2 = phome.findByPrimaryKey(p2pk);
182: } catch (FinderException e) {
183: }
184: }
185: }
186:
187: public void ejbStore() {
188: //logger.log(BasicLevel.DEBUG, "");
189: }
190:
191: public void ejbRemove() throws RemoveException {
192: //logger.log(BasicLevel.DEBUG, "");
193: }
194:
195: // ------------------------------------------------------------------
196: // ejbCreate methods
197: // ------------------------------------------------------------------
198:
199: public String ejbCreate(String name) throws CreateException {
200: //logger.log(BasicLevel.DEBUG, "");
201: this .name = name;
202: return null; // In CMP, should return null.
203: }
204:
205: public String ejbPostCreate(String name) throws CreateException {
206: //logger.log(BasicLevel.DEBUG, "");
207: try {
208: stateUpdate();
209: } catch (NamingException e) {
210: //logger.log(BasicLevel.ERROR, "FileEC ejbPostCreate raised exception " + e);
211: throw new CreateException("Error in ejbPostCreate:" + e);
212: }
213: return null; // In CMP, should return null.
214: }
215:
216: // ------------------------------------------------------------------
217: // File / FileLocal implementation
218: // ------------------------------------------------------------------
219:
220: public int getP1Value() {
221: //logger.log(BasicLevel.DEBUG, "");
222: int ret = p1.getValue();
223: return ret;
224: }
225:
226: public int getP2Value() {
227: //logger.log(BasicLevel.DEBUG, "");
228: int ret = p2.getValue();
229: return ret;
230: }
231:
232: public String getName() {
233: //logger.log(BasicLevel.DEBUG, "");
234: return this .name;
235: }
236:
237: public int getCount() {
238: //logger.log(BasicLevel.DEBUG, "");
239: return this .count;
240: }
241:
242: public PaperLocal getP1() {
243: //logger.log(BasicLevel.DEBUG, "");
244: return p1;
245: }
246:
247: public PaperLocal getP2() {
248: //logger.log(BasicLevel.DEBUG, "");
249: return p2;
250: }
251:
252: // ------------------------------------------------------------------
253: // private methods
254: // ------------------------------------------------------------------
255:
256: /**
257: * init non persistent bean data.
258: * This should be called when instance is created or activated.
259: */
260: private void stateUpdate() throws NamingException {
261: // lookup paperhome in JNDI
262: phome = (PaperLocalHome) ictx.lookup("java:comp/env/ejb/paper"));
263: }
264: }
|