KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > carol > rmi > jrmp > server > JLocalObjectStore


1 /**
2  * Copyright (C) 2002,2004 - INRIA (www.inria.fr)
3  *
4  * CAROL: Common Architecture for RMI ObjectWeb Layer
5  *
6  * This library is developed inside the ObjectWeb Consortium,
7  * http://www.objectweb.org
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22  * USA
23  *
24  * --------------------------------------------------------------------------
25  * $Id: JLocalObjectStore.java,v 1.3 2004/09/01 11:02:41 benoitf Exp $
26  * --------------------------------------------------------------------------
27  */

28 package org.objectweb.carol.rmi.jrmp.server;
29
30 import java.util.ArrayList JavaDoc;
31
32 /**
33  * The class is a naming context allocating integer identifier. This integer
34  * value is divided in two parts in order to reduce the synchronizaion
35  * conflicts. The 8 (MAX_SIZE constant) right bits are used to hash identifiers.
36  * The null value are stored with a special identifier: -1
37  * @author riviereg, sebastien chassande-barrioz
38  */

39 public class JLocalObjectStore {
40
41     private static int counter = 0;
42
43     // The number of arraylists MAX must be less that MASK
44
private static final int MAX = 100;
45
46     // MASK is used to divide the key into the two indexes.
47
private static final int MASK = 255;
48
49     private static final int MASK_SIZE = 8;
50
51     private static final Object JavaDoc EMPTY_SLOT = Boolean.FALSE;
52
53     public static ArrayList JavaDoc[] lists = new ArrayList JavaDoc[MAX];
54
55     static {
56         for (int i = 0; i < MAX; i++) {
57             lists[i] = new ArrayList JavaDoc();
58         }
59
60     }
61
62     /**
63      * Exports an object and allocates an integer identifier.
64      */

65     public static int storeObject(Object JavaDoc ob) {
66         // The context is often null so return a key that can be decoded
67
// quickly. This coresponding to a "no context send"
68
if (ob == null) {
69             return -1;
70         }
71
72         // pick the next array list to use
73
int i = 0;
74         synchronized (lists) {
75             counter++;
76             if (counter == MAX) {
77                 counter = 0;
78             }
79             i = counter;
80         }
81         ArrayList JavaDoc ar = lists[i];
82
83         int j;
84         synchronized (ar) {
85             j = ar.indexOf(EMPTY_SLOT);
86             if (j == -1) {
87                 // add the object at the end of the list
88
j = ar.size();
89                 ar.add(ob);
90             } else {
91                 // reuse an empty slot in order reduce the
92
// memory cost
93
ar.set(j, ob);
94             }
95         }
96         return i + (j << MASK_SIZE);
97     }
98
99     /**
100      * lookup an object by its integer identifier.
101      * @param key is the object identifier
102      * @return the Object associated to the identifier, or a null value if no
103      * object was found.
104      */

105     public static Object JavaDoc getObject(int key) {
106         if (key == -1) {
107             return null;
108         }
109         int j = key >> MASK_SIZE;
110         ArrayList JavaDoc ar = lists[key & MASK];
111         try {
112             //First attemp without synchronization in order to
113
// optimize the lookup
114
return ar.get(j);
115         } catch (RuntimeException JavaDoc e) {
116             //When new elements are stored, the ArrayList can be
117
// resized and then produces an Exception. With a
118
// synchronized, access this bad case is avoided.
119
synchronized (ar) {
120                 return (ar.size() > j ? ar.get(j) : null);
121             }
122         }
123     }
124
125     /**
126      * Unexport an object from the NamingContext. Empty slots are full with
127      * EMPTY_SLOT.
128      */

129     public static Object JavaDoc removeObject(int key) {
130         if (key < 0) {
131             return null;
132         }
133         Object JavaDoc ob;
134         ArrayList JavaDoc ar = lists[key & MASK];
135         int j = key >> MASK_SIZE;
136         synchronized (ar) {
137             ob = ar.get(j);
138             ar.set(j, EMPTY_SLOT);
139             int k = ar.size() - 1;
140
141             // clean up last elements if possible
142
while (k != -1 && (ar.get(k) == EMPTY_SLOT)) {
143                 ar.remove(k);
144                 k--;
145             }
146         }
147         return ob;
148     }
149 }
Popular Tags