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