1 /**
2 *
3 * Copyright 2004 The Apache Software Foundation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.geronimo.system.rmi;
18
19 import java.rmi.registry.LocateRegistry;
20 import java.rmi.registry.Registry;
21 import java.rmi.server.UnicastRemoteObject;
22 import java.net.InetSocketAddress;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.geronimo.gbean.GBeanInfo;
27 import org.apache.geronimo.gbean.GBeanInfoBuilder;
28 import org.apache.geronimo.gbean.GBeanLifecycle;
29
30 /**
31 * Thin GBean wrapper around the RMI Registry.
32 *
33 * @version $Rev: 355877 $ $Date: 2005-12-10 18:48:27 -0800 (Sat, 10 Dec 2005) $
34 */
35 public class RMIRegistryService implements GBeanLifecycle {
36 private static final Log log = LogFactory.getLog(RMIRegistryService.class);
37 private int port = Registry.REGISTRY_PORT;
38 private Registry registry;
39
40 public int getPort() {
41 return port;
42 }
43
44 public void setPort(int port) {
45 this.port = port;
46 }
47
48 public String getHost() {
49 return "0.0.0.0";
50 }
51
52 public String getProtocol() {
53 return "rmi";
54 }
55
56 public void doStart() throws Exception {
57 System.setProperty("java.rmi.server.RMIClassLoaderSpi",RMIClassLoaderSpiImpl.class.getName());
58 registry = LocateRegistry.createRegistry(port);
59 log.debug("Started RMI Registry on port " + port);
60 }
61
62 public void doStop() throws Exception {
63 UnicastRemoteObject.unexportObject(registry, true);
64 log.debug("Stopped RMI Registry");
65 }
66
67 public void doFail() {
68 try {
69 doStop();
70 } catch (Exception e) {
71 log.warn("RMI Registry failed");
72 }
73 }
74
75 public InetSocketAddress getListenAddress() {
76 return new InetSocketAddress(getHost(), getPort());
77 }
78
79 public static final GBeanInfo GBEAN_INFO;
80
81 static {
82 GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic("RMI Naming", RMIRegistryService.class);
83 infoFactory.addAttribute("host", String.class, false);
84 infoFactory.addAttribute("protocol", String.class, false);
85 infoFactory.addAttribute("port", int.class, true, true);
86 infoFactory.addAttribute("listenAddress", InetSocketAddress.class, false);
87 GBEAN_INFO = infoFactory.getBeanInfo();
88 }
89
90 public static GBeanInfo getGBeanInfo() {
91 return GBEAN_INFO;
92 }
93 }