WhereAreYou.java

00001 
00026 package org.objectweb.jonas.webapp.jonasadmin;
00027 
00028 import javax.management.ObjectName;
00029 import javax.servlet.http.HttpServletRequest;
00030 import javax.servlet.http.HttpServletResponse;
00031 
00032 import org.apache.struts.action.ActionServlet;
00033 import org.objectweb.jonas.common.JProp;
00034 import org.objectweb.jonas.jmx.CatalinaObjectName;
00035 import org.objectweb.jonas.jmx.JonasManagementRepr;
00036 import org.objectweb.jonas.jmx.JonasObjectName;
00037 import org.objectweb.jonas.jmx.ManagementException;
00038 import org.objectweb.jonas.webapp.taglib.TreeControl;
00039 import org.objectweb.jonas.webapp.taglib.TreeControlNode;
00040 
00044 public class WhereAreYou {
00045 
00046 // --------------------------------------------------------- Public Constants
00050     public final static String EXCEPTION_MBEANSERVER_NOTFOUND = "error.mbeanserver.notfound";
00051     public final static String EXCEPTION_JONASSERVER_NOTFOUND = "error.jonasserver.notfound";
00052     public final static String EXCEPTION_MBEAN_J2EEDOMAIN_NOTFOUND = "error.mbean.j2eedomain.notfound";
00053     public final static String EXCEPTION_MBEAN_J2EESERVER_NOTFOUND = "error.mbean.j2eeserver.notfound";
00054 
00058     public final static String SESSION_NAME = "WhereAreYou";
00059 
00060     // Constant environnement
00061     public final static String TAGS_IMAGES_ROOT_KEY = "tagsimagesroot";
00062     public final static String CONTEXT_MACRO = "${CONTEXT}";
00063     public final static String TREE_PAGE_REFRESH = "viewTree.do?select=";
00064 
00068     public final static String NODE_SEPARATOR = "*";
00069 
00070     // Deployment identification number
00071     public final static int DEPLOYMENT_EAR = 1;
00072     public final static int DEPLOYMENT_JAR = 2;
00073     public final static int DEPLOYMENT_WAR = 3;
00074     public final static int DEPLOYMENT_RAR = 4;
00075     public final static int DEPLOYMENT_DATASOURCE = 5;
00076     public final static int DEPLOYMENT_JMS = 6;
00077     public final static int DEPLOYMENT_MAIL = 7;
00078 
00079     // Deployment identification string
00080     public final static String DEPLOYMENT_STRING_EAR = "ear";
00081     public final static String DEPLOYMENT_STRING_JAR = "jar";
00082     public final static String DEPLOYMENT_STRING_WAR = "war";
00083     public final static String DEPLOYMENT_STRING_RAR = "rar";
00084     public final static String DEPLOYMENT_STRING_DATASOURCE = "datasource";
00085     public final static String DEPLOYMENT_STRING_JMS = "jms";
00086     public final static String DEPLOYMENT_STRING_MAILFACTORY = "mail";
00087 
00088 // --------------------------------------------------------- Properties variable
00089 
00090     private String imagesRoot = null;
00091     private TreeControl treeControl = null;
00092     private boolean treeToRefresh = false;
00093 
00094     private String applicationJonasServerName = null;
00095     private int applicationServerPort = 0;
00096     private String applicationContextPath = null;
00097 
00098     private boolean currentApplicationJonasServerName = false;
00099 
00100     private boolean catalinaServer = false;
00101     private String currentCatalinaDomainName = null;
00102     private String currentCatalinaServiceName = null;
00103     private String currentCatalinaEngineName = null;
00104     private String currentCatalinaDefaultHostName = null;
00105     private boolean jettyServer = false;
00106 
00107     // J2EE Management information (used in construction of JSR77 ObjectNames)
00108     private ObjectName currentDomain = null;
00109     private ObjectName currentJonasServer = null;
00110     private String currentDomainName = null;
00111     private String currentJonasServerName = null;
00112 
00116     private int currentJonasDeploymentType = 0;
00117 
00118 // --------------------------------------------------------- Constructor
00119 
00120     public WhereAreYou() {
00121     }
00122 
00123 // --------------------------------------------------------- Public Methods
00124 
00132     public void initialize(ActionServlet p_Servlet, HttpServletRequest p_Request)
00133         throws JonasAdminException {
00134         // Tags Images Root directory
00135         String sImagesRoot = (String) p_Servlet.getServletConfig().getInitParameter(
00136             TAGS_IMAGES_ROOT_KEY);
00137         if (sImagesRoot != null) {
00138             sImagesRoot = JonasAdminJmx.replace(sImagesRoot, CONTEXT_MACRO
00139                 , p_Request.getContextPath());
00140         }
00141         this.imagesRoot = sImagesRoot;
00142         // Initialize Servers properties
00143         refreshServers(p_Request);
00144 
00145     }
00146 
00152     public String getCurrentJonasServerName() {
00153         return currentJonasServerName;
00154     }
00155 
00161     public ObjectName getCurrentJonasServer() {
00162         return currentJonasServer;
00163     }
00164 
00170     public ObjectName getCurrentDomain() {
00171         return currentDomain;
00172     }
00173 
00179     public String getCurrentDomainName() {
00180         return currentDomainName;
00181     }
00182 
00188     public String getSelectedNameNode() {
00189         if (treeControl != null) {
00190             TreeControlNode oNode = treeControl.getSelected();
00191             if (oNode != null) {
00192                 return oNode.getName();
00193             }
00194         }
00195         return null;
00196     }
00197 
00205     public String getUrlToRefreshSelectedNode(HttpServletRequest p_Request
00206         , HttpServletResponse p_Response) {
00207         if (treeControl != null) {
00208             TreeControlNode oNode = treeControl.getSelected();
00209             if (oNode != null) {
00210                 // Disable refresh
00211                 treeToRefresh = false;
00212                 // Build URL refresh
00213                 StringBuffer sb = new StringBuffer();
00214                 sb.append(p_Request.getContextPath());
00215                 sb.append("/");
00216                 sb.append(TREE_PAGE_REFRESH);
00217                 sb.append(oNode.getName());
00218                 // Force the browser to refresh the tree (Netscape and Mozilla)
00219                 sb.append("&s=");
00220                 sb.append(Math.random());
00221 
00222                 return p_Response.encodeURL(sb.toString());
00223             }
00224         }
00225         return null;
00226     }
00227 
00234     public void selectNameNode(String p_Name, boolean p_Expand) {
00235         if (treeControl != null) {
00236             // Changing node ?
00237             if (p_Name.equals(getSelectedNameNode()) == false) {
00238                 // Change node
00239                 treeControl.selectNode(p_Name);
00240                 if (p_Expand == true) {
00241                     treeControl.expandSelectedParents();
00242                 }
00243                 treeToRefresh = true;
00244             }
00245             // Query node not found
00246             if ((getSelectedTreeControlNode() == null) && (p_Name != null) && (p_Name.length() > 0)) {
00247                 // Try to select the parent node
00248                 String s = p_Name.substring(0, p_Name.lastIndexOf(WhereAreYou.NODE_SEPARATOR));
00249                 if ((s != null) && (s.length() > 0)) {
00250                     selectNameNode(s, p_Expand);
00251                 }
00252             }
00253         }
00254     }
00255 
00261     public TreeControlNode getSelectedTreeControlNode() {
00262         if (treeControl != null) {
00263             return treeControl.getSelected();
00264         }
00265         return null;
00266     }
00267 
00273     public boolean isTreeToRefresh() {
00274         return treeToRefresh;
00275     }
00276 
00282     public void setTreeToRefresh(boolean treeToRefresh) {
00283         this.treeToRefresh = treeToRefresh;
00284     }
00285 
00292     public void refreshServers(HttpServletRequest p_Request)
00293         throws JonasAdminException {
00294         applicationServerPort = p_Request.getServerPort();
00295         applicationContextPath = p_Request.getContextPath();
00296         refreshCatalina(p_Request);
00297         refreshJetty(p_Request);
00298         refreshJonas(p_Request);
00299     }
00300 
00306     public void refreshCatalina(HttpServletRequest p_Request) {
00307         resetCatalina();
00308         try {
00309             String sServer = getServletServerName().toLowerCase();
00310             catalinaServer = (sServer.indexOf("tomcat") > -1);
00311             if (catalinaServer) {
00312                 ObjectName onServer = JonasAdminJmx.getFirstMbean(CatalinaObjectName.catalinaServer());
00313                 ObjectName[] aonServices = (ObjectName[]) JonasManagementRepr.getAttribute(onServer
00314                 , "serviceNames");
00315                 // Service : the first !
00316                 currentCatalinaServiceName = aonServices[0].getKeyProperty("serviceName");
00317                 // Domain
00318                 currentCatalinaDomainName = aonServices[0].getDomain();
00319                 // Engine
00320                 currentCatalinaEngineName = currentCatalinaDomainName;
00321                 ObjectName onEngine = CatalinaObjectName.catalinaEngine(currentCatalinaDomainName);
00322                 // Default Host
00323                 currentCatalinaDefaultHostName = (String) JonasManagementRepr.getAttribute(onEngine
00324                 , "defaultHost");
00325             }
00326         } catch (Exception e) {
00327             // none
00328         }
00329     }
00330 
00336     public void refreshJetty(HttpServletRequest p_Request) {
00337         resetJetty();
00338         try {
00339             String sServer = getServletServerName().toLowerCase();
00340             jettyServer = (sServer.indexOf("jetty") > -1);
00341             if (jettyServer == true) {
00342                 // none action
00343             }
00344         }
00345         catch (Exception e) {
00346             // none
00347         }
00348     }
00349 
00356     public void refreshJonas(HttpServletRequest p_Request)
00357         throws JonasAdminException {
00358         resetJonas();
00359         // Set J2EEDomain information
00360         currentDomain = JonasAdminJmx.getJ2eeDomainObjectName();
00361         if (currentDomain != null) {
00362             currentDomainName = currentDomain.getKeyProperty("name");
00363         }
00364         else {
00365             // Can't run jonasAdmin
00366             throw new JonasAdminException(EXCEPTION_MBEAN_J2EEDOMAIN_NOTFOUND);
00367         }
00368         // Set J2EEServer information
00369         currentJonasServer = JonasAdminJmx.getJ2eeServerObjectName(currentDomainName);
00370         if (currentJonasServer != null) {
00371             currentJonasServerName = currentJonasServer.getKeyProperty("name");
00372         }
00373         else {
00374             // Can't run jonasAdmin
00375             throw new JonasAdminException(EXCEPTION_MBEAN_J2EESERVER_NOTFOUND);
00376         }
00377         try {
00378             // Get the name of the Jonas Server who is running the application
00379             JProp oJProp = JProp.getInstance();
00380             applicationJonasServerName = oJProp.getValue(JProp.JONAS_NAME);
00381             // Detect if the current managed JOnAS server is the one where the application is running
00382             currentApplicationJonasServerName = applicationJonasServerName.equals(
00383                 currentJonasServerName);
00384         }
00385         catch (Exception e) {
00386             // Current Jonas not found, can't run jonasAdmin
00387             throw new JonasAdminException(EXCEPTION_JONASSERVER_NOTFOUND, e.getMessage(), e);
00388         }
00389     }
00390 
00397     public String toString() {
00398         StringBuffer sb = new StringBuffer();
00399         sb.append("imagesRoot=[").append(imagesRoot).append("] ");
00400         sb.append("treeControl=[").append(treeControl).append("] ");
00401         sb.append("catalinaServer=[").append(catalinaServer).append("] ");
00402         return sb.toString();
00403     }
00404 
00405 // --------------------------------------------------------- Protected Methods
00406 
00411     protected String getServletServerName() throws ManagementException {
00412         String servletServerName = (String) JonasManagementRepr.getAttribute(JonasObjectName.webContainerService()
00413             , "ServerName");
00414         return servletServerName;
00415     }
00416 
00420     protected void resetCatalina() {
00421         catalinaServer = false;
00422         currentCatalinaServiceName = null;
00423     }
00424 
00428     protected void resetJetty() {
00429         jettyServer = false;
00430     }
00431 
00435     protected void resetJonas() {
00436         applicationJonasServerName = null;
00437         currentApplicationJonasServerName = false;
00438         currentDomain = null;
00439         currentDomainName = null;
00440         currentJonasServer = null;
00441         currentJonasServerName = null;
00442     }
00443 
00444 // --------------------------------------------------------- Properties Methods
00445 
00446     public String getImagesRoot() {
00447         return imagesRoot;
00448     }
00449 
00450     public TreeControl getTreeControl() {
00451         return treeControl;
00452     }
00453 
00454     public void setTreeControl(TreeControl treeControl) {
00455         this.treeControl = treeControl;
00456     }
00457 
00458     public boolean isCatalinaServer() {
00459         return catalinaServer;
00460     }
00461 
00462     public String getCurrentCatalinaServiceName() {
00463         return currentCatalinaServiceName;
00464     }
00465 
00466     public boolean isCurrentApplicationJonasServerName() {
00467         return currentApplicationJonasServerName;
00468     }
00469 
00470     public int getCurrentJonasDeploymentType() {
00471         return currentJonasDeploymentType;
00472     }
00473 
00474     public void setCurrentJonasDeploymentType(int currentJonasDeploymentType) {
00475         this.currentJonasDeploymentType = currentJonasDeploymentType;
00476     }
00477 
00478     public int getApplicationServerPort() {
00479         return applicationServerPort;
00480     }
00481 
00482     public String getApplicationJonasServerName() {
00483         return applicationJonasServerName;
00484     }
00485 
00486     public String getApplicationContextPath() {
00487         return applicationContextPath;
00488     }
00489 
00490     public String getCurrentCatalinaDefaultHostName() {
00491         return currentCatalinaDefaultHostName;
00492     }
00493 
00494     public String getCurrentCatalinaEngineName() {
00495         return currentCatalinaEngineName;
00496     }
00497 
00498     public String getCurrentCatalinaDomainName() {
00499         return currentCatalinaDomainName;
00500     }
00501 
00502     public boolean isJettyServer() {
00503         return jettyServer;
00504     }
00505 }

Generated on Tue Feb 15 15:05:34 2005 for JOnAS by  doxygen 1.3.9.1