00001
00025
package org.objectweb.cjdbc.console.jmx;
00026
00027
import java.io.IOException;
00028
import java.rmi.RemoteException;
00029
00030
import javax.management.InstanceNotFoundException;
00031
import javax.management.MBeanException;
00032
import javax.management.MBeanServerConnection;
00033
import javax.management.NotificationFilter;
00034
import javax.management.NotificationListener;
00035
import javax.management.ObjectName;
00036
import javax.management.ReflectionException;
00037
import javax.management.remote.JMXConnector;
00038
import javax.management.remote.JMXConnectorFactory;
00039
import javax.management.remote.JMXServiceURL;
00040
import javax.naming.Context;
00041
00042
import org.objectweb.cjdbc.common.jmx.JmxConstants;
00043
import org.objectweb.cjdbc.common.jmx.JmxException;
00044
00053 public abstract class JmxClient
00054 {
00055
00056 protected MBeanServerConnection
server;
00057 protected String
remoteHostName;
00058 protected String
remoteHostAddress;
00059 protected String
remoteHostPort;
00060 protected Object
credentials;
00061 protected ObjectName
mbean;
00062
00072 public void connect(String host, String port, Object credentials)
00073
throws JmxException
00074 {
00075
try
00076 {
00077
this.credentials =
credentials;
00078
00079 javax.management.remote.JMXServiceURL address =
new JMXServiceURL(
"rmi",
00080 host, 0,
"/jndi/jrmp");
00081
00082
00083 java.util.Map environment =
new java.util.HashMap();
00084 environment.put(Context.INITIAL_CONTEXT_FACTORY,
00085
"com.sun.jndi.rmi.registry.RegistryContextFactory");
00086 environment.put(Context.PROVIDER_URL,
"rmi://" + host +
":" + port);
00087
00088
if (
credentials != null)
00089 environment.put(JMXConnector.CREDENTIALS,
credentials);
00090
00091 JMXConnector connector = JMXConnectorFactory.newJMXConnector(address,
00092 environment);
00093 connector.connect(environment);
00094
00095
00096
00097
remoteHostName = host;
00098
remoteHostAddress = host;
00099
remoteHostPort = port;
00100
00101
00102
00103
server = connector.getMBeanServerConnection();
00104
00105 }
00106
catch (Exception e)
00107 {
00108
throw new JmxException(e);
00109 }
00110 }
00111
00121 public void addNotificationListener(NotificationListener listener,
00122 NotificationFilter filter, Object handback)
00123
throws InstanceNotFoundException, IOException
00124 {
00125
server.addNotificationListener(
mbean, listener, filter, handback);
00126 }
00127
00134 public void checkConnection() throws
JmxException
00135 {
00136
try
00137 {
00138
int count =
server.getMBeanCount().intValue();
00139
00140
00141
00142
00143
00144
00145
if (
JmxConstants.DEBUG)
00146 System.out.println(
"#DEBUG@Server Count " + count +
" MBeans");
00147 }
00148
catch (Exception re)
00149 {
00150
try
00151 {
00152
if (
JmxConstants.DEBUG)
00153 System.out.println(
"Connection lost. Trying to reconnect...");
00154
connect(
remoteHostName,
remoteHostPort,
credentials);
00155 }
00156
catch (Exception e)
00157 {
00158
throw new JmxException(
"Could not reconnect to host:" +
remoteHostName);
00159 }
00160 }
00161 }
00162
00168 public boolean validateTarget()
00169 {
00170
try
00171 {
00172
return server.isRegistered(
mbean);
00173 }
00174
catch (Exception e)
00175 {
00176
return false;
00177 }
00178 }
00179
00190 public Object
invoke(String methodName, Object[] parameters)
00191
throws JmxException, MBeanException
00192 {
00193
if (parameters == null)
00194
return this.invoke(methodName, null, null);
00195
else
00196 {
00197
int size = parameters.length;
00198 String[] paramTypes =
new String[size];
00199
for (
int i = 0; i < size; i++)
00200 paramTypes[i] = parameters[i].getClass().getName();
00201
return this.invoke(methodName, parameters, paramTypes);
00202 }
00203 }
00204
00216 public Object
invoke(String methodName, Object[] parameters,
00217 String[] paramTypes)
throws JmxException, MBeanException
00218 {
00219
if (
JmxConstants.DEBUG)
00220 System.out.println(
"#DEBUG#" + methodName +
" for mbean:"
00221 +
mbean.getCanonicalName() +
" on:" +
this.remoteHostName);
00222
if (
JmxConstants.KEEP_CONNECTION_ALIVE)
00223
checkConnection();
00224
try
00225 {
00226
return server.invoke(
mbean, methodName, parameters, paramTypes);
00227 }
00228
catch (InstanceNotFoundException e)
00229 {
00230
throw new JmxException(e.getMessage());
00231 }
00232
catch (MBeanException e)
00233 {
00234
throw e;
00235 }
00236
catch (ReflectionException e)
00237 {
00238
throw new JmxException(e.getMessage());
00239 }
00240
catch (RemoteException e)
00241 {
00242
throw new JmxException(e.getMessage());
00243 }
00244
catch (java.io.IOException e)
00245 {
00246
throw new JmxException(e.getMessage(), e);
00247 }
00248 }
00249
00253 public String
getRemoteHostAddress()
00254 {
00255
return remoteHostAddress;
00256 }
00257
00261 public String
getRemoteHostName()
00262 {
00263
return remoteHostName;
00264 }
00265
00269 public String
getRemoteHostPort()
00270 {
00271
return remoteHostPort;
00272 }
00273
00277 public Object
getCredentials()
00278 {
00279
return credentials;
00280 }
00281
00288 public String
getRemoteName()
00289 {
00290
return remoteHostName +
":" +
remoteHostPort;
00291 }
00292 }