1 23 package org.objectweb.joram.client.jms.admin; 24 25 import java.io.File ; 26 import java.io.FileOutputStream ; 27 import java.io.IOException ; 28 import java.net.ConnectException ; 29 import java.net.UnknownHostException ; 30 import java.util.List ; 31 import java.util.Properties ; 32 import java.util.Vector ; 33 import java.util.Iterator ; 34 35 import javax.jms.Destination ; 36 37 import fr.dyade.aaa.util.management.MXWrapper; 38 import org.objectweb.joram.client.jms.admin.server.*; 39 import org.objectweb.joram.client.jms.Queue; 40 import org.objectweb.joram.client.jms.Topic; 41 import org.objectweb.joram.shared.JoramTracing; 42 import org.objectweb.util.monolog.api.BasicLevel; 43 44 47 public class JoramAdmin 48 implements JoramAdminMBean { 49 50 public long timeOut = 1000; 51 public PlatformAdmin platformAdmin; 52 53 static boolean isHa = false; 54 55 56 59 private String adminFileExportXML = null; 60 61 62 public JoramAdmin() 63 throws UnknownHostException , ConnectException , AdminException { 64 platformAdmin = new PlatformAdmin(); 65 registerMBean(); 66 } 67 68 public JoramAdmin(String hostName, 69 int port, 70 String name, 71 String password, 72 int cnxTimer, 73 String reliableClass) 74 throws UnknownHostException , ConnectException , AdminException { 75 platformAdmin = new PlatformAdmin(hostName,port,name,password,cnxTimer,reliableClass); 76 registerMBean(); 77 } 78 79 public JoramAdmin(String hostName, 80 int port, 81 String name, 82 String password, 83 int cnxTimer) 84 throws UnknownHostException , ConnectException , AdminException { 85 platformAdmin = new PlatformAdmin(hostName,port,name,password,cnxTimer); 86 registerMBean(); 87 } 88 89 public JoramAdmin(String name, 90 String password) 91 throws ConnectException , AdminException { 92 platformAdmin = new PlatformAdmin(name,password); 93 registerMBean(); 94 } 95 96 public JoramAdmin(javax.jms.TopicConnectionFactory cnxFact, 97 String name, 98 String password) 99 throws ConnectException , AdminException { 100 platformAdmin = new PlatformAdmin(cnxFact,name,password); 101 registerMBean(); 102 } 103 104 private void registerMBean() { 105 try { 106 MXWrapper.registerMBean(this, 107 "joramClient", 108 "type=JoramAdmin"); 109 } catch (Exception e) { 110 if (JoramTracing.dbgClient.isLoggable(BasicLevel.DEBUG)) 111 JoramTracing.dbgClient.log(BasicLevel.DEBUG, 112 "registerMBean",e); 113 } 114 } 115 116 private void unregisterMBean() { 117 try { 118 MXWrapper.unregisterMBean("joramClient", 119 "type=JoramAdmin"); 120 } catch (Exception e) { 121 if (JoramTracing.dbgClient.isLoggable(BasicLevel.DEBUG)) 122 JoramTracing.dbgClient.log(BasicLevel.DEBUG, 123 "unregisterMBean",e); 124 } 125 } 126 127 public PlatformAdmin getPlatformAdmin() { 128 return platformAdmin; 129 } 130 131 public void exit() { 132 platformAdmin.exit(); 133 unregisterMBean(); 134 } 135 136 139 public void setTimeOutToAbortRequest(long timeOut) { 140 this.timeOut = timeOut; 141 } 142 143 146 public long getTimeOutToAbortRequest() { 147 return timeOut; 148 } 149 150 162 public void setDefaultDMQ(int serverId, DeadMQueue dmq) 163 throws ConnectException , AdminException { 164 AdminModule.setDefaultDMQ(serverId,dmq); 165 } 166 167 176 public DeadMQueue getDefaultDMQ(int serverId) 177 throws ConnectException , AdminException { 178 return AdminModule.getDefaultDMQ(serverId); 179 } 180 181 188 public DeadMQueue getDefaultDMQ() 189 throws ConnectException , AdminException { 190 return AdminModule.getDefaultDMQ(); 191 } 192 193 197 public List getDestinations(int serverId) { 198 Vector destinations = new Vector (); 199 try { 200 List destList = AdminModule.getDestinations(serverId,timeOut); 201 Iterator destIt = destList.iterator(); 202 while (destIt.hasNext()) { 203 org.objectweb.joram.client.jms.Destination dest = 204 (org.objectweb.joram.client.jms.Destination) destIt.next(); 205 destinations.add(new String ("type=" + dest.getType() + 206 ", name=" + dest.getAdminName() + 207 ", id=" + dest.getName())); 208 } 209 } catch (Exception exc) {} 210 return destinations; 211 } 212 213 217 public List getDestinations() { 218 Vector destinations = new Vector (); 219 220 List list = platformAdmin.getServersIds(); 221 if (list != null) { 222 Iterator it = list.iterator(); 223 while (it.hasNext()) { 224 try { 225 Integer sid = (Integer ) it.next(); 226 List destList = AdminModule.getDestinations(sid.intValue(),timeOut); 227 Iterator destIt = destList.iterator(); 228 while (destIt.hasNext()) { 229 org.objectweb.joram.client.jms.Destination dest = 230 (org.objectweb.joram.client.jms.Destination) destIt.next(); 231 destinations.add(new String ("type=" + dest.getType() + 232 ", name=" + dest.getAdminName() + 233 ", id=" + dest.getName())); 234 } 235 } catch (Exception exc) {} 236 } 237 } 238 return destinations; 239 } 240 241 245 public List getUsers(int serverId) { 246 Vector users = new Vector (); 247 try { 248 List userList = AdminModule.getUsers(serverId,timeOut); 249 Iterator userIt = userList.iterator(); 250 while (userIt.hasNext()) { 251 User dest = (User) userIt.next(); 252 users.add(dest.toString()); 253 } 254 } catch (Exception exc) {} 255 return users; 256 } 257 258 262 public List getUsers() { 263 Vector users = new Vector (); 264 265 List list = platformAdmin.getServersIds(); 266 if (list != null) { 267 Iterator it = list.iterator(); 268 while (it.hasNext()) { 269 try { 270 Integer sid = (Integer ) it.next(); 271 List userList = AdminModule.getUsers(sid.intValue(),timeOut); 272 Iterator userIt = userList.iterator(); 273 while (userIt.hasNext()) { 274 User dest = (User) userIt.next(); 275 users.add(dest.toString()); 276 } 277 } catch (Exception exc) {} 278 } 279 } 280 return users; 281 } 282 283 288 public void createUser(String name, String password) 289 throws AdminException { 290 try { 291 User.create(name,password); 292 } catch (ConnectException exc) { 293 throw new AdminException("createUser() failed: admin connection " 294 + "has been lost."); 295 } 296 } 297 298 303 public void createUser(String name, String password, int serverId) 304 throws AdminException { 305 try { 306 User.create(name,password,serverId); 307 } catch (ConnectException exc) { 308 throw new AdminException("createUser() failed: admin connection " 309 + "has been lost."); 310 } 311 } 312 313 321 public Destination createQueue(String name) 322 throws AdminException { 323 try { 324 return createQueue(platformAdmin.getLocalServerId(), 325 name, 326 "org.objectweb.joram.mom.dest.Queue", 327 null); 328 } catch (ConnectException exc) { 329 throw new AdminException("createQueue() failed: admin connection " 330 + "has been lost."); 331 } 332 } 333 334 343 public Destination createQueue(int serverId, String name) 344 throws AdminException { 345 return createQueue(serverId, 346 name, 347 "org.objectweb.joram.mom.dest.Queue", 348 null); 349 } 350 351 362 public Destination createQueue(int serverId, 363 String name, 364 String className, 365 Properties prop) 366 throws AdminException { 367 try { 368 Queue queue = Queue.create(serverId, 369 name, 370 className, 371 prop); 372 return queue; 373 } catch (ConnectException exc) { 374 throw new AdminException("createQueue() failed: admin connection " 375 + "has been lost."); 376 } 377 } 378 379 380 386 public Destination createTopic(String name) 387 throws AdminException { 388 try { 389 return createTopic(platformAdmin.getLocalServerId(), 390 name, 391 "org.objectweb.joram.mom.dest.Topic", 392 null); 393 } catch (ConnectException exc) { 394 throw new AdminException("createTopic() failed: admin connection " 395 + "has been lost."); 396 } 397 } 398 399 408 public Destination createTopic(int serverId, String name) 409 throws AdminException { 410 return createTopic(serverId, 411 name, 412 "org.objectweb.joram.mom.dest.Topic", 413 null); 414 } 415 416 427 public Destination createTopic(int serverId, 428 String name, 429 String className, 430 Properties prop) 431 throws AdminException { 432 try { 433 Topic topic = Topic.create(serverId, 434 name, 435 className, 436 prop); 437 return topic; 438 } catch (ConnectException exc) { 439 throw new AdminException("createTopic() failed: admin connection " 440 + "has been lost."); 441 } 442 } 443 444 public static boolean executeXMLAdmin(String cfgDir, 445 String cfgFileName) 446 throws Exception { 447 return AdminModule.executeXMLAdmin(cfgDir, cfgFileName); 448 } 449 450 public static boolean executeXMLAdmin(String path) 451 throws Exception { 452 return AdminModule.executeXMLAdmin(path); 453 } 454 455 460 public boolean executeXMLAdminJMX(String path) 461 throws Exception { 462 throw new Exception ("Not implemented yet"); 463 464 } 465 466 467 474 public void exportRepositoryToFile(String exportDir) throws AdminException { 475 476 if (JoramTracing.dbgClient.isLoggable(BasicLevel.DEBUG)) { 477 JoramTracing.dbgClient.log(BasicLevel.DEBUG, "export repository to " + exportDir.toString()); 478 } 479 480 StringBuffer strbuf = new StringBuffer (); 481 int indent = 0; 482 strbuf.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); 483 strbuf.append("<!--\n"); 484 strbuf.append(" Exported JMS objects : \n"); 485 strbuf.append(" - destinations : Topic/Queue \n"); 486 strbuf.append(" The file can be reloaded through the admin interface (joramAdmin.executeXMLAdmin())\n"); 487 strbuf.append("-->\n"); 488 strbuf.append("<JoramAdmin>\n"); 489 indent += 2; 490 491 List srvList = platformAdmin.getServersIds(); 493 if (srvList != null) { 494 495 Iterator it = srvList.iterator(); 497 while (it.hasNext()) { 498 try { 499 Integer sid = (Integer ) it.next(); 500 501 List destList = AdminModule.getDestinations(sid.intValue(), timeOut); 503 Iterator destIt = destList.iterator(); 504 while (destIt.hasNext()) { 505 org.objectweb.joram.client.jms.Destination dest = (org.objectweb.joram.client.jms.Destination) destIt 506 .next(); 507 508 strbuf.append(dest.toXml(indent, sid.intValue())); 509 } 510 511 } catch (Exception exc) { 512 throw new AdminException("exportRepositoryToFile() failed - " + exc); 513 } 514 } 515 516 if (JoramTracing.dbgClient.isLoggable(BasicLevel.DEBUG)) { 517 JoramTracing.dbgClient.log(BasicLevel.DEBUG, "exported objects : \n" + strbuf.toString()); 518 } 519 } 520 521 indent -= 2; 522 strbuf.append("</JoramAdmin>"); 523 524 File exportFile = null; 526 FileOutputStream fos = null; 527 528 try { 529 exportFile = new File (exportDir, getAdminFileExportXML()); 530 fos = new FileOutputStream (exportFile); 531 fos.write(strbuf.toString().getBytes()); 532 } catch(Exception ioe) { 533 throw new AdminException("exportRepositoryToFile() failed - " + ioe); 534 } finally { 535 try { 536 exportFile = null; 537 fos.close(); 538 } catch (Exception e) { 539 if (JoramTracing.dbgClient.isLoggable(BasicLevel.DEBUG)) { 540 JoramTracing.dbgClient.log(BasicLevel.DEBUG, "Unable to close the file : " + fos); 541 } 542 } 543 if (JoramTracing.dbgClient.isLoggable(BasicLevel.DEBUG)) { 544 JoramTracing.dbgClient.log(BasicLevel.DEBUG, "File : " + exportDir + "/" + getAdminFileExportXML() + " created"); 545 } 546 } 547 } 548 549 550 public String getAdminFileExportXML() { 551 return adminFileExportXML; 552 } 553 554 public void setAdminFileExportXML(String adminFileExportXML) { 555 this.adminFileExportXML = adminFileExportXML; 556 } 557 558 559 public static boolean isHa() { 560 return isHa; 561 } 562 563 564 public static void setHa(boolean isHa) { 565 JoramAdmin.isHa = isHa; 566 AdminModule.setHa(isHa); 567 } 568 } 569 | Popular Tags |