corba/src/share/classes/com/sun/corba/se/impl/interceptors/ThreadCurrentStack.sjava
changeset 20911 1e2eaea46334
parent 20910 e15ddd3505c6
parent 19308 b5ed503c26ad
child 20912 0b31a2650a3c
equal deleted inserted replaced
20910:e15ddd3505c6 20911:1e2eaea46334
     1 /*
       
     2  * Copyright (c) 2000, 2002, 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.impl.corba.AnyImpl;
       
    29 import org.omg.PortableInterceptor.Current;
       
    30 import org.omg.PortableInterceptor.InvalidSlot;
       
    31 
       
    32 import com.sun.corba.se.impl.util.MinorCodes;
       
    33 import com.sun.corba.se.impl.core.ORB;
       
    34 
       
    35 /**
       
    36  * ThreadCurrentStack is the container of PICurrent instances for each thread
       
    37  */
       
    38 public class ThreadCurrentStack
       
    39 {
       
    40     // PICurrentPool is the container for reusable PICurrents
       
    41     private class PICurrentPool {
       
    42 
       
    43         // Contains a list of reusable PICurrents
       
    44         private java.util.ArrayList pool;
       
    45 
       
    46         // High water mark for the pool
       
    47         // If the pool size reaches this limit then putPICurrent will
       
    48         // not put PICurrent to the pool.
       
    49         private static final int  HIGH_WATER_MARK = 5;
       
    50 
       
    51         // currentIndex points to the last PICurrent in the list
       
    52         private int currentIndex;
       
    53 
       
    54         PICurrentPool( ) {
       
    55             pool = new java.util.ArrayList( HIGH_WATER_MARK );
       
    56             currentIndex = 0;
       
    57         }
       
    58 
       
    59         /**
       
    60          * Puts PICurrent to the re-usable pool.
       
    61          */
       
    62         void putPICurrent( PICurrent current ) {
       
    63             // If there are enough PICurrents in the pool, then don't add
       
    64             // this current to the pool.
       
    65             if( currentIndex >= HIGH_WATER_MARK ) {
       
    66                 return;
       
    67             }
       
    68             pool.add(currentIndex , current);
       
    69             currentIndex++;
       
    70         }
       
    71 
       
    72         /**
       
    73          * Gets PICurrent from the re-usable pool.
       
    74          */
       
    75         PICurrent getPICurrent( ) {
       
    76             // If there are no entries in the pool then return null
       
    77             if( currentIndex == 0 ) {
       
    78                 return null;
       
    79             }
       
    80             // Works like a stack, Gets the last one added first
       
    81             currentIndex--;
       
    82             return (PICurrent) pool.get(currentIndex);
       
    83         }
       
    84     }
       
    85    
       
    86     // Contains all the active PICurrents for each thread.
       
    87     // The ArrayList is made to behave like a stack.
       
    88     private java.util.ArrayList currentContainer;
       
    89 
       
    90     // Keeps track of number of PICurrents in the stack.
       
    91     private int currentIndex;
       
    92  
       
    93     // For Every Thread there will be a pool of re-usable ThreadCurrent's
       
    94     // stored in PICurrentPool
       
    95     private PICurrentPool currentPool;
       
    96 
       
    97     // The orb associated with this ThreadCurrentStack
       
    98     private ORB piOrb;
       
    99 
       
   100     /**
       
   101      * Constructs the stack and and PICurrentPool
       
   102      */
       
   103     ThreadCurrentStack( ORB piOrb, PICurrent current ) {
       
   104        this.piOrb = piOrb;
       
   105        currentIndex = 0;
       
   106        currentContainer = new java.util.ArrayList( );
       
   107        currentPool = new PICurrentPool( );
       
   108        currentContainer.add( currentIndex, current );
       
   109        currentIndex++;
       
   110     }
       
   111    
       
   112 
       
   113     /**
       
   114      * pushPICurrent goes through the following steps
       
   115      * 1: Checks to see if there is any PICurrent in PICurrentPool
       
   116      *    If present then use that instance to push into the ThreadCurrentStack
       
   117      *
       
   118      * 2:If there is no PICurrent in the pool, then creates a new one and pushes
       
   119      *    that into the ThreadCurrentStack
       
   120      */
       
   121     void pushPICurrent( ) {
       
   122         PICurrent current = currentPool.getPICurrent( );
       
   123         if( current == null ) {
       
   124             // get an existing PICurrent to get the slotSize
       
   125             PICurrent currentTemp = peekPICurrent();
       
   126             current = new PICurrent( piOrb, currentTemp.getSlotSize( ));
       
   127         }
       
   128         currentContainer.add( currentIndex, current );
       
   129         currentIndex++;
       
   130     }
       
   131 
       
   132     /**
       
   133      * popPICurrent does the following
       
   134      * 1: pops the top PICurrent in the ThreadCurrentStack
       
   135      *
       
   136      * 2: resets the slots in the PICurrent which resets the slotvalues to
       
   137      *    null if there are any previous sets. 
       
   138      *
       
   139      * 3: pushes the reset PICurrent into the PICurrentPool to reuse 
       
   140      */
       
   141     void  popPICurrent( ) {
       
   142         // Do not pop the PICurrent, If there is only one.
       
   143         // This should not happen, But an extra check for safety.
       
   144         if( currentIndex <= 1 ) {
       
   145             throw new org.omg.CORBA.INTERNAL( 
       
   146                       "Cannot pop the only PICurrent in the stack",
       
   147 		      MinorCodes.CANT_POP_ONLY_CURRENT_2,
       
   148 		      CompletionStatus.COMPLETED_NO );
       
   149         }
       
   150         currentIndex--;
       
   151         PICurrent current = (PICurrent)currentContainer.get( currentIndex );
       
   152         current.resetSlots( );
       
   153         currentPool.putPICurrent( current );
       
   154     }
       
   155 
       
   156     /**
       
   157      * peekPICurrent gets the top PICurrent in the ThreadCurrentStack without
       
   158      * popping.
       
   159      */
       
   160     PICurrent peekPICurrent( ) {
       
   161        return (PICurrent) currentContainer.get( currentIndex - 1);
       
   162     }
       
   163 
       
   164 }