Source for gnu.java.rmi.registry.RegistryImpl

   1: /* RegistryImpl.java --
   2:    Copyright (c) 1996, 1997, 1998, 1999, 2002, 2005
   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.java.rmi.registry;
  40: 
  41: import gnu.java.rmi.server.UnicastServerRef;
  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.LocateRegistry;
  49: import java.rmi.registry.Registry;
  50: import java.rmi.server.ObjID;
  51: import java.rmi.server.RMIClientSocketFactory;
  52: import java.rmi.server.RMIServerSocketFactory;
  53: import java.rmi.server.RMISocketFactory;
  54: import java.rmi.server.UnicastRemoteObject;
  55: import java.util.Enumeration;
  56: import java.util.Hashtable;
  57: 
  58: public class RegistryImpl
  59:     extends UnicastRemoteObject implements Registry {
  60: 
  61: private Hashtable bindings = new Hashtable();
  62: 
  63: public RegistryImpl(int port) throws RemoteException {
  64:     this(port, RMISocketFactory.getSocketFactory(), RMISocketFactory.getSocketFactory());
  65: }
  66: 
  67: public RegistryImpl(int port, RMIClientSocketFactory cf, RMIServerSocketFactory sf) throws RemoteException {
  68:     super(new UnicastServerRef(new ObjID(ObjID.REGISTRY_ID), port, sf));
  69:     // The following is unnecessary, because UnicastRemoteObject export itself automatically.
  70:     //((UnicastServerRef)getRef()).exportObject(this);
  71: }
  72: 
  73: public Remote lookup(String name) throws RemoteException, NotBoundException, AccessException {
  74:     Object obj = bindings.get(name);
  75:     if (obj == null) {
  76:         throw new NotBoundException(name);
  77:     }
  78:     return ((Remote)obj);
  79: }
  80: 
  81: public void bind(String name, Remote obj) throws RemoteException, AlreadyBoundException, AccessException {
  82:     if (bindings.containsKey(name)) {
  83:         throw new AlreadyBoundException(name);
  84:     }
  85:     bindings.put(name, obj);
  86: }
  87: 
  88: public void unbind(String name) throws RemoteException, NotBoundException, AccessException {
  89:     Object obj = bindings.remove(name);
  90:     if (obj == null) {
  91:         throw new NotBoundException(name);
  92:     }
  93: }
  94: 
  95: public void rebind(String name, Remote obj) throws RemoteException, AccessException {
  96:     bindings.put(name, obj);
  97: }
  98: 
  99: public String[] list() throws RemoteException, AccessException {
 100:     int size = bindings.size();
 101:     String[] strings = new String[size];
 102:     Enumeration e = bindings.keys();
 103:     for (int i = 0; i < size; i++) {
 104:         strings[i] = (String)e.nextElement();
 105:     }
 106:     return (strings);
 107: }
 108: 
 109: public static void version() {
 110:     System.out.println("rmiregistry ("
 111:                + System.getProperty("java.vm.name")
 112:                + ") "
 113:                + System.getProperty("java.vm.version"));
 114:     System.out.println("Copyright 2006 Free Software Foundation, Inc.");
 115:     System.out.println("This is free software; see the source for copying conditions.  There is NO");
 116:     System.out.println("warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.");
 117:     System.exit(0);
 118: }
 119: 
 120: public static void help() {
 121:     System.out.println(
 122: "Usage: rmiregistry [OPTION | PORT]\n" +
 123: "\n" +
 124: "    --help                Print this help, then exit\n" +
 125: "    --version             Print version number, then exit\n");
 126:     System.exit(0);
 127: }
 128: 
 129: public static void main(String[] args) {
 130:     int port = Registry.REGISTRY_PORT;
 131:     if (args.length > 0) {
 132:         if (args[0].equals("--version")) {
 133:             version();
 134:         }
 135:         else if (args[0].equals("--help")) {
 136:             help();
 137:         }
 138:         try {
 139:             port = Integer.parseInt(args[0]);
 140:         }
 141:         catch (NumberFormatException _) {
 142:             System.err.println("Bad port number - using default");
 143:         }
 144:     }
 145: 
 146:     try {
 147:         Registry impl = LocateRegistry.createRegistry(port);
 148:     }
 149:     catch (RemoteException _) {
 150:         System.err.println("Registry failed");
 151:     }
 152: }
 153: 
 154: }