KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > imr > ServerTable


1 package org.jacorb.imr;
2
3 /*
4  * JacORB - a free Java ORB
5  *
6  * Copyright (C) 1997-2004 Gerald Brose.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */

22
23 import java.util.*;
24 import java.io.*;
25 import org.jacorb.imr.RegistrationPackage.*;
26 import org.jacorb.imr.AdminPackage.*;
27
28 /**
29  * This class represents the server table of the implementation repository.
30  * It contains all servers, POAs and hosts, and is serialized on shutdown,
31  * deserialized on startup.
32  * <br> It provides methods for adding, deleting and listing servers,
33  * POAs and hosts.
34  *
35  * @author Nicolas Noffke
36  *
37  * $Id: ServerTable.java,v 1.10 2004/05/06 12:39:59 nicolas Exp $
38  */

39
40 public class ServerTable
41     implements Serializable
42 {
43     private Hashtable servers;
44     private transient ResourceLock servers_lock;
45     private Hashtable poas;
46     private transient ResourceLock poas_lock;
47     private Hashtable hosts;
48     private transient ResourceLock hosts_lock;
49
50     public transient ResourceLock table_lock;
51
52     public ServerTable()
53     {
54         servers = new Hashtable();
55         poas = new Hashtable();
56         hosts = new Hashtable();
57
58         initTransient();
59     }
60
61     /**
62      * This method initializes all transient attributes.
63      */

64
65     private void initTransient()
66     {
67         // The table lock is a special case. It is used to gain
68
// exclusive access to the server table on serialization. That
69
// means the exclusive lock is set on serialization and, if it
70
// was not transient, would be serialized as well. On startup
71
// of the repository, if the table is deserialized, the lock
72
// is still set, und must be realeased. That means that we
73
// have to distinguish between a new table and a deserialized
74
// one. So its cheaper to instanciate the lock on
75
// deserialization time again.
76
table_lock = new ResourceLock();
77
78         // The locks are needed, because the hashtables have to be
79
// copied to arrays sometimes (usually on command of the
80
// user), and that is done via Enumerations. Unfortunately
81
// Enumerations get messed up when altering the underlying
82
// structure while reading from them.
83
servers_lock = new ResourceLock();
84         poas_lock = new ResourceLock();
85         hosts_lock = new ResourceLock();
86     }
87
88     /**
89      * This method tests, if a server is known.
90      *
91      * @param name the servers name.
92      * @return true, if a server with the specified name has already
93      * been registered.
94      */

95
96     public boolean hasServer( String JavaDoc name )
97     {
98         return servers.containsKey(name);
99     }
100
101
102     /**
103      * This method gets a server for a specified name.
104      *
105      * @param name the servers name.
106      * @return ImRServerInfo the ImRServerInfo object with name <code>name</code>.
107      * @exception UnknownServerName thrown if the table does not contain
108      * an entry for <code>name</code>.
109      */

110
111     public ImRServerInfo getServer(String JavaDoc name)
112         throws UnknownServerName
113     {
114         ImRServerInfo _tmp = (ImRServerInfo) servers.get(name);
115         if (_tmp == null)
116             throw new UnknownServerName(name);
117
118         return _tmp;
119     }
120
121     /**
122      * Adds a server to the server table.
123      *
124      * @param name the servers name.
125      * @param server the servers corresponding ImRServerInfo object.
126      * @exception DuplicateServerName thrown if <code>name</code> is already
127      * in the table.
128      */

129
130     public void putServer(String JavaDoc name, ImRServerInfo server)
131         throws DuplicateServerName
132     {
133         if (servers.containsKey(name))
134             throw new DuplicateServerName(name);
135
136         table_lock.gainSharedLock();
137         servers_lock.gainSharedLock();
138
139         servers.put(name, server);
140
141         servers_lock.releaseSharedLock();
142         table_lock.releaseSharedLock();
143     }
144
145     /**
146      * Remove a server from the server table.
147      *
148      * @param name the servers name.
149      * @exception UnknownServerName thrown if no server with <code>name</code>
150      * is found in the table.
151      */

152
153     public void removeServer(String JavaDoc name)
154         throws UnknownServerName
155     {
156         table_lock.gainSharedLock();
157         servers_lock.gainSharedLock();
158
159         Object JavaDoc _obj = servers.remove(name);
160
161         servers_lock.releaseSharedLock();
162         table_lock.releaseSharedLock();
163
164         if (_obj == null)
165             throw new UnknownServerName(name);
166     }
167
168     /**
169      * Get the ImRPOAInfo object of a POA.
170      *
171      * @param name the POAs name.
172      * @return the ImRPOAInfo object for <code>name</code>,
173      * null if <code>name</code> not in the table.
174      */

175
176     public ImRPOAInfo getPOA (String JavaDoc name)
177     {
178         return (ImRPOAInfo) poas.get(name);
179     }
180
181     /**
182      * Add a POA to the server table.
183      *
184      * @param name the POAs name.
185      * @param poa the POAs ImRPOAInfo object.
186      */

187
188     public void putPOA(String JavaDoc name, ImRPOAInfo poa)
189     {
190         table_lock.gainSharedLock();
191         poas_lock.gainSharedLock();
192
193         poas.put(name, poa);
194
195         poas_lock.releaseSharedLock();
196         table_lock.releaseSharedLock();
197     }
198
199     /**
200      * Remove a POA from the server table.
201      *
202      * @param name the POAs name.
203      */

204
205     public void removePOA(String JavaDoc name)
206     {
207         table_lock.gainSharedLock();
208         poas_lock.gainSharedLock();
209
210         poas.remove(name);
211
212         poas_lock.releaseSharedLock();
213         table_lock.releaseSharedLock();
214     }
215
216     /**
217      * List all servers in the table.
218      *
219      * @return a ServerInfo array containing all servers.
220      * Used by the CORBA interface of the repository.
221      */

222
223     public ServerInfo[] getServers()
224     {
225         table_lock.gainSharedLock();
226         servers_lock.gainExclusiveLock();
227
228         //build array
229
ServerInfo[] _servers = new ServerInfo[servers.size()];
230         Enumeration _server_enum = servers.elements();
231
232         //copy elements from vector to array
233
int _i = 0;
234         while (_server_enum.hasMoreElements())
235             _servers[_i++] = ((ImRServerInfo) _server_enum.nextElement()).toServerInfo();
236
237         servers_lock.releaseExclusiveLock();
238         table_lock.releaseSharedLock();
239
240         return _servers;
241     }
242
243     /**
244      * List all hosts in the table.
245      *
246      * @return a HostInfo array containing all hosts.
247      * Used by the CORBA interface of the repository.
248      */

249
250     public HostInfo[] getHosts()
251     {
252         table_lock.gainSharedLock();
253         hosts_lock.gainExclusiveLock();
254
255         //build array
256
HostInfo[] _hosts = new HostInfo[hosts.size()];
257         Enumeration _host_enum = hosts.elements();
258     
259         //copy elements from vector to array
260
int _i = 0;
261         while (_host_enum.hasMoreElements())
262             _hosts[_i++] = ((ImRHostInfo) _host_enum.nextElement()).toHostInfo();
263     
264         hosts_lock.releaseExclusiveLock();
265         table_lock.releaseSharedLock();
266
267         return _hosts;
268     }
269
270     /**
271      * List all POAs in the table.
272      *
273      * @return a POAInfo array containing all POAs.
274      * Used by the CORBA interface of the repository.
275      */

276
277     public POAInfo[] getPOAs()
278     {
279         table_lock.gainSharedLock();
280         poas_lock.gainExclusiveLock();
281
282         //build array
283
POAInfo[] _poas = new POAInfo[poas.size()];
284         Enumeration _poa_enum = poas.elements();
285
286         //copy elements from vector to array
287
int _i = 0;
288         while (_poa_enum.hasMoreElements())
289             _poas[_i++] = ((ImRPOAInfo) _poa_enum.nextElement()).toPOAInfo();
290     
291         poas_lock.releaseExclusiveLock();
292         table_lock.releaseSharedLock();
293
294         return _poas;
295     }
296
297     /**
298      * Add a host to the table. If an entry for <code>name</code> is already
299      * in the table it is overwritten.
300      *
301      * @param name the hosts name.
302      * @param host the hosts ImRHostInfo object.
303      */

304
305     public void putHost(String JavaDoc name, ImRHostInfo host)
306     {
307         table_lock.gainSharedLock();
308         hosts_lock.gainSharedLock();
309
310         hosts.put(name, host);
311
312         hosts_lock.releaseSharedLock();
313         table_lock.releaseSharedLock();
314     }
315
316     /**
317      * Remove a host from the table.
318      *
319      * @param name the hosts name.
320      */

321
322     public Object JavaDoc removeHost(String JavaDoc name)
323     {
324         return hosts.remove(name);
325     }
326
327     /**
328      * Get the ImRHostInfo object of a host.
329      *
330      * @param name the hosts name.
331      * @return the ImRHostInfo object for <code>name</code>, null
332      * if <code>name</code> not in the table.
333      */

334
335     public ImRHostInfo getHost(String JavaDoc name)
336     {
337         return (ImRHostInfo) hosts.get(name);
338     }
339
340     /**
341      * Implemented from the Serializable interface. For
342      * automatic initializing after deserialization.
343      */

344
345     private void readObject(java.io.ObjectInputStream JavaDoc in)
346         throws java.io.IOException JavaDoc, java.io.NotActiveException JavaDoc,
347         ClassNotFoundException JavaDoc
348     {
349         in.defaultReadObject();
350         initTransient();
351     }
352 } // ServerTable
353

354
355
Popular Tags