001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: WhereAreYou.java 9680 2006-10-06 12:08:33Z danesa $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas.webapp.jonasadmin;
025:
026: import java.io.UnsupportedEncodingException;
027:
028: import javax.management.ObjectName;
029: import javax.servlet.http.HttpServletRequest;
030: import javax.servlet.http.HttpServletResponse;
031:
032: import org.apache.struts.action.ActionServlet;
033: import org.objectweb.jonas.common.JProp;
034: import org.objectweb.jonas.jmx.CatalinaObjectName;
035: import org.objectweb.jonas.jmx.J2eeObjectName;
036: import org.objectweb.jonas.jmx.JonasManagementRepr;
037: import org.objectweb.jonas.jmx.JonasObjectName;
038: import org.objectweb.jonas.jmx.ManagementException;
039: import org.objectweb.jonas.webapp.taglib.TreeControl;
040: import org.objectweb.jonas.webapp.taglib.TreeControlNode;
041:
042: /**
043: * @author Michel-Ange ANTON
044: */
045: public class WhereAreYou {
046:
047: // --------------------------------------------------------- Public Constants
048: /**
049: * Exceptions
050: */
051: public final static String EXCEPTION_MBEANSERVER_NOTFOUND = "error.mbeanserver.notfound";
052: public final static String EXCEPTION_JONASSERVER_NOTFOUND = "error.jonasserver.notfound";
053: public final static String EXCEPTION_MBEAN_J2EEDOMAIN_NOTFOUND = "error.mbean.j2eedomain.notfound";
054: public final static String EXCEPTION_MBEAN_J2EESERVER_NOTFOUND = "error.mbean.j2eeserver.notfound";
055:
056: /**
057: * Name of the variable in http session
058: */
059: public final static String SESSION_NAME = "WhereAreYou";
060:
061: // Constant environnement
062: public final static String TAGS_IMAGES_ROOT_KEY = "tagsimagesroot";
063: public final static String CONTEXT_MACRO = "${CONTEXT}";
064: public final static String TREE_PAGE_REFRESH = "viewTree.do?select=";
065:
066: /**
067: * Used separator in the ident node in the tree
068: */
069: public final static String NODE_SEPARATOR = "*";
070:
071: // Deployment identification number
072: public final static int DEPLOYMENT_EAR = 1;
073: public final static int DEPLOYMENT_JAR = 2;
074: public final static int DEPLOYMENT_WAR = 3;
075: public final static int DEPLOYMENT_RAR = 4;
076: public final static int DEPLOYMENT_DATASOURCE = 5;
077: public final static int DEPLOYMENT_JMS = 6;
078: public final static int DEPLOYMENT_MAIL = 7;
079:
080: // Deployment identification number for domain management
081: public final static int DOMAIN_DEPLOYMENT_EAR = 8;
082: public final static int DOMAIN_DEPLOYMENT_JAR = 9;
083: public final static int DOMAIN_DEPLOYMENT_WAR = 10;
084: public final static int DOMAIN_DEPLOYMENT_RAR = 11;
085:
086: // Deployment identification string
087: public final static String DEPLOYMENT_STRING_EAR = "ear";
088: public final static String DEPLOYMENT_STRING_JAR = "jar";
089: public final static String DEPLOYMENT_STRING_WAR = "war";
090: public final static String DEPLOYMENT_STRING_RAR = "rar";
091: public final static String DEPLOYMENT_STRING_DATASOURCE = "datasource";
092: public final static String DEPLOYMENT_STRING_JMS = "jms";
093: public final static String DEPLOYMENT_STRING_MAILFACTORY = "mail";
094:
095: // Deployment identification strings for domain management
096: public final static String DOMAIN_DEPLOYMENT_STRING_EAR = "domain-ear";
097: public final static String DOMAIN_DEPLOYMENT_STRING_JAR = "domain-jar";
098: public final static String DOMAIN_DEPLOYMENT_STRING_WAR = "domain-war";
099: public final static String DOMAIN_DEPLOYMENT_STRING_RAR = "domain-rar";
100:
101: // --------------------------------------------------------- Properties variable
102:
103: private String imagesRoot = null;
104: private TreeControl treeControl = null;
105: private boolean treeToRefresh = false;
106:
107: private int applicationServerPort = 0;
108: private String applicationContextPath = null;
109:
110: private boolean catalinaServer = false;
111: private String currentCatalinaDomainName = null;
112: private String currentCatalinaServiceName = null;
113: private String currentCatalinaEngineName = null;
114: private String currentCatalinaDefaultHostName = null;
115: private boolean jettyServer = false;
116:
117: // J2EE Management information (used in construction of JSR77 ObjectNames)
118: // These names correspond to the server being currently managed
119: private ObjectName currentDomain = null;
120: private ObjectName currentJonasServer = null;
121: private String currentDomainName = null;
122: private String currentJonasServerName = null;
123:
124: // The server hosting JonasAdmin application
125: private String adminJonasServerName = null;
126: // The hosting server's configuration directory
127: private String adminJonasServerConfDir = null;
128: private boolean theCurrentJonasServer = false;
129: /**
130: * Type of deployment in progress : JAR, EAR, WAR, RAR, DATASOURCE.
131: */
132: private int currentJonasDeploymentType = 0;
133:
134: // --------------------------------------------------------- Constructor
135:
136: public WhereAreYou() {
137: try {
138: // Get the name of the Jonas Server who is running the application
139: JProp oJProp = JProp.getInstance();
140: adminJonasServerName = oJProp.getValue(JProp.JONAS_NAME);
141: adminJonasServerConfDir = oJProp.getConfDir();
142: currentDomainName = oJProp.getValue(JProp.DOMAIN_NAME);
143: currentDomain = J2eeObjectName
144: .J2EEDomain(currentDomainName);
145: currentJonasServerName = adminJonasServerName;
146: currentJonasServer = J2eeObjectName.J2EEServer(
147: currentDomainName, currentJonasServerName);
148: } catch (Exception e) {
149: // Current Jonas not found, can't run jonasAdmin
150: throw new JonasAdminException(
151: EXCEPTION_JONASSERVER_NOTFOUND, e.getMessage(), e);
152: }
153: }
154:
155: // --------------------------------------------------------- Public Methods
156:
157: /**
158: * Initialize the properties in only reading.
159: *
160: * @param p_Servlet The instance of servlet to access to the parameter of the servlet config
161: * @param p_Request The request to access to the HTTP informations
162: * @throws JonasAdminException
163: */
164: public void initialize(ActionServlet p_Servlet,
165: HttpServletRequest p_Request) throws JonasAdminException {
166: // Tags Images Root directory
167: String sImagesRoot = (String) p_Servlet.getServletConfig()
168: .getInitParameter(TAGS_IMAGES_ROOT_KEY);
169: if (sImagesRoot != null) {
170: sImagesRoot = JonasAdminJmx.replace(sImagesRoot,
171: CONTEXT_MACRO, p_Request.getContextPath());
172: }
173: this .imagesRoot = sImagesRoot;
174: refreshServers(p_Request, currentJonasServerName);
175: }
176:
177: /**
178: * Return the current Jonas server name.
179: *
180: * @return The name
181: */
182: public String getCurrentJonasServerName() {
183: return currentJonasServerName;
184: }
185:
186: /**
187: * Return the ObjectName of the MBean associated to the current Jonas server.
188: *
189: * @return The ObjectName
190: */
191: public ObjectName getCurrentJonasServer() {
192: return currentJonasServer;
193: }
194:
195: /**
196: * Return the current J2EE domain ObjectName.
197: *
198: * @return The ObjectName
199: */
200: public ObjectName getCurrentDomain() {
201: return currentDomain;
202: }
203:
204: /**
205: * Return the current J2EE domain name.
206: *
207: * @return The name
208: */
209: public String getCurrentDomainName() {
210: return currentDomainName;
211: }
212:
213: /**
214: * Return the name of the selected node in the tree.
215: *
216: * @return The name
217: */
218: public String getSelectedNameNode() {
219: if (treeControl != null) {
220: TreeControlNode oNode = treeControl.getSelected();
221: if (oNode != null) {
222: return oNode.getName();
223: }
224: }
225: return null;
226: }
227:
228: /**
229: * Return the URL to refresh the selected node in the tree.
230: *
231: * @param p_Request HTTP request
232: * @param p_Response HTTP response
233: * @return The URL
234: */
235: public String getUrlToRefreshSelectedNode(
236: HttpServletRequest p_Request, HttpServletResponse p_Response) {
237: if (treeControl != null) {
238: TreeControlNode oNode = treeControl.getSelected();
239: if (oNode != null) {
240: // Disable refresh
241: treeToRefresh = false;
242: // Build URL refresh
243: String encodedName = null;
244: try {
245: encodedName = java.net.URLEncoder.encode(oNode
246: .getName(), "UTF-8");
247: } catch (UnsupportedEncodingException e) {
248: // Node name can't be encoded
249: encodedName = oNode.getName();
250: }
251: StringBuffer sb = new StringBuffer();
252: sb.append(p_Request.getContextPath());
253: sb.append("/");
254: sb.append(TREE_PAGE_REFRESH);
255: sb.append(encodedName);
256: // Force the browser to refresh the tree (Netscape and Mozilla)
257: sb.append("&s=");
258: sb.append(Math.random());
259:
260: return p_Response.encodeURL(sb.toString());
261: }
262: }
263: return null;
264: }
265:
266: /**
267: * Select the node in the tree.
268: *
269: * @param p_Name The name of node to select
270: * @param p_Expand Expand parent branch
271: */
272: public void selectNameNode(String p_Name, boolean p_Expand) {
273: if (treeControl != null) {
274: // Changing node ?
275: if (p_Name.equals(getSelectedNameNode()) == false) {
276: // Change node
277: treeControl.selectNode(p_Name);
278: if (p_Expand == true) {
279: treeControl.expandSelectedParents();
280: }
281: treeToRefresh = true;
282: }
283: // Query node not found
284: if ((getSelectedTreeControlNode() == null)
285: && (p_Name != null) && (p_Name.length() > 0)) {
286: // Try to select the parent node
287: String s = p_Name.substring(0, p_Name
288: .lastIndexOf(WhereAreYou.NODE_SEPARATOR));
289: if ((s != null) && (s.length() > 0)) {
290: selectNameNode(s, p_Expand);
291: }
292: }
293: }
294: }
295:
296: /**
297: * Return the selected node in the tree.
298: *
299: * @return The selected node
300: */
301: public TreeControlNode getSelectedTreeControlNode() {
302: if (treeControl != null) {
303: return treeControl.getSelected();
304: }
305: return null;
306: }
307:
308: /**
309: * Return the state of current display of tree.
310: *
311: * @return True if the current tree is not correctly displaying
312: */
313: public boolean isTreeToRefresh() {
314: return treeToRefresh;
315: }
316:
317: /**
318: * Change the state of current display of tree.
319: *
320: * @param treeToRefresh True to refresh
321: */
322: public void setTreeToRefresh(boolean treeToRefresh) {
323: this .treeToRefresh = treeToRefresh;
324: }
325:
326: /**
327: * Refresh all the servers properties.
328: *
329: * @param p_Request The request to access to the HTTP informations
330: * @throws ManagementException if could not connect to the MBeanServer
331: * @throws JonasAdminException
332: */
333: public void refreshServers(HttpServletRequest p_Request,
334: String serverName) throws ManagementException,
335: JonasAdminException {
336: refreshJonas(p_Request, serverName);
337: applicationServerPort = p_Request.getServerPort();
338: applicationContextPath = p_Request.getContextPath();
339: refreshCatalina(p_Request);
340: refreshJetty(p_Request);
341: }
342:
343: public void refreshServers(HttpServletRequest p_Request) {
344: // Nothing to do, no server change
345: }
346:
347: /**
348: * Refresh the Catalina server properties.
349: *
350: * @param p_Request The request to access to the HTTP informations
351: */
352: public void refreshCatalina(HttpServletRequest p_Request) {
353: resetCatalina();
354: try {
355: String servletServerName = getServletServerName();
356: if (servletServerName == null) {
357: return;
358: }
359: String sServer = servletServerName.toLowerCase();
360: catalinaServer = (sServer.indexOf("tomcat") > -1);
361: if (catalinaServer) {
362: ObjectName onServer = JonasAdminJmx.getFirstMbean(
363: CatalinaObjectName.catalinaServer(),
364: currentJonasServerName);
365: ObjectName[] aonServices = (ObjectName[]) JonasManagementRepr
366: .getAttribute(onServer, "serviceNames",
367: currentJonasServerName);
368: // Service : the first !
369: currentCatalinaServiceName = aonServices[0]
370: .getKeyProperty("serviceName");
371: // Domain
372: currentCatalinaDomainName = aonServices[0].getDomain();
373: // Engine
374: currentCatalinaEngineName = currentCatalinaDomainName;
375: ObjectName onEngine = CatalinaObjectName
376: .catalinaEngine(currentCatalinaDomainName);
377: // Default Host
378: currentCatalinaDefaultHostName = (String) JonasManagementRepr
379: .getAttribute(onEngine, "defaultHost",
380: currentJonasServerName);
381: }
382: } catch (Exception e) {
383: // none
384: }
385: }
386:
387: /**
388: * Refresh the Jetty server properties.
389: *
390: * @param p_Request The request to access to the HTTP informations
391: */
392: public void refreshJetty(HttpServletRequest p_Request) {
393: resetJetty();
394: try {
395: String servletServerName = getServletServerName();
396: if (servletServerName == null) {
397: return;
398: }
399: String sServer = servletServerName.toLowerCase();
400: jettyServer = (sServer.indexOf("jetty") > -1);
401: if (jettyServer == true) {
402: // none action
403: }
404: } catch (Exception e) {
405: // none
406: }
407: }
408:
409: /**
410: * Refresh the managed JOnAS server properties.
411: *
412: * @param p_Request The request to access to the HTTP informations
413: * @throws ManagementException if could not connect to the MBeanServer
414: * @throws JonasAdminException
415: */
416: public void refreshJonas(HttpServletRequest p_Request,
417: String serverName) throws ManagementException,
418: JonasAdminException {
419: resetJonas();
420: // Set J2EEServer information
421: currentJonasServerName = serverName;
422: currentJonasServer = J2eeObjectName.J2EEServer(
423: currentDomainName, serverName);
424:
425: // Detect if the current managed JOnAS server is the one where the application is running
426: theCurrentJonasServer = adminJonasServerName
427: .equals(currentJonasServerName);
428:
429: }
430:
431: /**
432: * Return in a string the contents of the instance for each propertie.
433: * Debug utility.
434: *
435: * @return The string contents
436: */
437: public String toString() {
438: StringBuffer sb = new StringBuffer();
439: sb.append("imagesRoot=[").append(imagesRoot).append("] ");
440: sb.append("treeControl=[").append(treeControl).append("] ");
441: sb.append("catalinaServer=[").append(catalinaServer).append(
442: "] ");
443: return sb.toString();
444: }
445:
446: // --------------------------------------------------------- Protected Methods
447:
448: /**
449: * Return the name of the servlet server.
450: * @return The name of the servlet server.
451: */
452: protected String getServletServerName() throws ManagementException {
453: String servletServerName = null;
454: ObjectName on = JonasObjectName.webContainerService();
455: if (JonasManagementRepr
456: .isRegistered(on, currentJonasServerName)) {
457: servletServerName = (String) JonasManagementRepr
458: .getAttribute(on, "ServerName",
459: currentJonasServerName);
460: }
461: return servletServerName;
462: }
463:
464: /**
465: * Reset the Catalina server properties.
466: */
467: protected void resetCatalina() {
468: catalinaServer = false;
469: currentCatalinaServiceName = null;
470: }
471:
472: /**
473: * Reset the Jetty server properties.
474: */
475: protected void resetJetty() {
476: jettyServer = false;
477: }
478:
479: /**
480: * Reset the JOnAS server properties.
481: */
482: protected void resetJonas() {
483: theCurrentJonasServer = false;
484: currentJonasServer = null;
485: currentJonasServerName = null;
486: }
487:
488: /**
489: * Reset the domain properties.
490: */
491: protected void resetDomain() {
492: currentDomain = null;
493: currentDomainName = null;
494: }
495:
496: // --------------------------------------------------------- Properties Methods
497:
498: public String getImagesRoot() {
499: return imagesRoot;
500: }
501:
502: public TreeControl getTreeControl() {
503: return treeControl;
504: }
505:
506: public void setTreeControl(TreeControl treeControl) {
507: this .treeControl = treeControl;
508: }
509:
510: public boolean isCatalinaServer() {
511: return catalinaServer;
512: }
513:
514: public String getCurrentCatalinaServiceName() {
515: return currentCatalinaServiceName;
516: }
517:
518: public boolean isCurrentJonasServer() {
519: return theCurrentJonasServer;
520: }
521:
522: public int getCurrentJonasDeploymentType() {
523: return currentJonasDeploymentType;
524: }
525:
526: public void setCurrentJonasDeploymentType(
527: int currentJonasDeploymentType) {
528: this .currentJonasDeploymentType = currentJonasDeploymentType;
529: }
530:
531: public int getApplicationServerPort() {
532: return applicationServerPort;
533: }
534:
535: public String getApplicationContextPath() {
536: return applicationContextPath;
537: }
538:
539: public String getCurrentCatalinaDefaultHostName() {
540: return currentCatalinaDefaultHostName;
541: }
542:
543: public String getCurrentCatalinaEngineName() {
544: return currentCatalinaEngineName;
545: }
546:
547: public String getCurrentCatalinaDomainName() {
548: return currentCatalinaDomainName;
549: }
550:
551: public boolean isJettyServer() {
552: return jettyServer;
553: }
554:
555: /**
556: * @return Returns the adminJonasServerName.
557: */
558: public String getAdminJonasServerName() {
559: return adminJonasServerName;
560: }
561:
562: public String getAdminJonasServerConfDir() {
563: return adminJonasServerConfDir;
564: }
565: }
|