KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > webapp > jonasadmin > WhereAreYou


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: WhereAreYou.java,v 1.36 2005/07/25 21:03:55 pasmith Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.webapp.jonasadmin;
27
28 import java.io.UnsupportedEncodingException JavaDoc;
29
30 import javax.management.ObjectName JavaDoc;
31 import javax.servlet.http.HttpServletRequest JavaDoc;
32 import javax.servlet.http.HttpServletResponse JavaDoc;
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 /**
44  * @author Michel-Ange ANTON
45  */

46 public class WhereAreYou {
47
48 // --------------------------------------------------------- Public Constants
49
/**
50      * Exceptions
51      */

52     public final static String JavaDoc EXCEPTION_MBEANSERVER_NOTFOUND = "error.mbeanserver.notfound";
53     public final static String JavaDoc EXCEPTION_JONASSERVER_NOTFOUND = "error.jonasserver.notfound";
54     public final static String JavaDoc EXCEPTION_MBEAN_J2EEDOMAIN_NOTFOUND = "error.mbean.j2eedomain.notfound";
55     public final static String JavaDoc EXCEPTION_MBEAN_J2EESERVER_NOTFOUND = "error.mbean.j2eeserver.notfound";
56
57     /**
58      * Name of the variable in http session
59      */

60     public final static String JavaDoc SESSION_NAME = "WhereAreYou";
61
62     // Constant environnement
63
public final static String JavaDoc TAGS_IMAGES_ROOT_KEY = "tagsimagesroot";
64     public final static String JavaDoc CONTEXT_MACRO = "${CONTEXT}";
65     public final static String JavaDoc TREE_PAGE_REFRESH = "viewTree.do?select=";
66
67     /**
68      * Used separator in the ident node in the tree
69      */

70     public final static String JavaDoc NODE_SEPARATOR = "*";
71
72     // Deployment identification number
73
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     // Deployment identification number for domain management
82
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     // Deployment identification string
88
public final static String JavaDoc DEPLOYMENT_STRING_EAR = "ear";
89     public final static String JavaDoc DEPLOYMENT_STRING_JAR = "jar";
90     public final static String JavaDoc DEPLOYMENT_STRING_WAR = "war";
91     public final static String JavaDoc DEPLOYMENT_STRING_RAR = "rar";
92     public final static String JavaDoc DEPLOYMENT_STRING_DATASOURCE = "datasource";
93     public final static String JavaDoc DEPLOYMENT_STRING_JMS = "jms";
94     public final static String JavaDoc DEPLOYMENT_STRING_MAILFACTORY = "mail";
95     
96 // Deployment identification strings for domain management
97
public final static String JavaDoc DOMAIN_DEPLOYMENT_STRING_EAR = "domain-ear";
98     public final static String JavaDoc DOMAIN_DEPLOYMENT_STRING_JAR = "domain-jar";
99     public final static String JavaDoc DOMAIN_DEPLOYMENT_STRING_WAR = "domain-war";
100     public final static String JavaDoc DOMAIN_DEPLOYMENT_STRING_RAR = "domain-rar";
101
102 // --------------------------------------------------------- Properties variable
103

104     private String JavaDoc imagesRoot = null;
105     private TreeControl treeControl = null;
106     private boolean treeToRefresh = false;
107
108     private int applicationServerPort = 0;
109     private String JavaDoc applicationContextPath = null;
110
111     private boolean catalinaServer = false;
112     private String JavaDoc currentCatalinaDomainName = null;
113     private String JavaDoc currentCatalinaServiceName = null;
114     private String JavaDoc currentCatalinaEngineName = null;
115     private String JavaDoc currentCatalinaDefaultHostName = null;
116     private boolean jettyServer = false;
117
118     // J2EE Management information (used in construction of JSR77 ObjectNames)
119
// These names correspond to the server being currently managed
120
private ObjectName JavaDoc currentDomain = null;
121     private ObjectName JavaDoc currentJonasServer = null;
122     private String JavaDoc currentDomainName = null;
123     private String JavaDoc currentJonasServerName = null;
124
125     // The server hosting JonasAdmin application
126
private String JavaDoc adminJonasServerName = null;
127     private boolean theCurrentJonasServer = false;
128     /**
129      * Type of deployment in progress : JAR, EAR, WAR, RAR, DATASOURCE.
130      */

131     private int currentJonasDeploymentType = 0;
132
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         } catch (Exception JavaDoc e) {
142             // Current Jonas not found, can't run jonasAdmin
143
throw new JonasAdminException(EXCEPTION_JONASSERVER_NOTFOUND, e.getMessage(), e);
144         }
145     }
146
147 // --------------------------------------------------------- Public Methods
148

149     /**
150      * Initialize the properties in only reading.
151      *
152      * @param p_Servlet The instance of servlet to access to the parameter of the servlet config
153      * @param p_Request The request to access to the HTTP informations
154      * @throws JonasAdminException
155      */

156     public void initialize(ActionServlet p_Servlet, HttpServletRequest JavaDoc p_Request)
157         throws JonasAdminException {
158         // Tags Images Root directory
159
String JavaDoc sImagesRoot = (String JavaDoc) 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         // Initialize Servers properties
168
refreshServers(p_Request);
169     }
170
171     /**
172      * Return the current Jonas server name.
173      *
174      * @return The name
175      */

176     public String JavaDoc getCurrentJonasServerName() {
177         return currentJonasServerName;
178     }
179
180     /**
181      * Return the ObjectName of the MBean associated to the current Jonas server.
182      *
183      * @return The ObjectName
184      */

185     public ObjectName JavaDoc getCurrentJonasServer() {
186         return currentJonasServer;
187     }
188
189     /**
190      * Return the current J2EE domain ObjectName.
191      *
192      * @return The ObjectName
193      */

194     public ObjectName JavaDoc getCurrentDomain() {
195         return currentDomain;
196     }
197
198     /**
199      * Return the current J2EE domain name.
200      *
201      * @return The name
202      */

203     public String JavaDoc getCurrentDomainName() {
204         return currentDomainName;
205     }
206
207     /**
208      * Return the name of the selected node in the tree.
209      *
210      * @return The name
211      */

212     public String JavaDoc 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     /**
223      * Return the URL to refresh the selected node in the tree.
224      *
225      * @param p_Request HTTP request
226      * @param p_Response HTTP response
227      * @return The URL
228      */

229     public String JavaDoc getUrlToRefreshSelectedNode(HttpServletRequest JavaDoc p_Request
230         , HttpServletResponse JavaDoc p_Response) {
231         if (treeControl != null) {
232             TreeControlNode oNode = treeControl.getSelected();
233             if (oNode != null) {
234                 // Disable refresh
235
treeToRefresh = false;
236                 // Build URL refresh
237
String JavaDoc encodedName = null;
238                 try {
239                     encodedName = java.net.URLEncoder.encode(oNode.getName(), "UTF-8");
240                 } catch (UnsupportedEncodingException JavaDoc e) {
241                     // Node name can't be encoded
242
encodedName = oNode.getName();
243                 }
244                 StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
245                 sb.append(p_Request.getContextPath());
246                 sb.append("/");
247                 sb.append(TREE_PAGE_REFRESH);
248                 sb.append(encodedName);
249                 // Force the browser to refresh the tree (Netscape and Mozilla)
250
sb.append("&s=");
251                 sb.append(Math.random());
252
253                 return p_Response.encodeURL(sb.toString());
254             }
255         }
256         return null;
257     }
258
259     /**
260      * Select the node in the tree.
261      *
262      * @param p_Name The name of node to select
263      * @param p_Expand Expand parent branch
264      */

265     public void selectNameNode(String JavaDoc p_Name, boolean p_Expand) {
266         if (treeControl != null) {
267             // Changing node ?
268
if (p_Name.equals(getSelectedNameNode()) == false) {
269                 // Change node
270
treeControl.selectNode(p_Name);
271                 if (p_Expand == true) {
272                     treeControl.expandSelectedParents();
273                 }
274                 treeToRefresh = true;
275             }
276             // Query node not found
277
if ((getSelectedTreeControlNode() == null) && (p_Name != null) && (p_Name.length() > 0)) {
278                 // Try to select the parent node
279
String JavaDoc 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     /**
288      * Return the selected node in the tree.
289      *
290      * @return The selected node
291      */

292     public TreeControlNode getSelectedTreeControlNode() {
293         if (treeControl != null) {
294             return treeControl.getSelected();
295         }
296         return null;
297     }
298
299     /**
300      * Return the state of current display of tree.
301      *
302      * @return True if the current tree is not correctly displaying
303      */

304     public boolean isTreeToRefresh() {
305         return treeToRefresh;
306     }
307
308     /**
309      * Change the state of current display of tree.
310      *
311      * @param treeToRefresh True to refresh
312      */

313     public void setTreeToRefresh(boolean treeToRefresh) {
314         this.treeToRefresh = treeToRefresh;
315     }
316
317     /**
318      * Refresh all the servers properties.
319      *
320      * @param p_Request The request to access to the HTTP informations
321      * @throws JonasAdminException
322      */

323     public void refreshServers(HttpServletRequest JavaDoc 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     /**
333      * Refresh the Catalina server properties.
334      *
335      * @param p_Request The request to access to the HTTP informations
336      */

337     public void refreshCatalina(HttpServletRequest JavaDoc p_Request) {
338         resetCatalina();
339         try {
340             String JavaDoc sServer = getServletServerName().toLowerCase();
341             catalinaServer = (sServer.indexOf("tomcat") > -1);
342             if (catalinaServer) {
343                 ObjectName JavaDoc onServer = JonasAdminJmx.getFirstMbean(CatalinaObjectName.catalinaServer());
344                 ObjectName JavaDoc[] aonServices = (ObjectName JavaDoc[]) JonasManagementRepr.getAttribute(onServer
345                 , "serviceNames");
346                 // Service : the first !
347
currentCatalinaServiceName = aonServices[0].getKeyProperty("serviceName");
348                 // Domain
349
currentCatalinaDomainName = aonServices[0].getDomain();
350                 // Engine
351
currentCatalinaEngineName = currentCatalinaDomainName;
352                 ObjectName JavaDoc onEngine = CatalinaObjectName.catalinaEngine(currentCatalinaDomainName);
353                 // Default Host
354
currentCatalinaDefaultHostName = (String JavaDoc) JonasManagementRepr.getAttribute(onEngine
355                 , "defaultHost");
356             }
357         } catch (Exception JavaDoc e) {
358             // none
359
}
360     }
361
362     /**
363      * Refresh the Jetty server properties.
364      *
365      * @param p_Request The request to access to the HTTP informations
366      */

367     public void refreshJetty(HttpServletRequest JavaDoc p_Request) {
368         resetJetty();
369         try {
370             String JavaDoc sServer = getServletServerName().toLowerCase();
371             jettyServer = (sServer.indexOf("jetty") > -1);
372             if (jettyServer == true) {
373                 // none action
374
}
375         }
376         catch (Exception JavaDoc e) {
377             // none
378
}
379     }
380
381     /**
382      * Refresh the managed JOnAS server properties.
383      *
384      * @param p_Request The request to access to the HTTP informations
385      * @throws JonasAdminException
386      */

387     public void refreshJonas(HttpServletRequest JavaDoc p_Request)
388         throws JonasAdminException {
389         resetJonas();
390         // Set J2EEDomain information
391
currentDomain = JonasAdminJmx.getJ2eeDomainObjectName();
392         if (currentDomain != null) {
393             currentDomainName = currentDomain.getKeyProperty("name");
394         } else {
395             // Can't run jonasAdmin
396
throw new JonasAdminException(EXCEPTION_MBEAN_J2EEDOMAIN_NOTFOUND);
397         }
398         // Set J2EEServer information
399
currentJonasServer = JonasAdminJmx.getJ2eeServerObjectName(currentDomainName);
400         if (currentJonasServer != null) {
401             currentJonasServerName = currentJonasServer.getKeyProperty("name");
402         } else {
403             // Can't run jonasAdmin
404
throw new JonasAdminException(EXCEPTION_MBEAN_J2EESERVER_NOTFOUND);
405         }
406         try {
407             // Detect if the current managed JOnAS server is the one where the application is running
408
theCurrentJonasServer = adminJonasServerName.equals(currentJonasServerName);
409         } catch (Exception JavaDoc e) {
410             // Current Jonas not found, can't run jonasAdmin
411
throw new JonasAdminException(EXCEPTION_JONASSERVER_NOTFOUND, e.getMessage(), e);
412         }
413     }
414
415     /**
416      * Return in a string the contents of the instance for each propertie.
417      * Debug utility.
418      *
419      * @return The string contents
420      */

421     public String JavaDoc toString() {
422         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
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 // --------------------------------------------------------- Protected Methods
430

431     /**
432      * Return the name of the servlet server.
433      * @return The name of the servlet server.
434      */

435     protected String JavaDoc getServletServerName() throws ManagementException {
436         String JavaDoc servletServerName = (String JavaDoc) JonasManagementRepr.getAttribute(JonasObjectName.webContainerService()
437             , "ServerName");
438         return servletServerName;
439     }
440
441     /**
442      * Reset the Catalina server properties.
443      */

444     protected void resetCatalina() {
445         catalinaServer = false;
446         currentCatalinaServiceName = null;
447     }
448
449     /**
450      * Reset the Jetty server properties.
451      */

452     protected void resetJetty() {
453         jettyServer = false;
454     }
455
456     /**
457      * Reset the JOnAS server properties.
458      */

459     protected void resetJonas() {
460         theCurrentJonasServer = false;
461         currentDomain = null;
462         currentDomainName = null;
463         currentJonasServer = null;
464         currentJonasServerName = null;
465     }
466
467 // --------------------------------------------------------- Properties Methods
468

469     public String JavaDoc 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 JavaDoc 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 JavaDoc getApplicationContextPath() {
506         return applicationContextPath;
507     }
508
509     public String JavaDoc getCurrentCatalinaDefaultHostName() {
510         return currentCatalinaDefaultHostName;
511     }
512
513     public String JavaDoc getCurrentCatalinaEngineName() {
514         return currentCatalinaEngineName;
515     }
516
517     public String JavaDoc getCurrentCatalinaDomainName() {
518         return currentCatalinaDomainName;
519     }
520
521     public boolean isJettyServer() {
522         return jettyServer;
523     }
524     /**
525      * @return Returns the adminJonasServerName.
526      */

527     public String JavaDoc getAdminJonasServerName() {
528         return adminJonasServerName;
529     }
530 }
531
Popular Tags