00001
00025 package org.objectweb.cjdbc.console.jmx;
00026
00027
import java.io.DataInputStream;
00028
import java.io.File;
00029
import java.io.FileInputStream;
00030
import java.io.FileNotFoundException;
00031
import java.io.IOException;
00032
import java.util.ArrayList;
00033
00034
import javax.management.MBeanException;
00035
00036
import org.objectweb.cjdbc.common.jmx.JmxConstants;
00037
import org.objectweb.cjdbc.common.jmx.JmxException;
00038
00046 public class ControllerJmxClient extends JmxClient
00047 {
00048
00052 public void connect(String host, String port, Object credentials)
00053
throws JmxException
00054 {
00055 mbean =
JmxConstants.getControllerObjectName();
00056 super.connect(host, port, credentials);
00057
00058
if (!
this.validateTarget())
00059 {
00060
throw new JmxException(
"Invalid MBean target");
00061 }
00062 }
00063
00067 public boolean isDistributedVirtualDatabase(String databaseName)
00068
throws JmxException, MBeanException
00069 {
00070
return ((Boolean)
invoke(
"isDistributedVirtualDatabase",
00071
new Object[]{databaseName},
new String[]{
"java.lang.String"}))
00072 .booleanValue();
00073 }
00074
00080 public void connect(String controllerName)
throws JmxException
00081 {
00082
int index = controllerName.indexOf(
":");
00083 String ip = controllerName.substring(0, index);
00084 String port = controllerName.substring(index + 1);
00085
this.connect(ip, port, credentials);
00086 }
00087
00095 public String
getInfo() throws
JmxException, MBeanException
00096 {
00097 StringBuffer info =
new StringBuffer();
00098 info.append((String) (
invoke(
"viewConfiguration", null)));
00099 info.append((String) (
invoke(
"viewDatabases", null)));
00100
return info.toString();
00101 }
00102
00110 public String
getXml() throws
JmxException, MBeanException
00111 {
00112
return (String) (
invoke(
"viewInfo", null, null));
00113 }
00114
00122 public String
viewDatabasesXml() throws
JmxException, MBeanException
00123 {
00124
return (String) (
invoke(
"viewDatabasesXml", null, null));
00125 }
00126
00135 public String
viewDatabaseXml(String databaseName)
throws JmxException,
00136 MBeanException
00137 {
00138
return (String) (
invoke(
"viewDatabaseXml",
new Object[]{databaseName},
00139
new String[]{
"java.lang.String"}));
00140 }
00141
00148 public void refreshLogConfiguration() throws
JmxException, MBeanException
00149 {
00150
invoke(
"refreshLogConfiguration", null, null);
00151
00152 }
00153
00162 public void addVirtualDatabase(String xmlConfig)
throws JmxException,
00163 MBeanException
00164 {
00165
invoke(
"addVirtualDatabases",
new Object[]{xmlConfig},
00166
new String[]{
"java.lang.String"});
00167 }
00168
00172 private void addDriver(byte[] bytes)
throws JmxException, MBeanException
00173 {
00174
invoke(
"addDriver",
new Object[]{bytes},
new String[]{
"[B"});
00175 }
00176
00186 public void addDriver(String filename)
throws FileNotFoundException,
00187 IOException,
JmxException, MBeanException
00188 {
00189 File file;
00190 FileInputStream fileInput = null;
00191 file =
new File(filename);
00192 fileInput =
new FileInputStream(file);
00193
00194
00195
long size = file.length();
00196
if (size > Integer.MAX_VALUE)
00197 {
00198
00199
throw new IOException(
"File is too large");
00200 }
00201 byte[] bytes =
new byte[(
int) size];
00202
00203
int nb = fileInput.read(bytes);
00204 fileInput.close();
00205
if (nb != size)
00206 {
00207
throw new IOException(
"Failed to read the file");
00208 }
00209
addDriver(bytes);
00210 }
00211
00216 public void addVirtualDatabases(String xml, String virtualName,
00217
boolean autoEnable, String checkPoint)
throws JmxException,
00218 MBeanException
00219 {
00220
00221
invoke(
"addVirtualDatabase",
new Object[]{xml, virtualName,
00222
new Boolean(autoEnable), checkPoint},
new String[]{
"java.lang.String",
00223
"java.lang.String",
"boolean",
"java.lang.String"});
00224 }
00225
00227 public void addVirtualDatabases(String xml)
throws JmxException,
00228 MBeanException
00229 {
00230
invoke(
"addVirtualDatabases",
new Object[]{xml},
00231
new String[]{
"java.lang.String"});
00232 }
00233
00237 public ArrayList
listVirtualDatabases() throws
JmxException, MBeanException
00238 {
00239
return (ArrayList)
invoke(
"listVirtualDatabases", null, null);
00240 }
00241
00245 public ArrayList
listBackends(String virtualDbName)
throws JmxException,
00246 MBeanException
00247 {
00248
return (ArrayList)
invoke(
"listBackends",
new Object[]{virtualDbName},
00249
new String[]{
"java.lang.String"});
00250 }
00251
00255 public String
loadXML(String filename)
throws IOException,
JmxException,
00256 MBeanException
00257 {
00258 FileInputStream fis =
new FileInputStream(filename);
00259 DataInputStream dis =
new DataInputStream(fis);
00260 StringBuffer sb =
new StringBuffer();
00261 String concat =
"";
00262
while ((concat = DataInputStream.readUTF(dis)) != null)
00263 sb.append(concat);
00264
return (String)
invoke(
"loadXML",
new Object[]{sb.toString()},
00265
new String[]{
"java.lang.String"});
00266 }
00267
00272 public String loadXmlConfiguration(String filename, String virtualName,
00273
boolean autoEnable, String checkPoint)
throws Exception
00274 {
00275 FileInputStream fis =
new FileInputStream(filename);
00276 DataInputStream dis =
new DataInputStream(fis);
00277 StringBuffer sb =
new StringBuffer();
00278 String concat =
"";
00279
while ((concat = DataInputStream.readUTF(dis)) != null)
00280 sb.append(concat);
00281
return (String) invoke(
"loadXML",
new Object[]{sb.toString(), virtualName,
00282
new Boolean(autoEnable), checkPoint},
new String[]{
"java.lang.String",
00283
"java.lang.String",
"boolean",
"java.lang.String"});
00284 }
00285
00289 public String removeVirtualDatabase(String virtualname)
throws Exception
00290 {
00291
return (String) invoke(
"removeVirtualDatabase",
new Object[]{virtualname},
00292
new String[]{
"java.lang.String"});
00293 }
00294
00298 public String saveConfiguration() throws Exception
00299 {
00300
return (String) server.invoke(mbean,
"saveConfiguration", null, null);
00301 }
00302
00306 public void shutdown(
int level)
throws Exception
00307 {
00308 invoke(
"shutdown",
new Object[]{
new Integer(level)},
new String[]{
"int"});
00309 }
00310
00314 public void shutdownDatabase(String databaseName,
int level)
00315
throws JmxException, MBeanException
00316 {
00317 invoke(
"shutdownDatabase",
new Object[]{databaseName,
new Integer(level)},
00318
new String[]{
"java.lang.String",
"int"});
00319 }
00320
00324 public String generateReport() throws JmxException, MBeanException
00325 {
00326
return (String) invoke(
"generateReport", null, null);
00327 }
00328
00332 public String generateLogReport() throws JmxException, MBeanException
00333 {
00334
return (String) invoke(
"generateLogReport", null, null);
00335 }
00336
00340 public ArrayList listDatabaseClients(String virtualDbName)
00341
throws JmxException, MBeanException
00342 {
00343
return (ArrayList) invoke(
"listDatabaseClients",
00344
new Object[]{virtualDbName},
new String[]{
"java.lang.String"});
00345 }
00346
00350 public boolean isCacheEnableForDatabase(String virtualDbName)
00351
throws JmxException, MBeanException
00352 {
00353
return ((Boolean) invoke(
"isCacheEnableForDatabase",
00354
new Object[]{virtualDbName},
new String[]{
"java.lang.String"}))
00355 .booleanValue();
00356 }
00357
00361 public File[] listAvailableDumpFiles() throws JmxException, MBeanException
00362 {
00363
return (File[]) invoke(
"listAvailableDumpFiles", null, null);
00364 }
00365
00369 public boolean removeDumpFile(File dumpFile)
throws JmxException,
00370 MBeanException
00371 {
00372
return ((Boolean) invoke(
"removeDumpFile",
new Object[]{dumpFile},
00373
new String[]{
"java.io.File"})).booleanValue();
00374 }
00375
00379 public String viewLogConfigurationFile() throws JmxException, MBeanException
00380 {
00381
return (String) invoke(
"viewLogConfigurationFile", null, null);
00382 }
00383
00387 public void updateLogConfigurationFile(String newConfiguration)
00388
throws JmxException, MBeanException
00389 {
00390 invoke(
"updateLogConfigurationFile",
new Object[]{newConfiguration},
00391
new String[]{
"java.lang.String"});
00392
00393 }
00394
00395 }