KickJava   Java API By Example, From Geeks To Geeks.

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


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.rmi.Remote JavaDoc;
22 import java.rmi.RemoteException JavaDoc;
23 import java.util.Set JavaDoc;
24 import java.io.Serializable JavaDoc;
25
26 /**
27  * Manage equivalences between objects in the cluster. Two objects are equivalent if
28  * their keys have the same value (key1.equals(key2)).
29  * The keys prefixed with "REG_" are reserved by the ClusterRegistryImpl. No other
30  * module should generate such keys.
31  */

32 public final class DistributedEquiv {
33     private static Object JavaDoc lock = new Object JavaDoc();
34     private static DistributedEquivSystem des = null;
35
36     /**
37      * The first one which calls this method starts the DistributedEquiv system.
38      * It can be the only one to stop it with the ref it gets.
39      */

40     public static DistributedEquiv start() throws ConfigException {
41         synchronized (lock) {
42             if (des != null)
43                 throw new ConfigException("DistributedEquiv already started");
44             try {
45                 des = new DistributedEquivSystem();
46             } catch (ConfigException e) {
47                 throw e;
48             } catch (Exception JavaDoc e) {
49                 throw new ConfigException(
50                     e.getClass().getName() + ": " + e.getMessage());
51             }
52         }
53         return new DistributedEquiv();
54     }
55
56     public void stop() throws ConfigException {
57         synchronized (lock) {
58             des.terminate();
59             des = null;
60         }
61     }
62
63     /**
64      * @return true if succesfully exported
65      */

66     static boolean exportObject(Serializable JavaDoc key, byte[] obj)
67         throws ConfigException, RemoteException JavaDoc {
68         DistributedEquivSystem d = des;
69         if (d == null)
70             throw new ConfigException("DistributedEquiv not started");
71         return d.exportObject(key, obj);
72     }
73
74     /**
75      * @return true if succesfully unexported
76      */

77     static boolean unexportObject(Serializable JavaDoc key) throws ConfigException {
78         DistributedEquivSystem d = des;
79         if (d == null)
80             throw new ConfigException("DistributedEquiv not started");
81         return d.unexportObject(key);
82     }
83
84     /**
85      * @return <code>null<code> if not exported.
86      */

87     static ClusterStubData getGlobal(Serializable JavaDoc key)
88         throws ConfigException, RemoteException JavaDoc {
89         DistributedEquivSystem d = des;
90         if (d == null)
91             throw new ConfigException("DistributedEquiv not started");
92         return d.getGlobal(key);
93     }
94
95     /**
96      * @return <code>null<code> if not exported.
97      */

98     static Remote JavaDoc getLocal(Serializable JavaDoc key) throws ConfigException {
99         DistributedEquivSystem d = des;
100         if (d == null)
101             throw new ConfigException("DistributedEquiv not started");
102         return d.getLocal(key);
103     }
104
105     static Set JavaDoc keySet() throws ConfigException {
106         DistributedEquivSystem d = des;
107         if (d == null)
108             throw new ConfigException("DistributedEquiv not started");
109         return d.keySet();
110     }
111 }
112
Popular Tags