1 25 26 package org.objectweb.jonas.webapp.jonasadmin; 27 28 import java.io.UnsupportedEncodingException ; 29 30 import javax.management.ObjectName ; 31 import javax.servlet.http.HttpServletRequest ; 32 import javax.servlet.http.HttpServletResponse ; 33 34 import org.apache.struts.action.ActionServlet; 35 import org.objectweb.jonas.common.JProp; 36 import org.objectweb.jonas.jmx.CatalinaObjectName; 37 import org.objectweb.jonas.jmx.JonasManagementRepr; 38 import org.objectweb.jonas.jmx.JonasObjectName; 39 import org.objectweb.jonas.jmx.ManagementException; 40 import org.objectweb.jonas.webapp.taglib.TreeControl; 41 import org.objectweb.jonas.webapp.taglib.TreeControlNode; 42 43 46 public class WhereAreYou { 47 48 52 public final static String EXCEPTION_MBEANSERVER_NOTFOUND = "error.mbeanserver.notfound"; 53 public final static String EXCEPTION_JONASSERVER_NOTFOUND = "error.jonasserver.notfound"; 54 public final static String EXCEPTION_MBEAN_J2EEDOMAIN_NOTFOUND = "error.mbean.j2eedomain.notfound"; 55 public final static String EXCEPTION_MBEAN_J2EESERVER_NOTFOUND = "error.mbean.j2eeserver.notfound"; 56 57 60 public final static String SESSION_NAME = "WhereAreYou"; 61 62 public final static String TAGS_IMAGES_ROOT_KEY = "tagsimagesroot"; 64 public final static String CONTEXT_MACRO = "${CONTEXT}"; 65 public final static String TREE_PAGE_REFRESH = "viewTree.do?select="; 66 67 70 public final static String NODE_SEPARATOR = "*"; 71 72 public final static int DEPLOYMENT_EAR = 1; 74 public final static int DEPLOYMENT_JAR = 2; 75 public final static int DEPLOYMENT_WAR = 3; 76 public final static int DEPLOYMENT_RAR = 4; 77 public final static int DEPLOYMENT_DATASOURCE = 5; 78 public final static int DEPLOYMENT_JMS = 6; 79 public final static int DEPLOYMENT_MAIL = 7; 80 81 public final static int DOMAIN_DEPLOYMENT_EAR = 8; 83 public final static int DOMAIN_DEPLOYMENT_JAR = 9; 84 public final static int DOMAIN_DEPLOYMENT_WAR = 10; 85 public final static int DOMAIN_DEPLOYMENT_RAR = 11; 86 87 public final static String DEPLOYMENT_STRING_EAR = "ear"; 89 public final static String DEPLOYMENT_STRING_JAR = "jar"; 90 public final static String DEPLOYMENT_STRING_WAR = "war"; 91 public final static String DEPLOYMENT_STRING_RAR = "rar"; 92 public final static String DEPLOYMENT_STRING_DATASOURCE = "datasource"; 93 public final static String DEPLOYMENT_STRING_JMS = "jms"; 94 public final static String DEPLOYMENT_STRING_MAILFACTORY = "mail"; 95 96 public final static String DOMAIN_DEPLOYMENT_STRING_EAR = "domain-ear"; 98 public final static String DOMAIN_DEPLOYMENT_STRING_JAR = "domain-jar"; 99 public final static String DOMAIN_DEPLOYMENT_STRING_WAR = "domain-war"; 100 public final static String DOMAIN_DEPLOYMENT_STRING_RAR = "domain-rar"; 101 102 104 private String imagesRoot = null; 105 private TreeControl treeControl = null; 106 private boolean treeToRefresh = false; 107 108 private int applicationServerPort = 0; 109 private String applicationContextPath = null; 110 111 private boolean catalinaServer = false; 112 private String currentCatalinaDomainName = null; 113 private String currentCatalinaServiceName = null; 114 private String currentCatalinaEngineName = null; 115 private String currentCatalinaDefaultHostName = null; 116 private boolean jettyServer = false; 117 118 private ObjectName currentDomain = null; 121 private ObjectName currentJonasServer = null; 122 private String currentDomainName = null; 123 private String currentJonasServerName = null; 124 125 private String adminJonasServerName = null; 127 private boolean theCurrentJonasServer = false; 128 131 private int currentJonasDeploymentType = 0; 132 133 134 136 public WhereAreYou() { 137 try { 138 JProp oJProp = JProp.getInstance(); 140 adminJonasServerName = oJProp.getValue(JProp.JONAS_NAME); 141 } catch (Exception e) { 142 throw new JonasAdminException(EXCEPTION_JONASSERVER_NOTFOUND, e.getMessage(), e); 144 } 145 } 146 147 149 156 public void initialize(ActionServlet p_Servlet, HttpServletRequest p_Request) 157 throws JonasAdminException { 158 String sImagesRoot = (String ) p_Servlet.getServletConfig().getInitParameter( 160 TAGS_IMAGES_ROOT_KEY); 161 if (sImagesRoot != null) { 162 sImagesRoot = JonasAdminJmx.replace(sImagesRoot, CONTEXT_MACRO 163 , p_Request.getContextPath()); 164 } 165 this.imagesRoot = sImagesRoot; 166 167 refreshServers(p_Request); 169 } 170 171 176 public String getCurrentJonasServerName() { 177 return currentJonasServerName; 178 } 179 180 185 public ObjectName getCurrentJonasServer() { 186 return currentJonasServer; 187 } 188 189 194 public ObjectName getCurrentDomain() { 195 return currentDomain; 196 } 197 198 203 public String getCurrentDomainName() { 204 return currentDomainName; 205 } 206 207 212 public String getSelectedNameNode() { 213 if (treeControl != null) { 214 TreeControlNode oNode = treeControl.getSelected(); 215 if (oNode != null) { 216 return oNode.getName(); 217 } 218 } 219 return null; 220 } 221 222 229 public String getUrlToRefreshSelectedNode(HttpServletRequest p_Request 230 , HttpServletResponse p_Response) { 231 if (treeControl != null) { 232 TreeControlNode oNode = treeControl.getSelected(); 233 if (oNode != null) { 234 treeToRefresh = false; 236 String encodedName = null; 238 try { 239 encodedName = java.net.URLEncoder.encode(oNode.getName(), "UTF-8"); 240 } catch (UnsupportedEncodingException e) { 241 encodedName = oNode.getName(); 243 } 244 StringBuffer sb = new StringBuffer (); 245 sb.append(p_Request.getContextPath()); 246 sb.append("/"); 247 sb.append(TREE_PAGE_REFRESH); 248 sb.append(encodedName); 249 sb.append("&s="); 251 sb.append(Math.random()); 252 253 return p_Response.encodeURL(sb.toString()); 254 } 255 } 256 return null; 257 } 258 259 265 public void selectNameNode(String p_Name, boolean p_Expand) { 266 if (treeControl != null) { 267 if (p_Name.equals(getSelectedNameNode()) == false) { 269 treeControl.selectNode(p_Name); 271 if (p_Expand == true) { 272 treeControl.expandSelectedParents(); 273 } 274 treeToRefresh = true; 275 } 276 if ((getSelectedTreeControlNode() == null) && (p_Name != null) && (p_Name.length() > 0)) { 278 String s = p_Name.substring(0, p_Name.lastIndexOf(WhereAreYou.NODE_SEPARATOR)); 280 if ((s != null) && (s.length() > 0)) { 281 selectNameNode(s, p_Expand); 282 } 283 } 284 } 285 } 286 287 292 public TreeControlNode getSelectedTreeControlNode() { 293 if (treeControl != null) { 294 return treeControl.getSelected(); 295 } 296 return null; 297 } 298 299 304 public boolean isTreeToRefresh() { 305 return treeToRefresh; 306 } 307 308 313 public void setTreeToRefresh(boolean treeToRefresh) { 314 this.treeToRefresh = treeToRefresh; 315 } 316 317 323 public void refreshServers(HttpServletRequest p_Request) 324 throws JonasAdminException { 325 applicationServerPort = p_Request.getServerPort(); 326 applicationContextPath = p_Request.getContextPath(); 327 refreshCatalina(p_Request); 328 refreshJetty(p_Request); 329 refreshJonas(p_Request); 330 } 331 332 337 public void refreshCatalina(HttpServletRequest p_Request) { 338 resetCatalina(); 339 try { 340 String sServer = getServletServerName().toLowerCase(); 341 catalinaServer = (sServer.indexOf("tomcat") > -1); 342 if (catalinaServer) { 343 ObjectName onServer = JonasAdminJmx.getFirstMbean(CatalinaObjectName.catalinaServer()); 344 ObjectName [] aonServices = (ObjectName []) JonasManagementRepr.getAttribute(onServer 345 , "serviceNames"); 346 currentCatalinaServiceName = aonServices[0].getKeyProperty("serviceName"); 348 currentCatalinaDomainName = aonServices[0].getDomain(); 350 currentCatalinaEngineName = currentCatalinaDomainName; 352 ObjectName onEngine = CatalinaObjectName.catalinaEngine(currentCatalinaDomainName); 353 currentCatalinaDefaultHostName = (String ) JonasManagementRepr.getAttribute(onEngine 355 , "defaultHost"); 356 } 357 } catch (Exception e) { 358 } 360 } 361 362 367 public void refreshJetty(HttpServletRequest p_Request) { 368 resetJetty(); 369 try { 370 String sServer = getServletServerName().toLowerCase(); 371 jettyServer = (sServer.indexOf("jetty") > -1); 372 if (jettyServer == true) { 373 } 375 } 376 catch (Exception e) { 377 } 379 } 380 381 387 public void refreshJonas(HttpServletRequest p_Request) 388 throws JonasAdminException { 389 resetJonas(); 390 currentDomain = JonasAdminJmx.getJ2eeDomainObjectName(); 392 if (currentDomain != null) { 393 currentDomainName = currentDomain.getKeyProperty("name"); 394 } else { 395 throw new JonasAdminException(EXCEPTION_MBEAN_J2EEDOMAIN_NOTFOUND); 397 } 398 currentJonasServer = JonasAdminJmx.getJ2eeServerObjectName(currentDomainName); 400 if (currentJonasServer != null) { 401 currentJonasServerName = currentJonasServer.getKeyProperty("name"); 402 } else { 403 throw new JonasAdminException(EXCEPTION_MBEAN_J2EESERVER_NOTFOUND); 405 } 406 try { 407 theCurrentJonasServer = adminJonasServerName.equals(currentJonasServerName); 409 } catch (Exception e) { 410 throw new JonasAdminException(EXCEPTION_JONASSERVER_NOTFOUND, e.getMessage(), e); 412 } 413 } 414 415 421 public String toString() { 422 StringBuffer sb = new StringBuffer (); 423 sb.append("imagesRoot=[").append(imagesRoot).append("] "); 424 sb.append("treeControl=[").append(treeControl).append("] "); 425 sb.append("catalinaServer=[").append(catalinaServer).append("] "); 426 return sb.toString(); 427 } 428 429 431 435 protected String getServletServerName() throws ManagementException { 436 String servletServerName = (String ) JonasManagementRepr.getAttribute(JonasObjectName.webContainerService() 437 , "ServerName"); 438 return servletServerName; 439 } 440 441 444 protected void resetCatalina() { 445 catalinaServer = false; 446 currentCatalinaServiceName = null; 447 } 448 449 452 protected void resetJetty() { 453 jettyServer = false; 454 } 455 456 459 protected void resetJonas() { 460 theCurrentJonasServer = false; 461 currentDomain = null; 462 currentDomainName = null; 463 currentJonasServer = null; 464 currentJonasServerName = null; 465 } 466 467 469 public String getImagesRoot() { 470 return imagesRoot; 471 } 472 473 public TreeControl getTreeControl() { 474 return treeControl; 475 } 476 477 public void setTreeControl(TreeControl treeControl) { 478 this.treeControl = treeControl; 479 } 480 481 public boolean isCatalinaServer() { 482 return catalinaServer; 483 } 484 485 public String getCurrentCatalinaServiceName() { 486 return currentCatalinaServiceName; 487 } 488 489 public boolean isCurrentJonasServer() { 490 return theCurrentJonasServer; 491 } 492 493 public int getCurrentJonasDeploymentType() { 494 return currentJonasDeploymentType; 495 } 496 497 public void setCurrentJonasDeploymentType(int currentJonasDeploymentType) { 498 this.currentJonasDeploymentType = currentJonasDeploymentType; 499 } 500 501 public int getApplicationServerPort() { 502 return applicationServerPort; 503 } 504 505 public String getApplicationContextPath() { 506 return applicationContextPath; 507 } 508 509 public String getCurrentCatalinaDefaultHostName() { 510 return currentCatalinaDefaultHostName; 511 } 512 513 public String getCurrentCatalinaEngineName() { 514 return currentCatalinaEngineName; 515 } 516 517 public String getCurrentCatalinaDomainName() { 518 return currentCatalinaDomainName; 519 } 520 521 public boolean isJettyServer() { 522 return jettyServer; 523 } 524 527 public String getAdminJonasServerName() { 528 return adminJonasServerName; 529 } 530 } 531 | Popular Tags |