2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / gnu / java / rmi / registry / RegistryImpl.java
blob007d5a97de98805a5e22b47b9bb42d0b9230f11e
1 /*
2 Copyright (c) 1996, 1997, 1998, 1999, 2002 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package gnu.java.rmi.registry;
40 import java.rmi.registry.Registry;
41 import java.rmi.registry.LocateRegistry;
42 import java.rmi.RemoteException;
43 import java.rmi.NotBoundException;
44 import java.rmi.AccessException;
45 import java.rmi.AlreadyBoundException;
46 import java.rmi.Remote;
47 import java.rmi.server.UnicastRemoteObject;
48 import java.rmi.server.ObjID;
49 import java.util.Hashtable;
50 import java.util.Enumeration;
51 import java.rmi.server.RMISocketFactory;
52 import java.rmi.server.RMIClientSocketFactory;
53 import java.rmi.server.RMIServerSocketFactory;
54 import gnu.java.rmi.server.UnicastServerRef;
56 public class RegistryImpl
57 extends UnicastRemoteObject implements Registry {
59 private Hashtable bindings = new Hashtable();
61 public RegistryImpl(int port) throws RemoteException {
62 this(port, RMISocketFactory.getSocketFactory(), RMISocketFactory.getSocketFactory());
65 public RegistryImpl(int port, RMIClientSocketFactory cf, RMIServerSocketFactory sf) throws RemoteException {
66 super(new UnicastServerRef(new ObjID(ObjID.REGISTRY_ID), port, sf));
67 // The following is unnecessary, because UnicastRemoteObject export itself automatically.
68 //((UnicastServerRef)getRef()).exportObject(this);
71 public Remote lookup(String name) throws RemoteException, NotBoundException, AccessException {
72 Object obj = bindings.get(name);
73 if (obj == null) {
74 throw new NotBoundException(name);
76 return ((Remote)obj);
79 public void bind(String name, Remote obj) throws RemoteException, AlreadyBoundException, AccessException {
80 if (bindings.containsKey(name)) {
81 throw new AlreadyBoundException(name);
83 bindings.put(name, obj);
86 public void unbind(String name) throws RemoteException, NotBoundException, AccessException {
87 Object obj = bindings.remove(name);
88 if (obj == null) {
89 throw new NotBoundException(name);
93 public void rebind(String name, Remote obj) throws RemoteException, AccessException {
94 bindings.put(name, obj);
97 public String[] list() throws RemoteException, AccessException {
98 int size = bindings.size();
99 String[] strings = new String[size];
100 Enumeration e = bindings.keys();
101 for (int i = 0; i < size; i++) {
102 strings[i] = (String)e.nextElement();
104 return (strings);
107 public static void version() {
108 System.out.println("rmiregistry ("
109 + System.getProperty("java.vm.name")
110 + ") "
111 + System.getProperty("java.vm.version"));
112 System.out.println("Copyright 2002 Free Software Foundation, Inc.");
113 System.out.println("This is free software; see the source for copying conditions. There is NO");
114 System.out.println("warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.");
115 System.exit(0);
118 public static void help() {
119 System.out.println(
120 "Usage: rmiregistry [OPTION | PORT]\n" +
121 "\n" +
122 " --help Print this help, then exit\n" +
123 " --version Print version number, then exit\n");
124 System.exit(0);
127 public static void main(String[] args) {
128 int port = Registry.REGISTRY_PORT;
129 if (args.length > 0) {
130 if (args[0].equals("--version")) {
131 version();
133 else if (args[0].equals("--help")) {
134 help();
136 try {
137 port = Integer.parseInt(args[0]);
139 catch (NumberFormatException _) {
140 System.err.println("Bad port number - using default");
144 try {
145 Registry impl = LocateRegistry.createRegistry(port);
147 catch (RemoteException _) {
148 System.err.println("Registry failed");