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: MainEC2.java 5987 2004-12-17 11:05:12Z camillej $
023: * --------------------------------------------------------------------------
024: */
025:
026: package org.objectweb.jonas.jtests.beans.relation.remon;
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 MainEC2 implements javax.ejb.EntityBean {
047:
048: private AttributeHome attrhome = 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 String getPf1();
058:
059: public abstract void setPf1(String pf1);
060:
061: public abstract Attribute getAttr();
062:
063: public abstract void setAttr(Attribute a);
064:
065: // ------------------------------------------------------------------
066: // EntityBean implementation
067: // ------------------------------------------------------------------
068:
069: static protected Logger logger = null;
070: EntityContext ejbContext;
071:
072: /**
073: * Create a new instance with:
074: * @param id the primary key
075: * @param pf1 value cmp field
076: * @param att foreign key attribute
077: * @throws CreateException Failure to create an entity EJB object.
078: * @throws DuplicateKeyException An object with the same key already exists.
079: */
080: public String ejbCreate(String id, String pf1, String att)
081: throws CreateException, DuplicateKeyException {
082: logger.log(BasicLevel.DEBUG, "");
083:
084: // Init here the bean fields
085: setId(id);
086: setPf1(pf1);
087:
088: // In CMP, should return null.
089: return null;
090: }
091:
092: /**
093: * Make here the relations between ejbs
094: * @throws CreateException Failure to create the attribute
095: */
096: public void ejbPostCreate(String id, String pf1, String aname)
097: throws CreateException {
098: logger.log(BasicLevel.DEBUG, "id=" + id);
099: Attribute attr = attrhome.create(aname);
100: setAttr(attr);
101: }
102:
103: /**
104: * Set the associated entity context. The container invokes this method
105: * on an instance after the instance has been created.
106: * This method is called in an unspecified transaction context.
107: *
108: * @param ctx - An EntityContext interface for the instance. The instance
109: * should store the reference to the context in an instance variable.
110: * @throws EJBException Thrown by the method to indicate a failure caused by a
111: * system-level error.
112: */
113: public void setEntityContext(EntityContext ctx) {
114: if (logger == null)
115: logger = Log.getLogger(Log.JONAS_TESTS_PREFIX);
116: logger.log(BasicLevel.DEBUG, "");
117: ejbContext = ctx;
118: try {
119: Context ictx = new InitialContext();
120: attrhome = (AttributeHome) PortableRemoteObject.narrow(ictx
121: .lookup("java:comp/env/ejb/attribute"),
122: AttributeHome.class);
123: } catch (NamingException e) {
124: throw new EJBException("Impossible to get AttributeHome:",
125: e);
126: }
127: }
128:
129: /**
130: * Unset the associated entity context. The container calls this method
131: * before removing the instance.
132: * This is the last method that the container invokes on the instance.
133: * The Java garbage collector will eventually invoke the finalize() method
134: * on the instance.
135: * This method is called in an unspecified transaction context.
136: *
137: * @throws EJBException Thrown by the method to indicate a failure caused by a
138: * system-level error.
139: */
140: public void unsetEntityContext() {
141: logger.log(BasicLevel.DEBUG, "");
142: ejbContext = null;
143: }
144:
145: /**
146: * A container invokes this method before it removes the EJB object
147: * that is currently associated with the instance. This method is
148: * invoked when a client invokes a remove operation on the enterprise Bean's
149: * home interface or the EJB object's remote interface. This method
150: * transitions the instance from the ready state to the pool of available
151: * instances.
152: *
153: * This method is called in the transaction context of the remove operation.
154: * @throws RemoveException The enterprise Bean does not allow destruction of the object.
155: * @throws EJBException - Thrown by the method to indicate a failure caused by a system-level
156: * error.
157: */
158: public void ejbRemove() throws RemoveException {
159: logger.log(BasicLevel.DEBUG, "");
160: getAttr().remove();
161: }
162:
163: /**
164: * A container invokes this method to instruct the instance to synchronize
165: * its state by loading it state from the underlying database.
166: * This method always executes in the proper transaction context.
167: *
168: * @throws EJBException Thrown by the method to indicate a failure caused by
169: * a system-level error.
170: */
171: public void ejbLoad() {
172: logger.log(BasicLevel.DEBUG, "");
173: }
174:
175: /**
176: * A container invokes this method to instruct the instance to synchronize
177: * its state by storing it to the underlying database.
178: * This method always executes in the proper transaction context.
179: *
180: * @throws EJBException Thrown by the method to indicate a failure caused by
181: * a system-level error.
182: */
183: public void ejbStore() {
184: logger.log(BasicLevel.DEBUG, "");
185: }
186:
187: /**
188: * A container invokes this method on an instance before the instance
189: * becomes disassociated with a specific EJB object.
190: */
191: public void ejbPassivate() {
192: logger.log(BasicLevel.DEBUG, "");
193: }
194:
195: /**
196: * A container invokes this method when the instance is taken out of
197: * the pool of available instances to become associated with a specific
198: * EJB object.
199: */
200: public void ejbActivate() {
201: logger.log(BasicLevel.DEBUG, "");
202: }
203:
204: }
|