1 /*
   2  * Copyright (c) 2002, 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.resolver ;
  27 
  28 import org.omg.CORBA.portable.InputStream ;
  29 import org.omg.CORBA.portable.OutputStream ;
  30 import org.omg.CORBA.portable.ApplicationException ;
  31 import org.omg.CORBA.portable.RemarshalException ;
  32 
  33 import com.sun.corba.se.spi.ior.IOR ;
  34 import com.sun.corba.se.spi.ior.IORFactories ;
  35 import com.sun.corba.se.spi.ior.IORTemplate ;
  36 import com.sun.corba.se.spi.ior.ObjectKey ;
  37 import com.sun.corba.se.spi.ior.ObjectKeyFactory ;
  38 import com.sun.corba.se.spi.ior.iiop.IIOPAddress ;
  39 import com.sun.corba.se.spi.ior.iiop.IIOPProfileTemplate ;
  40 import com.sun.corba.se.spi.ior.iiop.IIOPFactories ;
  41 import com.sun.corba.se.spi.ior.iiop.GIOPVersion ;
  42 import com.sun.corba.se.spi.logging.CORBALogDomains ;
  43 import com.sun.corba.se.spi.orb.ORB ;
  44 import com.sun.corba.se.spi.resolver.Resolver ;
  45 
  46 import com.sun.corba.se.impl.logging.ORBUtilSystemException ;
  47 import com.sun.corba.se.impl.orbutil.ORBUtility ;
  48 
  49 public class BootstrapResolverImpl implements Resolver {
  50     private org.omg.CORBA.portable.Delegate bootstrapDelegate ;
  51     private ORBUtilSystemException wrapper ;
  52 
  53     public BootstrapResolverImpl(ORB orb, String host, int port) {
  54         wrapper = ORBUtilSystemException.get( orb,
  55             CORBALogDomains.ORB_RESOLVER ) ;
  56 
  57         // Create a new IOR with the magic of INIT
  58         byte[] initialKey = "INIT".getBytes() ;
  59         ObjectKey okey = orb.getObjectKeyFactory().create(initialKey) ;
  60 
  61         IIOPAddress addr = IIOPFactories.makeIIOPAddress( orb, host, port ) ;
  62         IIOPProfileTemplate ptemp = IIOPFactories.makeIIOPProfileTemplate(
  63             orb, GIOPVersion.V1_0, addr);
  64 
  65         IORTemplate iortemp = IORFactories.makeIORTemplate( okey.getTemplate() ) ;
  66         iortemp.add( ptemp ) ;
  67 
  68         IOR initialIOR = iortemp.makeIOR( (com.sun.corba.se.spi.orb.ORB)orb,
  69             "", okey.getId() ) ;
  70 
  71         bootstrapDelegate = ORBUtility.makeClientDelegate( initialIOR ) ;
  72     }
  73 
  74     /**
  75      * For the BootStrap operation we do not expect to have more than one
  76      * parameter. We do not want to extend BootStrap protocol any further,
  77      * as INS handles most of what BootStrap can handle in a portable way.
  78      *
  79      * @return InputStream which contains the response from the
  80      * BootStrapOperation.
  81      */
  82     private InputStream invoke( String operationName, String parameter )
  83     {
  84         boolean remarshal = true;
  85 
  86         // Invoke.
  87 
  88         InputStream inStream = null;
  89 
  90         // If there is a location forward then you will need
  91         // to invoke again on the updated information.
  92         // Just calling this same routine with the same host/port
  93         // does not take the location forward info into account.
  94 
  95         while (remarshal) {
  96             org.omg.CORBA.Object objref = null ;
  97             remarshal = false;
  98 
  99             OutputStream os = (OutputStream) bootstrapDelegate.request( objref,
 100                 operationName, true);
 101 
 102             if ( parameter != null ) {
 103                 os.write_string( parameter );
 104             }
 105 
 106             try {
 107                 // The only reason a null objref is passed is to get the version of
 108                 // invoke used by streams.  Otherwise the PortableInterceptor
 109                 // call stack will become unbalanced since the version of
 110                 // invoke which only takes the stream does not call
 111                 // PortableInterceptor ending points.
 112                 // Note that the first parameter is ignored inside invoke.
 113 
 114                 inStream = bootstrapDelegate.invoke( objref, os);
 115             } catch (ApplicationException e) {
 116                 throw wrapper.bootstrapApplicationException( e ) ;
 117             } catch (RemarshalException e) {
 118                 // XXX log this
 119                 remarshal = true;
 120             }
 121         }
 122 
 123         return inStream;
 124     }
 125 
 126     public org.omg.CORBA.Object resolve( String identifier )
 127     {
 128         InputStream inStream = null ;
 129         org.omg.CORBA.Object result = null ;
 130 
 131         try {
 132             inStream = invoke( "get", identifier ) ;
 133 
 134             result = inStream.read_Object();
 135 
 136             // NOTE: do note trap and ignore errors.
 137             // Let them flow out.
 138         } finally {
 139             bootstrapDelegate.releaseReply( null, inStream ) ;
 140         }
 141 
 142         return result ;
 143     }
 144 
 145     public java.util.Set list()
 146     {
 147         InputStream inStream = null ;
 148         java.util.Set result = new java.util.HashSet() ;
 149 
 150         try {
 151             inStream = invoke( "list", null ) ;
 152 
 153             int count = inStream.read_long();
 154             for (int i=0; i < count; i++)
 155                 result.add( inStream.read_string() ) ;
 156 
 157             // NOTE: do note trap and ignore errors.
 158             // Let them flow out.
 159         } finally {
 160             bootstrapDelegate.releaseReply( null, inStream ) ;
 161         }
 162 
 163         return result ;
 164     }
 165 }