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 com.sun.corba.se.spi.orb.ORB;
  29 import org.omg.PortableInterceptor.Current;
  30 import org.omg.PortableInterceptor.InvalidSlot;
  31 import org.omg.CORBA.Any;
  32 import org.omg.CORBA.BAD_INV_ORDER;
  33 import org.omg.CORBA.CompletionStatus;
  34 
  35 import com.sun.corba.se.spi.logging.CORBALogDomains ;
  36 import com.sun.corba.se.impl.logging.OMGSystemException ;
  37 
  38 /**
  39  * PICurrent is the implementation of Current as specified in the Portable
  40  * Interceptors Spec orbos/99-12-02.
  41  * IMPORTANT: PICurrent is implemented with the assumption that get_slot()
  42  * or set_slot() will not be called in ORBInitializer.pre_init() and
  43  * post_init().
  44  */
  45 public class PICurrent extends org.omg.CORBA.LocalObject
  46     implements Current
  47 {
  48     // slotCounter is used to keep track of ORBInitInfo.allocate_slot_id()
  49     private int slotCounter;
  50 
  51     // The ORB associated with this PICurrent object.
  52     private ORB myORB;
  53 
  54     private OMGSystemException wrapper ;
  55 
  56     // True if the orb is still initialzing and get_slot and set_slot are not
  57     // to be called.
  58     private boolean orbInitializing;
  59 
  60     // ThreadLocal contains a stack of SlotTable which are used
  61     // for resolve_initial_references( "PICurrent" );
  62     private ThreadLocal threadLocalSlotTable
  63         = new ThreadLocal( ) {
  64             protected Object initialValue( ) {
  65                 SlotTable table = new SlotTable( myORB, slotCounter );
  66                 return new SlotTableStack( myORB, table );
  67             }
  68         };
  69 
  70     /**
  71      * PICurrent constructor which will be called for every ORB
  72      * initialization.
  73      */
  74     PICurrent( ORB myORB ) {
  75         this.myORB = myORB;
  76         wrapper = OMGSystemException.get( myORB,
  77             CORBALogDomains.RPC_PROTOCOL ) ;
  78         this.orbInitializing = true;
  79         slotCounter = 0;
  80     }
  81 
  82 
  83     /**
  84      * This method will be called from ORBInitInfo.allocate_slot_id( ).
  85      * simply returns a slot id by incrementing slotCounter.
  86      */
  87     int allocateSlotId( ) {
  88         int slotId = slotCounter;
  89         slotCounter = slotCounter + 1;
  90         return slotId;
  91     }
  92 
  93 
  94     /**
  95      * This method gets the SlotTable which is on the top of the
  96      * ThreadLocalStack.
  97      */
  98     SlotTable getSlotTable( ) {
  99         SlotTable table = (SlotTable)
 100                 ((SlotTableStack)threadLocalSlotTable.get()).peekSlotTable();
 101         return table;
 102     }
 103 
 104     /**
 105      * This method pushes a SlotTable on the SlotTableStack. When there is
 106      * a resolve_initial_references("PICurrent") after this call. The new
 107      * PICurrent will be returned.
 108      */
 109     void pushSlotTable( ) {
 110         SlotTableStack st = (SlotTableStack)threadLocalSlotTable.get();
 111         st.pushSlotTable( );
 112     }
 113 
 114 
 115     /**
 116      * This method pops a SlotTable on the SlotTableStack.
 117      */
 118     void popSlotTable( ) {
 119         SlotTableStack st = (SlotTableStack)threadLocalSlotTable.get();
 120         st.popSlotTable( );
 121     }
 122 
 123     /**
 124      * This method sets the slot data at the given slot id (index) in the
 125      * Slot Table which is on the top of the SlotTableStack.
 126      */
 127     public void set_slot( int id, Any data ) throws InvalidSlot
 128     {
 129         if( orbInitializing ) {
 130             // As per ptc/00-08-06 if the ORB is still initializing, disallow
 131             // calls to get_slot and set_slot.  If an attempt is made to call,
 132             // throw a BAD_INV_ORDER.
 133             throw wrapper.invalidPiCall3() ;
 134         }
 135 
 136         getSlotTable().set_slot( id, data );
 137     }
 138 
 139     /**
 140      * This method gets the slot data at the given slot id (index) from the
 141      * Slot Table which is on the top of the SlotTableStack.
 142      */
 143     public Any get_slot( int id ) throws InvalidSlot
 144     {
 145         if( orbInitializing ) {
 146             // As per ptc/00-08-06 if the ORB is still initializing, disallow
 147             // calls to get_slot and set_slot.  If an attempt is made to call,
 148             // throw a BAD_INV_ORDER.
 149             throw wrapper.invalidPiCall4() ;
 150         }
 151 
 152         return getSlotTable().get_slot( id );
 153     }
 154 
 155     /**
 156      * This method resets all the slot data to null in the
 157      * Slot Table which is on the top of SlotTableStack.
 158      */
 159     void resetSlotTable( ) {
 160         getSlotTable().resetSlots();
 161     }
 162 
 163     /**
 164      * Called from ORB when the ORBInitializers are about to start
 165      * initializing.
 166      */
 167     void setORBInitializing( boolean init ) {
 168         this.orbInitializing = init;
 169     }
 170 }