Merge from the pain train
[official-gcc.git] / libjava / gnu / java / rmi / registry / RegistryImpl.java
blob43033c4b8ad84e656f6ca1f3075b2a35ffff1326
1 /*
2 Copyright (c) 1996, 1997, 1998, 1999, 2002, 2005
3 Free Software Foundation, Inc.
5 This file is part of GNU Classpath.
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA
20 02111-1307 USA.
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.
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. */
39 package gnu.java.rmi.registry;
41 import java.rmi.registry.Registry;
42 import java.rmi.registry.LocateRegistry;
43 import java.rmi.RemoteException;
44 import java.rmi.NotBoundException;
45 import java.rmi.AccessException;
46 import java.rmi.AlreadyBoundException;
47 import java.rmi.Remote;
48 import java.rmi.server.UnicastRemoteObject;
49 import java.rmi.server.ObjID;
50 import java.util.Hashtable;
51 import java.util.Enumeration;
52 import java.rmi.server.RMISocketFactory;
53 import java.rmi.server.RMIClientSocketFactory;
54 import java.rmi.server.RMIServerSocketFactory;
55 import gnu.java.rmi.server.UnicastServerRef;
57 public class RegistryImpl
58 extends UnicastRemoteObject implements Registry {
60 private Hashtable bindings = new Hashtable();
62 public RegistryImpl(int port) throws RemoteException {
63 this(port, RMISocketFactory.getSocketFactory(), RMISocketFactory.getSocketFactory());
66 public RegistryImpl(int port, RMIClientSocketFactory cf, RMIServerSocketFactory sf) throws RemoteException {
67 super(new UnicastServerRef(new ObjID(ObjID.REGISTRY_ID), port, sf));
68 // The following is unnecessary, because UnicastRemoteObject export itself automatically.
69 //((UnicastServerRef)getRef()).exportObject(this);
72 public Remote lookup(String name) throws RemoteException, NotBoundException, AccessException {
73 Object obj = bindings.get(name);
74 if (obj == null) {
75 throw new NotBoundException(name);
77 return ((Remote)obj);
80 public void bind(String name, Remote obj) throws RemoteException, AlreadyBoundException, AccessException {
81 if (bindings.containsKey(name)) {
82 throw new AlreadyBoundException(name);
84 bindings.put(name, obj);
87 public void unbind(String name) throws RemoteException, NotBoundException, AccessException {
88 Object obj = bindings.remove(name);
89 if (obj == null) {
90 throw new NotBoundException(name);
94 public void rebind(String name, Remote obj) throws RemoteException, AccessException {
95 bindings.put(name, obj);
98 public String[] list() throws RemoteException, AccessException {
99 int size = bindings.size();
100 String[] strings = new String[size];
101 Enumeration e = bindings.keys();
102 for (int i = 0; i < size; i++) {
103 strings[i] = (String)e.nextElement();
105 return (strings);
108 public static void version() {
109 System.out.println("rmiregistry ("
110 + System.getProperty("java.vm.name")
111 + ") "
112 + System.getProperty("java.vm.version"));
113 System.out.println("Copyright 2005 Free Software Foundation, Inc.");
114 System.out.println("This is free software; see the source for copying conditions. There is NO");
115 System.out.println("warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.");
116 System.exit(0);
119 public static void help() {
120 System.out.println(
121 "Usage: rmiregistry [OPTION | PORT]\n" +
122 "\n" +
123 " --help Print this help, then exit\n" +
124 " --version Print version number, then exit\n");
125 System.exit(0);
128 public static void main(String[] args) {
129 int port = Registry.REGISTRY_PORT;
130 if (args.length > 0) {
131 if (args[0].equals("--version")) {
132 version();
134 else if (args[0].equals("--help")) {
135 help();
137 try {
138 port = Integer.parseInt(args[0]);
140 catch (NumberFormatException _) {
141 System.err.println("Bad port number - using default");
145 try {
146 Registry impl = LocateRegistry.createRegistry(port);
148 catch (RemoteException _) {
149 System.err.println("Registry failed");