00001
00025
package org.objectweb.cjdbc.console.jmx;
00026
00027
import java.io.IOException;
00028
import java.util.HashMap;
00029
import java.util.HashSet;
00030
import java.util.Map;
00031
import java.util.Set;
00032
00033
import javax.management.InstanceNotFoundException;
00034
import javax.management.MBeanServerConnection;
00035
import javax.management.MBeanServerInvocationHandler;
00036
import javax.management.NotificationListener;
00037
import javax.management.ObjectName;
00038
import javax.management.remote.JMXConnector;
00039
import javax.management.remote.JMXConnectorFactory;
00040
import javax.management.remote.JMXServiceURL;
00041
import javax.naming.Context;
00042
import javax.security.auth.Subject;
00043
00044
import org.objectweb.cjdbc.common.exceptions.VirtualDatabaseException;
00045
import org.objectweb.cjdbc.common.jmx.JmxConstants;
00046
import org.objectweb.cjdbc.common.jmx.mbeans.ControllerMBean;
00047
import org.objectweb.cjdbc.common.jmx.mbeans.DataCollectorMBean;
00048
import org.objectweb.cjdbc.common.jmx.mbeans.DatabaseBackendMBean;
00049
import org.objectweb.cjdbc.common.jmx.mbeans.VirtualDatabaseMBean;
00050
import org.objectweb.cjdbc.common.users.AdminUser;
00051
import org.objectweb.cjdbc.controller.authentication.PasswordAuthenticator;
00052
00060 public class RmiJmxClient
00061 {
00062 private JMXConnector
connector;
00063 private Object
credentials;
00064 private String
remoteHostAddress;
00065 private String
remoteHostPort;
00066
00067 private NotificationListener
notificationListener;
00068
00069
00070 private ControllerMBean
controllerMBean;
00071 private VirtualDatabaseMBean
virtualDbMBean;
00072 private DatabaseBackendMBean
backendMBean;
00073 private DataCollectorMBean
dataMBean;
00074
00080 public NotificationListener
getNotificationListener()
00081 {
00082
return notificationListener;
00083 }
00084
00090 public void setNotificationListener(NotificationListener notificationListener)
00091 {
00092
this.notificationListener = notificationListener;
00093 }
00094
00100 public Object
getCredentials()
00101 {
00102
return credentials;
00103 }
00104
00114 public RmiJmxClient(String port, String host, String jmxUser,
00115 String jmxPassword)
throws IOException
00116 {
00117
this(port, host,
PasswordAuthenticator.createCredentials(jmxUser,
00118 jmxPassword));
00119 }
00120
00128 public RmiJmxClient(String url, Object credentials)
throws IOException
00129 {
00130
int index = url.indexOf(
":");
00131 String ip = url.substring(0, index);
00132 String port = url.substring(index + 1);
00133
connect(port, ip,
credentials);
00134 }
00135
00144 public RmiJmxClient(String port, String host, Object credentials)
00145
throws IOException
00146 {
00147
connect(port, host,
credentials);
00148 }
00149
00158 public void connect(String port, String host, Object credentials)
00159
throws IOException
00160 {
00161 JMXServiceURL address =
new JMXServiceURL(
"rmi", host, 0,
"/jndi/jrmp");
00162
00163 Map environment =
new HashMap();
00164 environment.put(Context.INITIAL_CONTEXT_FACTORY,
00165
"com.sun.jndi.rmi.registry.RegistryContextFactory");
00166 environment.put(Context.PROVIDER_URL,
"rmi://" + host +
":" + port);
00167
00168
00169
00170
00171
if (
credentials != null)
00172 {
00173
00174 environment.put(JMXConnector.CREDENTIALS,
credentials);
00175 }
00176
00177
this.credentials =
credentials;
00178
00179
connector = JMXConnectorFactory.connect(address, environment);
00180
remoteHostAddress = host;
00181
remoteHostPort = port;
00182 }
00183
00195 public VirtualDatabaseMBean
getVirtualDatabaseProxy(String database,
00196 String user, String password)
throws InstanceNotFoundException,
00197 IOException,
VirtualDatabaseException
00198 {
00199
if (
virtualDbMBean != null &&
isValidConnection()
00200 &&
virtualDbMBean.getName().equals(database))
00201 {
00202
return virtualDbMBean;
00203 }
00204
else
00205 {
00206 ObjectName db =
JmxConstants.getVirtualDbObjectName(database);
00207
00208
00209
AdminUser dbUser =
new AdminUser(user, password);
00210 Set principals =
new HashSet();
00211 principals.add(dbUser);
00212 Subject subj =
new Subject(
true, principals,
new HashSet(),
new HashSet());
00213
00214
00215
00216 MBeanServerConnection delegateConnection =
connector
00217 .getMBeanServerConnection(subj);
00218
00219
00220 VirtualDatabaseMBean local = (VirtualDatabaseMBean) MBeanServerInvocationHandler
00221 .newProxyInstance(delegateConnection, db, VirtualDatabaseMBean.class,
00222
false);
00223
00224
00225
boolean authenticated =
false;
00226
try
00227 {
00228 authenticated = local.checkAdminAuthentication(user, password);
00229 }
00230
catch (Exception e)
00231 {
00232
throw new VirtualDatabaseException(
00233
"Could not check authentication. MBean is not accessible.");
00234 }
00235
if (!authenticated)
00236
throw new VirtualDatabaseException(
"Authentication Failed");
00237
00238
00239
if (
notificationListener != null)
00240 {
00241 delegateConnection.addNotificationListener(db,
notificationListener,
00242 null, null);
00243 }
00244
00245
this.virtualDbMBean = local;
00246
00247
return virtualDbMBean;
00248 }
00249 }
00250
00258 public ControllerMBean
getControllerProxy() throws InstanceNotFoundException,
00259 IOException
00260 {
00261
00262
if (
controllerMBean != null &&
isValidConnection())
00263 {
00264
return controllerMBean;
00265 }
00266
else
00267 {
00268 ObjectName db =
JmxConstants.getControllerObjectName();
00269
00270
00271
controllerMBean = (ControllerMBean) MBeanServerInvocationHandler
00272 .newProxyInstance(
connector.getMBeanServerConnection(), db,
00273 ControllerMBean.class,
false);
00274
00275
00276
if (
notificationListener != null)
00277 {
00278
connector.getMBeanServerConnection().addNotificationListener(db,
00279
notificationListener, null, null);
00280 }
00281
00282
return controllerMBean;
00283 }
00284 }
00285
00292 public DataCollectorMBean
getDataCollectorProxy() throws IOException
00293 {
00294
00295
if (
dataMBean != null &&
isValidConnection())
00296 {
00297
return dataMBean;
00298 }
00299
else
00300 {
00301 ObjectName db =
JmxConstants.getDataCollectorObjectName();
00302
00303
00304
dataMBean = (DataCollectorMBean) MBeanServerInvocationHandler
00305 .newProxyInstance(
connector.getMBeanServerConnection(), db,
00306 DataCollectorMBean.class,
false);
00307
return dataMBean;
00308 }
00309 }
00310
00322 public DatabaseBackendMBean
getDatabaseBackendProxy(String vdb,
00323 String backend, String user, String password)
00324
throws InstanceNotFoundException, IOException
00325 {
00326
if (
backendMBean != null &&
isValidConnection())
00327 {
00328
try
00329 {
00330
if (
backendMBean.getName().equals(backend))
00331
return backendMBean;
00332 }
00333
catch (Exception e)
00334 {
00335
00336 }
00337 }
00338
00339
00340
AdminUser dbUser =
new AdminUser(user, password);
00341 Set principals =
new HashSet();
00342 principals.add(dbUser);
00343 Subject subj =
new Subject(
true, principals,
new HashSet(),
new HashSet());
00344
00345 ObjectName db =
JmxConstants.getDatabaseBackendObjectName(vdb, backend);
00346 MBeanServerConnection delegateConnection =
connector
00347 .getMBeanServerConnection(subj);
00348
00349
if (
notificationListener != null)
00350 {
00351 delegateConnection.addNotificationListener(db,
notificationListener,
00352 null, null);
00353 }
00354
00355
00356
backendMBean = (DatabaseBackendMBean) MBeanServerInvocationHandler
00357 .newProxyInstance(delegateConnection, db, DatabaseBackendMBean.class,
00358
false);
00359
return backendMBean;
00360 }
00361
00368 public String
getRemoteName()
00369 {
00370
return remoteHostAddress +
":" +
remoteHostPort;
00371 }
00372
00378 public String
getRemoteHostAddress()
00379 {
00380
return remoteHostAddress;
00381 }
00382
00388 public String
getRemoteHostPort()
00389 {
00390
return remoteHostPort;
00391 }
00392
00398 public void reconnect() throws Exception
00399 {
00400
connect(
remoteHostPort,
remoteHostAddress,
credentials);
00401 }
00402
00408 public boolean isValidConnection()
00409 {
00410
try
00411 {
00412
connector.getMBeanServerConnection().getMBeanCount();
00413
return true;
00414 }
00415
catch (Exception e)
00416 {
00417
controllerMBean = null;
00418
backendMBean = null;
00419
virtualDbMBean = null;
00420
dataMBean = null;
00421
return false;
00422 }
00423 }
00424 }