KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jalisto > se > impl > client > RemoteIdentityProvider


1 /*
2  * Jalisto - JAva LIght STOrage
3  * Copyright (C) 2000-2005 Xcalia http://www.xcalia.com
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * Xcalia
20  * 71, rue Desnouettes
21  * 75014 Paris - France
22  * http://www.xcalia.com
23  */

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 JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.Iterator JavaDoc;
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 JavaDoc();
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 JavaDoc makeNewFileOid(String JavaDoc objectClassName) {
52         short clid = ((Short JavaDoc) 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 JavaDoc floid) {
77         transactionnalFloids.remove(floid);
78     }
79
80     public void addFloidForClid(ArrayList JavaDoc floids, short clid) {
81         Iterator JavaDoc 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 JavaDoc(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 JavaDoc transactionnalFloids;
110
111     private InternalMetaRepository repository;
112     private ClientCommunicationAgent connexion;
113 }
114
Popular Tags