KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > impl > interceptors > SlotTableStack


1 /*
2  * @(#)SlotTableStack.java 1.11 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package com.sun.corba.se.impl.interceptors;
9
10 import org.omg.CORBA.CompletionStatus JavaDoc;
11 import org.omg.CORBA.INTERNAL JavaDoc;
12 import org.omg.PortableInterceptor.Current JavaDoc;
13 import org.omg.PortableInterceptor.InvalidSlot JavaDoc;
14
15 import com.sun.corba.se.impl.corba.AnyImpl;
16
17 import com.sun.corba.se.impl.logging.InterceptorsSystemException;
18 import com.sun.corba.se.spi.logging.CORBALogDomains;
19
20 import com.sun.corba.se.spi.orb.ORB;
21
22 /**
23  * SlotTableStack is the container of SlotTable instances for each thread
24  */

25 public class SlotTableStack
26 {
27     // SlotTablePool is the container for reusable SlotTables'
28
private class SlotTablePool {
29
30         // Contains a list of reusable SlotTable
31
private SlotTable[] pool;
32
33         // High water mark for the pool
34
// If the pool size reaches this limit then putSlotTable will
35
// not put SlotTable to the pool.
36
private final int HIGH_WATER_MARK = 5;
37
38         // currentIndex points to the last SlotTable in the list
39
private int currentIndex;
40
41         SlotTablePool( ) {
42             pool = new SlotTable[HIGH_WATER_MARK];
43             currentIndex = 0;
44         }
45
46         /**
47          * Puts SlotTable to the re-usable pool.
48          */

49         void putSlotTable( SlotTable table ) {
50             // If there are enough SlotTables in the pool, then don't add
51
// this table to the pool.
52
if( currentIndex >= HIGH_WATER_MARK ) {
53                 // Let the garbage collector collect it.
54
return;
55             }
56             pool[currentIndex] = table;
57             currentIndex++;
58         }
59
60         /**
61          * Gets SlotTable from the re-usable pool.
62          */

63         SlotTable getSlotTable( ) {
64             // If there are no entries in the pool then return null
65
if( currentIndex == 0 ) {
66                 return null;
67             }
68             // Works like a stack, Gets the last one added first
69
currentIndex--;
70             return pool[currentIndex];
71         }
72     }
73    
74     // Contains all the active SlotTables for each thread.
75
// The List is made to behave like a stack.
76
private java.util.List JavaDoc tableContainer;
77
78     // Keeps track of number of PICurrents in the stack.
79
private int currentIndex;
80  
81     // For Every Thread there will be a pool of re-usable SlotTables'
82
// stored in SlotTablePool
83
private SlotTablePool tablePool;
84
85     // The ORB associated with this slot table stack
86
private ORB orb;
87
88     private InterceptorsSystemException wrapper ;
89
90     /**
91      * Constructs the stack and and SlotTablePool
92      */

93     SlotTableStack( ORB orb, SlotTable table ) {
94        this.orb = orb;
95        wrapper = InterceptorsSystemException.get( orb, CORBALogDomains.RPC_PROTOCOL ) ;
96
97        currentIndex = 0;
98        tableContainer = new java.util.ArrayList JavaDoc( );
99        tablePool = new SlotTablePool( );
100        // SlotTableStack will be created with one SlotTable on the stack.
101
// This table is used as the reference to query for number of
102
// allocated slots to create other slottables.
103
tableContainer.add( currentIndex, table );
104        currentIndex++;
105     }
106    
107
108     /**
109      * pushSlotTable pushes a fresh Slot Table on to the stack by doing the
110      * following,
111      * 1: Checks to see if there is any SlotTable in SlotTablePool
112      * If present then use that instance to push into the SlotTableStack
113      *
114      * 2: If there is no SlotTable in the pool, then creates a new one and
115      * pushes that into the SlotTableStack
116      */

117     void pushSlotTable( ) {
118         SlotTable table = tablePool.getSlotTable( );
119         if( table == null ) {
120             // get an existing PICurrent to get the slotSize
121
SlotTable tableTemp = peekSlotTable();
122             table = new SlotTable( orb, tableTemp.getSize( ));
123         }
124         // NOTE: Very important not to always "add" - otherwise a memory leak.
125
if (currentIndex == tableContainer.size()) {
126             // Add will cause the table to grow.
127
tableContainer.add( currentIndex, table );
128         } else if (currentIndex > tableContainer.size()) {
129         throw wrapper.slotTableInvariant( new Integer JavaDoc( currentIndex ),
130         new Integer JavaDoc( tableContainer.size() ) ) ;
131         } else {
132             // Set will override unused slots.
133
tableContainer.set( currentIndex, table );
134         }
135         currentIndex++;
136     }
137
138     /**
139      * popSlotTable does the following
140      * 1: pops the top SlotTable in the SlotTableStack
141      *
142      * 2: resets the slots in the SlotTable which resets the slotvalues to
143      * null if there are any previous sets.
144      *
145      * 3: puts the reset SlotTable into the SlotTablePool to reuse
146      */

147     void popSlotTable( ) {
148         if( currentIndex <= 1 ) {
149             // Do not pop the SlotTable, If there is only one.
150
// This should not happen, But an extra check for safety.
151
throw wrapper.cantPopOnlyPicurrent() ;
152         }
153         currentIndex--;
154         SlotTable table = (SlotTable)tableContainer.get( currentIndex );
155         tableContainer.set( currentIndex, null ); // Do not leak memory.
156
table.resetSlots( );
157         tablePool.putSlotTable( table );
158     }
159
160     /**
161      * peekSlotTable gets the top SlotTable from the SlotTableStack without
162      * popping.
163      */

164     SlotTable peekSlotTable( ) {
165        return (SlotTable) tableContainer.get( currentIndex - 1);
166     }
167
168 }
169
170 // End of file.
171
Popular Tags