KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > carol > cmi > ClusterRegistryImpl


1 /*
2  * Copyright (C) 2002-2003, Simon Nieuviarts
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17  * USA
18  */

19 package org.objectweb.carol.cmi;
20
21 import java.io.ByteArrayOutputStream JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.rmi.AlreadyBoundException JavaDoc;
24 import java.rmi.NotBoundException JavaDoc;
25 import java.rmi.Remote JavaDoc;
26 import java.rmi.RemoteException JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Set JavaDoc;
30 import java.util.TreeSet JavaDoc;
31
32 import org.objectweb.carol.util.configuration.TraceCarol;
33
34 public final class ClusterRegistryImpl implements ClusterRegistryInternal {
35     /**
36      * The non-clustered objects registry. Maps names to serialized regular
37      * stubs for non clustered objects.
38      */

39     private HashMap JavaDoc lreg = new HashMap JavaDoc();
40     public static final String JavaDoc REG_PREFIX = "REG_";
41
42     // Protect the registry against multiple starts in the same JVM
43
private static Object JavaDoc lock = new Object JavaDoc();
44     private boolean started = false;
45
46     private ClusterRegistryImpl() throws RemoteException JavaDoc {
47         synchronized (lock) {
48             //TODO Could be defeated by several class loaders ? Check in JOnAS
49
if (started) throw new RemoteException JavaDoc("Can't start multiple cluster registries in the same JVM");
50             started = true;
51         }
52     }
53
54     public static ClusterRegistryKiller start(int port)
55         throws RemoteException JavaDoc {
56         if (TraceCarol.isDebugCmiRegistry())
57             TraceCarol.debugCmiRegistry("registry starting on port " + port);
58         ClusterRegistryImpl creg = new ClusterRegistryImpl();
59         ClusterRegistryInternal stub =
60             (ClusterRegistryInternal) LowerOrb.exportRegistry(creg, port);
61         ClusterRegistryKiller k = new ClusterRegistryKiller(creg, port);
62         return k;
63     }
64
65     public Object JavaDoc lookup(String JavaDoc n) throws NotBoundException JavaDoc, RemoteException JavaDoc {
66         Object JavaDoc obj;
67         synchronized (lreg) {
68             obj = lreg.get(n);
69         }
70         if (obj != null) {
71             if (TraceCarol.isDebugCmiRegistry())
72                 TraceCarol.debugCmiRegistry("local lookup of " + n);
73             return obj;
74         }
75         try {
76             if (TraceCarol.isDebugCmiRegistry())
77                 TraceCarol.debugCmiRegistry("global lookup of " + n);
78             ClusterStubData csd = DistributedEquiv.getGlobal(REG_PREFIX + n);
79             if (csd != null) {
80                 if (TraceCarol.isDebugCmiRegistry())
81                     TraceCarol.debugCmiRegistry("returned a cluster stub");
82                 ByteArrayOutputStream JavaDoc b = new ByteArrayOutputStream JavaDoc();
83                 b.write(CLUSTERED);
84                 CmiOutputStream os = new CmiOutputStream(b);
85                 csd.write(os);
86                 os.flush();
87                 return b.toByteArray();
88             }
89         } catch (ConfigException e) {
90             throw new RemoteException JavaDoc(e.toString());
91         } catch (IOException JavaDoc e) {
92             throw new RemoteException JavaDoc(e.toString());
93         }
94         throw new NotBoundException JavaDoc(n);
95     }
96
97     public void bindSingle(String JavaDoc n, Remote JavaDoc obj)
98         throws AlreadyBoundException JavaDoc, RemoteException JavaDoc {
99 /*
100             byte[] prefix = new byte[obj.length + 1];
101             prefix[0] = NOT_CLUSTERED;
102             System.arraycopy(obj, 0, prefix, 1, obj.length);
103 */

104         Object JavaDoc cur;
105         if (TraceCarol.isDebugCmiRegistry())
106             TraceCarol.debugCmiRegistry("Local bind of " + n);
107         synchronized (lreg) {
108             cur = lreg.get(n);
109             if (cur != null)
110                 throw new AlreadyBoundException JavaDoc(n);
111             lreg.put(n, obj);
112         }
113     }
114
115     public void rebindSingle(String JavaDoc n, Remote JavaDoc obj) throws RemoteException JavaDoc {
116 /*
117         byte[] prefix = new byte[obj.length + 1];
118         prefix[0] = NOT_CLUSTERED;
119         System.arraycopy(obj, 0, prefix, 1, obj.length);
120 */

121         if (TraceCarol.isDebugCmiRegistry())
122             TraceCarol.debugCmiRegistry("Local rebind of " + n);
123         synchronized (lreg) {
124             lreg.put(n, obj);
125         }
126     }
127
128     public void bindCluster(String JavaDoc n, byte[] obj)
129         throws AlreadyBoundException JavaDoc, RemoteException JavaDoc {
130         Object JavaDoc cur;
131         if (TraceCarol.isDebugCmiRegistry())
132             TraceCarol.debugCmiRegistry("Global bind of " + n);
133         synchronized (lreg) {
134             try {
135                 if (!DistributedEquiv.exportObject(REG_PREFIX + n, obj)) {
136                     throw new AlreadyBoundException JavaDoc(n);
137                 }
138             } catch (ConfigException e) {
139                 throw new RemoteException JavaDoc(e.toString());
140             }
141         }
142     }
143
144     public void unbind(String JavaDoc n)
145         throws NotBoundException JavaDoc, RemoteException JavaDoc {
146             Object JavaDoc obj;
147             synchronized (lreg) {
148                 obj = lreg.remove(n);
149                 if (obj != null) {
150                     if (TraceCarol.isDebugCmiRegistry()) {
151                         TraceCarol.debugCmiRegistry("Local unbind of " + n);
152                     }
153                     return;
154                 }
155                 if (TraceCarol.isDebugCmiRegistry())
156                     TraceCarol.debugCmiRegistry("Global unbind of " + n);
157                 try {
158                     if (!DistributedEquiv.unexportObject(REG_PREFIX + n)) {
159                         throw new NotBoundException JavaDoc(n);
160                     }
161                 } catch (ConfigException e) {
162                     throw new RemoteException JavaDoc(e.toString());
163                 }
164             }
165     }
166
167     public void rebindCluster(String JavaDoc n, byte[] obj) throws RemoteException JavaDoc {
168         Object JavaDoc cur;
169         ClusterConfig cc = null;
170         if (TraceCarol.isDebugCmiRegistry())
171             TraceCarol.debugCmiRegistry("Global rebind of " + n);
172
173         synchronized (lreg) {
174             try {
175                 String JavaDoc name = REG_PREFIX + n;
176                 DistributedEquiv.unexportObject(name);
177                 DistributedEquiv.exportObject(name, obj);
178             } catch (ConfigException e) {
179                 throw new RemoteException JavaDoc(e.toString());
180             }
181         }
182     }
183
184     public String JavaDoc[] list() throws RemoteException JavaDoc {
185         Set JavaDoc s = new TreeSet JavaDoc();
186         try {
187             Set JavaDoc global = DistributedEquiv.keySet();
188             Iterator JavaDoc it = global.iterator();
189             while (it.hasNext()) {
190                 Object JavaDoc o = it.next();
191                 if (o instanceof String JavaDoc) {
192                     String JavaDoc str = (String JavaDoc) o;
193                     if (str.startsWith(REG_PREFIX)) {
194                         s.add(str.substring(4));
195                     }
196                 }
197             }
198         } catch (ConfigException e) {
199             throw new RemoteException JavaDoc(e.toString());
200         }
201         synchronized (lreg) {
202             Set JavaDoc local = lreg.keySet();
203             Iterator JavaDoc it = local.iterator();
204             while (it.hasNext()) {
205                 s.add(it.next());
206             }
207         }
208         int n = s.size();
209         Iterator JavaDoc it = s.iterator();
210         String JavaDoc[] tab = new String JavaDoc[n];
211         for (int i = 0; i < n; i++) {
212             tab[i] = (String JavaDoc) it.next();
213         }
214         return tab;
215     }
216
217     public void test() throws RemoteException JavaDoc {
218     }
219
220     public static void main(String JavaDoc args[]) throws Exception JavaDoc {
221         DistributedEquiv.start();
222         start(Integer.parseInt(args[0]));
223         System.out.println("Cluster service started");
224         while (true) {
225             try {
226                 Thread.sleep(Long.MAX_VALUE);
227             } catch (InterruptedException JavaDoc e) {
228             }
229         }
230     }
231 }
232
Popular Tags