001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  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.kernel.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: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
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 String host = "0.0.0.0";
039        private Registry registry;
040    
041        public int getPort() {
042            return port;
043        }
044    
045        public void setPort(int port) {
046            this.port = port;
047        }
048    
049        public String getHost() {
050            return host;
051        }
052    
053        public void setHost(String host) {
054            this.host = host;
055        }
056        
057        public String getProtocol() {
058            return "rmi";
059        }
060    
061        public void doStart() throws Exception {
062            System.setProperty("java.rmi.server.RMIClassLoaderSpi",RMIClassLoaderSpiImpl.class.getName());
063            registry = LocateRegistry.createRegistry(port);
064            log.debug("Started RMI Registry on port " + port);
065        }
066    
067        public void doStop() throws Exception {
068            UnicastRemoteObject.unexportObject(registry, true);
069            log.debug("Stopped RMI Registry");
070        }
071    
072        public void doFail() {
073            try {
074                doStop();
075            } catch (Exception e) {
076                log.warn("RMI Registry failed");
077            }
078        }
079    
080        public InetSocketAddress getListenAddress() {
081            return new InetSocketAddress(getHost(), getPort());
082        }
083    
084        public static final GBeanInfo GBEAN_INFO;
085    
086        static {
087            GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic("RMI Naming", RMIRegistryService.class);
088            infoFactory.addAttribute("host", String.class, true, true);
089            infoFactory.addAttribute("protocol", String.class, false);
090            infoFactory.addAttribute("port", int.class, true, true);
091            infoFactory.addAttribute("listenAddress", InetSocketAddress.class, false);
092            GBEAN_INFO = infoFactory.getBeanInfo();
093        }
094    
095        public static GBeanInfo getGBeanInfo() {
096            return GBEAN_INFO;
097        }
098    }