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