Source for gnu.java.rmi.registry.RegistryImpl

   1: /* RegistryImpl.java --
   2:    Copyright (c) 1996-2016 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   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.
  10: 
  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.
  15: 
  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., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  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.
  25: 
  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. */
  37: 
  38: package gnu.java.rmi.registry;
  39: 
  40: import gnu.java.rmi.server.UnicastServerRef;
  41: 
  42: import java.rmi.AccessException;
  43: import java.rmi.AlreadyBoundException;
  44: import java.rmi.NotBoundException;
  45: import java.rmi.Remote;
  46: import java.rmi.RemoteException;
  47: import java.rmi.registry.LocateRegistry;
  48: import java.rmi.registry.Registry;
  49: import java.rmi.server.ObjID;
  50: import java.rmi.server.RMIClientSocketFactory;
  51: import java.rmi.server.RMIServerSocketFactory;
  52: import java.rmi.server.RMISocketFactory;
  53: import java.rmi.server.UnicastRemoteObject;
  54: import java.util.Enumeration;
  55: import java.util.Hashtable;
  56: 
  57: public class RegistryImpl
  58:         extends UnicastRemoteObject implements Registry {
  59: 
  60: private Hashtable bindings = new Hashtable();
  61: 
  62: public RegistryImpl(int port) throws RemoteException {
  63:         this(port, RMISocketFactory.getSocketFactory(), RMISocketFactory.getSocketFactory());
  64: }
  65: 
  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);
  70: }
  71: 
  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);
  76:         }
  77:         return ((Remote)obj);
  78: }
  79: 
  80: public void bind(String name, Remote obj) throws RemoteException, AlreadyBoundException, AccessException {
  81:         if (bindings.containsKey(name)) {
  82:                 throw new AlreadyBoundException(name);
  83:         }
  84:         bindings.put(name, obj);
  85: }
  86: 
  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);
  91:         }
  92: }
  93: 
  94: public void rebind(String name, Remote obj) throws RemoteException, AccessException {
  95:         bindings.put(name, obj);
  96: }
  97: 
  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();
 104:         }
 105:         return (strings);
 106: }
 107: 
 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 2016 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);
 117: }
 118: 
 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);
 126: }
 127: 
 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();
 133:                 }
 134:                 else if (args[0].equals("--help")) {
 135:                         help();
 136:                 }
 137:                 try {
 138:                         port = Integer.parseInt(args[0]);
 139:                 }
 140:                 catch (NumberFormatException _) {
 141:                         System.err.println("Bad port number - using default");
 142:                 }
 143:         }
 144: 
 145:         try {
 146:                 Registry impl = LocateRegistry.createRegistry(port);
 147:         }
 148:         catch (RemoteException _) {
 149:                 System.err.println("Registry failed");
 150:         }
 151: }
 152: 
 153: }