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