KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > proxy > ejb > DynamicIIOPStub


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.proxy.ejb;
23
24 import javax.rmi.CORBA.Util JavaDoc;
25 import org.omg.CORBA_2_3.portable.InputStream JavaDoc;
26 import org.omg.CORBA_2_3.portable.OutputStream JavaDoc;
27 import org.omg.CORBA.portable.ApplicationException JavaDoc;
28 import org.omg.CORBA.portable.RemarshalException JavaDoc;
29 import org.omg.CORBA.SystemException JavaDoc;
30
31 import org.jboss.iiop.rmi.marshal.strategy.StubStrategy;
32 import org.jboss.logging.Logger;
33 import org.jboss.proxy.ejb.HandleImplIIOP;
34 import org.jboss.proxy.ejb.HomeHandleImplIIOP;
35
36 /**
37  * Dynamically generated IIOP stub classes extend this abstract superclass,
38  * which extends <code>javax.rmi.CORBA.Stub</code>.
39  *
40  * A <code>DynamicIIOPStub</code> is a local proxy of a remote object. It has
41  * methods (<code>invoke()</code>, <code>invokeBoolean()</code>,
42  * <code>invokeByte()</code>, and so on) that send an IIOP request to the
43  * server that implements the remote object, receive the reply from the
44  * server, and return the results to the caller. All of these methods take
45  * the IDL name of the operation, a <code>StubStrategy</code> instance to
46  * be used for marshalling parameters and unmarshalling the result, plus an
47  * array of operation parameters.
48  *
49  * @author <a HREF="mailto:reverbel@ime.usp.br">Francisco Reverbel</a>
50  * @version $Revision: 37459 $
51  */

52 public abstract class DynamicIIOPStub
53       extends javax.rmi.CORBA.Stub JavaDoc
54 {
55
56    // Attributes -------------------------------------------------------------
57

58    /**
59     * My handle (either a HandleImplIIOP or a HomeHandleImplIIOP).
60     */

61    private Object JavaDoc handle = null;
62
63    // Static ------------------------------------------------------------------
64

65    private static final Logger logger =
66                            Logger.getLogger(DynamicIIOPStub.class);
67
68    private static void trace(String JavaDoc msg)
69    {
70       if (logger.isTraceEnabled())
71          logger.trace(msg);
72    }
73
74    // Constructor -------------------------------------------------------------
75

76    /**
77     * Constructs a <code>DynamicIIOPStub</code>.
78     */

79    public DynamicIIOPStub()
80    {
81       super();
82    }
83
84    // Methods used by dynamically generated IIOP stubs ------------------------
85

86    /**
87     * Sends a request message to the server, receives the reply from the
88     * server, and returns an <code>Object</code> result to the caller.
89     */

90    public Object JavaDoc invoke(String JavaDoc operationName,
91                         StubStrategy stubStrategy, Object JavaDoc[] params)
92          throws Throwable JavaDoc
93    {
94       if (operationName.equals("_get_handle")
95           && this instanceof javax.ejb.EJBObject JavaDoc) {
96          if (handle == null) {
97             handle = new HandleImplIIOP(this);
98          }
99          return handle;
100       }
101       else if (operationName.equals("_get_homeHandle")
102                && this instanceof javax.ejb.EJBHome JavaDoc) {
103          if (handle == null) {
104             handle = new HomeHandleImplIIOP(this);
105          }
106          return handle;
107       }
108       else if (!_is_local()) {
109          // remote call path
110

111          // To check whether this is a local stub or not we must call
112
// org.omg.CORBA.portable.ObjectImpl._is_local(), and _not_
113
// javax.rmi.CORBA.Util.isLocal(Stub s), which in Sun's JDK
114
// always return false.
115

116          InputStream JavaDoc in = null;
117          try {
118             try {
119                OutputStream JavaDoc out =
120                   (OutputStream JavaDoc)_request(operationName, true);
121                stubStrategy.writeParams(out, params);
122                trace("sent request: " + operationName);
123                in = (InputStream JavaDoc)_invoke(out);
124                if (stubStrategy.isNonVoid()) {
125                   trace("received reply");
126                   return stubStrategy.readRetval(in);
127                   //Object retval = stubStrategy.readRetval(in);
128
//trace("retval: " + retval);
129
//return retval;
130
}
131                else
132                   return null;
133             }
134             catch (ApplicationException JavaDoc ex) {
135                trace("got application exception");
136                in =(InputStream JavaDoc)ex.getInputStream();
137                throw stubStrategy.readException(ex.getId(), in);
138             }
139             catch (RemarshalException JavaDoc ex) {
140                trace("got remarshal exception");
141                return invoke(operationName, stubStrategy, params);
142             }
143          }
144          catch (SystemException JavaDoc ex) {
145             if (logger.isTraceEnabled()) {
146                logger.trace("CORBA system exception in IIOP stub", ex);
147             }
148             throw Util.mapSystemException(ex);
149          }
150          finally {
151             _releaseReply(in);
152          }
153       }
154       else {
155          // local call path
156
org.omg.CORBA.portable.ServantObject JavaDoc so =
157             _servant_preinvoke(operationName, java.lang.Object JavaDoc.class);
158          if (so == null)
159             return invoke(operationName, stubStrategy, params);
160          try {
161             //params = Util.copyObjects(params, _orb());
162
Object JavaDoc retval =
163                ((LocalIIOPInvoker)so.servant).invoke(operationName,
164                                                      params,
165                                                      null, /* tx */
166                                                      null, /* identity */
167                                                      null /* credential */);
168             return stubStrategy.convertLocalRetval(retval);
169             //retval = stubStrategy.convertLocalRetval(retval);
170
//return Util.copyObject(retval, _orb());
171
}
172          catch(Throwable JavaDoc e) {
173             //Throwable ex = (Throwable)Util.copyObject(e, _orb());
174
Throwable JavaDoc ex = e;
175             if (stubStrategy.isDeclaredException(ex))
176                throw ex;
177             else
178                throw Util.wrapException(ex);
179          }
180          finally {
181             _servant_postinvoke(so);
182          }
183       }
184    }
185       
186    /**
187     * Sends a request message to the server, receives the reply from the
188     * server, and returns a <code>boolean</code> result to the caller.
189     */

190    public boolean invokeBoolean(String JavaDoc operationName,
191                                 StubStrategy stubStrategy, Object JavaDoc[] params)
192          throws Throwable JavaDoc
193    {
194       return ((Boolean JavaDoc)invoke(operationName,
195                               stubStrategy, params)).booleanValue();
196    }
197    
198    /**
199     * Sends a request message to the server, receives the reply from the
200     * server, and returns a <code>byte</code> result to the caller.
201     */

202    public byte invokeByte(String JavaDoc operationName,
203                           StubStrategy stubStrategy, Object JavaDoc[] params)
204          throws Throwable JavaDoc
205    {
206          return ((Number JavaDoc)invoke(operationName,
207                                 stubStrategy, params)).byteValue();
208       }
209    
210    /**
211     * Sends a request message to the server, receives the reply from the
212     * server, and returns a <code>char</code> result to the caller.
213     */

214    public char invokeChar(String JavaDoc operationName,
215                           StubStrategy stubStrategy, Object JavaDoc[] params)
216          throws Throwable JavaDoc
217    {
218       return ((Character JavaDoc)invoke(operationName,
219                                 stubStrategy, params)).charValue();
220    }
221    
222    /**
223     * Sends a request message to the server, receives the reply from the
224     * server, and returns a <code>short</code> result to the caller.
225     */

226    public short invokeShort(String JavaDoc operationName,
227                             StubStrategy stubStrategy, Object JavaDoc[] params)
228          throws Throwable JavaDoc
229    {
230       return ((Number JavaDoc)invoke(operationName,
231                              stubStrategy, params)).shortValue();
232    }
233    
234    /**
235     * Sends a request message to the server, receives the reply from the
236     * server, and returns an <code>int</code> result to the caller.
237     */

238    public int invokeInt(String JavaDoc operationName,
239                         StubStrategy stubStrategy, Object JavaDoc[] params)
240          throws Throwable JavaDoc
241    {
242       return ((Number JavaDoc)invoke(operationName, stubStrategy, params)).intValue();
243    }
244    
245    /**
246     * Sends a request message to the server, receives the reply from the
247     * server, and returns a <code>long</code> result to the caller.
248     */

249    public long invokeLong(String JavaDoc operationName,
250                           StubStrategy stubStrategy, Object JavaDoc[] params)
251          throws Throwable JavaDoc
252    {
253       return ((Number JavaDoc)invoke(operationName, stubStrategy, params)).longValue();
254    }
255    
256    /**
257     * Sends a request message to the server, receives the reply from the
258     * server, and returns a <code>float</code> result to the caller.
259     */

260    public float invokeFloat(String JavaDoc operationName,
261                             StubStrategy stubStrategy, Object JavaDoc[] params)
262          throws Throwable JavaDoc
263    {
264       return ((Number JavaDoc)invoke(operationName,
265                              stubStrategy, params)).floatValue();
266    }
267    
268    /**
269     * Sends a request message to the server, receives the reply from the
270     * server, and returns a <code>double</code> result to the caller.
271     */

272    public double invokeDouble(String JavaDoc operationName,
273                               StubStrategy stubStrategy, Object JavaDoc[] params)
274          throws Throwable JavaDoc
275    {
276       return ((Number JavaDoc)invoke(operationName,
277                              stubStrategy, params)).doubleValue();
278    }
279    
280 }
281
Popular Tags