Package org.apache.geronimo.kernel.rmi

Source Code of org.apache.geronimo.kernel.rmi.RMIRegistryService

/**
*  Licensed to the Apache Software Foundation (ASF) under one or more
*  contributor license agreements.  See the NOTICE file distributed with
*  this work for additional information regarding copyright ownership.
*  The ASF licenses this file to You 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 org.apache.geronimo.kernel.rmi;

import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.RMIClientSocketFactory;
import java.rmi.server.RMIServerSocketFactory;
import java.rmi.server.RMISocketFactory;
import java.rmi.server.UnicastRemoteObject;
import java.net.InetSocketAddress;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.geronimo.gbean.GBeanInfo;
import org.apache.geronimo.gbean.GBeanInfoBuilder;
import org.apache.geronimo.gbean.GBeanLifecycle;

/**
* Thin GBean wrapper around the RMI Registry.
*
* @version $Rev: 738734 $ $Date: 2009-01-29 11:47:45 +0800 (Thu, 29 Jan 2009) $
*/
public class RMIRegistryService implements GBeanLifecycle {
    private static final Logger log = LoggerFactory.getLogger(RMIRegistryService.class);
   
    private int port = Registry.REGISTRY_PORT;
    private String host = "0.0.0.0";
    private Registry registry;

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }
   
    public String getProtocol() {
        return "rmi";
    }

    public void doStart() throws Exception {
        System.setProperty("java.rmi.server.RMIClassLoaderSpi",RMIClassLoaderSpiImpl.class.getName());
        if (System.getProperty("java.rmi.server.hostname") == null && host != null && !host.equals("0.0.0.0")) {
            System.setProperty("java.rmi.server.hostname", host);
        }
        RMIClientSocketFactory socketFactory = RMISocketFactory.getDefaultSocketFactory();
        RMIServerSocketFactory serverSocketFactory = new GeronimoRMIServerSocketFactory(host);
        registry = LocateRegistry.createRegistry(port, socketFactory, serverSocketFactory);
        log.debug("Started RMI Registry on port: {}", port);
    }

    public void doStop() throws Exception {
        UnicastRemoteObject.unexportObject(registry, true);
        log.debug("Stopped RMI Registry");
    }

    public void doFail() {
        try {
            doStop();
        } catch (Exception e) {
            log.warn("RMI Registry failed");
        }
    }

    public InetSocketAddress getListenAddress() {
        return new InetSocketAddress(getHost(), getPort());
    }

    public static final GBeanInfo GBEAN_INFO;

    static {
        GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic("RMI Naming", RMIRegistryService.class);
        infoFactory.addAttribute("host", String.class, true, true);
        infoFactory.addAttribute("protocol", String.class, false);
        infoFactory.addAttribute("port", int.class, true, true);
        infoFactory.addAttribute("listenAddress", InetSocketAddress.class, false);
        GBEAN_INFO = infoFactory.getBeanInfo();
    }

    public static GBeanInfo getGBeanInfo() {
        return GBEAN_INFO;
    }
}
TOP

Related Classes of org.apache.geronimo.kernel.rmi.RMIRegistryService

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.