001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.proxy.ejb;
023:
024: import javax.rmi.CORBA.Util;
025:
026: import org.jboss.iiop.rmi.marshal.strategy.StubStrategy;
027: import org.jboss.logging.Logger;
028: import org.omg.CORBA.SystemException;
029: import org.omg.CORBA.portable.ApplicationException;
030: import org.omg.CORBA.portable.RemarshalException;
031: import org.omg.CORBA_2_3.portable.InputStream;
032: import org.omg.CORBA_2_3.portable.OutputStream;
033:
034: /**
035: * Dynamically generated IIOP stub classes extend this abstract superclass,
036: * which extends <code>javax.rmi.CORBA.Stub</code>.
037: *
038: * A <code>DynamicIIOPStub</code> is a local proxy of a remote object. It has
039: * methods (<code>invoke()</code>, <code>invokeBoolean()</code>,
040: * <code>invokeByte()</code>, and so on) that send an IIOP request to the
041: * server that implements the remote object, receive the reply from the
042: * server, and return the results to the caller. All of these methods take
043: * the IDL name of the operation, a <code>StubStrategy</code> instance to
044: * be used for marshalling parameters and unmarshalling the result, plus an
045: * array of operation parameters.
046: *
047: * @author <a href="mailto:reverbel@ime.usp.br">Francisco Reverbel</a>
048: * @version $Revision: 60892 $
049: */
050: public abstract class DynamicIIOPStub extends javax.rmi.CORBA.Stub {
051: /** @since 4.2.0 */
052: static final long serialVersionUID = 3283717238950231589L;
053:
054: // Attributes -------------------------------------------------------------
055:
056: /**
057: * My handle (either a HandleImplIIOP or a HomeHandleImplIIOP).
058: */
059: private Object handle = null;
060:
061: // Static ------------------------------------------------------------------
062:
063: private static final Logger logger = Logger
064: .getLogger(DynamicIIOPStub.class);
065:
066: private static void trace(String msg) {
067: if (logger.isTraceEnabled())
068: logger.trace(msg);
069: }
070:
071: // Constructor -------------------------------------------------------------
072:
073: /**
074: * Constructs a <code>DynamicIIOPStub</code>.
075: */
076: public DynamicIIOPStub() {
077: super ();
078: }
079:
080: // Methods used by dynamically generated IIOP stubs ------------------------
081:
082: /**
083: * Sends a request message to the server, receives the reply from the
084: * server, and returns an <code>Object</code> result to the caller.
085: */
086: public Object invoke(String operationName,
087: StubStrategy stubStrategy, Object[] params)
088: throws Throwable {
089: if (operationName.equals("_get_handle")
090: && this instanceof javax.ejb.EJBObject) {
091: if (handle == null) {
092: handle = new HandleImplIIOP(this );
093: }
094: return handle;
095: } else if (operationName.equals("_get_homeHandle")
096: && this instanceof javax.ejb.EJBHome) {
097: if (handle == null) {
098: handle = new HomeHandleImplIIOP(this );
099: }
100: return handle;
101: } else if (!_is_local()) {
102: // remote call path
103:
104: // To check whether this is a local stub or not we must call
105: // org.omg.CORBA.portable.ObjectImpl._is_local(), and _not_
106: // javax.rmi.CORBA.Util.isLocal(Stub s), which in Sun's JDK
107: // always return false.
108:
109: InputStream in = null;
110: try {
111: try {
112: OutputStream out = (OutputStream) _request(
113: operationName, true);
114: stubStrategy.writeParams(out, params);
115: trace("sent request: " + operationName);
116: in = (InputStream) _invoke(out);
117: if (stubStrategy.isNonVoid()) {
118: trace("received reply");
119: return stubStrategy.readRetval(in);
120: //Object retval = stubStrategy.readRetval(in);
121: //trace("retval: " + retval);
122: //return retval;
123: } else
124: return null;
125: } catch (ApplicationException ex) {
126: trace("got application exception");
127: in = (InputStream) ex.getInputStream();
128: throw stubStrategy.readException(ex.getId(), in);
129: } catch (RemarshalException ex) {
130: trace("got remarshal exception");
131: return invoke(operationName, stubStrategy, params);
132: }
133: } catch (SystemException ex) {
134: if (logger.isTraceEnabled()) {
135: logger.trace("CORBA system exception in IIOP stub",
136: ex);
137: }
138: throw Util.mapSystemException(ex);
139: } finally {
140: _releaseReply(in);
141: }
142: } else {
143: // local call path
144: org.omg.CORBA.portable.ServantObject so = _servant_preinvoke(
145: operationName, java.lang.Object.class);
146: if (so == null)
147: return invoke(operationName, stubStrategy, params);
148: try {
149: //params = Util.copyObjects(params, _orb());
150: Object retval = ((LocalIIOPInvoker) so.servant).invoke(
151: operationName, params, null, /* tx */
152: null, /* identity */
153: null /* credential */);
154: return stubStrategy.convertLocalRetval(retval);
155: //retval = stubStrategy.convertLocalRetval(retval);
156: //return Util.copyObject(retval, _orb());
157: } catch (Throwable e) {
158: //Throwable ex = (Throwable)Util.copyObject(e, _orb());
159: Throwable ex = e;
160: if (stubStrategy.isDeclaredException(ex))
161: throw ex;
162: else
163: throw Util.wrapException(ex);
164: } finally {
165: _servant_postinvoke(so);
166: }
167: }
168: }
169:
170: /**
171: * Sends a request message to the server, receives the reply from the
172: * server, and returns a <code>boolean</code> result to the caller.
173: */
174: public boolean invokeBoolean(String operationName,
175: StubStrategy stubStrategy, Object[] params)
176: throws Throwable {
177: return ((Boolean) invoke(operationName, stubStrategy, params))
178: .booleanValue();
179: }
180:
181: /**
182: * Sends a request message to the server, receives the reply from the
183: * server, and returns a <code>byte</code> result to the caller.
184: */
185: public byte invokeByte(String operationName,
186: StubStrategy stubStrategy, Object[] params)
187: throws Throwable {
188: return ((Number) invoke(operationName, stubStrategy, params))
189: .byteValue();
190: }
191:
192: /**
193: * Sends a request message to the server, receives the reply from the
194: * server, and returns a <code>char</code> result to the caller.
195: */
196: public char invokeChar(String operationName,
197: StubStrategy stubStrategy, Object[] params)
198: throws Throwable {
199: return ((Character) invoke(operationName, stubStrategy, params))
200: .charValue();
201: }
202:
203: /**
204: * Sends a request message to the server, receives the reply from the
205: * server, and returns a <code>short</code> result to the caller.
206: */
207: public short invokeShort(String operationName,
208: StubStrategy stubStrategy, Object[] params)
209: throws Throwable {
210: return ((Number) invoke(operationName, stubStrategy, params))
211: .shortValue();
212: }
213:
214: /**
215: * Sends a request message to the server, receives the reply from the
216: * server, and returns an <code>int</code> result to the caller.
217: */
218: public int invokeInt(String operationName,
219: StubStrategy stubStrategy, Object[] params)
220: throws Throwable {
221: return ((Number) invoke(operationName, stubStrategy, params))
222: .intValue();
223: }
224:
225: /**
226: * Sends a request message to the server, receives the reply from the
227: * server, and returns a <code>long</code> result to the caller.
228: */
229: public long invokeLong(String operationName,
230: StubStrategy stubStrategy, Object[] params)
231: throws Throwable {
232: return ((Number) invoke(operationName, stubStrategy, params))
233: .longValue();
234: }
235:
236: /**
237: * Sends a request message to the server, receives the reply from the
238: * server, and returns a <code>float</code> result to the caller.
239: */
240: public float invokeFloat(String operationName,
241: StubStrategy stubStrategy, Object[] params)
242: throws Throwable {
243: return ((Number) invoke(operationName, stubStrategy, params))
244: .floatValue();
245: }
246:
247: /**
248: * Sends a request message to the server, receives the reply from the
249: * server, and returns a <code>double</code> result to the caller.
250: */
251: public double invokeDouble(String operationName,
252: StubStrategy stubStrategy, Object[] params)
253: throws Throwable {
254: return ((Number) invoke(operationName, stubStrategy, params))
255: .doubleValue();
256: }
257:
258: }
|