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