1 17 18 package org.sape.carbon.services.jmx.server.mx4j; 19 20 import java.rmi.server.ExportException ; 21 22 import javax.management.Attribute ; 23 import javax.management.InstanceAlreadyExistsException ; 24 import javax.management.MBeanException ; 25 import javax.management.MBeanServer ; 26 import javax.management.ObjectInstance ; 27 import javax.management.ObjectName ; 28 import javax.naming.InitialContext ; 29 30 import org.sape.carbon.core.component.ComponentConfiguration; 31 import org.sape.carbon.core.component.lifecycle.Configurable; 32 import org.sape.carbon.core.component.lifecycle.Startable; 33 import org.sape.carbon.services.jmx.server.MBeanServerRetreiveException; 34 import org.sape.carbon.services.jmx.server.MBeanServerService; 35 36 import mx4j.adaptor.rmi.jrmp.JRMPAdaptorMBean; 37 import mx4j.util.StandardMBeanProxy; 38 import org.apache.commons.logging.Log; 39 import org.apache.commons.logging.LogFactory; 40 41 42 54 public class DefaultJrmpRemotingImpl 55 implements MBeanServerService, Configurable, Startable { 56 57 60 private Log log = LogFactory.getLog(this.getClass()); 61 62 63 private ObjectName registryObjectName; 64 65 66 private ObjectInstance registryObjectInstance; 67 68 69 private static final String REGISTRY_OBJECT_NAME_STRING = 70 "Naming:type=rmiregistry"; 71 72 73 private static final String NAMING_SERVICE_CLASS_NAME = 74 "mx4j.tools.naming.NamingService"; 75 76 77 private ObjectName adaptorObjectName; 78 79 80 private JRMPAdaptorMBean adaptorMBean; 81 82 83 private static final String ADAPTOR_OBJECT_NAME_STRING = 84 "Adaptor:protocol=JRMP"; 85 86 87 private static final String ADAPTOR_CLASS_NAME = 88 "mx4j.adaptor.rmi.jrmp.JRMPAdaptor"; 89 90 91 private static final String URL_ID = "rmi://"; 92 93 94 private Class initialContextFactoryClass; 95 96 97 private String hostname; 98 99 100 private int port; 101 102 103 private MBeanServerService mbeanServerService; 104 105 112 public void configure(ComponentConfiguration componentConfiguration) 113 throws Exception { 114 115 JrmpRemotingConfiguration config = 116 (JrmpRemotingConfiguration) componentConfiguration; 117 118 this.initialContextFactoryClass = 119 config.getInitialContextFactoryClass(); 120 121 this.hostname = config.getHostname(); 122 this.port = config.getPort(); 123 this.mbeanServerService = config.getMBeanServerService(); 124 125 126 MBeanServer server = getMBeanServer(); 127 128 this.registryObjectName = new ObjectName (REGISTRY_OBJECT_NAME_STRING); 130 131 try { 132 this.registryObjectInstance = 133 server.createMBean( 134 NAMING_SERVICE_CLASS_NAME, 135 this.registryObjectName, 136 null); 137 } catch (InstanceAlreadyExistsException iaee) { 138 } 140 141 boolean started = false; 142 while (!started) { 143 started = true; 144 try { 145 server.invoke(this.registryObjectName, "start", null, null); 147 } catch (MBeanException mbe) { 148 if (this.port > (config.getPort() + 5)) { 149 throw mbe; 150 } 151 152 started = false; 153 if (mbe.getTargetException() instanceof ExportException ) { 154 server.setAttribute( 155 this.registryObjectName, 156 new Attribute ("Port", 157 new Integer (++this.port))); 158 } 159 } 160 161 } 162 if (log.isDebugEnabled()) { 163 log.debug("Able to start naming server on port [" 164 + this.port + "]"); 165 } 166 167 this.adaptorObjectName = new ObjectName (ADAPTOR_OBJECT_NAME_STRING); 169 170 171 try { 172 server.createMBean(ADAPTOR_CLASS_NAME, 173 this.adaptorObjectName, null); 174 175 } catch (InstanceAlreadyExistsException iaee) { 176 } 178 179 this.adaptorMBean = 180 (JRMPAdaptorMBean) 181 StandardMBeanProxy.create( 182 JRMPAdaptorMBean.class, 183 server, 184 adaptorObjectName); 185 186 187 String jndiName = "jrmp"; 189 this.adaptorMBean.putNamingProperty( 190 InitialContext.INITIAL_CONTEXT_FACTORY, 191 this.initialContextFactoryClass.getName()); 192 this.adaptorMBean.putNamingProperty( 193 InitialContext.PROVIDER_URL, 194 this.URL_ID + this.hostname + ":" + this.port); 195 196 this.adaptorMBean.setJNDIName(jndiName); 197 198 } 199 200 205 public void start() throws Exception { 206 MBeanServer server = getMBeanServer(); 208 209 210 211 212 this.adaptorMBean.start(); 214 } 215 216 221 public void stop() throws Exception { 222 MBeanServer server = getMBeanServer(); 224 225 this.adaptorMBean.stop(); 226 227 server.invoke(this.registryObjectName, "stop", null, null); 229 } 230 231 238 public MBeanServer getMBeanServer() throws MBeanServerRetreiveException { 239 return this.mbeanServerService.getMBeanServer(); 240 } 241 242 } 243 | Popular Tags |