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: PaperEC.java 8462 2006-06-14 08:56:18Z coqp $
023: * --------------------------------------------------------------------------
024: */
025:
026: package org.objectweb.jonas.jtests.beans.folder;
027:
028: import javax.ejb.EJBException;
029: import javax.ejb.EJBLocalHome;
030: import javax.ejb.EntityBean;
031: import javax.ejb.EntityContext;
032: import javax.ejb.CreateException;
033: import javax.ejb.RemoveException;
034: import javax.naming.Context;
035: import javax.naming.InitialContext;
036: import javax.naming.NamingException;
037: import javax.rmi.PortableRemoteObject;
038:
039: import org.objectweb.jonas.common.Log;
040: import org.objectweb.util.monolog.api.Logger;
041: import org.objectweb.util.monolog.api.BasicLevel;
042:
043: /**
044: * Implementation for the bean PaperEC.
045: * @author Philippe Durieux, Philippe Coq
046: */
047: public class PaperEC implements EntityBean {
048:
049: static protected Logger logger = null;
050: EntityContext ejbContext;
051: InitialContext ictx;
052: Context myEnv;
053:
054: // ------------------------------------------------------------------
055: // State of the bean.
056: // They must be public for Container Managed Persistance.
057: // ------------------------------------------------------------------
058: public String name;
059: public int value;
060:
061: /**
062: * Check environment variables
063: */
064: void checkEnv(String method) {
065:
066: // Check directly in my context
067: logger.log(BasicLevel.DEBUG, "Check directly in my context");
068: try {
069: String value = (String) myEnv.lookup("myname");
070: if (!value.equals("myentity")) {
071: logger.log(BasicLevel.ERROR,
072: ": myEnv.lookup failed: myname=" + value);
073: throw new EJBException("FileEC 1: " + method);
074: }
075: } catch (NamingException e) {
076: logger.log(BasicLevel.ERROR,
077: ": myEnv.lookup raised exception:\n" + e);
078: throw new EJBException("FileEC 2: " + method);
079: }
080: // Check from initial Context
081: logger.log(BasicLevel.DEBUG, "Check from initial Context");
082: try {
083: String value = (String) ictx.lookup("java:comp/env/myname");
084: if (!value.equals("myentity")) {
085: logger.log(BasicLevel.ERROR,
086: ": ictx.lookup failed: myname=" + value);
087: throw new EJBException("FileEC 6: " + method);
088: }
089: } catch (NamingException e) {
090: logger.log(BasicLevel.ERROR,
091: ": ictx.lookup raised exception:\n" + e);
092: throw new EJBException("FileEC 7: " + method);
093: }
094: logger.log(BasicLevel.DEBUG, ": checkEnv OK");
095: }
096:
097: // ------------------------------------------------------------------
098: // EntityBean implementation
099: // ------------------------------------------------------------------
100:
101: public void setEntityContext(EntityContext ctx) {
102: if (logger == null) {
103: logger = Log.getLogger(Log.JONAS_TESTS_PREFIX);
104: }
105: logger.log(BasicLevel.DEBUG, "");
106: ejbContext = ctx;
107: try {
108: // Get initial Context
109: ictx = new InitialContext();
110: myEnv = (Context) ictx.lookup("java:comp/env");
111: } catch (NamingException e) {
112: throw new EJBException("PaperEC: Cannot get filehome:" + e);
113: }
114: checkEnv("setEntityContext");
115:
116: // Check that we can do "getEJBLocalHome"
117: EJBLocalHome homel = ctx.getEJBLocalHome();
118: if (homel == null) {
119: throw new EJBException(
120: "PaperEC: setEntityContext cannot get EJBLocalHome");
121: }
122: }
123:
124: public void unsetEntityContext() {
125: logger.log(BasicLevel.DEBUG, "");
126: ejbContext = null;
127: }
128:
129: public void ejbActivate() {
130: logger.log(BasicLevel.DEBUG, "");
131: }
132:
133: public void ejbPassivate() {
134: logger.log(BasicLevel.DEBUG, "");
135: }
136:
137: public void ejbLoad() {
138: logger.log(BasicLevel.DEBUG, "");
139: }
140:
141: public void ejbStore() {
142: logger.log(BasicLevel.DEBUG, "");
143: }
144:
145: public void ejbRemove() throws RemoveException {
146: logger.log(BasicLevel.DEBUG, "");
147: }
148:
149: // ------------------------------------------------------------------
150: // ejbCreate methods
151: // ------------------------------------------------------------------
152:
153: public String ejbCreate(String name) throws CreateException {
154: logger.log(BasicLevel.DEBUG, "");
155: this .name = name;
156: this .value = 0;
157: return null; // In CMP, should return null.
158: }
159:
160: public String ejbPostCreate(String name) throws CreateException {
161: logger.log(BasicLevel.DEBUG, "");
162: return null; // In CMP, should return null.
163: }
164:
165: // Not used for PaperEC only for PaperEC2
166: public int ejbHomeGetTotalValeur() throws javax.ejb.FinderException {
167: return 0;
168: }
169:
170: // ------------------------------------------------------------------
171: // PaperLocal implementation
172: // ------------------------------------------------------------------
173:
174: public String getName() {
175: logger.log(BasicLevel.DEBUG, "");
176: return this .name;
177: }
178:
179: public int getValue() {
180: logger.log(BasicLevel.DEBUG, "");
181: return this .value;
182: }
183:
184: public void setValue(int v) {
185: logger.log(BasicLevel.DEBUG, "");
186: this.value = v;
187: }
188: }
|