001    /**
002     *
003     * Copyright 2004 The Apache Software Foundation
004     *
005     *  Licensed under the Apache License, Version 2.0 (the "License");
006     *  you may not use this file except in compliance with the License.
007     *  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    package org.apache.geronimo.system.rmi;
018    
019    import java.rmi.registry.LocateRegistry;
020    import java.rmi.registry.Registry;
021    import java.rmi.server.UnicastRemoteObject;
022    import java.net.InetSocketAddress;
023    
024    import org.apache.commons.logging.Log;
025    import org.apache.commons.logging.LogFactory;
026    import org.apache.geronimo.gbean.GBeanInfo;
027    import org.apache.geronimo.gbean.GBeanInfoBuilder;
028    import org.apache.geronimo.gbean.GBeanLifecycle;
029    
030    /**
031     * Thin GBean wrapper around the RMI Registry.
032     *
033     * @version $Rev: 355877 $ $Date: 2005-12-10 18:48:27 -0800 (Sat, 10 Dec 2005) $
034     */
035    public class RMIRegistryService implements GBeanLifecycle {
036        private static final Log log = LogFactory.getLog(RMIRegistryService.class);
037        private int port = Registry.REGISTRY_PORT;
038        private Registry registry;
039    
040        public int getPort() {
041            return port;
042        }
043    
044        public void setPort(int port) {
045            this.port = port;
046        }
047    
048        public String getHost() {
049            return "0.0.0.0";
050        }
051    
052        public String getProtocol() {
053            return "rmi";
054        }
055    
056        public void doStart() throws Exception {
057            System.setProperty("java.rmi.server.RMIClassLoaderSpi",RMIClassLoaderSpiImpl.class.getName());
058            registry = LocateRegistry.createRegistry(port);
059            log.debug("Started RMI Registry on port " + port);
060        }
061    
062        public void doStop() throws Exception {
063            UnicastRemoteObject.unexportObject(registry, true);
064            log.debug("Stopped RMI Registry");
065        }
066    
067        public void doFail() {
068            try {
069                doStop();
070            } catch (Exception e) {
071                log.warn("RMI Registry failed");
072            }
073        }
074    
075        public InetSocketAddress getListenAddress() {
076            return new InetSocketAddress(getHost(), getPort());
077        }
078    
079        public static final GBeanInfo GBEAN_INFO;
080    
081        static {
082            GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic("RMI Naming", RMIRegistryService.class);
083            infoFactory.addAttribute("host", String.class, false);
084            infoFactory.addAttribute("protocol", String.class, false);
085            infoFactory.addAttribute("port", int.class, true, true);
086            infoFactory.addAttribute("listenAddress", InetSocketAddress.class, false);
087            GBEAN_INFO = infoFactory.getBeanInfo();
088        }
089    
090        public static GBeanInfo getGBeanInfo() {
091            return GBEAN_INFO;
092        }
093    }