KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)ReflectiveTie.java 1.13 05/08/26
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.rmi.Remote JavaDoc;
11 import java.rmi.RemoteException JavaDoc;
12
13 import javax.rmi.CORBA.Tie JavaDoc;
14
15 import java.lang.reflect.Method JavaDoc ;
16 import java.lang.reflect.InvocationTargetException JavaDoc ;
17
18 import org.omg.CORBA.SystemException JavaDoc;
19 import org.omg.CORBA_2_3.portable.InputStream JavaDoc;
20 import org.omg.CORBA_2_3.portable.OutputStream JavaDoc;
21 import org.omg.CORBA.portable.ResponseHandler JavaDoc;
22 import org.omg.CORBA.portable.UnknownException JavaDoc;
23 import org.omg.PortableServer.Servant JavaDoc;
24 import org.omg.PortableServer.POA JavaDoc;
25 import org.omg.PortableServer.POAManager JavaDoc;
26
27 import com.sun.corba.se.spi.presentation.rmi.PresentationManager ;
28 import com.sun.corba.se.spi.presentation.rmi.IDLNameTranslator ;
29 import com.sun.corba.se.spi.presentation.rmi.DynamicMethodMarshaller ;
30
31 import com.sun.corba.se.spi.orb.ORB ;
32
33 import com.sun.corba.se.impl.logging.ORBUtilSystemException ;
34
35 import com.sun.corba.se.impl.oa.poa.POAManagerImpl ;
36
37 public final class ReflectiveTie extends Servant JavaDoc implements Tie JavaDoc
38 {
39     private Remote JavaDoc target = null ;
40     private PresentationManager pm ;
41     private PresentationManager.ClassData classData = null ;
42     private ORBUtilSystemException wrapper = null ;
43
44     public ReflectiveTie( PresentationManager pm, ORBUtilSystemException wrapper )
45     {
46         SecurityManager JavaDoc s = System.getSecurityManager();
47         if (s != null) {
48             s.checkPermission(new DynamicAccessPermission("access"));
49         }
50     this.pm = pm ;
51     this.wrapper = wrapper ;
52     }
53
54     public String JavaDoc[] _all_interfaces(org.omg.PortableServer.POA JavaDoc poa,
55     byte[] objectId)
56     {
57     return classData.getTypeIds() ;
58     }
59
60     public void setTarget(Remote JavaDoc target)
61     {
62         this.target = target;
63
64     if (target == null) {
65         classData = null ;
66     } else {
67         Class JavaDoc targetClass = target.getClass() ;
68         classData = pm.getClassData( targetClass ) ;
69     }
70     }
71     
72     public Remote JavaDoc getTarget()
73     {
74         return target;
75     }
76     
77     public org.omg.CORBA.Object JavaDoc thisObject()
78     {
79         return _this_object();
80     }
81     
82     public void deactivate()
83     {
84         try{
85         _poa().deactivate_object(_poa().servant_to_id(this));
86         } catch (org.omg.PortableServer.POAPackage.WrongPolicy JavaDoc exception){
87         // ignore
88
} catch (org.omg.PortableServer.POAPackage.ObjectNotActive JavaDoc exception){
89         // ignore
90
} catch (org.omg.PortableServer.POAPackage.ServantNotActive JavaDoc exception){
91         // ignore
92
}
93     }
94     
95     public org.omg.CORBA.ORB JavaDoc orb() {
96         return _orb();
97     }
98     
99     public void orb(org.omg.CORBA.ORB JavaDoc orb) {
100         try {
101         ORB myORB = (ORB)orb ;
102
103             ((org.omg.CORBA_2_3.ORB JavaDoc)orb).set_delegate(this);
104         } catch (ClassCastException JavaDoc e) {
105         throw wrapper.badOrbForServant( e ) ;
106         }
107     }
108     
109     public org.omg.CORBA.portable.OutputStream JavaDoc _invoke(String JavaDoc method,
110     org.omg.CORBA.portable.InputStream JavaDoc _in, ResponseHandler JavaDoc reply)
111     {
112     Method JavaDoc javaMethod = null ;
113     DynamicMethodMarshaller dmm = null;
114
115         try {
116             InputStream JavaDoc in = (InputStream JavaDoc) _in;
117
118         javaMethod = classData.getIDLNameTranslator().getMethod( method ) ;
119         if (javaMethod == null)
120         throw wrapper.methodNotFoundInTie( method,
121             target.getClass().getName() ) ;
122
123         dmm = pm.getDynamicMethodMarshaller( javaMethod ) ;
124
125         Object JavaDoc[] args = dmm.readArguments( in ) ;
126
127         Object JavaDoc result = javaMethod.invoke( target, args ) ;
128
129         OutputStream JavaDoc os = (OutputStream JavaDoc)reply.createReply() ;
130
131         dmm.writeResult( os, result ) ;
132
133         return os ;
134     } catch (IllegalAccessException JavaDoc ex) {
135         throw wrapper.invocationErrorInReflectiveTie( ex,
136         javaMethod.getName(),
137             javaMethod.getDeclaringClass().getName() ) ;
138     } catch (IllegalArgumentException JavaDoc ex) {
139         throw wrapper.invocationErrorInReflectiveTie( ex,
140         javaMethod.getName(),
141             javaMethod.getDeclaringClass().getName() ) ;
142     } catch (InvocationTargetException JavaDoc ex) {
143         // Unwrap the actual exception so that it can be wrapped by an
144
// UnknownException or thrown if it is a system exception.
145
// This is expected in the server dispatcher code.
146
Throwable JavaDoc thr = ex.getCause() ;
147         if (thr instanceof SystemException JavaDoc)
148         throw (SystemException JavaDoc)thr ;
149         else if ((thr instanceof Exception JavaDoc) &&
150         dmm.isDeclaredException( thr )) {
151         OutputStream JavaDoc os = (OutputStream JavaDoc)reply.createExceptionReply() ;
152         dmm.writeException( os, (Exception JavaDoc)thr ) ;
153         return os ;
154         } else
155         throw new UnknownException JavaDoc( thr ) ;
156         }
157     }
158 }
159
Popular Tags