KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > impl > presentation > rmi > StubInvocationHandlerImpl


1 /*
2  * @(#)StubFactoryImpl.java 1.9 03/12/03
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package com.sun.corba.se.impl.presentation.rmi ;
9
10 import java.security.AccessController JavaDoc;
11 import java.security.PrivilegedAction JavaDoc;
12
13 import java.lang.reflect.Method JavaDoc ;
14 import java.lang.reflect.InvocationHandler JavaDoc ;
15 import java.lang.reflect.Proxy JavaDoc ;
16 import java.lang.reflect.InvocationTargetException JavaDoc ;
17
18 import java.io.ObjectInputStream JavaDoc ;
19 import java.io.ObjectOutputStream JavaDoc ;
20 import java.io.IOException JavaDoc ;
21
22 import java.rmi.Remote JavaDoc ;
23
24 import javax.rmi.CORBA.Util JavaDoc ;
25
26 import org.omg.CORBA.portable.ObjectImpl JavaDoc ;
27 import org.omg.CORBA.portable.Delegate JavaDoc ;
28 import org.omg.CORBA.portable.ServantObject JavaDoc ;
29 import org.omg.CORBA.portable.ApplicationException JavaDoc ;
30 import org.omg.CORBA.portable.RemarshalException JavaDoc ;
31
32 import org.omg.CORBA.SystemException JavaDoc ;
33
34 import com.sun.corba.se.spi.orb.ORB ;
35
36 import com.sun.corba.se.pept.transport.ContactInfoList ;
37
38 import com.sun.corba.se.spi.transport.CorbaContactInfoList ;
39
40 import com.sun.corba.se.spi.protocol.CorbaClientDelegate ;
41 import com.sun.corba.se.spi.protocol.LocalClientRequestDispatcher ;
42
43 import com.sun.corba.se.spi.presentation.rmi.IDLNameTranslator ;
44 import com.sun.corba.se.spi.presentation.rmi.DynamicMethodMarshaller ;
45 import com.sun.corba.se.spi.presentation.rmi.PresentationManager ;
46 import com.sun.corba.se.spi.presentation.rmi.StubAdapter ;
47
48 import com.sun.corba.se.spi.orbutil.proxy.InvocationHandlerFactory ;
49 import com.sun.corba.se.spi.orbutil.proxy.LinkedInvocationHandler ;
50
51 import com.sun.corba.se.impl.corba.CORBAObjectImpl ;
52
53 public final class StubInvocationHandlerImpl implements LinkedInvocationHandler
54 {
55     private transient PresentationManager.ClassData classData ;
56     private transient PresentationManager pm ;
57     private transient org.omg.CORBA.Object JavaDoc stub ;
58     private transient Proxy JavaDoc self ;
59
60     public void setProxy( Proxy JavaDoc self )
61     {
62     this.self = self ;
63     }
64
65     public Proxy JavaDoc getProxy()
66     {
67     return self ;
68     }
69
70     public StubInvocationHandlerImpl( PresentationManager pm,
71     PresentationManager.ClassData classData, org.omg.CORBA.Object JavaDoc stub )
72     {
73     SecurityManager JavaDoc s = System.getSecurityManager();
74     if (s != null) {
75         s.checkPermission(new DynamicAccessPermission("access"));
76     }
77     this.classData = classData ;
78     this.pm = pm ;
79     this.stub = stub ;
80     }
81
82     private boolean isLocal()
83     {
84     boolean result = false ;
85     Delegate JavaDoc delegate = StubAdapter.getDelegate( stub ) ;
86
87     if (delegate instanceof CorbaClientDelegate) {
88         CorbaClientDelegate cdel = (CorbaClientDelegate)delegate ;
89         ContactInfoList cil = cdel.getContactInfoList() ;
90         if (cil instanceof CorbaContactInfoList) {
91         CorbaContactInfoList ccil = (CorbaContactInfoList)cil ;
92         LocalClientRequestDispatcher lcrd =
93             ccil.getLocalClientRequestDispatcher() ;
94         result = lcrd.useLocalInvocation( null ) ;
95         }
96     }
97      
98     return result ;
99     }
100
101     /** Invoke the given method with the args and return the result.
102      * This may result in a remote invocation.
103      * @param proxy The proxy used for this class (null if not using java.lang.reflect.Proxy)
104      */

105     public Object JavaDoc invoke( Object JavaDoc proxy, final Method JavaDoc method,
106     Object JavaDoc[] args ) throws Throwable JavaDoc
107     {
108     String JavaDoc giopMethodName = classData.getIDLNameTranslator().
109         getIDLName( method ) ;
110     DynamicMethodMarshaller dmm =
111         pm.getDynamicMethodMarshaller( method ) ;
112
113     Delegate JavaDoc delegate = null ;
114     try {
115         delegate = StubAdapter.getDelegate( stub ) ;
116     } catch (SystemException JavaDoc ex) {
117         throw Util.mapSystemException(ex) ;
118     }
119
120     if (!isLocal()) {
121         try {
122         org.omg.CORBA_2_3.portable.InputStream JavaDoc in = null ;
123         try {
124             // create request
125
org.omg.CORBA_2_3.portable.OutputStream JavaDoc out =
126             (org.omg.CORBA_2_3.portable.OutputStream JavaDoc)
127             delegate.request( stub, giopMethodName, true);
128
129             // marshal arguments
130
dmm.writeArguments( out, args ) ;
131
132             // finish invocation
133
in = (org.omg.CORBA_2_3.portable.InputStream JavaDoc)
134             delegate.invoke( stub, out);
135     
136             // unmarshal result
137
return dmm.readResult( in ) ;
138         } catch (ApplicationException JavaDoc ex) {
139             throw dmm.readException( ex ) ;
140         } catch (RemarshalException JavaDoc ex) {
141             return invoke( proxy, method, args ) ;
142         } finally {
143             delegate.releaseReply( stub, in );
144         }
145         } catch (SystemException JavaDoc ex) {
146         throw Util.mapSystemException(ex) ;
147         }
148     } else {
149         // local branch
150
ORB orb = (ORB)delegate.orb( stub ) ;
151         ServantObject JavaDoc so = delegate.servant_preinvoke( stub, giopMethodName,
152         method.getDeclaringClass() );
153         if (so == null) {
154         return invoke( stub, method, args ) ;
155         }
156         try {
157         Object JavaDoc[] copies = dmm.copyArguments( args, orb ) ;
158
159             if (!method.isAccessible()) {
160             // Make sure that we can invoke a method from a normally
161
// inaccessible package, as this reflective class must always
162
// be able to invoke a non-public method.
163
AccessController.doPrivileged(new PrivilegedAction JavaDoc() {
164             public Object JavaDoc run() {
165                 method.setAccessible( true ) ;
166                 return null ;
167             }
168             } ) ;
169         }
170
171         Object JavaDoc result = method.invoke( so.servant, copies ) ;
172
173         return dmm.copyResult( result, orb ) ;
174         } catch (InvocationTargetException JavaDoc ex) {
175         Throwable JavaDoc mex = ex.getCause() ;
176         // mex should never be null, as null cannot be thrown
177
Throwable JavaDoc exCopy = (Throwable JavaDoc)Util.copyObject(mex,orb);
178         if (dmm.isDeclaredException( exCopy ))
179             throw exCopy ;
180         else
181             throw Util.wrapException(exCopy);
182         } catch (Throwable JavaDoc thr) {
183         if (thr instanceof ThreadDeath JavaDoc)
184             throw (ThreadDeath JavaDoc)thr ;
185
186         // This is not a user thrown exception from the
187
// method call, so don't copy it. This is either
188
// an error or a reflective invoke exception.
189
throw Util.wrapException( thr ) ;
190         } finally {
191         delegate.servant_postinvoke( stub, so);
192         }
193     }
194     }
195 }
196
Popular Tags