1 24 package org.objectweb.jalisto.se.impl.server; 25 26 import org.objectweb.jalisto.se.api.internal.InternalPhysicalFileAccess; 27 import org.objectweb.jalisto.se.impl.LogicalOid; 28 import org.objectweb.jalisto.se.impl.InFileAddress; 29 30 public class IdentityProvider { 31 32 private IdentityProvider(InternalPhysicalFileAccess physicalAccess) { 33 floids = new long[0]; 34 clid = -1; 35 pid = 0; 36 this.physicalAccess = physicalAccess; 37 } 38 39 private IdentityProvider(Object [] table) { 40 floids = (long[]) table[0]; 41 clid = ((Short ) table[1]).shortValue(); 42 pid = ((Integer ) table[2]).intValue(); 43 } 44 45 public Object getNewPid() { 46 pid++; 47 Integer result = new Integer (pid); 48 return result; 49 } 50 51 public Object getNewClid() { 52 clid++; 53 if (floids.length < clid + 1) { 54 long[] newFloids = new long[clid + 1]; 55 System.arraycopy(floids, 0, newFloids, 0, floids.length); 56 floids = newFloids; 57 } 58 59 floids[clid] = 0; 60 Short result = new Short (clid); 61 return result; 62 } 63 64 public LogicalOid allocateNewFloid(short clid) { 65 LogicalOid result = new LogicalOid(floids[clid]++ + LogicalOid.classMulti * clid); 66 return result; 67 } 68 69 public synchronized long reserveFloids(short clid, int number) { 70 long result = floids[clid]; 71 floids[clid] = floids[clid] + number; 72 return result; 73 } 74 75 public void commit() { 76 updateStateInBase(true); 77 } 78 79 private synchronized void updateStateInBase(boolean isUpdate) { 80 Object [] datasToSave = new Object [3]; 81 datasToSave[0] = floids; 82 datasToSave[1] = new Short (clid); 83 datasToSave[2] = new Integer (pid); 84 physicalAccess.writeObjectInBase(OID_PROVIDER_IFA, datasToSave, isUpdate); 85 } 86 87 public long[] getFloidCounters() { 88 return floids; 89 } 90 91 public short getClidCounter() { 92 return clid; 93 } 94 95 public void resetFloidCounters() { 96 for (int i = 0; i < floids.length; i++) { 97 floids[i] = 0; 98 } 99 } 100 101 public void resetClidCounter() { 102 clid = -1; 103 } 104 105 public void resetAll() { 106 resetClidCounter(); 107 resetFloidCounters(); 108 updateStateInBase(true); 109 } 110 111 public long getCurrentFloidCounter(short clid) { 112 return floids[clid]; 113 } 114 115 116 public String toString() { 117 StringBuffer sb = new StringBuffer (); 118 sb.append("FileIdentityProviderImplPage : \n"); 119 sb.append("\tfloids : ").append(floids).append("\n"); 120 sb.append("\tclid : ").append(clid).append("\n"); 121 sb.append("\tpid : ").append(pid).append("\n"); 122 return sb.toString(); 123 } 124 125 128 129 public static IdentityProvider getAnIdentityProvider(InternalPhysicalFileAccess physicalAccess) { 130 IdentityProvider oidProvider; 131 try { 132 Object [] datas = (Object []) physicalAccess.readFileObjectAt(OID_PROVIDER_IFA).getData(); 133 oidProvider = new IdentityProvider(datas); 134 oidProvider.physicalAccess = physicalAccess; 135 } catch (Exception e) { 136 oidProvider = new IdentityProvider(physicalAccess); 137 oidProvider.updateStateInBase(false); 138 } 139 return oidProvider; 140 } 141 142 143 private transient InternalPhysicalFileAccess physicalAccess; 144 private long[] floids; 145 private short clid; 146 private int pid; 147 148 private static final InFileAddress OID_PROVIDER_IFA = new InFileAddress("ipi"); 149 } 150 | Popular Tags |