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