StubInvocationHandlerImpl.java revision 608:7e06bf1dcb09
1/*
2 * Copyright (c) 2003, 2006, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.corba.se.impl.presentation.rmi ;
27
28import java.security.AccessController;
29import java.security.PrivilegedAction;
30
31import java.lang.reflect.Method ;
32import java.lang.reflect.InvocationHandler ;
33import java.lang.reflect.Proxy ;
34import java.lang.reflect.InvocationTargetException ;
35
36import java.io.ObjectInputStream ;
37import java.io.ObjectOutputStream ;
38import java.io.IOException ;
39
40import java.rmi.Remote ;
41
42import javax.rmi.CORBA.Util ;
43
44import org.omg.CORBA.portable.ObjectImpl ;
45import org.omg.CORBA.portable.Delegate ;
46import org.omg.CORBA.portable.ServantObject ;
47import org.omg.CORBA.portable.ApplicationException ;
48import org.omg.CORBA.portable.RemarshalException ;
49
50import org.omg.CORBA.SystemException ;
51
52import com.sun.corba.se.spi.orb.ORB ;
53
54import com.sun.corba.se.pept.transport.ContactInfoList ;
55
56import com.sun.corba.se.spi.transport.CorbaContactInfoList ;
57
58import com.sun.corba.se.spi.protocol.CorbaClientDelegate ;
59import com.sun.corba.se.spi.protocol.LocalClientRequestDispatcher ;
60
61import com.sun.corba.se.spi.presentation.rmi.IDLNameTranslator ;
62import com.sun.corba.se.spi.presentation.rmi.DynamicMethodMarshaller ;
63import com.sun.corba.se.spi.presentation.rmi.PresentationManager ;
64import com.sun.corba.se.spi.presentation.rmi.StubAdapter ;
65
66import com.sun.corba.se.spi.orbutil.proxy.InvocationHandlerFactory ;
67import com.sun.corba.se.spi.orbutil.proxy.LinkedInvocationHandler ;
68
69import com.sun.corba.se.impl.corba.CORBAObjectImpl ;
70
71public final class StubInvocationHandlerImpl implements LinkedInvocationHandler
72{
73    private transient PresentationManager.ClassData classData ;
74    private transient PresentationManager pm ;
75    private transient org.omg.CORBA.Object stub ;
76    private transient Proxy self ;
77
78    public void setProxy( Proxy self )
79    {
80        this.self = self ;
81    }
82
83    public Proxy getProxy()
84    {
85        return self ;
86    }
87
88    public StubInvocationHandlerImpl( PresentationManager pm,
89        PresentationManager.ClassData classData, org.omg.CORBA.Object stub )
90    {
91        SecurityManager s = System.getSecurityManager();
92        if (s != null) {
93            s.checkPermission(new DynamicAccessPermission("access"));
94        }
95        this.classData = classData ;
96        this.pm = pm ;
97        this.stub = stub ;
98    }
99
100    private boolean isLocal()
101    {
102        boolean result = false ;
103        Delegate delegate = StubAdapter.getDelegate( stub ) ;
104
105        if (delegate instanceof CorbaClientDelegate) {
106            CorbaClientDelegate cdel = (CorbaClientDelegate)delegate ;
107            ContactInfoList cil = cdel.getContactInfoList() ;
108            if (cil instanceof CorbaContactInfoList) {
109                CorbaContactInfoList ccil = (CorbaContactInfoList)cil ;
110                LocalClientRequestDispatcher lcrd =
111                    ccil.getLocalClientRequestDispatcher() ;
112                result = lcrd.useLocalInvocation( null ) ;
113            }
114        }
115
116        return result ;
117    }
118
119    /** Invoke the given method with the args and return the result.
120     *  This may result in a remote invocation.
121     *  @param proxy The proxy used for this class (null if not using java.lang.reflect.Proxy)
122     */
123    public Object invoke( Object proxy, final Method method,
124        Object[] args ) throws Throwable
125    {
126        String giopMethodName = classData.getIDLNameTranslator().
127            getIDLName( method )  ;
128        DynamicMethodMarshaller dmm =
129            pm.getDynamicMethodMarshaller( method ) ;
130
131        Delegate delegate = null ;
132        try {
133            delegate = StubAdapter.getDelegate( stub ) ;
134        } catch (SystemException ex) {
135            throw Util.mapSystemException(ex) ;
136        }
137
138        if (!isLocal()) {
139            try {
140                org.omg.CORBA_2_3.portable.InputStream in = null ;
141                try {
142                    // create request
143                    org.omg.CORBA_2_3.portable.OutputStream out =
144                        (org.omg.CORBA_2_3.portable.OutputStream)
145                        delegate.request( stub, giopMethodName, true);
146
147                    // marshal arguments
148                    dmm.writeArguments( out, args ) ;
149
150                    // finish invocation
151                    in = (org.omg.CORBA_2_3.portable.InputStream)
152                        delegate.invoke( stub, out);
153
154                    // unmarshal result
155                    return dmm.readResult( in ) ;
156                } catch (ApplicationException ex) {
157                    throw dmm.readException( ex ) ;
158                } catch (RemarshalException ex) {
159                    return invoke( proxy, method, args ) ;
160                } finally {
161                    delegate.releaseReply( stub, in );
162                }
163            } catch (SystemException ex) {
164                throw Util.mapSystemException(ex) ;
165            }
166        } else {
167            // local branch
168            ORB orb = (ORB)delegate.orb( stub ) ;
169            ServantObject so = delegate.servant_preinvoke( stub, giopMethodName,
170                method.getDeclaringClass() );
171            if (so == null) {
172                return invoke( stub, method, args ) ;
173            }
174            try {
175                Object[] copies = dmm.copyArguments( args, orb ) ;
176
177                if (!method.isAccessible()) {
178                    // Make sure that we can invoke a method from a normally
179                    // inaccessible package, as this reflective class must always
180                    // be able to invoke a non-public method.
181                    AccessController.doPrivileged(new PrivilegedAction() {
182                        public Object run() {
183                            method.setAccessible( true ) ;
184                            return null ;
185                        }
186                    } ) ;
187                }
188
189                Object result = method.invoke( so.servant, copies ) ;
190
191                return dmm.copyResult( result, orb ) ;
192            } catch (InvocationTargetException ex) {
193                Throwable mex = ex.getCause() ;
194                // mex should never be null, as null cannot be thrown
195                Throwable exCopy = (Throwable)Util.copyObject(mex,orb);
196                if (dmm.isDeclaredException( exCopy ))
197                    throw exCopy ;
198                else
199                    throw Util.wrapException(exCopy);
200            } catch (Throwable thr) {
201                if (thr instanceof ThreadDeath)
202                    throw (ThreadDeath)thr ;
203
204                // This is not a user thrown exception from the
205                // method call, so don't copy it.  This is either
206                // an error or a reflective invoke exception.
207                throw Util.wrapException( thr ) ;
208            } finally {
209                delegate.servant_postinvoke( stub, so);
210            }
211        }
212    }
213}
214