001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999-2004 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: JSessionRemote.java 7900 2006-01-18 16:04:21Z durieuxp $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas_ejb.container;
025:
026: import java.rmi.Remote;
027: import java.rmi.RemoteException;
028:
029: import javax.ejb.EJBHome;
030: import javax.ejb.EJBObject;
031: import javax.ejb.Handle;
032: import javax.ejb.RemoveException;
033: import javax.rmi.PortableRemoteObject;
034:
035: import org.objectweb.carol.rmi.exception.RmiUtility;
036: import org.objectweb.carol.util.configuration.ConfigurationRepository;
037:
038: import org.objectweb.jonas_ejb.lib.EJBInvocation;
039: import org.objectweb.jonas_ejb.svc.JHandleIIOP;
040:
041: import org.objectweb.util.monolog.api.BasicLevel;
042:
043: /**
044: * Generic part of the EJBObject implementation
045: * @author Philippe Coq
046: * @author Philippe Durieux
047: */
048: public abstract class JSessionRemote extends JRemote implements Remote {
049:
050: /**
051: * <code>bs</code>
052: */
053: protected JSessionSwitch bs;
054:
055: /**
056: * constructor
057: * @param bf The Session Factory
058: * @throws RemoteException Thrown when the method failed due to a system-level failure.
059: */
060: public JSessionRemote(JSessionFactory bf) throws RemoteException {
061: super ((JFactory) bf);
062: if (TraceEjb.isDebugIc()) {
063: TraceEjb.interp.log(BasicLevel.DEBUG, "");
064: }
065: }
066:
067: // --------------------------------------------------------------------------
068: // EJBObject implementation
069: // remove() is implemented in the generated part.
070: // --------------------------------------------------------------------------
071:
072: /**
073: * remove is implemented in the generated part.
074: * @throws RemoteException Thrown when the method failed due to a system-level failure.
075: * @throws RemoveException Thrown when the method failed due to remove failure.
076: */
077: public abstract void remove() throws RemoteException,
078: RemoveException;
079:
080: /**
081: * @return the enterprise Bean's home interface.
082: */
083: public EJBHome getEJBHome() {
084: /*
085: * try/catch block is commented because the encapsulated code don't
086: * throw a RemoteException
087: * If the code changes and throws a such exception, let's think
088: * to uncomment it
089: *
090: * try {
091: */
092:
093: return bf.getHome();
094:
095: /*
096: * } catch (RemoteException e) {
097: * // check if rmi exception mapping is needed - if yes the method rethrows it
098: * RmiUtility.rethrowRmiException(e);
099: * // if not, throws the exception just as it is
100: * throw e;
101: * }
102: */
103:
104: }
105:
106: /**
107: * @throws RemoteException Always : Session bean has never a primary key
108: * @return never carried out because a exception is thrown
109: */
110: public Object getPrimaryKey() throws RemoteException {
111:
112: try {
113: throw new RemoteException("Session bean has no primary key");
114: } catch (RemoteException e) {
115: // check if rmi exception mapping is needed - if yes the method rethrows it
116: RmiUtility.rethrowRmiException(e);
117: // if not, throws the exception just as it is
118: throw e;
119: }
120: }
121:
122: /**
123: * Tests if a given EJB is identical to the invoked EJB object. This is
124: * different whether the bean is stateless or stateful.
125: * @param obj - An object to test for identity with the invoked object.
126: * @return True if the given EJB object is identical to the invoked object.
127: * @throws RemoteException Thrown when the method failed due to a system-level failure.
128: */
129: public boolean isIdentical(EJBObject obj) throws RemoteException {
130: if (TraceEjb.isDebugIc()) {
131: TraceEjb.interp.log(BasicLevel.DEBUG, "");
132: }
133: try {
134: boolean ret = false;
135: JSessionFactory sf = (JSessionFactory) bf;
136: if (sf.isStateful()) {
137: // For stateful sessions, just compare both objects.
138: if (obj != null) {
139: ret = ((obj.equals(PortableRemoteObject
140: .toStub(this ))) || (obj.equals(this )));
141: }
142: } else {
143: // In case of Stateless session bean, we must compare the 2 Home
144: // We cannot cast the remote EJBObject cbecause it's a stub.
145: String myhome = getEJBHome().getEJBMetaData()
146: .getHomeInterfaceClass().getName();
147: if (obj != null) {
148: ret = obj.getEJBHome().getEJBMetaData()
149: .getHomeInterfaceClass().getName().equals(
150: myhome);
151: }
152: }
153: return ret;
154: } catch (RemoteException e) {
155: // check if rmi exception mapping is needed - if yes the method
156: // rethrows it
157: RmiUtility.rethrowRmiException(e);
158: // if not, throws the exception just as it is
159: throw e;
160: }
161: }
162:
163: /**
164: * Obtains a handle for the EJB object. The handle can be used at later time
165: * to re-obtain a reference to the EJB object, possibly in a different JVM.
166: * @return A handle for the EJB object.
167: * @exception RemoteException Thrown when the method failed due to a
168: * system-level failure.
169: */
170: public Handle getHandle() throws RemoteException {
171: if (TraceEjb.isDebugIc()) {
172: TraceEjb.interp.log(BasicLevel.DEBUG, "");
173: }
174:
175: /*
176: * try/catch block is commented because the encapsulated code don't
177: * throw a RemoteException
178: * If the code changes and throws a such exception, let's think
179: * to uncomment it
180: *
181: * try {
182: */
183: // for iiop, a specific interoperable Handle is created with the use of
184: // HandleDelegate
185: String protocol = ConfigurationRepository
186: .getCurrentConfiguration().getProtocol().getName();
187: if (TraceEjb.interp.isLoggable(BasicLevel.DEBUG)) {
188: TraceEjb.interp.log(BasicLevel.DEBUG, "Current protocol = "
189: + protocol);
190: }
191:
192: if (protocol.equals("iiop")) {
193: return new JHandleIIOP(this );
194: } else {
195: return new JSessionHandle(this );
196: }
197:
198: /*
199: * } catch (RemoteException e) {
200: * // check if rmi exception mapping is needed - if yes the method rethrows it
201: * RmiUtility.rethrowRmiException(e);
202: * // if not, throws the exception just as it is
203: * throw e;
204: * }
205: */
206:
207: }
208:
209: // ---------------------------------------------------------------
210: // other public methods, for internal use.
211: // ---------------------------------------------------------------
212:
213: /**
214: * finish initialization
215: * @param bs the SessionSwitch
216: */
217: public void setSessionSwitch(JSessionSwitch bs) {
218: if (TraceEjb.isDebugIc()) {
219: TraceEjb.interp.log(BasicLevel.DEBUG, "");
220: }
221: this .bs = bs;
222: }
223:
224: /**
225: * @return the JSessionSwitch for this Session
226: */
227: public JSessionSwitch getSessionSwitch() {
228: return bs;
229: }
230:
231: /**
232: * preInvoke is called before any request.
233: * @param txa Transaction Attribute (Supports, Required, ...)
234: * @return A RequestCtx object
235: * @throws RemoteException Thrown when the method failed due to a
236: * system-level failure.
237: */
238: public RequestCtx preInvoke(int txa) throws RemoteException {
239: if (TraceEjb.isDebugIc()) {
240: TraceEjb.interp.log(BasicLevel.DEBUG, "");
241: }
242: RequestCtx rctx = bf.preInvokeRemote(txa);
243: bs.setMustCommit(rctx.mustCommit); // for remove stateful session
244: bs.enlistConnections(rctx.currTx); // Enlist connection list to tx
245: return rctx;
246: }
247:
248: /**
249: * Check if the access to the bean is authorized
250: * @param ejbInv object containing security signature of the method, args of
251: * method, etc
252: */
253: public void checkSecurity(EJBInvocation ejbInv) {
254: if (TraceEjb.isDebugIc()) {
255: TraceEjb.interp.log(BasicLevel.DEBUG, "");
256: }
257: bf.checkSecurity(ejbInv);
258: }
259:
260: /**
261: * postInvoke is called after any request.
262: * @param rctx The RequestCtx that was returned at preInvoke()
263: * @throws RemoteException Thrown when the method failed due to a
264: * system-level failure.
265: */
266: public void postInvoke(RequestCtx rctx) throws RemoteException {
267: if (TraceEjb.isDebugIc()) {
268: TraceEjb.interp.log(BasicLevel.DEBUG, "");
269: }
270: bs.delistConnections(rctx.currTx);
271: // save current tx (for Bean Managed only)
272: bs.saveBeanTx();
273: try {
274: bf.postInvokeRemote(rctx);
275: } finally {
276: bs.releaseICtx(rctx, rctx.sysExc != null);
277: }
278: }
279:
280: }
|