/* * RegistryImpl.java * , * Copyright 2007 Eamonn McManus * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.mcmanus.eamonn.customregistry; import java.rmi.AlreadyBoundException; import java.rmi.NotBoundException; import java.rmi.Remote; import java.rmi.RemoteException; import java.rmi.StubNotFoundException; import java.rmi.registry.Registry; import java.rmi.server.RemoteStub; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; public class RegistryImpl implements Registry { private ConcurrentMap map = new ConcurrentHashMap(); public void bind(String name, Remote obj) throws AlreadyBoundException, RemoteException { nameNotNull(name); objNotNull(obj); if (map.putIfAbsent(name, obj) != null) throw new AlreadyBoundException(name); } public String[] list() { return map.keySet().toArray(new String[0]); } public Remote lookup(String name) throws NotBoundException { nameNotNull(name); Remote obj = map.get(name); if (obj == null) throw new NotBoundException(name); return obj; } public void rebind(String name, Remote obj) throws RemoteException { nameNotNull(name); objNotNull(obj); map.put(name, obj); } public void unbind(String name) throws NotBoundException { nameNotNull(name); if (map.remove(name) == null) throw new NotBoundException(name); } private static void nameNotNull(String name) { if (name == null) throw new NullPointerException("Name"); } private static void objNotNull(Remote obj) { if (obj == null) throw new NullPointerException("Remote object"); } }