KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jalisto > se > impl > server > IdentityProvider


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.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 JavaDoc[] table) {
40         floids = (long[]) table[0];
41         clid = ((Short JavaDoc) table[1]).shortValue();
42         pid = ((Integer JavaDoc) table[2]).intValue();
43     }
44
45     public Object JavaDoc getNewPid() {
46         pid++;
47         Integer JavaDoc result = new Integer JavaDoc(pid);
48         return result;
49     }
50
51     public Object JavaDoc 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 JavaDoc result = new Short JavaDoc(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 JavaDoc[] datasToSave = new Object JavaDoc[3];
81         datasToSave[0] = floids;
82         datasToSave[1] = new Short JavaDoc(clid);
83         datasToSave[2] = new Integer JavaDoc(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 JavaDoc toString() {
117         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
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     /**
126      * ***************************** STATIC **************************************
127      */

128
129     public static IdentityProvider getAnIdentityProvider(InternalPhysicalFileAccess physicalAccess) {
130         IdentityProvider oidProvider;
131         try {
132             Object JavaDoc[] datas = (Object JavaDoc[]) physicalAccess.readFileObjectAt(OID_PROVIDER_IFA).getData();
133             oidProvider = new IdentityProvider(datas);
134             oidProvider.physicalAccess = physicalAccess;
135         } catch (Exception JavaDoc 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