1 /*
   2  * Copyright (c) 2000, 2003, 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.interceptors;
  27 
  28 import java.util.Iterator ;
  29 
  30 import org.omg.IOP.TaggedComponent;
  31 
  32 import org.omg.CORBA.BAD_INV_ORDER;
  33 import org.omg.CORBA.BAD_PARAM;
  34 import org.omg.CORBA.INTERNAL;
  35 import org.omg.CORBA.CompletionStatus;
  36 import org.omg.CORBA.INV_POLICY;
  37 import org.omg.CORBA.NO_IMPLEMENT;
  38 import org.omg.CORBA.Policy;
  39 import org.omg.CORBA.LocalObject;
  40 
  41 import org.omg.PortableInterceptor.IORInfo;
  42 import org.omg.PortableInterceptor.ObjectReferenceTemplate;
  43 import org.omg.PortableInterceptor.ObjectReferenceFactory;
  44 
  45 import com.sun.corba.se.spi.orb.ORB ;
  46 
  47 import com.sun.corba.se.spi.oa.ObjectAdapter;
  48 
  49 import com.sun.corba.se.spi.legacy.interceptor.IORInfoExt;
  50 import com.sun.corba.se.spi.legacy.interceptor.UnknownType;
  51 
  52 import com.sun.corba.se.spi.ior.IORTemplate;
  53 import com.sun.corba.se.spi.ior.TaggedProfileTemplate;
  54 import com.sun.corba.se.spi.ior.TaggedComponentFactoryFinder ;
  55 
  56 import com.sun.corba.se.spi.logging.CORBALogDomains ;
  57 
  58 import com.sun.corba.se.impl.logging.InterceptorsSystemException ;
  59 import com.sun.corba.se.impl.logging.OMGSystemException ;
  60 import com.sun.corba.se.impl.logging.ORBUtilSystemException ;
  61 
  62 /**
  63  * IORInfoImpl is the implementation of the IORInfo class, as described
  64  * in orbos/99-12-02, section 7.
  65  */
  66 public final class IORInfoImpl
  67     extends LocalObject
  68     implements IORInfo, IORInfoExt
  69 {
  70     // State values that determine which methods are allowed.
  71     // get_effective_policy, manager_id, and adapter_state are valid unless STATE_DONE
  72 
  73     // add_component, and add_component_to_profile are valid.
  74     private static final int STATE_INITIAL = 0 ;
  75 
  76     // adapter_template, and R/W to current_factory are valid
  77     private static final int STATE_ESTABLISHED = 1 ;
  78 
  79     // No methods are valid in this state
  80     private static final int STATE_DONE = 2 ;
  81 
  82     // The current state of this object
  83     private int state = STATE_INITIAL ;
  84 
  85     // The ObjectAdapter associated with this IORInfo object.
  86     private ObjectAdapter adapter;
  87 
  88     private ORB orb ;
  89 
  90     private ORBUtilSystemException orbutilWrapper ;
  91     private InterceptorsSystemException wrapper ;
  92     private OMGSystemException omgWrapper ;
  93 
  94     /**
  95      * Creates a new IORInfo implementation.  This info object will establish
  96      * tagged components with the template for the provided IOR Template.
  97      */
  98     IORInfoImpl( ObjectAdapter adapter ) {
  99         this.orb = adapter.getORB() ;
 100 
 101         orbutilWrapper = ORBUtilSystemException.get( orb,
 102             CORBALogDomains.RPC_PROTOCOL ) ;
 103         wrapper = InterceptorsSystemException.get( orb,
 104             CORBALogDomains.RPC_PROTOCOL ) ;
 105         omgWrapper = OMGSystemException.get( orb,
 106             CORBALogDomains.RPC_PROTOCOL ) ;
 107 
 108         this.adapter = adapter;
 109     }
 110 
 111     /**
 112      * An ORB service implementation may determine what server side policy
 113      * of a particular type is in effect for an IOR being constructed by
 114      * calling the get_effective_policy operation.  When the IOR being
 115      * constructed is for an object implemented using a POA, all Policy
 116      * objects passed to the PortableServer::POA::create_POA call that
 117      * created that POA are accessible via get_effective_policy.
 118      * <p>
 119      * If a policy for the given type is not known to the ORB, then this
 120      * operation will raise INV_POLICY with a standard minor code of 2.
 121      *
 122      * @param type The CORBA::PolicyType specifying the type of policy to
 123      *   return.
 124      * @return The effective CORBA::Policy object of the requested type.
 125      *   If the given policy type is known, but no policy of that tpye is
 126      *   in effect, then this operation will return a nil object reference.
 127      */
 128     public Policy get_effective_policy (int type) {
 129         checkState( STATE_INITIAL, STATE_ESTABLISHED ) ;
 130 
 131         return adapter.getEffectivePolicy( type );
 132     }
 133 
 134     /**
 135      * A portable ORB service implementation calls this method from its
 136      * implementation of establish_components to add a tagged component to
 137      * the set which will be included when constructing IORs.  The
 138      * components in this set will be included in all profiles.
 139      * <p>
 140      * Any number of components may exist with the same component ID.
 141      *
 142      * @param tagged_component The IOP::TaggedComponent to add
 143      */
 144     public void add_ior_component (TaggedComponent tagged_component) {
 145         checkState( STATE_INITIAL ) ;
 146 
 147         if( tagged_component == null ) nullParam();
 148         addIORComponentToProfileInternal( tagged_component,
 149                                           adapter.getIORTemplate().iterator());
 150     }
 151 
 152     /**
 153      * A portable ORB service implementation calls this method from its
 154      * implementation of establish_components to add a tagged component to
 155      * the set which will be included when constructing IORs.  The
 156      * components in this set will be included in the specified profile.
 157      * <p>
 158      * Any number of components may exist with the same component ID.
 159      * <p>
 160      * If the given profile ID does not define a known profile or it is
 161      * impossible to add components to thgat profile, BAD_PARAM is raised
 162      * with a minor code of TBD_BP + 3.
 163      *
 164      * @param tagged_component The IOP::TaggedComponent to add.
 165      * @param profile_id The IOP::ProfileId tof the profile to which this
 166      *     component will be added.
 167      */
 168     public void add_ior_component_to_profile (
 169         TaggedComponent tagged_component, int profile_id )
 170     {
 171         checkState( STATE_INITIAL ) ;
 172 
 173         if( tagged_component == null ) nullParam();
 174         addIORComponentToProfileInternal(
 175             tagged_component, adapter.getIORTemplate().iteratorById(
 176             profile_id ) );
 177     }
 178 
 179     /**
 180      * @param type The type of the server port
 181      *     (see connection.ORBSocketFactory for discussion).
 182      * @return The listen port number for that type.
 183      * @throws UnknownType if no port of the given type is found.
 184      */
 185     public int getServerPort(String type)
 186         throws UnknownType
 187     {
 188         checkState( STATE_INITIAL, STATE_ESTABLISHED ) ;
 189 
 190         int port =
 191             orb.getLegacyServerSocketManager()
 192                 .legacyGetTransientOrPersistentServerPort(type);
 193         if (port == -1) {
 194             throw new UnknownType();
 195         }
 196         return port;
 197     }
 198 
 199     public ObjectAdapter getObjectAdapter()
 200     {
 201         return adapter;
 202     }
 203 
 204     public int manager_id()
 205     {
 206         checkState( STATE_INITIAL, STATE_ESTABLISHED) ;
 207 
 208         return adapter.getManagerId() ;
 209     }
 210 
 211     public short state()
 212     {
 213         checkState( STATE_INITIAL, STATE_ESTABLISHED) ;
 214 
 215         return adapter.getState() ;
 216     }
 217 
 218     public ObjectReferenceTemplate adapter_template()
 219     {
 220         checkState( STATE_ESTABLISHED) ;
 221 
 222         // At this point, the iortemp must contain only a single
 223         // IIOPProfileTemplate.  This is a restriction of our
 224         // implementation.  Also, note the the ObjectReferenceTemplate
 225         // is called when a certain POA is created in a certain ORB
 226         // in a certain server, so the server_id, orb_id, and
 227         // poa_id operations must be well-defined no matter what
 228         // kind of implementation is used: e.g., if a POA creates
 229         // IORs with multiple profiles, they must still all agree
 230         // about this information.  Thus, we are justified in
 231         // extracting the single IIOPProfileTemplate to create
 232         // an ObjectReferenceTemplate here.
 233 
 234         return adapter.getAdapterTemplate() ;
 235     }
 236 
 237     public ObjectReferenceFactory current_factory()
 238     {
 239         checkState( STATE_ESTABLISHED) ;
 240 
 241         return adapter.getCurrentFactory() ;
 242     }
 243 
 244     public void current_factory( ObjectReferenceFactory factory )
 245     {
 246         checkState( STATE_ESTABLISHED) ;
 247 
 248         adapter.setCurrentFactory( factory ) ;
 249     }
 250 
 251     /**
 252      * Internal utility method to add an IOR component to the set of profiles
 253      * present in the iterator.
 254      */
 255     private void addIORComponentToProfileInternal(
 256         TaggedComponent tagged_component, Iterator iterator )
 257     {
 258         // Convert the given IOP::TaggedComponent into the appropriate
 259         // type for the TaggedProfileTemplate
 260         TaggedComponentFactoryFinder finder =
 261             orb.getTaggedComponentFactoryFinder();
 262         Object newTaggedComponent = finder.create( orb, tagged_component );
 263 
 264         // Iterate through TaggedProfileTemplates and add the given tagged
 265         // component to the appropriate one(s).
 266         boolean found = false;
 267         while( iterator.hasNext() ) {
 268             found = true;
 269             TaggedProfileTemplate taggedProfileTemplate =
 270                 (TaggedProfileTemplate)iterator.next();
 271             taggedProfileTemplate.add( newTaggedComponent );
 272         }
 273 
 274         // If no profile was found with the given id, throw a BAD_PARAM:
 275         // (See orbos/00-08-06, section 21.5.3.3.)
 276         if( !found ) {
 277             throw omgWrapper.invalidProfileId() ;
 278         }
 279     }
 280 
 281     /**
 282      * Called when an invalid null parameter was passed.  Throws a
 283      * BAD_PARAM with a minor code of 1
 284      */
 285     private void nullParam()
 286     {
 287         throw orbutilWrapper.nullParam() ;
 288     }
 289 
 290     // REVISIT: add minor codes!
 291 
 292     private void checkState( int expectedState )
 293     {
 294         if (expectedState != state)
 295             throw wrapper.badState1( new Integer(expectedState), new Integer(state) ) ;
 296     }
 297 
 298     private void checkState( int expectedState1, int expectedState2 )
 299     {
 300         if ((expectedState1 != state) && (expectedState2 != state))
 301             throw wrapper.badState2( new Integer(expectedState1),
 302                 new Integer(expectedState2), new Integer(state) ) ;
 303     }
 304 
 305     void makeStateEstablished()
 306     {
 307         checkState( STATE_INITIAL ) ;
 308 
 309         state = STATE_ESTABLISHED ;
 310     }
 311 
 312     void makeStateDone()
 313     {
 314         checkState( STATE_ESTABLISHED ) ;
 315 
 316         state = STATE_DONE ;
 317     }
 318 }