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 
  26 package com.sun.corba.se.impl.presentation.rmi ;
  27 
  28 import java.io.ObjectInputStream ;
  29 import java.io.ObjectOutputStream ;
  30 import java.io.IOException ;
  31 import java.io.Serializable ;
  32 
  33 import java.rmi.RemoteException ;
  34 
  35 import javax.rmi.CORBA.Tie ;
  36 
  37 import org.omg.CORBA_2_3.portable.ObjectImpl ;
  38 
  39 import org.omg.CORBA.portable.Delegate ;
  40 import org.omg.CORBA.portable.OutputStream ;
  41 
  42 import org.omg.CORBA.SystemException ;
  43 import org.omg.CORBA.ORB ;
  44 
  45 import com.sun.corba.se.spi.orbutil.proxy.InvocationHandlerFactory ;
  46 import com.sun.corba.se.spi.presentation.rmi.PresentationManager ;
  47 import com.sun.corba.se.spi.presentation.rmi.StubAdapter ;
  48 import com.sun.corba.se.spi.presentation.rmi.DynamicStub ;
  49 import com.sun.corba.se.impl.presentation.rmi.StubConnectImpl ;
  50 import com.sun.corba.se.spi.logging.CORBALogDomains ;
  51 import com.sun.corba.se.impl.logging.UtilSystemException ;
  52 import com.sun.corba.se.impl.ior.StubIORImpl ;
  53 import com.sun.corba.se.impl.util.RepositoryId ;
  54 import com.sun.corba.se.impl.util.JDKBridge ;
  55 import com.sun.corba.se.impl.util.Utility ;
  56 
  57 // XXX Do we need _get_codebase?
  58 public class DynamicStubImpl extends ObjectImpl
  59     implements DynamicStub, Serializable
  60 {
  61     private static final long serialVersionUID = 4852612040012087675L;
  62 
  63     private String[] typeIds ;
  64     private StubIORImpl ior ;
  65     private DynamicStub self = null ;  // The actual DynamicProxy for this stub.
  66 
  67     public void setSelf( DynamicStub self )
  68     {
  69         // XXX Should probably only allow this once.
  70         this.self = self ;
  71     }
  72 
  73     public DynamicStub getSelf()
  74     {
  75         return self ;
  76     }
  77 
  78     public DynamicStubImpl( String[] typeIds )
  79     {
  80         this.typeIds = typeIds ;
  81         ior = null ;
  82     }
  83 
  84     public void setDelegate( Delegate delegate )
  85     {
  86         _set_delegate( delegate ) ;
  87     }
  88 
  89     public Delegate getDelegate()
  90     {
  91         return _get_delegate() ;
  92     }
  93 
  94     public ORB getORB()
  95     {
  96         return (ORB)_orb() ;
  97     }
  98 
  99     public String[] _ids()
 100     {
 101         return typeIds ;
 102     }
 103 
 104     public String[] getTypeIds()
 105     {
 106         return _ids() ;
 107     }
 108 
 109     public void connect( ORB orb ) throws RemoteException
 110     {
 111         ior = StubConnectImpl.connect( ior, self, this, orb ) ;
 112     }
 113 
 114     public boolean isLocal()
 115     {
 116         return _is_local() ;
 117     }
 118 
 119     public OutputStream request( String operation,
 120         boolean responseExpected )
 121     {
 122         return _request( operation, responseExpected ) ;
 123     }
 124 
 125     private void readObject( ObjectInputStream stream ) throws
 126         IOException, ClassNotFoundException
 127     {
 128         ior = new StubIORImpl() ;
 129         ior.doRead( stream ) ;
 130     }
 131 
 132     private void writeObject( ObjectOutputStream stream ) throws
 133         IOException
 134     {
 135         if (ior == null)
 136             ior = new StubIORImpl( this ) ;
 137         ior.doWrite( stream ) ;
 138     }
 139 
 140     public Object readResolve()
 141     {
 142         String repositoryId = ior.getRepositoryId() ;
 143         String cname = RepositoryId.cache.getId( repositoryId ).getClassName() ;
 144 
 145         Class cls = null ;
 146 
 147         try {
 148             cls = JDKBridge.loadClass( cname, null, null ) ;
 149         } catch (ClassNotFoundException exc) {
 150             // XXX log this
 151         }
 152 
 153         PresentationManager pm =
 154             com.sun.corba.se.spi.orb.ORB.getPresentationManager() ;
 155         PresentationManager.ClassData classData = pm.getClassData( cls ) ;
 156         InvocationHandlerFactoryImpl ihfactory =
 157             (InvocationHandlerFactoryImpl)classData.getInvocationHandlerFactory() ;
 158         return ihfactory.getInvocationHandler( this ) ;
 159     }
 160 }