KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > carol > rmi > jrmp > interceptor > JContextStore


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: JContextStore.java,v 1.3 2004/09/01 11:02:41 benoitf Exp $
26  * --------------------------------------------------------------------------
27  */

28 package org.objectweb.carol.rmi.jrmp.interceptor;
29
30 import java.util.ArrayList JavaDoc;
31 import java.util.Iterator JavaDoc;
32
33 /**
34  * Class <code>JContextObjectStore</code> is the CAROL JRMP Client Interceptor
35  * Contexts Storage System
36  * @author Guillaume Riviere (Guillaume.Riviere@inrialpes.fr)
37  * @version 1.0, 10/03/2003
38  */

39 public class JContextStore {
40
41     private static int counter = 0;
42
43     public static ArrayList JavaDoc lists = new ArrayList JavaDoc();
44
45     // The number of arraylists MAX must be less that MASK
46
private static final int MAX = 100;
47
48     // MASK is used to divide the key into the two indexes.
49
private static final int MASK = 256;
50
51     static {
52         int i = 0;
53         while (i != MAX) {
54             lists.add(new ArrayList JavaDoc());
55             i++;
56         }
57
58     }
59
60     /**
61      * Stote an object context
62      */

63     public static int storeObject(Object JavaDoc ob) {
64         // The context is often null so return a key that can be decoded
65
// quickly. This coresponding to a "no context send"
66
if (ob == null) {
67             return -1;
68         }
69
70         int i = 0;
71         ArrayList JavaDoc ar;
72
73         // pick the next array list to use
74
synchronized (lists) {
75             counter++;
76             if (counter == MAX) {
77                 counter = 0;
78             }
79             i = counter;
80         }
81
82         ar = (ArrayList JavaDoc) lists.get(i);
83
84         // add the object at position j.
85
int j;
86         synchronized (ar) {
87             ar.add(ob);
88             j = ar.size() - 1;
89         }
90
91         i = j * MASK + i;
92         return i;
93     }
94
95     /**
96      * Get an object from the store and remove it from the arrayList. Mark empty
97      * slots in the arrayList with Boolean.FALSE.
98      */

99
100     public static Object JavaDoc getObject(int key) {
101         if (key == -1) {
102             return null;
103         }
104         Object JavaDoc ob;
105         int i = key % MASK;
106         int j = key / MASK;
107         ArrayList JavaDoc ar = (ArrayList JavaDoc) lists.get(i);
108
109         synchronized (ar) {
110             ob = ar.get(j);
111             ar.set(j, Boolean.FALSE);
112             int k = ar.size() - 1;
113
114             // only remove keys from the end so as not to alter
115
// the index of other keys.
116
while (k != -1 && (ar.get(k) == Boolean.FALSE)) {
117                 ar.remove(k);
118                 k--;
119             }
120         }
121         return ob;
122     }
123
124     public String JavaDoc toString() {
125         String JavaDoc result = "";
126         int x = 0;
127         for (Iterator JavaDoc i = lists.iterator(); i.hasNext();) {
128             int y = 0;
129             ArrayList JavaDoc ara = (ArrayList JavaDoc) i.next();
130             for (Iterator JavaDoc j = ara.iterator(); j.hasNext();) {
131                 result += "object at x=" + x + ", y=" + y + " object:" + j.next();
132                 y++;
133             }
134             x++;
135         }
136         return result;
137     }
138 }
139
140
Popular Tags