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