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