1:
38:
39: package ;
40:
41: import ;
42:
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55: import ;
56: import ;
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:
70:
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: }