| 1 25 26 package org.objectweb.jonas.webapp.jonasadmin; 27 28 import java.io.File ; 29 import java.util.ArrayList ; 30 import java.util.Collections ; 31 import java.util.Iterator ; 32 import java.util.List ; 33 import java.util.Set ; 34 35 import javax.management.MalformedObjectNameException ; 36 import javax.management.ObjectName ; 37 import javax.naming.Context ; 38 import javax.naming.NameClassPair ; 39 import javax.naming.NamingEnumeration ; 40 import javax.naming.NamingException ; 41 import javax.servlet.http.HttpServletRequest ; 42 43 import org.apache.struts.util.MessageResources; 44 import org.objectweb.jonas.jmx.J2eeObjectName; 45 import org.objectweb.jonas.jmx.JonasManagementRepr; 46 import org.objectweb.jonas.jmx.JonasObjectName; 47 import org.objectweb.jonas.jmx.ManagementException; 48 import org.objectweb.jonas.mail.MailServiceImpl; 49 import org.objectweb.jonas.jmx.CatalinaObjectName; 50 import org.objectweb.jonas.webapp.jonasadmin.common.BeanComparator; 51 import org.objectweb.jonas.webapp.jonasadmin.logging.LoggerItem; 52 import org.objectweb.jonas.webapp.jonasadmin.mbean.MbeanItem; 53 import org.objectweb.jonas.webapp.jonasadmin.service.container.WebAppItem; 54 55 60 public class JonasAdminJmx { 61 62 64 private static Object s_Synchro = new Object (); 65 66 68 71 protected JonasAdminJmx() { 72 } 73 74 76 85 public static String replace(String template, String placeholder, String value) { 86 if (template == null) { 87 return (null); 88 } 89 if ((placeholder == null) || (value == null)) { 90 return (template); 91 } 92 String sRet = new String (template); 93 while (true) { 94 int index = sRet.indexOf(placeholder); 95 if (index < 0) { 96 break; 97 } 98 StringBuffer temp = new StringBuffer (sRet.substring(0, index)); 99 temp.append(value); 100 temp.append(sRet.substring(index + placeholder.length())); 101 sRet = temp.toString(); 102 } 103 return (sRet); 104 105 } 106 107 112 public static ObjectName getJ2eeDomainObjectName() { 113 ObjectName onDomains = J2eeObjectName.J2EEDomains(); 115 return getFirstMbean(onDomains); 116 } 117 118 126 public static ObjectName getJ2eeServerObjectName(String p_DomainName) { 127 ObjectName pattern_server_on = J2eeObjectName.J2EEServers(p_DomainName); 129 ObjectName server_on = null; 130 Iterator it = JonasManagementRepr.queryNames(pattern_server_on).iterator(); 131 if (it.hasNext()) { 132 server_on = (ObjectName ) it.next(); 134 } 135 return server_on; 136 } 137 138 147 public static ObjectName getJ2eeServerObjectName() { 148 ObjectName pattern_server_on = J2eeObjectName.J2EEServers(); 150 ObjectName server_on = null; 151 Iterator it = JonasManagementRepr.queryNames(pattern_server_on).iterator(); 152 if (it.hasNext()) { 153 server_on = (ObjectName ) it.next(); 155 } 156 return server_on; 157 } 158 159 166 public static ArrayList getListRemoteJonasServerItem(HttpServletRequest p_Request) 167 throws Exception { 168 synchronized (s_Synchro) { 169 ArrayList oServers = new ArrayList (); 170 String sCurrentRMIConnectorName = JonasManagementRepr.getCurrentRMIConnectorName(); 172 173 Set setRmiConnector = JonasManagementRepr.getRMIConnectorsNames(); 175 if (setRmiConnector.size() > 0) { 176 Iterator oIt = setRmiConnector.iterator(); 177 while (oIt.hasNext()) { 178 JonasServerItem oItem = new JonasServerItem(); 179 oItem.setNameRmiConnector(oIt.next().toString()); 180 JonasManagementRepr.setCurrentRMIConnectorName(oItem.getNameRmiConnector()); 182 ObjectName server_on = getJ2eeServerObjectName(); 184 if (server_on != null) { 185 String serverName = server_on.getKeyProperty("name"); 186 oItem.setNameServer(serverName); 187 oServers.add(oItem); 188 } 189 } 190 try { 191 JonasManagementRepr.setCurrentRMIConnectorName(sCurrentRMIConnectorName); 193 } 194 catch (Exception e) { 195 if (oServers.size() > 0) { 197 JonasServerItem oItem = (JonasServerItem) oServers.get(0); 198 JonasManagementRepr.setCurrentRMIConnectorName(oItem.getNameRmiConnector()); 199 } 200 } 201 } 202 return oServers; 203 } 204 } 205 206 213 public static boolean hasMBeanName(ObjectName p_On) 214 throws ManagementException { 215 synchronized (s_Synchro) { 216 ArrayList al = new ArrayList (); 217 Iterator itNames = JonasManagementRepr.queryNames(p_On).iterator(); 218 if (itNames.hasNext()) { 219 return true; 220 } 221 return false; 222 } 223 } 224 225 232 public static String getFirstMBeanName(ObjectName p_On) 233 throws ManagementException { 234 synchronized (s_Synchro) { 235 ArrayList al = new ArrayList (); 236 Iterator itNames = JonasManagementRepr.queryNames(p_On).iterator(); 237 if (itNames.hasNext()) { 238 return itNames.next().toString(); 239 } 240 return null; 241 } 242 } 243 244 251 public static List getListMBeanName(ObjectName p_On) 252 throws ManagementException { 253 synchronized (s_Synchro) { 254 ArrayList al = new ArrayList (); 255 Iterator itNames = JonasManagementRepr.queryNames(p_On).iterator(); 256 while (itNames.hasNext()) { 257 ObjectName item = (ObjectName ) itNames.next(); 258 al.add(item.toString()); 259 } 260 Collections.sort(al); 261 return al; 262 } 263 } 264 265 273 public static ObjectName getFirstMbean(ObjectName p_On) 274 throws ManagementException { 275 synchronized (s_Synchro) { 276 ArrayList al = new ArrayList (); 277 Iterator itNames = JonasManagementRepr.queryNames(p_On).iterator(); 278 if (itNames.hasNext()) { 279 return (ObjectName ) itNames.next(); 280 } 281 return null; 282 } 283 } 284 285 293 public static List getListMbean(ObjectName p_On) 294 throws ManagementException { 295 synchronized (s_Synchro) { 296 ArrayList al = new ArrayList (); 297 Iterator itNames = JonasManagementRepr.queryNames(p_On).iterator(); 298 while (itNames.hasNext()) { 299 al.add(itNames.next()); 300 } 301 return al; 302 } 303 } 304 305 313 public static String extractValueMbeanName(String pName, String pMBeanName) { 314 String sValue = null; 315 try { 316 String sSearch = pName.trim() + "="; 317 int iPos = pMBeanName.indexOf(sSearch); 318 if (iPos > -1) { 319 sValue = pMBeanName.substring(iPos + sSearch.length()); 320 iPos = sValue.indexOf(","); 321 if (iPos > -1) { 322 sValue = sValue.substring(0, iPos); 323 } 324 } 325 } catch (NullPointerException e) { 326 sValue = null; 328 } 329 return sValue; 330 } 331 332 338 public static String extractFilename(String p_Path) { 339 String sFilename = null; 340 try { 341 int iPosSeparator = p_Path.lastIndexOf("/"); 342 if (iPosSeparator < 0) { 343 iPosSeparator = p_Path.lastIndexOf("\\"); 344 if (iPosSeparator < 0) { 345 sFilename = new String (p_Path); 346 } 347 else { 348 sFilename = p_Path.substring(iPosSeparator + 1); 349 } 350 } 351 else { 352 sFilename = p_Path.substring(iPosSeparator + 1); 353 } 354 if (sFilename.length() > 0) { 355 int iPos_1 = p_Path.indexOf(DIR_AUTOLOAD + "/" + sFilename); 356 int iPos_2 = p_Path.indexOf(DIR_AUTOLOAD + "\\" + sFilename); 357 if (iPos_1 > -1) { 358 sFilename = DIR_AUTOLOAD + "/" + sFilename; 359 } 360 else if (iPos_2 > -1) { 361 sFilename = DIR_AUTOLOAD + "\\" + sFilename; 362 } 363 } 364 else { 365 sFilename = null; 366 } 367 } 368 catch (NullPointerException e) { 369 sFilename = null; 371 } 372 return sFilename; 373 } 374 375 378 private static String DIR_RARS = "rars"; 379 private static String DIR_AUTOLOAD = "autoload"; 380 private static String DIR_CONF = "conf"; 381 382 private static ArrayList getFilenames(String p_Directory, String p_Extension) { 383 ArrayList al = new ArrayList (); 384 String sExt = "." + p_Extension.toLowerCase(); 385 String sFile; 386 387 File oFile = new File (p_Directory); 388 String [] asFiles = oFile.list(); 389 int iPos; 390 if (asFiles != null) { 391 for (int i = 0; i < asFiles.length; i++) { 392 oFile = new File (p_Directory, asFiles[i]); 393 if (oFile.isFile() == true) { 394 sFile = oFile.getName().toLowerCase(); 395 iPos = sFile.lastIndexOf(sExt); 396 if (iPos > -1) { 397 if (iPos == (sFile.length() - sExt.length())) { 398 al.add(oFile.getName()); 399 } 400 } 401 } 402 } 403 } 404 Collections.sort(al); 405 return al; 406 } 407 408 private static ArrayList getDirectories(String p_Directory) { 409 ArrayList al = new ArrayList (); 410 String sFile; 411 412 File oFile = new File (p_Directory); 413 String [] asFiles = oFile.list(); 414 415 if (asFiles != null) { 416 for (int i = 0; i < asFiles.length; i++) { 417 oFile = new File (p_Directory, asFiles[i]); 418 if (oFile.isDirectory() == true) { 419 al.add(oFile.getName()); 420 } 421 } 422 } 423 Collections.sort(al); 424 return al; 425 } 426 427 private static void appendDirectory(ArrayList p_List, String p_Dir) { 428 String sDir = p_Dir + "/"; 429 for (int i = 0; i < p_List.size(); i++) { 430 p_List.set(i, sDir + p_List.get(i)); 431 } 432 } 433 434 440 public static ArrayList getJarFilesDeployable() 441 throws ManagementException { 442 ObjectName on = JonasObjectName.ejbService(); 443 ArrayList al = (ArrayList ) JonasManagementRepr.getAttribute(on, "DeployableJars"); 444 String sDir = (String ) JonasManagementRepr.getAttribute(on, "EjbjarsDirectory"); 445 446 String sEarDir = null; 447 try { 448 on = JonasObjectName.earService(); 449 sEarDir = (String ) JonasManagementRepr.getAttribute(on, "AppsDirectory"); 450 } 451 catch (Exception e) { 452 } 454 return prepareContainersToDisplay(al, sDir, sEarDir); 455 } 456 457 463 public static ArrayList getRarFilesDeployable() 464 throws ManagementException { 465 ObjectName on = JonasObjectName.resourceService(); 466 ArrayList al = (ArrayList ) JonasManagementRepr.getAttribute(on, "DeployableRars"); 467 String sDir = (String ) JonasManagementRepr.getAttribute(on, "RarsDirectory"); 468 469 String sEarDir = null; 470 try { 471 on = JonasObjectName.earService(); 472 sEarDir = (String ) JonasManagementRepr.getAttribute(on, "AppsDirectory"); 473 } 474 catch (Exception e) { 475 } 477 return prepareContainersToDisplay(al, sDir, sEarDir); 478 } 479 480 486 public static ArrayList getEarFilesDeployable() 487 throws ManagementException { 488 ObjectName on = JonasObjectName.earService(); 489 ArrayList al = (ArrayList ) JonasManagementRepr.getAttribute(on, "DeployableEars"); 490 String sDir = (String ) JonasManagementRepr.getAttribute(on, "AppsDirectory"); 491 return prepareContainersToDisplay(al, sDir, null); 492 } 493 494 500 public static ArrayList getWarFilesDeployable() 501 throws ManagementException { 502 ObjectName on = JonasObjectName.webContainerService(); 503 ArrayList al = (ArrayList ) JonasManagementRepr.getAttribute(on, "DeployableWars"); 504 String sDir = (String ) JonasManagementRepr.getAttribute(on, "WebappsDirectory"); 505 506 String sEarDir = null; 507 try { 508 on = JonasObjectName.earService(); 509 sEarDir = (String ) JonasManagementRepr.getAttribute(on, "AppsDirectory"); 510 } 511 catch (Exception e) { 512 } 514 return prepareContainersToDisplay(al, sDir, sEarDir); 515 } 516 517 523 public static ArrayList getJarFilesDeployed() 524 throws ManagementException { 525 ObjectName on = JonasObjectName.ejbService(); 526 ArrayList al = (ArrayList ) JonasManagementRepr.getAttribute(on, "DeployedJars"); 527 String sDir = (String ) JonasManagementRepr.getAttribute(on, "EjbjarsDirectory"); 528 529 String sEarDir = null; 530 try { 531 on = JonasObjectName.earService(); 532 sEarDir = (String ) JonasManagementRepr.getAttribute(on, "AppsDirectory"); 533 } 534 catch (Exception e) { 535 } 537 return prepareContainersToDisplay(al, sDir, sEarDir); 538 } 539 540 546 public static ArrayList getEarFilesDeployed() 547 throws ManagementException { 548 ObjectName on = JonasObjectName.earService(); 549 ArrayList al = (ArrayList ) JonasManagementRepr.getAttribute(on, "DeployedEars"); 550 String sDir = (String ) JonasManagementRepr.getAttribute(on, "AppsDirectory"); 551 return prepareContainersToDisplay(al, sDir, null); 552 } 553 554 560 public static ArrayList getRarFilesDeployed() 561 throws ManagementException { 562 ObjectName on = JonasObjectName.resourceService(); 563 ArrayList al = (ArrayList ) JonasManagementRepr.getAttribute(on, "DeployedRars"); 564 String sDir = (String ) JonasManagementRepr.getAttribute(on, "RarsDirectory"); 565 566 String sEarDir = null; 567 try { 568 on = JonasObjectName.earService(); 569 sEarDir = (String ) JonasManagementRepr.getAttribute(on, "AppsDirectory"); 570 } 571 catch (ManagementException e) { 572 } 574 return prepareContainersToDisplay(al, sDir, sEarDir); 575 } 576 577 583 public static ArrayList getWarFilesDeployed() 584 throws ManagementException { 585 ObjectName on = JonasObjectName.webContainerService(); 586 ArrayList al = (ArrayList ) JonasManagementRepr.getAttribute(on, "DeployedWars"); 587 String sDir = (String ) JonasManagementRepr.getAttribute(on, "WebappsDirectory"); 588 589 String sEarDir = null; 590 try { 591 on = JonasObjectName.earService(); 592 sEarDir = (String ) JonasManagementRepr.getAttribute(on, "AppsDirectory"); 593 } 594 catch (ManagementException e) { 595 } 597 return prepareContainersToDisplay(al, sDir, sEarDir); 598 } 599 600 608 public static ArrayList prepareContainersToDisplay(ArrayList p_Containers 609 , String p_ContainerDir, String p_EarDir) { 610 int iPos; 611 String sPath; 612 boolean bAdd; 613 ArrayList al = new ArrayList (); 614 for (int i = 0; i < p_Containers.size(); i++) { 615 sPath = p_Containers.get(i).toString(); 617 iPos = sPath.indexOf(p_ContainerDir); 618 if (iPos > -1) { 619 sPath = sPath.substring(p_ContainerDir.length()); 620 } 621 if (sPath.endsWith("/") == true) { 623 sPath = sPath.substring(0, sPath.length() - 1); 624 } 625 bAdd = true; 627 if (p_EarDir != null) { 629 iPos = sPath.indexOf(p_EarDir); 630 if (iPos > -1) { 631 bAdd = false; 632 } 633 634 } 635 if (bAdd == true) { 636 al.add(sPath); 637 } 638 } 639 Collections.sort(al); 641 return al; 642 } 643 644 private static ArrayList getFilesDeployed(ObjectName on) 645 throws ManagementException { 646 ArrayList alDeployed = new ArrayList (); 647 String sPath; 648 String sFile; 649 Iterator itNames = JonasAdminJmx.getListMBeanName(on).iterator(); 650 while (itNames.hasNext()) { 651 sPath = JonasAdminJmx.extractValueMbeanName("fname", itNames.next().toString()); 652 sFile = JonasAdminJmx.extractFilename(sPath); 653 if (sFile != null) { 654 alDeployed.add(sFile); 655 } 656 } 657 Collections.sort(alDeployed); 658 return alDeployed; 659 660 } 661 662 668 public static ArrayList getMailFilesDeployable() 669 throws ManagementException { 670 ObjectName on = JonasObjectName.mailService(); 671 return (ArrayList ) JonasManagementRepr.getAttribute(on, "MailFactoryPropertiesFiles"); 672 } 673 674 680 public static ArrayList getMimePartMailFilesDeployable() 681 throws ManagementException { 682 ObjectName on = JonasObjectName.mailService(); 683 return (ArrayList ) JonasManagementRepr.getAttribute(on 684 , "MimePartMailFactoryPropertiesFiles"); 685 } 686 687 693 public static ArrayList getSessionMailFilesDeployable() 694 throws ManagementException { 695 ObjectName on = JonasObjectName.mailService(); 696 return (ArrayList  |