KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > impl > oa > toa > TransientObjectManager


1 /*
2  * @(#)TransientObjectManager.java 1.30 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  * Licensed Materials - Property of IBM
9  * RMI-IIOP v1.0
10  * Copyright IBM Corp. 1998 1999 All Rights Reserved
11  *
12  * US Government Users Restricted Rights - Use, duplication or
13  * disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
14  */

15
16 package com.sun.corba.se.impl.oa.toa;
17
18 import com.sun.corba.se.impl.orbutil.ORBUtility ;
19 import com.sun.corba.se.spi.orb.ORB ;
20
21 public final class TransientObjectManager {
22     private ORB orb ;
23     private int maxSize = 128;
24     private Element[] elementArray;
25     private Element freeList;
26
27     void dprint( String JavaDoc msg ) {
28     ORBUtility.dprint( this, msg ) ;
29     }
30
31     public TransientObjectManager( ORB orb )
32     {
33     this.orb = orb ;
34
35         elementArray = new Element[maxSize];
36         elementArray[maxSize-1] = new Element(maxSize-1,null);
37         for ( int i=maxSize-2; i>=0; i-- )
38             elementArray[i] = new Element(i,elementArray[i+1]);
39         freeList = elementArray[0];
40     }
41
42     public synchronized byte[] storeServant(java.lang.Object JavaDoc servant, java.lang.Object JavaDoc servantData)
43     {
44         if ( freeList == null )
45             doubleSize();
46
47         Element elem = freeList;
48         freeList = (Element)freeList.servant;
49         
50         byte[] result = elem.getKey(servant, servantData);
51     if (orb.transientObjectManagerDebugFlag)
52         dprint( "storeServant returns key for element " + elem ) ;
53     return result ;
54     }
55
56     public synchronized java.lang.Object JavaDoc lookupServant(byte transientKey[])
57     {
58         int index = ORBUtility.bytesToInt(transientKey,0);
59         int counter = ORBUtility.bytesToInt(transientKey,4);
60
61     if (orb.transientObjectManagerDebugFlag)
62         dprint( "lookupServant called with index=" + index + ", counter=" + counter ) ;
63
64         if (elementArray[index].counter == counter &&
65             elementArray[index].valid ) {
66         if (orb.transientObjectManagerDebugFlag)
67             dprint( "\tcounter is valid" ) ;
68             return elementArray[index].servant;
69     }
70
71         // servant not found
72
if (orb.transientObjectManagerDebugFlag)
73         dprint( "\tcounter is invalid" ) ;
74         return null;
75     }
76
77     public synchronized java.lang.Object JavaDoc lookupServantData(byte transientKey[])
78     {
79         int index = ORBUtility.bytesToInt(transientKey,0);
80         int counter = ORBUtility.bytesToInt(transientKey,4);
81
82     if (orb.transientObjectManagerDebugFlag)
83         dprint( "lookupServantData called with index=" + index + ", counter=" + counter ) ;
84
85         if (elementArray[index].counter == counter &&
86             elementArray[index].valid ) {
87         if (orb.transientObjectManagerDebugFlag)
88             dprint( "\tcounter is valid" ) ;
89             return elementArray[index].servantData;
90     }
91
92         // servant not found
93
if (orb.transientObjectManagerDebugFlag)
94         dprint( "\tcounter is invalid" ) ;
95         return null;
96     }
97
98     public synchronized void deleteServant(byte transientKey[])
99     {
100         int index = ORBUtility.bytesToInt(transientKey,0);
101     if (orb.transientObjectManagerDebugFlag)
102         dprint( "deleting servant at index=" + index ) ;
103
104         elementArray[index].delete(freeList);
105         freeList = elementArray[index];
106     }
107
108     public synchronized byte[] getKey(java.lang.Object JavaDoc servant)
109     {
110         for ( int i=0; i<maxSize; i++ )
111             if ( elementArray[i].valid &&
112                  elementArray[i].servant == servant )
113                 return elementArray[i].toBytes();
114
115         // if we come here Object does not exist
116
return null;
117     }
118
119     private void doubleSize()
120     {
121         // Assume caller is synchronized
122

123         Element old[] = elementArray;
124         int oldSize = maxSize;
125         maxSize *= 2;
126         elementArray = new Element[maxSize];
127
128         for ( int i=0; i<oldSize; i++ )
129             elementArray[i] = old[i];
130
131         elementArray[maxSize-1] = new Element(maxSize-1,null);
132         for ( int i=maxSize-2; i>=oldSize; i-- )
133             elementArray[i] = new Element(i,elementArray[i+1]);
134         freeList = elementArray[oldSize];
135     }
136 }
137
138
139 final class Element {
140     java.lang.Object JavaDoc servant=null; // also stores "next pointer" in free list
141
java.lang.Object JavaDoc servantData=null;
142     int index=-1;
143     int counter=0;
144     boolean valid=false; // valid=true if this Element contains
145
// a valid servant
146

147     Element(int i, java.lang.Object JavaDoc next)
148     {
149         servant = next;
150         index = i;
151     }
152
153     byte[] getKey(java.lang.Object JavaDoc servant, java.lang.Object JavaDoc servantData)
154     {
155         this.servant = servant;
156         this.servantData = servantData;
157         this.valid = true;
158
159         return toBytes();
160     }
161
162     byte[] toBytes()
163     {
164         // Convert the index+counter into an 8-byte (big-endian) key.
165

166         byte key[] = new byte[8];
167         ORBUtility.intToBytes(index, key, 0);
168         ORBUtility.intToBytes(counter, key, 4);
169
170         return key;
171     }
172
173     void delete(Element freeList)
174     {
175         if ( !valid ) // prevent double deletion
176
return;
177         counter++;
178         servantData = null;
179         valid = false;
180
181         // add this to freeList
182
servant = freeList;
183     }
184
185     public String JavaDoc toString()
186     {
187     return "Element[" + index + ", " + counter + "]" ;
188     }
189 }
190
191
Popular Tags