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.RMIClientSocketFactory;
022 import java.rmi.server.RMIServerSocketFactory;
023 import java.rmi.server.RMISocketFactory;
024 import java.rmi.server.UnicastRemoteObject;
025 import java.net.InetSocketAddress;
026
027 import org.apache.commons.logging.Log;
028 import org.apache.commons.logging.LogFactory;
029 import org.apache.geronimo.gbean.GBeanInfo;
030 import org.apache.geronimo.gbean.GBeanInfoBuilder;
031 import org.apache.geronimo.gbean.GBeanLifecycle;
032
033 /**
034 * Thin GBean wrapper around the RMI Registry.
035 *
036 * @version $Rev: 738735 $ $Date: 2009-01-29 11:48:01 +0800 (Thu, 29 Jan 2009) $
037 */
038 public class RMIRegistryService implements GBeanLifecycle {
039 private static final Log log = LogFactory.getLog(RMIRegistryService.class);
040 private int port = Registry.REGISTRY_PORT;
041 private String host = "0.0.0.0";
042 private Registry registry;
043
044 public int getPort() {
045 return port;
046 }
047
048 public void setPort(int port) {
049 this.port = port;
050 }
051
052 public String getHost() {
053 return host;
054 }
055
056 public void setHost(String host) {
057 this.host = host;
058 }
059
060 public String getProtocol() {
061 return "rmi";
062 }
063
064 public void doStart() throws Exception {
065 System.setProperty("java.rmi.server.RMIClassLoaderSpi",RMIClassLoaderSpiImpl.class.getName());
066 if (System.getProperty("java.rmi.server.hostname") == null && host != null && !host.equals("0.0.0.0")) {
067 System.setProperty("java.rmi.server.hostname", host);
068 }
069 RMIClientSocketFactory socketFactory = RMISocketFactory.getDefaultSocketFactory();
070 RMIServerSocketFactory serverSocketFactory = new GeronimoRMIServerSocketFactory(host);
071 registry = LocateRegistry.createRegistry(port, socketFactory, serverSocketFactory);
072 log.debug("Started RMI Registry on port " + port);
073 }
074
075 public void doStop() throws Exception {
076 UnicastRemoteObject.unexportObject(registry, true);
077 log.debug("Stopped RMI Registry");
078 }
079
080 public void doFail() {
081 try {
082 doStop();
083 } catch (Exception e) {
084 log.warn("RMI Registry failed");
085 }
086 }
087
088 public InetSocketAddress getListenAddress() {
089 return new InetSocketAddress(getHost(), getPort());
090 }
091
092 public static final GBeanInfo GBEAN_INFO;
093
094 static {
095 GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic("RMI Naming", RMIRegistryService.class);
096 infoFactory.addAttribute("host", String.class, true, true);
097 infoFactory.addAttribute("protocol", String.class, false);
098 infoFactory.addAttribute("port", int.class, true, true);
099 infoFactory.addAttribute("listenAddress", InetSocketAddress.class, false);
100 GBEAN_INFO = infoFactory.getBeanInfo();
101 }
102
103 public static GBeanInfo getGBeanInfo() {
104 return GBEAN_INFO;
105 }
106 }