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: AEC2.java 5995 2004-12-17 15:08:36Z joaninh $
023: * --------------------------------------------------------------------------
024: */
025:
026: package org.objectweb.jonas.jtests.beans.relation.s3pkcomp;
027:
028: import org.objectweb.jonas.common.Log;
029: import org.objectweb.util.monolog.api.BasicLevel;
030: import org.objectweb.util.monolog.api.Logger;
031:
032: import javax.ejb.CreateException;
033: import javax.ejb.DuplicateKeyException;
034: import javax.ejb.EntityContext;
035: import javax.ejb.RemoveException;
036: import javax.ejb.FinderException;
037: import javax.ejb.EJBException;
038: import javax.naming.Context;
039: import javax.naming.InitialContext;
040: import javax.naming.NamingException;
041: import javax.rmi.PortableRemoteObject;
042:
043: import java.util.Collection;
044: import java.util.ArrayList;
045: import java.util.Iterator;
046:
047: /**
048: * @author H. Joanin
049: */
050: public abstract class AEC2 implements javax.ejb.EntityBean {
051:
052: private BHomeLocal bhl = null;
053:
054: public void m1() {
055: }
056:
057: public void assignB(Collection c) throws FinderException {
058: ArrayList al = new ArrayList(c.size());
059: for (Iterator it = c.iterator(); it.hasNext();)
060: al.add(bhl.findByPrimaryKey((Pk) it.next()));
061: setB(al);
062: }
063:
064: public void assignBInNewTx(Collection c) throws FinderException {
065: assignB(c);
066: }
067:
068: public Collection retrieveB() {
069: Collection bs = getB();
070: ArrayList result = new ArrayList(bs.size());
071: for (Iterator it = bs.iterator(); it.hasNext();)
072: result.add(((BLocal) it.next()).getPrimaryKey());
073: return result;
074: }
075:
076: public Collection retrieveBInNewTx() {
077: return retrieveB();
078: }
079:
080: public void addInB(Pk pkb) throws FinderException {
081: getB().add(bhl.findByPrimaryKey(pkb));
082: }
083:
084: public void addInBInNewTx(Pk pkb) throws FinderException {
085: addInB(pkb);
086: }
087:
088: public void addAllInB(Collection pkbs) throws FinderException {
089: ArrayList al = new ArrayList();
090: for (Iterator it = pkbs.iterator(); it.hasNext();)
091: al.add(bhl.findByPrimaryKey((Pk) it.next()));
092: getB().addAll(al);
093: }
094:
095: public void addAllInBInNewTx(Collection pkbs)
096: throws FinderException {
097: addAllInB(pkbs);
098: }
099:
100: public void removeFromB(Pk pkb) throws FinderException {
101: getB().remove(bhl.findByPrimaryKey(pkb));
102: }
103:
104: public void removeFromBInNewTx(Pk pkb) throws FinderException {
105: removeFromB(pkb);
106: }
107:
108: public void clearB() {
109: getB().clear();
110: }
111:
112: public void clearBInNewTx() {
113: clearB();
114: }
115:
116: public boolean containAllInB(Collection pkbs)
117: throws FinderException {
118: ArrayList al = new ArrayList(pkbs.size());
119: for (Iterator it = pkbs.iterator(); it.hasNext();)
120: al.add(bhl.findByPrimaryKey((Pk) it.next()));
121: return getB().containsAll(al);
122: }
123:
124: /**
125: * It returns true the multivalued relation contains the bean B defined
126: * by the primary key specified by the parameter.
127: * This method has the transactional attribut TX_SUPPORTS.
128: * @throw a FinderException if the primary key does not match to a bean.
129: */
130: public boolean containInB(Pk pkb) throws FinderException {
131: return (getB().contains(bhl.findByPrimaryKey(pkb)));
132: }
133:
134: // ------------------------------------------------------------------
135: // Get and Set accessor methods of the bean's abstract schema
136: // ------------------------------------------------------------------
137: public abstract Integer getId();
138:
139: public abstract void setId(Integer id);
140:
141: public abstract Collection getB();
142:
143: public abstract void setB(Collection bl);
144:
145: // ------------------------------------------------------------------
146: // EntityBean implementation
147: // ------------------------------------------------------------------
148:
149: static protected Logger logger = null;
150: EntityContext ejbContext;
151:
152: /**
153: * The Entity bean can define 0 or more ejbCreate methods.
154: *
155: * @throws CreateException Failure to create an entity EJB object.
156: * @throws DuplicateKeyException An object with the same key already exists.
157: */
158: public String ejbCreate(int id) throws CreateException,
159: DuplicateKeyException {
160: logger.log(BasicLevel.DEBUG, "");
161:
162: // Init here the bean fields
163: setId(new Integer(id));
164:
165: // In CMP, should return null.
166: return null;
167: }
168:
169: /**
170: * Set the associated entity context. The container invokes this method
171: * on an instance after the instance has been created.
172: * This method is called in an unspecified transaction context.
173: *
174: * @param ctx - An EntityContext interface for the instance. The instance
175: * should store the reference to the context in an instance variable.
176: * @throws EJBException Thrown by the method to indicate a failure caused by a
177: * system-level error.
178: */
179: public void setEntityContext(EntityContext ctx) {
180: if (logger == null)
181: logger = Log.getLogger(Log.JONAS_TESTS_PREFIX);
182: logger.log(BasicLevel.DEBUG, "");
183: ejbContext = ctx;
184: try {
185: Context ictx = new InitialContext();
186: bhl = (BHomeLocal) ictx.lookup("java:comp/env/ejb/b");
187: } catch (NamingException e) {
188: throw new EJBException("Impossible to fetch the ", e);
189: }
190: }
191:
192: /**
193: * Unset the associated entity context. The container calls this method
194: * before removing the instance.
195: * This is the last method that the container invokes on the instance.
196: * The Java garbage collector will eventually invoke the finalize() method
197: * on the instance.
198: * This method is called in an unspecified transaction context.
199: *
200: * @throws EJBException Thrown by the method to indicate a failure caused by a
201: * system-level error.
202: */
203: public void unsetEntityContext() {
204: logger.log(BasicLevel.DEBUG, "");
205: ejbContext = null;
206: }
207:
208: /**
209: * A container invokes this method before it removes the EJB object
210: * that is currently associated with the instance. This method is
211: * invoked when a client invokes a remove operation on the enterprise Bean's
212: * home interface or the EJB object's remote interface. This method
213: * transitions the instance from the ready state to the pool of available
214: * instances.
215: *
216: * This method is called in the transaction context of the remove operation.
217: * @throws RemoveException The enterprise Bean does not allow destruction of the object.
218: * @throws EJBException - Thrown by the method to indicate a failure caused by a system-level
219: * error.
220: */
221: public void ejbRemove() throws RemoveException {
222: logger.log(BasicLevel.DEBUG, "");
223: }
224:
225: /**
226: * A container invokes this method to instruct the instance to synchronize
227: * its state by loading it state from the underlying database.
228: * This method always executes in the proper transaction context.
229: *
230: * @throws EJBException Thrown by the method to indicate a failure caused by
231: * a system-level error.
232: */
233: public void ejbLoad() {
234: logger.log(BasicLevel.DEBUG, "");
235: }
236:
237: /**
238: * A container invokes this method to instruct the instance to synchronize
239: * its state by storing it to the underlying database.
240: * This method always executes in the proper transaction context.
241: *
242: * @throws EJBException Thrown by the method to indicate a failure caused by
243: * a system-level error.
244: */
245: public void ejbStore() {
246: logger.log(BasicLevel.DEBUG, "");
247: }
248:
249: /**
250: * There must be an ejbPostCreate par ejbCreate method
251: *
252: * @throws CreateException Failure to create an entity EJB object.
253: */
254: public void ejbPostCreate(int id) throws CreateException {
255: logger.log(BasicLevel.DEBUG, "id=" + id);
256: }
257:
258: /**
259: * A container invokes this method on an instance before the instance
260: * becomes disassociated with a specific EJB object.
261: */
262: public void ejbPassivate() {
263: logger.log(BasicLevel.DEBUG, "");
264: }
265:
266: /**
267: * A container invokes this method when the instance is taken out of
268: * the pool of available instances to become associated with a specific
269: * EJB object.
270: */
271: public void ejbActivate() {
272: logger.log(BasicLevel.DEBUG, "");
273: }
274:
275: }
|