1 |
779 |
jeremybenn |
/* RegistryImpl.java -- the RMI registry implementation
|
2 |
|
|
Copyright (c) 1996, 1997, 1998, 1999, 2002, 2005, 2006
|
3 |
|
|
Free Software Foundation, Inc.
|
4 |
|
|
|
5 |
|
|
This file is part of GNU Classpath.
|
6 |
|
|
|
7 |
|
|
GNU Classpath is free software; you can redistribute it and/or modify
|
8 |
|
|
it under the terms of the GNU General Public License as published by
|
9 |
|
|
the Free Software Foundation; either version 2, or (at your option)
|
10 |
|
|
any later version.
|
11 |
|
|
|
12 |
|
|
GNU Classpath is distributed in the hope that it will be useful, but
|
13 |
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
14 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
15 |
|
|
General Public License for more details.
|
16 |
|
|
|
17 |
|
|
You should have received a copy of the GNU General Public License
|
18 |
|
|
along with GNU Classpath; see the file COPYING. If not, write to the
|
19 |
|
|
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
20 |
|
|
02110-1301 USA.
|
21 |
|
|
|
22 |
|
|
Linking this library statically or dynamically with other modules is
|
23 |
|
|
making a combined work based on this library. Thus, the terms and
|
24 |
|
|
conditions of the GNU General Public License cover the whole
|
25 |
|
|
combination.
|
26 |
|
|
|
27 |
|
|
As a special exception, the copyright holders of this library give you
|
28 |
|
|
permission to link this library with independent modules to produce an
|
29 |
|
|
executable, regardless of the license terms of these independent
|
30 |
|
|
modules, and to copy and distribute the resulting executable under
|
31 |
|
|
terms of your choice, provided that you also meet, for each linked
|
32 |
|
|
independent module, the terms and conditions of the license of that
|
33 |
|
|
module. An independent module is a module which is not derived from
|
34 |
|
|
or based on this library. If you modify this library, you may extend
|
35 |
|
|
this exception to your version of the library, but you are not
|
36 |
|
|
obligated to do so. If you do not wish to do so, delete this
|
37 |
|
|
exception statement from your version. */
|
38 |
|
|
|
39 |
|
|
package gnu.classpath.tools.rmiregistry;
|
40 |
|
|
|
41 |
|
|
import gnu.classpath.tools.common.Persistent;
|
42 |
|
|
|
43 |
|
|
import java.rmi.AccessException;
|
44 |
|
|
import java.rmi.AlreadyBoundException;
|
45 |
|
|
import java.rmi.NotBoundException;
|
46 |
|
|
import java.rmi.Remote;
|
47 |
|
|
import java.rmi.RemoteException;
|
48 |
|
|
import java.rmi.registry.Registry;
|
49 |
|
|
import java.util.ArrayList;
|
50 |
|
|
import java.util.Map;
|
51 |
|
|
|
52 |
|
|
/**
|
53 |
|
|
* The optionally persistent registry implementation.
|
54 |
|
|
*
|
55 |
|
|
* @author Audrius Meskauskas (audriusa@bioinformatics.org)
|
56 |
|
|
*/
|
57 |
|
|
public class RegistryImpl implements Registry
|
58 |
|
|
{
|
59 |
|
|
/**
|
60 |
|
|
* The binding table.
|
61 |
|
|
*/
|
62 |
|
|
Map bindings;
|
63 |
|
|
|
64 |
|
|
/**
|
65 |
|
|
* Create the registry implementation that uses the given bidirectinal
|
66 |
|
|
* table to keep the data.
|
67 |
|
|
*/
|
68 |
|
|
public RegistryImpl(Map aTable)
|
69 |
|
|
{
|
70 |
|
|
bindings = aTable;
|
71 |
|
|
}
|
72 |
|
|
|
73 |
|
|
/** @inheritDoc */
|
74 |
|
|
public Remote lookup(String name) throws RemoteException, NotBoundException,
|
75 |
|
|
AccessException
|
76 |
|
|
{
|
77 |
|
|
Object obj = bindings.get(name);
|
78 |
|
|
if (obj == null)
|
79 |
|
|
throw new NotBoundException(name);
|
80 |
|
|
return ((Remote) obj);
|
81 |
|
|
}
|
82 |
|
|
|
83 |
|
|
/** @inheritDoc */
|
84 |
|
|
public void bind(String name, Remote obj) throws RemoteException,
|
85 |
|
|
AlreadyBoundException, AccessException
|
86 |
|
|
{
|
87 |
|
|
if (Main.verbose)
|
88 |
|
|
System.out.println("Bind "+name);
|
89 |
|
|
if (bindings.containsKey(name))
|
90 |
|
|
throw new AlreadyBoundException(name);
|
91 |
|
|
bindings.put(name, obj);
|
92 |
|
|
}
|
93 |
|
|
|
94 |
|
|
/** @inheritDoc */
|
95 |
|
|
public void unbind(String name) throws RemoteException, NotBoundException,
|
96 |
|
|
AccessException
|
97 |
|
|
{
|
98 |
|
|
if (name.equals(Main.STOP))
|
99 |
|
|
{
|
100 |
|
|
if (bindings instanceof Persistent)
|
101 |
|
|
((Persistent) bindings).writeContent();
|
102 |
|
|
// Terminate in 10 seconds.
|
103 |
|
|
System.out.println("Shutdown command received. Will terminate in 10 s");
|
104 |
|
|
Persistent.timer.schedule(new Persistent.ExitTask(), 10000);
|
105 |
|
|
}
|
106 |
|
|
else
|
107 |
|
|
{
|
108 |
|
|
if (Main.verbose)
|
109 |
|
|
System.out.println("Unbind "+name);
|
110 |
|
|
|
111 |
|
|
if (!bindings.containsKey(name))
|
112 |
|
|
throw new NotBoundException(name);
|
113 |
|
|
else
|
114 |
|
|
bindings.remove(name);
|
115 |
|
|
}
|
116 |
|
|
}
|
117 |
|
|
|
118 |
|
|
/** @inheritDoc */
|
119 |
|
|
public void rebind(String name, Remote obj) throws RemoteException,
|
120 |
|
|
AccessException
|
121 |
|
|
{
|
122 |
|
|
if (Main.verbose)
|
123 |
|
|
System.out.println("Rebind "+name);
|
124 |
|
|
bindings.put(name, obj);
|
125 |
|
|
}
|
126 |
|
|
|
127 |
|
|
/** @inheritDoc */
|
128 |
|
|
public String[] list() throws RemoteException, AccessException
|
129 |
|
|
{
|
130 |
|
|
// Create a separated array to prevent race conditions.
|
131 |
|
|
ArrayList keys = new ArrayList(bindings.keySet());
|
132 |
|
|
int n = keys.size();
|
133 |
|
|
String[] rt = new String[n];
|
134 |
|
|
for (int i = 0; i < n; i++)
|
135 |
|
|
rt[i] = (String) keys.get(i);
|
136 |
|
|
return rt;
|
137 |
|
|
}
|
138 |
|
|
}
|