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: P2EC2.java 5995 2004-12-17 15:08:36Z joaninh $
023: * --------------------------------------------------------------------------
024: */
025:
026: package org.objectweb.jonas.jtests.beans.relation.dass;
027:
028: import javax.ejb.CreateException;
029: import javax.ejb.DuplicateKeyException;
030: import javax.ejb.EJBException;
031: import javax.ejb.EntityContext;
032: import javax.ejb.FinderException;
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.BasicLevel;
041: import org.objectweb.util.monolog.api.Logger;
042:
043: /**
044: * @author Ph Durieux
045: */
046: public abstract class P2EC2 implements javax.ejb.EntityBean {
047:
048: private P1HomeLocal p1home = null;
049:
050: // ------------------------------------------------------------------
051: // Get and Set accessor methods of the bean's abstract schema
052: // ------------------------------------------------------------------
053: public abstract String getId();
054:
055: public abstract void setId(String id);
056:
057: public abstract P1Local getP1();
058:
059: public abstract void setP1(P1Local p1);
060:
061: // ------------------------------------------------------------------
062: // EntityBean implementation
063: // ------------------------------------------------------------------
064:
065: static protected Logger logger = null;
066: EntityContext ejbContext;
067:
068: /**
069: * The Entity bean can define 0 or more ejbCreate methods.
070: *
071: * @throws CreateException Failure to create an entity EJB object.
072: * @throws DuplicateKeyException An object with the same key already exists.
073: */
074: public String ejbCreate(String id) throws CreateException,
075: DuplicateKeyException {
076: logger.log(BasicLevel.DEBUG, "");
077:
078: // Init here the bean fields
079: setId(id);
080:
081: // In CMP, should return null.
082: return null;
083: }
084:
085: /**
086: * There must be an ejbPostCreate par ejbCreate method
087: * @throws CreateException Failure to create an entity EJB object.
088: */
089: public void ejbPostCreate(String id) throws CreateException {
090: logger.log(BasicLevel.DEBUG, "id=" + id);
091: try {
092: P1Local p1l = p1home.findByPrimaryKey(id);
093: setP1(p1l);
094: } catch (FinderException e) {
095: throw new CreateException(
096: "P2 cannot exist without matching P1");
097: }
098: }
099:
100: /**
101: * Set the associated entity context. The container invokes this method
102: * on an instance after the instance has been created.
103: * This method is called in an unspecified transaction context.
104: *
105: * @param ctx - An EntityContext interface for the instance. The instance
106: * should store the reference to the context in an instance variable.
107: * @throws EJBException Thrown by the method to indicate a failure caused by a
108: * system-level error.
109: */
110: public void setEntityContext(EntityContext ctx) {
111: if (logger == null)
112: logger = Log.getLogger(Log.JONAS_TESTS_PREFIX);
113: logger.log(BasicLevel.DEBUG, "");
114: ejbContext = ctx;
115: try {
116: Context ictx = new InitialContext();
117: p1home = (P1HomeLocal) ictx.lookup("java:comp/env/ejb/p1");
118: } catch (NamingException e) {
119: throw new EJBException("Impossible to get HomeLocal:", e);
120: }
121: }
122:
123: /**
124: * Unset the associated entity context. The container calls this method
125: * before removing the instance.
126: * This is the last method that the container invokes on the instance.
127: * The Java garbage collector will eventually invoke the finalize() method
128: * on the instance.
129: * This method is called in an unspecified transaction context.
130: *
131: * @throws EJBException Thrown by the method to indicate a failure caused by a
132: * system-level error.
133: */
134: public void unsetEntityContext() {
135: logger.log(BasicLevel.DEBUG, "");
136: ejbContext = null;
137: }
138:
139: /**
140: * A container invokes this method before it removes the EJB object
141: * that is currently associated with the instance. This method is
142: * invoked when a client invokes a remove operation on the enterprise Bean's
143: * home interface or the EJB object's remote interface. This method
144: * transitions the instance from the ready state to the pool of available
145: * instances.
146: *
147: * This method is called in the transaction context of the remove operation.
148: * @throws RemoveException The enterprise Bean does not allow destruction of the object.
149: * @throws EJBException - Thrown by the method to indicate a failure caused by a system-level
150: * error.
151: */
152: public void ejbRemove() throws RemoveException {
153: logger.log(BasicLevel.DEBUG, "");
154: }
155:
156: /**
157: * A container invokes this method to instruct the instance to synchronize
158: * its state by loading it state from the underlying database.
159: * This method always executes in the proper transaction context.
160: *
161: * @throws EJBException Thrown by the method to indicate a failure caused by
162: * a system-level error.
163: */
164: public void ejbLoad() {
165: logger.log(BasicLevel.DEBUG, "");
166: }
167:
168: /**
169: * A container invokes this method to instruct the instance to synchronize
170: * its state by storing it to the underlying database.
171: * This method always executes in the proper transaction context.
172: *
173: * @throws EJBException Thrown by the method to indicate a failure caused by
174: * a system-level error.
175: */
176: public void ejbStore() {
177: logger.log(BasicLevel.DEBUG, "");
178: }
179:
180: /**
181: * A container invokes this method on an instance before the instance
182: * becomes disassociated with a specific EJB object.
183: */
184: public void ejbPassivate() {
185: logger.log(BasicLevel.DEBUG, "");
186: }
187:
188: /**
189: * A container invokes this method when the instance is taken out of
190: * the pool of available instances to become associated with a specific
191: * EJB object.
192: */
193: public void ejbActivate() {
194: logger.log(BasicLevel.DEBUG, "");
195: }
196:
197: // ---------------------------------------------------------------------
198: // P1Remote Implementation
199: // ---------------------------------------------------------------------
200:
201: /**
202: *
203: */
204: public String getP1Value() {
205: P1Local p1l = getP1();
206: return p1l.getPf1();
207: }
208: }
|