1 7 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 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 servant, java.lang.Object 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 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 if (orb.transientObjectManagerDebugFlag) 73 dprint( "\tcounter is invalid" ) ; 74 return null; 75 } 76 77 public synchronized java.lang.Object 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 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 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 return null; 117 } 118 119 private void doubleSize() 120 { 121 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 servant=null; java.lang.Object servantData=null; 142 int index=-1; 143 int counter=0; 144 boolean valid=false; 147 Element(int i, java.lang.Object next) 148 { 149 servant = next; 150 index = i; 151 } 152 153 byte[] getKey(java.lang.Object servant, java.lang.Object 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 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 ) return; 177 counter++; 178 servantData = null; 179 valid = false; 180 181 servant = freeList; 183 } 184 185 public String toString() 186 { 187 return "Element[" + index + ", " + counter + "]" ; 188 } 189 } 190 191 | Popular Tags |