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