StubConnectImpl.java revision 608:7e06bf1dcb09
1/* 2 * Copyright (c) 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.impl.presentation.rmi ; 27 28import java.rmi.RemoteException; 29 30import javax.rmi.CORBA.Tie; 31 32import org.omg.CORBA.ORB; 33import org.omg.CORBA.SystemException; 34import org.omg.CORBA.BAD_OPERATION; 35import org.omg.CORBA.BAD_INV_ORDER; 36 37import org.omg.CORBA.portable.ObjectImpl; 38import org.omg.CORBA.portable.Delegate; 39 40import com.sun.corba.se.spi.presentation.rmi.StubAdapter; 41 42import com.sun.corba.se.spi.logging.CORBALogDomains ; 43 44import com.sun.corba.se.impl.util.Utility; 45 46import com.sun.corba.se.impl.ior.StubIORImpl ; 47 48import com.sun.corba.se.impl.logging.UtilSystemException ; 49 50import com.sun.corba.se.impl.corba.CORBAObjectImpl ; 51 52public abstract class StubConnectImpl 53{ 54 static UtilSystemException wrapper = UtilSystemException.get( 55 CORBALogDomains.RMIIIOP ) ; 56 57 /** Connect the stub to the orb if necessary. 58 * @param ior The StubIORImpl for this stub (may be null) 59 * @param proxy The externally visible stub seen by the user (may be the same as stub) 60 * @param stub The stub implementation that extends ObjectImpl 61 * @param orb The ORB to which we connect the stub. 62 */ 63 public static StubIORImpl connect( StubIORImpl ior, org.omg.CORBA.Object proxy, 64 org.omg.CORBA.portable.ObjectImpl stub, ORB orb ) throws RemoteException 65 { 66 Delegate del = null ; 67 68 try { 69 try { 70 del = StubAdapter.getDelegate( stub ); 71 72 if (del.orb(stub) != orb) 73 throw wrapper.connectWrongOrb() ; 74 } catch (org.omg.CORBA.BAD_OPERATION err) { 75 if (ior == null) { 76 // No IOR, can we get a Tie for this stub? 77 Tie tie = (javax.rmi.CORBA.Tie) Utility.getAndForgetTie(proxy); 78 if (tie == null) 79 throw wrapper.connectNoTie() ; 80 81 // Is the tie already connected? If it is, check that it's 82 // connected to the same ORB, otherwise connect it. 83 ORB existingOrb = orb ; 84 try { 85 existingOrb = tie.orb(); 86 } catch (BAD_OPERATION exc) { 87 // Thrown when tie is an ObjectImpl and its delegate is not set. 88 tie.orb(orb); 89 } catch (BAD_INV_ORDER exc) { 90 // Thrown when tie is a Servant and its delegate is not set. 91 tie.orb(orb); 92 } 93 94 if (existingOrb != orb) 95 throw wrapper.connectTieWrongOrb() ; 96 97 // Get the delegate for the stub from the tie. 98 del = StubAdapter.getDelegate( tie ) ; 99 ObjectImpl objref = new CORBAObjectImpl() ; 100 objref._set_delegate( del ) ; 101 ior = new StubIORImpl( objref ) ; 102 } else { 103 // ior is initialized, so convert ior to an object, extract 104 // the delegate, and set it on ourself 105 del = ior.getDelegate( orb ) ; 106 } 107 108 StubAdapter.setDelegate( stub, del ) ; 109 } 110 } catch (SystemException exc) { 111 throw new RemoteException("CORBA SystemException", exc ); 112 } 113 114 return ior ; 115 } 116} 117