00001
00025
package org.objectweb.cjdbc.controller.jmx;
00026
00027
import java.net.InetAddress;
00028
import java.net.UnknownHostException;
00029
import java.rmi.Remote;
00030
import java.rmi.registry.LocateRegistry;
00031
import java.rmi.server.UnicastRemoteObject;
00032
import java.util.ArrayList;
00033
import java.util.Date;
00034
import java.util.Hashtable;
00035
import java.util.Iterator;
00036
import java.util.List;
00037
00038
import javax.management.Notification;
00039
import javax.management.remote.JMXAuthenticator;
00040
import javax.management.remote.JMXConnectorServer;
00041
import javax.management.remote.rmi.RMIConnectorServer;
00042
import javax.naming.Context;
00043
00044
import org.objectweb.cjdbc.common.i18n.Translate;
00045
import org.objectweb.cjdbc.common.jmx.JmxException;
00046
import org.objectweb.cjdbc.common.jmx.notifications.JmxNotification;
00047
import org.objectweb.cjdbc.common.log.Trace;
00048
import org.objectweb.cjdbc.common.net.RMISSLClientSocketFactory;
00049
import org.objectweb.cjdbc.common.net.RMISSLServerSocketFactory;
00050
import org.objectweb.cjdbc.common.net.SSLConfiguration;
00051
import org.objectweb.cjdbc.common.net.SocketFactoryFactory;
00052
import org.objectweb.cjdbc.controller.authentication.PasswordAuthenticator;
00053
00060 public class RmiConnector
00061 {
00062 static Trace
logger = Trace
00063 .getLogger(
"org.objectweb.cjdbc.controller.jmx");
00064
00065 private String
controllerName;
00066 private String
hostName;
00067 private int port;
00068 private JMXAuthenticator
authenticator;
00069 private SSLConfiguration
sslConfig;
00070
00076 private JMXConnectorServer
connection;
00077 private Remote
rmiRegistry;
00078
00079 private static List
rmiConnectors =
new ArrayList();
00080
00092 public RmiConnector(String controllerName, String hostName,
int port,
00093 JMXAuthenticator authenticator, SSLConfiguration sslConfig)
00094
throws JmxException
00095 {
00096
if (
hostName != null)
00097 {
00098
this.hostName =
hostName;
00099 }
00100
else
00101 {
00102
try
00103 {
00105
this.hostName = InetAddress.getLocalHost().getHostName();
00106 }
00107
catch (UnknownHostException ex)
00108 {
00109
throw new JmxException(ex);
00110 }
00111 }
00112
this.controllerName =
controllerName;
00113
this.port =
port;
00114
this.authenticator =
authenticator;
00115
this.sslConfig =
sslConfig;
00116
00117
addRmiConnector(
this);
00118 }
00119
00125 public JMXAuthenticator
getAuthenticator()
00126 {
00127
return authenticator;
00128 }
00129
00135 public void setAuthenticator(JMXAuthenticator authenticator)
00136 {
00137
this.authenticator = authenticator;
00138 }
00139
00145 public int getPort()
00146 {
00147
return port;
00148 }
00149
00155 public void setPort(
int port)
00156 {
00157
this.port = port;
00158 }
00159
00165 public SSLConfiguration
getSslConfig()
00166 {
00167
return sslConfig;
00168 }
00169
00175 public void setSslConfig(SSLConfiguration sslConfig)
00176 {
00177
this.sslConfig = sslConfig;
00178 }
00179
00185 public JMXConnectorServer
getConnection()
00186 {
00187
return connection;
00188 }
00189
00195 public void start() throws
JmxException
00196 {
00197
createNamingService();
00198
createJRMPAdaptor();
00199 }
00200
00206 public void stop() throws
JmxException
00207 {
00208
try
00209 {
00210
if (
connection != null)
00211
connection.stop();
00212
if (
rmiRegistry != null)
00213 UnicastRemoteObject.unexportObject(
rmiRegistry,
true);
00214 }
00215
catch (Exception e)
00216 {
00217
throw new JmxException(e);
00218 }
00219 finally
00220 {
00221
connection = null;
00222
rmiRegistry = null;
00223 }
00224 }
00225
00231 private void createNamingService() throws
JmxException
00232 {
00233
try
00234 {
00235
00236
logger.info(
Translate.get(
"jmx.create.naming.service",
new String[]{
""
00237 +
port}));
00238
rmiRegistry = LocateRegistry.createRegistry(
port);
00239 }
00240
catch (Exception e)
00241 {
00242
throw new JmxException(e);
00243 }
00244 }
00245
00246 private void createJRMPAdaptor() throws
JmxException
00247 {
00248
try
00249 {
00250
00251 logger.info(
Translate.get(
"jmx.create.jrmp.adaptor",
"" + port));
00252
00253
00254
00255 logger.debug(
Translate.get(
"jmx.prepare.jndi"));
00256
00257 javax.management.remote.JMXServiceURL address =
new javax.management.remote.JMXServiceURL(
00258
"rmi", hostName, 0,
"/jndi/jrmp");
00259
00260 java.util.Map environment =
new java.util.HashMap();
00261 environment.put(Context.INITIAL_CONTEXT_FACTORY,
00262
"com.sun.jndi.rmi.registry.RegistryContextFactory");
00263 environment.put(Context.PROVIDER_URL,
"rmi://" + hostName +
":" + port);
00264
00265
if (authenticator == null)
00266 {
00267 authenticator =
PasswordAuthenticator.NO_AUTHENICATION;
00268 }
00269
00270
if (authenticator != null)
00271 {
00272 environment.put(JMXConnectorServer.AUTHENTICATOR, authenticator);
00273 }
00274
00275
00276
if (sslConfig != null)
00277 {
00278 logger.info(
Translate.get(
"jmx.create.jrmp.ssl.enabled"));
00279
00280
RMISSLClientSocketFactory csf =
new RMISSLClientSocketFactory();
00281
RMISSLServerSocketFactory ssf =
new RMISSLServerSocketFactory(
00282
SocketFactoryFactory.createServerFactory(sslConfig));
00283 environment.put(RMIConnectorServer.RMI_CLIENT_SOCKET_FACTORY_ATTRIBUTE,
00284 csf);
00285 environment.put(RMIConnectorServer.RMI_SERVER_SOCKET_FACTORY_ATTRIBUTE,
00286 ssf);
00287 }
00288
00289 connection = javax.management.remote.JMXConnectorServerFactory
00290 .newJMXConnectorServer(address, environment,
MBeanServerManager
00291 .getInstance());
00292
00293 connection.start();
00294 }
00295
catch (Exception e)
00296 {
00297
throw new JmxException(e);
00298 }
00299 }
00300
00306 public static List getRmiConnectors()
00307 {
00308
return rmiConnectors;
00309 }
00310
00316 private static synchronized void addRmiConnector(
RmiConnector pRmiConnector)
00317 {
00318 rmiConnectors.add(pRmiConnector);
00319 }
00320
00324 public String getControllerName()
00325 {
00326
return controllerName;
00327 }
00328
00332 public String getHostName()
00333 {
00334
return hostName;
00335 }
00336
00337 private Date myDate;
00338 private long time;
00339 private JmxNotification cjdbcNotification;
00340 private Notification notification;
00341 private static long sequence = 0;
00342
00358 public synchronized void sendNotification(
AbstractStandardMBean mbean,
00359 String type, String priority, String description, Hashtable data)
00360 {
00361
00362 myDate =
new Date();
00363 time = myDate.getTime();
00364
00365 cjdbcNotification =
new JmxNotification(priority,
"" + sequence, type,
00366 description,
"" + time, controllerName, mbean.getClass().getName(),
00367
"mbeanName", hostName,
"" + port, data);
00368 notification =
new Notification(type, mbean, sequence, myDate.getTime(),
00369 description);
00370 notification.setUserData(cjdbcNotification.toString());
00371 mbean.
sendNotification(notification);
00372 }
00373
00385 public static void broadcastNotification(
AbstractStandardMBean mbean,
00386 String type, String priority, String description, Hashtable data)
00387 {
00388 sequence++;
00389 logger.info(
"Sending notification:" + description +
"(Message No:"
00390 + sequence +
")");
00391 Iterator iter = rmiConnectors.iterator();
00392
RmiConnector rmi;
00393
while (iter.hasNext())
00394 {
00395 rmi = ((
RmiConnector) iter.next());
00396 rmi.
sendNotification(mbean, type, priority, description, data);
00397 }
00398 }
00399 }