StubAdapter.java revision 608:7e06bf1dcb09
1/* 2 * Copyright (c) 2003, 2004, 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.spi.presentation.rmi ; 27 28import javax.rmi.CORBA.Tie ; 29 30import org.omg.CORBA.portable.Delegate ; 31import org.omg.CORBA.portable.ObjectImpl ; 32import org.omg.CORBA.portable.OutputStream ; 33 34import org.omg.PortableServer.POA ; 35import org.omg.PortableServer.POAManager ; 36import org.omg.PortableServer.Servant ; 37 38import org.omg.PortableServer.POAPackage.WrongPolicy ; 39import org.omg.PortableServer.POAPackage.ServantNotActive ; 40import org.omg.PortableServer.POAManagerPackage.AdapterInactive ; 41 42import org.omg.CORBA.ORB ; 43 44import com.sun.corba.se.spi.logging.CORBALogDomains ; 45import com.sun.corba.se.impl.logging.ORBUtilSystemException ; 46 47// XXX Getting rid of this requires introducing an ObjectAdapterManager abstraction 48// as an interface into the OA framework. 49import com.sun.corba.se.impl.oa.poa.POAManagerImpl ; 50 51/** Provide access to stub delegate and type id information 52 * independent of the stub type. This class exists because 53 * ObjectImpl does not have an interface for the 3 delegate and 54 * type id methods, so a DynamicStub has a different type. 55 * We cannot simply change ObjectImpl as it is a standard API. 56 * We also cannot change the code generation of Stubs, as that 57 * is also standard. Hence I am left with this ugly class. 58 */ 59public abstract class StubAdapter 60{ 61 private StubAdapter() {} 62 63 private static ORBUtilSystemException wrapper = 64 ORBUtilSystemException.get( CORBALogDomains.RPC_PRESENTATION ) ; 65 66 public static boolean isStubClass( Class cls ) 67 { 68 return (ObjectImpl.class.isAssignableFrom( cls )) || 69 (DynamicStub.class.isAssignableFrom( cls )) ; 70 } 71 72 public static boolean isStub( Object stub ) 73 { 74 return (stub instanceof DynamicStub) || 75 (stub instanceof ObjectImpl) ; 76 } 77 78 public static void setDelegate( Object stub, Delegate delegate ) 79 { 80 if (stub instanceof DynamicStub) 81 ((DynamicStub)stub).setDelegate( delegate ) ; 82 else if (stub instanceof ObjectImpl) 83 ((ObjectImpl)stub)._set_delegate( delegate ) ; 84 else 85 throw wrapper.setDelegateRequiresStub() ; 86 } 87 88 /** Use implicit activation to get an object reference for the servant. 89 */ 90 public static org.omg.CORBA.Object activateServant( Servant servant ) 91 { 92 POA poa = servant._default_POA() ; 93 org.omg.CORBA.Object ref = null ; 94 95 try { 96 ref = poa.servant_to_reference( servant ) ; 97 } catch (ServantNotActive sna) { 98 throw wrapper.getDelegateServantNotActive( sna ) ; 99 } catch (WrongPolicy wp) { 100 throw wrapper.getDelegateWrongPolicy( wp ) ; 101 } 102 103 // Make sure that the POAManager is activated if no other 104 // POAManager state management has taken place. 105 POAManager mgr = poa.the_POAManager() ; 106 if (mgr instanceof POAManagerImpl) { 107 POAManagerImpl mgrImpl = (POAManagerImpl)mgr ; 108 mgrImpl.implicitActivation() ; 109 } 110 111 return ref ; 112 } 113 114 /** Given any Tie, return the corresponding object refernce, activating 115 * the Servant if necessary. 116 */ 117 public static org.omg.CORBA.Object activateTie( Tie tie ) 118 { 119 /** Any implementation of Tie should be either a Servant or an ObjectImpl, 120 * depending on which style of code generation is used. rmic -iiop by 121 * default results in an ObjectImpl-based Tie, while rmic -iiop -poa 122 * results in a Servant-based Tie. Dynamic RMI-IIOP also uses Servant-based 123 * Ties (see impl.presentation.rmi.ReflectiveTie). 124 */ 125 if (tie instanceof ObjectImpl) { 126 return tie.thisObject() ; 127 } else if (tie instanceof Servant) { 128 Servant servant = (Servant)tie ; 129 return activateServant( servant ) ; 130 } else { 131 throw wrapper.badActivateTieCall() ; 132 } 133 } 134 135 136 /** This also gets the delegate from a Servant by 137 * using Servant._this_object() 138 */ 139 public static Delegate getDelegate( Object stub ) 140 { 141 if (stub instanceof DynamicStub) 142 return ((DynamicStub)stub).getDelegate() ; 143 else if (stub instanceof ObjectImpl) 144 return ((ObjectImpl)stub)._get_delegate() ; 145 else if (stub instanceof Tie) { 146 Tie tie = (Tie)stub ; 147 org.omg.CORBA.Object ref = activateTie( tie ) ; 148 return getDelegate( ref ) ; 149 } else 150 throw wrapper.getDelegateRequiresStub() ; 151 } 152 153 public static ORB getORB( Object stub ) 154 { 155 if (stub instanceof DynamicStub) 156 return ((DynamicStub)stub).getORB() ; 157 else if (stub instanceof ObjectImpl) 158 return (ORB)((ObjectImpl)stub)._orb() ; 159 else 160 throw wrapper.getOrbRequiresStub() ; 161 } 162 163 public static String[] getTypeIds( Object stub ) 164 { 165 if (stub instanceof DynamicStub) 166 return ((DynamicStub)stub).getTypeIds() ; 167 else if (stub instanceof ObjectImpl) 168 return ((ObjectImpl)stub)._ids() ; 169 else 170 throw wrapper.getTypeIdsRequiresStub() ; 171 } 172 173 public static void connect( Object stub, 174 ORB orb ) throws java.rmi.RemoteException 175 { 176 if (stub instanceof DynamicStub) 177 ((DynamicStub)stub).connect( 178 (com.sun.corba.se.spi.orb.ORB)orb ) ; 179 else if (stub instanceof javax.rmi.CORBA.Stub) 180 ((javax.rmi.CORBA.Stub)stub).connect( orb ) ; 181 else if (stub instanceof ObjectImpl) 182 orb.connect( (org.omg.CORBA.Object)stub ) ; 183 else 184 throw wrapper.connectRequiresStub() ; 185 } 186 187 public static boolean isLocal( Object stub ) 188 { 189 if (stub instanceof DynamicStub) 190 return ((DynamicStub)stub).isLocal() ; 191 else if (stub instanceof ObjectImpl) 192 return ((ObjectImpl)stub)._is_local() ; 193 else 194 throw wrapper.isLocalRequiresStub() ; 195 } 196 197 public static OutputStream request( Object stub, 198 String operation, boolean responseExpected ) 199 { 200 if (stub instanceof DynamicStub) 201 return ((DynamicStub)stub).request( operation, 202 responseExpected ) ; 203 else if (stub instanceof ObjectImpl) 204 return ((ObjectImpl)stub)._request( operation, 205 responseExpected ) ; 206 else 207 throw wrapper.requestRequiresStub() ; 208 } 209} 210