1 24 package org.objectweb.jalisto.se.impl.client; 25 26 import org.objectweb.jalisto.se.impl.LogicalOid; 27 import org.objectweb.jalisto.se.api.internal.InternalMetaRepository; 28 import org.objectweb.jalisto.se.api.remote.ClientCommunicationAgent; 29 import org.objectweb.jalisto.se.exception.JalistoException; 30 31 import java.util.ArrayList ; 32 import java.util.HashMap ; 33 import java.util.Iterator ; 34 35 public class RemoteIdentityProvider { 36 37 public RemoteIdentityProvider(ClientCommunicationAgent connexion, InternalMetaRepository repository) { 38 this.connexion = connexion; 39 this.repository = repository; 40 this.numberOfOidToReserve = 1000; 41 this.transactionnalFloids = new HashMap (); 42 int size = repository.getAllClassNames().size(); 43 currentFloids = new long[size]; 44 maxFloids = new long[size]; 45 for (int i = 0; i < size; i++) { 46 currentFloids[i] = -1; 47 maxFloids[i] = -1; 48 } 49 } 50 51 public Object makeNewFileOid(String objectClassName) { 52 short clid = ((Short ) repository.getClidFromClassName(objectClassName)).shortValue(); 53 if ((currentFloids[clid] == -1) || (currentFloids[clid] >= maxFloids[clid])) { 54 retrieveFloids(clid); 55 } 56 LogicalOid result = new LogicalOid(currentFloids[clid]++ + LogicalOid.classMulti * clid); 57 transactionnalFloids.put(result, null); 58 return result; 59 } 60 61 public void addClass(short clid) { 62 if (clid == maxFloids.length) { 63 long[] newCurrentFloids = new long[currentFloids.length + 1]; 64 long[] newMaxFloids = new long[maxFloids.length + 1]; 65 System.arraycopy(currentFloids, 0, newCurrentFloids, 0, currentFloids.length); 66 System.arraycopy(maxFloids, 0, newMaxFloids, 0, maxFloids.length); 67 currentFloids = newCurrentFloids; 68 maxFloids = newMaxFloids; 69 currentFloids[clid] = -1; 70 maxFloids[clid] = -1; 71 } else { 72 throw new JalistoException(); 73 } 74 } 75 76 public void useFloid(Object floid) { 77 transactionnalFloids.remove(floid); 78 } 79 80 public void addFloidForClid(ArrayList floids, short clid) { 81 Iterator iterator = transactionnalFloids.keySet().iterator(); 82 while (iterator.hasNext()) { 83 LogicalOid floid = (LogicalOid) iterator.next(); 84 if (floid.getClid() == clid) { 85 floids.add(floid); 86 } 87 } 88 } 89 90 public void commit() { 91 connexion.makeOids(new ArrayList (transactionnalFloids.keySet())); 92 transactionnalFloids.clear(); 93 } 94 95 public void rollback() { 96 transactionnalFloids.clear(); 97 } 98 99 private void retrieveFloids(short clid) { 100 long floid = connexion.reserveFloids(clid, numberOfOidToReserve).longValue(); 101 currentFloids[clid] = floid; 102 maxFloids[clid] = floid + numberOfOidToReserve; 103 } 104 105 private int numberOfOidToReserve; 106 107 private long[] currentFloids; 108 private long[] maxFloids; 109 private HashMap transactionnalFloids; 110 111 private InternalMetaRepository repository; 112 private ClientCommunicationAgent connexion; 113 } 114 | Popular Tags |