1 23 24 39 40 package com.sun.enterprise.admin.jmx.remote.server.rmi; 41 42 import java.io.IOException ; 43 import java.net.ServerSocket ; 44 import java.util.Map ; 45 import java.util.HashMap ; 46 import java.rmi.RemoteException ; 47 import java.rmi.server.RMIServerSocketFactory ; 48 import java.rmi.server.RMIClientSocketFactory ; 49 import java.util.logging.Logger ; 50 import java.util.logging.Level ; 51 import javax.management.MBeanServer ; 52 import javax.management.remote.JMXServiceURL ; 53 import javax.management.remote.JMXConnectorServer ; 54 import javax.management.remote.JMXConnectorServerFactory ; 55 import javax.management.remote.JMXAuthenticator ; 56 import javax.management.remote.rmi.RMIConnectorServer ; 57 import javax.rmi.ssl.SslRMIClientSocketFactory ; 58 59 import com.sun.enterprise.admin.jmx.remote.IStringManager; 60 import com.sun.enterprise.admin.jmx.remote.StringManagerFactory; 61 import com.sun.enterprise.admin.jmx.remote.server.rmi.RemoteJmxProtocol; 62 import com.sun.enterprise.admin.jmx.remote.server.rmi.JmxServiceUrlFactory; 63 64 73 74 public class JmxConnectorServerDriver { 75 private static IStringManager sm = StringManagerFactory.getServerStringManager( JmxConnectorServerDriver.class ); 76 private final Map env; 77 private JMXServiceURL url; 78 private JMXServiceURL jconsoleurl; 79 private int port; 80 private RemoteJmxProtocol protocol; 81 private boolean secureRegistry; 82 private boolean ssl; 83 private RMIServerSocketFactory rmissf; 84 private RMIClientSocketFactory rmicsf; 85 private boolean auth; 86 private JMXAuthenticator authenticator; 87 private Logger logger; 88 private MBeanServer mbs; 89 90 94 95 public JmxConnectorServerDriver() { 96 this.env = new HashMap (); 97 this.protocol = RemoteJmxProtocol.RMIJRMP; 98 this.secureRegistry = false; 99 this.port = getFreePort(); 100 this.ssl = false; 101 this.auth = false; 102 this.authenticator = null; 103 logger = Logger.getLogger(this.getClass().getName()); 104 } 105 106 109 public void setProtocol(final RemoteJmxProtocol protocol) { 110 this.protocol = protocol; 111 } 112 115 public void setPort(final int port) { 116 this.port = port; 118 } 119 121 public void setSsl(final boolean ssl) { 122 this.ssl = ssl; 123 } 124 125 130 public void setRmiServerSocketFactory(final RMIServerSocketFactory f) { 131 if (ssl && f == null) 132 throw new IllegalArgumentException ("Internal: null server socket factory passed with ssl ON"); 133 this.rmissf = f; 134 } 135 140 public void setRmiClientSocketFactory(final RMIClientSocketFactory f) { 141 if (ssl && f == null) 142 throw new IllegalArgumentException ("Internal: null client socket factory passed with ssl ON"); 143 this.rmicsf = f; 144 } 145 149 public void setAuthentication(final boolean auth) { 150 this.auth = auth; 151 } 152 153 public void setLogger (final Logger logger) { 154 if (logger == null) 155 throw new IllegalArgumentException ("Internal: null logger"); 156 this.logger = logger; 157 } 158 161 public void setAuthenticator(final JMXAuthenticator authenticator) { 162 this.authenticator = authenticator; 166 } 167 169 public void setRmiRegistrySecureFlag(final boolean secure) { 170 this.secureRegistry = secure; 171 } 172 176 public void setMBeanServer(final MBeanServer mbs) { 177 if (mbs == null) 178 throw new IllegalArgumentException ("null mbs"); 179 this.mbs = mbs; 180 } 181 187 public JMXConnectorServer startConnectorServer() throws IOException { 188 prepare(); 190 formJmxServiceUrl(); 191 createEnvironment(); 192 193 final JMXConnectorServer cs = 194 JMXConnectorServerFactory.newJMXConnectorServer(url, env, mbs); 195 196 cs.start(); 197 logStartup(cs); 198 return ( cs ); 199 } 200 201 public JMXConnectorServer startJconsoleConnectorServer() throws IOException { 202 final RMIClientSocketFactory cf = new SslRMIClientSocketFactory (); 204 final Map jconsoleenv = new HashMap (env); 205 if (ssl) 206 jconsoleenv.put(RMIConnectorServer.RMI_CLIENT_SOCKET_FACTORY_ATTRIBUTE, cf); 207 final JMXConnectorServer jconsolecs = 208 JMXConnectorServerFactory.newJMXConnectorServer(jconsoleurl, jconsoleenv, mbs); 209 jconsolecs.start(); 210 logJconsoleStartup(jconsolecs); 211 return ( jconsolecs ); 212 } 213 214 220 public void stopConnectorServer(final JMXConnectorServer cs) throws IOException { 221 final String cad = cs.getAddress().toString(); 222 if (cs.isActive()) { 223 logger.log(Level.FINE, "rjmx.lc.stopping", cad); 224 cs.stop(); 225 } 226 else { 227 final String msg = "JMX Connector Server: " + cad + " is not active"; 228 logger.fine(msg); 229 } 230 } 231 232 private void logStartup(final JMXConnectorServer cs) { 233 logger.log(Level.FINE, "rjmx.lc.address", cs.getAddress().toString()); 234 logger.log(Level.FINE, "rjmx.lc.status", "" + cs.isActive()); 235 } 236 237 238 private void logJconsoleStartup(final JMXConnectorServer cs) { 239 logger.log(Level.INFO, "rjmx.std.address", cs.getAddress().toString()); 240 logger.log(Level.INFO, "rjmx.std.status", "" + cs.isActive()); 241 } 242 243 private void formJmxServiceUrl() { 244 if (protocol == RemoteJmxProtocol.RMIJRMP) { 246 this.url = JmxServiceUrlFactory.forRmiWithJndiInAppserver( 247 JmxServiceUrlFactory.localhost(), this.port); 248 this.jconsoleurl = JmxServiceUrlFactory.forJconsoleOverRmiWithJndiInAppserver( 249 JmxServiceUrlFactory.localhost(), this.port); 250 } 251 } 252 private void prepare() { 253 if (protocol == RemoteJmxProtocol.RMIJRMP) { 254 new RmiStubRegistryHandler(port, secureRegistry, logger); 255 } 256 } 257 258 private void createEnvironment() { 259 env.clear(); 260 handleSsl(); 261 handleAuth(); 262 } 263 264 private void handleSsl () { 265 if (protocol == RemoteJmxProtocol.RMIJRMP) 266 env.put(RMIConnectorServer.RMI_SERVER_SOCKET_FACTORY_ATTRIBUTE, rmissf); 267 if (ssl) env.put(RMIConnectorServer.RMI_CLIENT_SOCKET_FACTORY_ATTRIBUTE, rmicsf); 268 } 269 private void handleAuth() { 270 if (protocol == RemoteJmxProtocol.RMIJRMP || 271 protocol == RemoteJmxProtocol.RMIIIOP) { 272 if (auth) { 273 if (authenticator == null) { 274 String msg = "Internal: The authentication is on, but the authenticator is null"; 275 throw new IllegalArgumentException ("msg"); 276 } 277 env.put(JMXConnectorServer.AUTHENTICATOR, authenticator); 278 } 279 } 280 } 281 282 302 public static int getFreePort() { 303 int freePort = 0; 304 boolean portFound = false; 305 ServerSocket serverSocket = null; 306 307 synchronized (JmxConnectorServerDriver.class) { 308 try { 309 311 serverSocket = new ServerSocket (0); 312 freePort = serverSocket.getLocalPort(); 313 portFound = true; 314 } catch(Exception e) { 315 } finally { 317 if (!portFound) freePort = 0; 318 try { 319 if (serverSocket != null) { 320 serverSocket.close(); 321 if (! serverSocket.isClosed()) 322 throw new Exception ("local exception ..."); 323 } 324 } catch(Exception e) { 325 freePort = 0; 327 } 328 } 329 return freePort; 330 } 331 } 332 333 334 } 335 | Popular Tags |