1 24 25 package org.objectweb.cjdbc.controller.jmx; 26 27 import java.net.InetAddress ; 28 import java.net.UnknownHostException ; 29 import java.rmi.Remote ; 30 import java.rmi.registry.LocateRegistry ; 31 import java.rmi.server.UnicastRemoteObject ; 32 import java.util.ArrayList ; 33 import java.util.Date ; 34 import java.util.Hashtable ; 35 import java.util.Iterator ; 36 import java.util.List ; 37 38 import javax.management.Notification ; 39 import javax.management.remote.JMXAuthenticator ; 40 import javax.management.remote.JMXConnectorServer ; 41 import javax.management.remote.rmi.RMIConnectorServer ; 42 import javax.naming.Context ; 43 44 import org.objectweb.cjdbc.common.i18n.Translate; 45 import org.objectweb.cjdbc.common.jmx.JmxException; 46 import org.objectweb.cjdbc.common.jmx.notifications.JmxNotification; 47 import org.objectweb.cjdbc.common.log.Trace; 48 import org.objectweb.cjdbc.common.net.RMISSLClientSocketFactory; 49 import org.objectweb.cjdbc.common.net.RMISSLServerSocketFactory; 50 import org.objectweb.cjdbc.common.net.SSLConfiguration; 51 import org.objectweb.cjdbc.common.net.SocketFactoryFactory; 52 import org.objectweb.cjdbc.controller.authentication.PasswordAuthenticator; 53 54 60 public class RmiConnector 61 { 62 static Trace logger = Trace 63 .getLogger("org.objectweb.cjdbc.controller.jmx"); 64 65 private String controllerName; 66 private String hostName; 67 private int port; 68 private JMXAuthenticator authenticator; 69 private SSLConfiguration sslConfig; 70 71 76 private JMXConnectorServer connection; 77 private Remote rmiRegistry; 78 79 private static List rmiConnectors = new ArrayList (); 80 81 92 public RmiConnector(String controllerName, String hostName, int port, 93 JMXAuthenticator authenticator, SSLConfiguration sslConfig) 94 throws JmxException 95 { 96 if (hostName != null) 97 { 98 this.hostName = hostName; 99 } 100 else 101 { 102 try 103 { 104 105 this.hostName = InetAddress.getLocalHost().getHostName(); 106 } 107 catch (UnknownHostException ex) 108 { 109 throw new JmxException(ex); 110 } 111 } 112 this.controllerName = controllerName; 113 this.port = port; 114 this.authenticator = authenticator; 115 this.sslConfig = sslConfig; 116 117 addRmiConnector(this); 118 } 119 120 125 public JMXAuthenticator getAuthenticator() 126 { 127 return authenticator; 128 } 129 130 135 public void setAuthenticator(JMXAuthenticator authenticator) 136 { 137 this.authenticator = authenticator; 138 } 139 140 145 public int getPort() 146 { 147 return port; 148 } 149 150 155 public void setPort(int port) 156 { 157 this.port = port; 158 } 159 160 165 public SSLConfiguration getSslConfig() 166 { 167 return sslConfig; 168 } 169 170 175 public void setSslConfig(SSLConfiguration sslConfig) 176 { 177 this.sslConfig = sslConfig; 178 } 179 180 185 public JMXConnectorServer getConnection() 186 { 187 return connection; 188 } 189 190 195 public void start() throws JmxException 196 { 197 createNamingService(); 198 createJRMPAdaptor(); 199 } 200 201 206 public void stop() throws JmxException 207 { 208 try 209 { 210 if (connection != null) 211 connection.stop(); 212 if (rmiRegistry != null) 213 UnicastRemoteObject.unexportObject(rmiRegistry, true); 214 } 215 catch (Exception e) 216 { 217 throw new JmxException(e); 218 } 219 finally 220 { 221 connection = null; 222 rmiRegistry = null; 223 } 224 } 225 226 231 private void createNamingService() throws JmxException 232 { 233 try 234 { 235 logger.info(Translate.get("jmx.create.naming.service", new String []{"" 237 + port})); 238 rmiRegistry = LocateRegistry.createRegistry(port); 239 } 240 catch (Exception e) 241 { 242 throw new JmxException(e); 243 } 244 } 245 246 private void createJRMPAdaptor() throws JmxException 247 { 248 try 249 { 250 logger.info(Translate.get("jmx.create.jrmp.adaptor", "" + port)); 252 253 logger.debug(Translate.get("jmx.prepare.jndi")); 256 257 javax.management.remote.JMXServiceURL address = new javax.management.remote.JMXServiceURL ( 258 "rmi", hostName, 0, "/jndi/jrmp"); 259 260 java.util.Map environment = new java.util.HashMap (); 261 environment.put(Context.INITIAL_CONTEXT_FACTORY, 262 "com.sun.jndi.rmi.registry.RegistryContextFactory"); 263 environment.put(Context.PROVIDER_URL, "rmi://" + hostName + ":" + port); 264 265 if (authenticator == null) 266 { 267 authenticator = PasswordAuthenticator.NO_AUTHENICATION; 268 } 269 270 if (authenticator != null) 271 { 272 environment.put(JMXConnectorServer.AUTHENTICATOR, authenticator); 273 } 274 275 if (sslConfig != null) 277 { 278 logger.info(Translate.get("jmx.create.jrmp.ssl.enabled")); 279 280 RMISSLClientSocketFactory csf = new RMISSLClientSocketFactory(); 281 RMISSLServerSocketFactory ssf = new RMISSLServerSocketFactory( 282 SocketFactoryFactory.createServerFactory(sslConfig)); 283 environment.put(RMIConnectorServer.RMI_CLIENT_SOCKET_FACTORY_ATTRIBUTE, 284 csf); 285 environment.put(RMIConnectorServer.RMI_SERVER_SOCKET_FACTORY_ATTRIBUTE, 286 ssf); 287 } 288 289 connection = javax.management.remote.JMXConnectorServerFactory 290 .newJMXConnectorServer(address, environment, MBeanServerManager 291 .getInstance()); 292 293 connection.start(); 294 } 295 catch (Exception e) 296 { 297 throw new JmxException(e); 298 } 299 } 300 301 306 public static List getRmiConnectors() 307 { 308 return rmiConnectors; 309 } 310 311 316 private static synchronized void addRmiConnector(RmiConnector pRmiConnector) 317 { 318 rmiConnectors.add(pRmiConnector); 319 } 320 321 324 public String getControllerName() 325 { 326 return controllerName; 327 } 328 329 332 public String getHostName() 333 { 334 return hostName; 335 } 336 337 private Date myDate; 338 private long time; 339 private JmxNotification cjdbcNotification; 340 private Notification notification; 341 private static long sequence = 0; 342 343 359 public synchronized void sendNotification(AbstractStandardMBean mbean, 360 String type, String priority, String description, Hashtable data) 361 { 362 363 myDate = new Date (); 364 time = myDate.getTime(); 365 366 cjdbcNotification = new JmxNotification(priority, "" + sequence, type, 367 description, "" + time, controllerName, mbean.getClass().getName(), 368 "mbeanName", hostName, "" + port, data); 369 notification = new Notification (type, mbean, sequence, myDate.getTime(), 370 description); 371 notification.setUserData(cjdbcNotification.toString()); 372 mbean.sendNotification(notification); 373 } 374 375 388 public static void broadcastNotification(AbstractStandardMBean mbean, 389 String type, String priority, String description, Hashtable data) 390 { 391 sequence++; 392 logger.info("Sending notification:" + description + "(Message No:" 393 + sequence + ")"); 394 Iterator iter = rmiConnectors.iterator(); 395 RmiConnector rmi; 396 while (iter.hasNext()) 397 { 398 rmi = ((RmiConnector) iter.next()); 399 rmi.sendNotification(mbean, type, priority, description, data); 400 } 401 } 402 } | Popular Tags |