1 22 23 package org.continuent.sequoia.controller.jmx; 24 25 import java.net.InetAddress ; 26 import java.net.UnknownHostException ; 27 import java.rmi.Remote ; 28 import java.rmi.registry.LocateRegistry ; 29 import java.rmi.server.UnicastRemoteObject ; 30 import java.util.Date ; 31 import java.util.Hashtable ; 32 33 import javax.management.Notification ; 34 import javax.management.remote.JMXAuthenticator ; 35 import javax.management.remote.JMXConnectorServer ; 36 import javax.management.remote.rmi.RMIConnectorServer ; 37 import javax.naming.Context ; 38 39 import org.continuent.sequoia.common.authentication.PasswordAuthenticator; 40 import org.continuent.sequoia.common.i18n.Translate; 41 import org.continuent.sequoia.common.jmx.JmxException; 42 import org.continuent.sequoia.common.jmx.notifications.JmxNotification; 43 import org.continuent.sequoia.common.log.Trace; 44 import org.continuent.sequoia.common.net.RMISSLClientSocketFactory; 45 import org.continuent.sequoia.common.net.RMISSLServerSocketFactory; 46 import org.continuent.sequoia.common.net.SSLConfiguration; 47 import org.continuent.sequoia.common.net.SocketFactoryFactory; 48 49 55 public class RmiConnector 56 { 57 static Trace logger = Trace 58 .getLogger("org.continuent.sequoia.controller.jmx"); 59 60 private String controllerName; 61 private String hostName; 62 private int port; 63 private JMXAuthenticator authenticator; 64 private SSLConfiguration sslConfig; 65 66 71 private JMXConnectorServer connection; 72 private Remote rmiRegistry; 73 74 85 public RmiConnector(String controllerName, String hostName, int port, 86 JMXAuthenticator authenticator, SSLConfiguration sslConfig) 87 throws JmxException 88 { 89 if (hostName != null) 90 { 91 this.hostName = hostName; 92 } 93 else 94 { 95 try 96 { 97 98 this.hostName = InetAddress.getLocalHost().getHostName(); 99 } 100 catch (UnknownHostException ex) 101 { 102 throw new JmxException(ex); 103 } 104 } 105 this.controllerName = controllerName; 106 this.port = port; 107 this.authenticator = authenticator; 108 this.sslConfig = sslConfig; 109 } 110 111 116 public JMXAuthenticator getAuthenticator() 117 { 118 return authenticator; 119 } 120 121 126 public void setAuthenticator(JMXAuthenticator authenticator) 127 { 128 this.authenticator = authenticator; 129 } 130 131 136 public int getPort() 137 { 138 return port; 139 } 140 141 146 public void setPort(int port) 147 { 148 this.port = port; 149 } 150 151 156 public SSLConfiguration getSslConfig() 157 { 158 return sslConfig; 159 } 160 161 166 public void setSslConfig(SSLConfiguration sslConfig) 167 { 168 this.sslConfig = sslConfig; 169 } 170 171 176 public JMXConnectorServer getConnection() 177 { 178 return connection; 179 } 180 181 186 public void start() throws JmxException 187 { 188 createNamingService(); 189 createJRMPAdaptor(); 190 } 191 192 197 public void stop() throws JmxException 198 { 199 try 200 { 201 if (connection != null) 202 connection.stop(); 203 if (rmiRegistry != null) 204 UnicastRemoteObject.unexportObject(rmiRegistry, true); 205 } 206 catch (Exception e) 207 { 208 throw new JmxException(e); 209 } 210 finally 211 { 212 connection = null; 213 rmiRegistry = null; 214 } 215 } 216 217 222 private void createNamingService() throws JmxException 223 { 224 try 225 { 226 logger.info(Translate.get("jmx.create.naming.service", new String []{"" 228 + port})); 229 rmiRegistry = LocateRegistry.createRegistry(port); 230 } 231 catch (Exception e) 232 { 233 throw new JmxException(e); 234 } 235 } 236 237 private void createJRMPAdaptor() throws JmxException 238 { 239 try 240 { 241 logger.info(Translate.get("jmx.create.jrmp.adaptor", "" + port)); 243 244 logger.debug(Translate.get("jmx.prepare.jndi")); 247 248 251 javax.management.remote.JMXServiceURL address = new javax.management.remote.JMXServiceURL ( 252 "rmi", hostName, 0, "/jndi/jrmp"); 253 254 java.util.Map environment = new java.util.HashMap (); 255 environment.put(Context.INITIAL_CONTEXT_FACTORY, 256 "com.sun.jndi.rmi.registry.RegistryContextFactory"); 257 environment.put(Context.PROVIDER_URL, "rmi://" + hostName + ":" + port); 258 259 if (authenticator == null) 260 { 261 authenticator = PasswordAuthenticator.NO_AUTHENICATION; 262 } 263 264 if (authenticator != null) 265 { 266 environment.put(JMXConnectorServer.AUTHENTICATOR, authenticator); 267 } 268 269 if (sslConfig != null) 271 { 272 logger.info(Translate.get("jmx.create.jrmp.ssl.enabled")); 273 274 RMISSLClientSocketFactory csf = new RMISSLClientSocketFactory(); 275 RMISSLServerSocketFactory ssf = new RMISSLServerSocketFactory( 276 SocketFactoryFactory.createServerFactory(sslConfig)); 277 environment.put(RMIConnectorServer.RMI_CLIENT_SOCKET_FACTORY_ATTRIBUTE, 278 csf); 279 environment.put(RMIConnectorServer.RMI_SERVER_SOCKET_FACTORY_ATTRIBUTE, 280 ssf); 281 } 282 283 connection = javax.management.remote.JMXConnectorServerFactory 284 .newJMXConnectorServer(address, environment, MBeanServerManager 285 .getInstance()); 286 287 connection.start(); 288 } 289 catch (Exception e) 290 { 291 throw new JmxException(e); 292 } 293 } 294 295 298 public String getControllerName() 299 { 300 return controllerName; 301 } 302 303 306 public String getHostName() 307 { 308 return hostName; 309 } 310 311 private Date myDate; 312 private long time; 313 private JmxNotification sequoiaNotification; 314 private Notification notification; 315 private static long sequence = 0; 316 317 333 public synchronized void sendNotification(AbstractStandardMBean mbean, 334 String type, String priority, String description, Hashtable data) 335 { 336 337 myDate = new Date (); 338 time = myDate.getTime(); 339 340 sequoiaNotification = new JmxNotification(priority, "" + sequence, type, 341 description, "" + time, controllerName, mbean.getClass().getName(), 342 "mbeanName", hostName, "" + port, data); 343 notification = new Notification (type, mbean, sequence, myDate.getTime(), 344 description); 345 notification.setUserData(sequoiaNotification.toString()); 346 mbean.sendNotification(notification); 347 } 348 349 } | Popular Tags |