KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > j2eemanagement > servlets > MgmtServlet


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: MgmtServlet.java,v 1.21 2005/06/24 12:25:55 danesa Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.j2eemanagement.servlets;
27
28 //import java
29
import java.io.IOException JavaDoc;
30 import java.io.PrintWriter JavaDoc;
31 import java.net.MalformedURLException JavaDoc;
32 import java.rmi.RemoteException JavaDoc;
33 import java.util.List JavaDoc;
34
35 import javax.management.AttributeNotFoundException JavaDoc;
36 import javax.management.InstanceNotFoundException JavaDoc;
37 import javax.management.MBeanException JavaDoc;
38 import javax.management.MBeanServerConnection JavaDoc;
39 import javax.management.ObjectName JavaDoc;
40 import javax.management.ReflectionException JavaDoc;
41 import javax.management.j2ee.ListenerRegistration JavaDoc;
42 import javax.management.j2ee.Management JavaDoc;
43 import javax.management.j2ee.ManagementHome JavaDoc;
44 import javax.management.remote.JMXConnector JavaDoc;
45 import javax.management.remote.JMXConnectorFactory JavaDoc;
46 import javax.management.remote.JMXServiceURL JavaDoc;
47 import javax.naming.Context JavaDoc;
48 import javax.naming.InitialContext JavaDoc;
49 import javax.rmi.PortableRemoteObject JavaDoc;
50 import javax.servlet.ServletConfig JavaDoc;
51 import javax.servlet.ServletException JavaDoc;
52 import javax.servlet.http.HttpServlet JavaDoc;
53 import javax.servlet.http.HttpServletRequest JavaDoc;
54 import javax.servlet.http.HttpServletResponse JavaDoc;
55
56 import org.objectweb.jonas.mejb.DomainManagement;
57 import org.objectweb.jonas.mejb.DomainManagementHome;
58
59 /**
60  * This servlet is an example which shows how to access the MEJB from a servlet.
61  * @author JOnAS team
62  * @author Adriana Danes
63  * @author Michel-Ange Anton
64  */

65 public class MgmtServlet extends HttpServlet JavaDoc {
66
67     // ---------------------------------------------------------- Constants
68
/** Printing constant*/
69     static final String JavaDoc APP_TITLE_LARGE = "J2EE Management sample with Servlet accessing the MEJB";
70     /** Printing constant*/
71     static final String JavaDoc APP_TITLE = "J2EE Management sample";
72     /** Parameter */
73     static final String JavaDoc PARAM_DOMAIN = "domainName";
74     /** Parameter */
75     static final String JavaDoc PARAM_VIEW = "view";
76     /** Parameter */
77     static final String JavaDoc VIEW_INIT = "init";
78     /** Parameter */
79     static final String JavaDoc VIEW_NOTIFICATIONS = "notifications";
80     /** Parameter */
81     static final String JavaDoc VIEW_OTHER = "other";
82     /** Deploable module types */
83     static final String JavaDoc JAR = "jar";
84     /** Deploable module types */
85     static final String JavaDoc WAR = "war";
86     /** Deploable module types */
87     static final String JavaDoc RAR = "rar";
88     /** Deploable module types */
89     static final String JavaDoc EAR = "ear";
90     /**
91      * Listener object
92      */

93     private MyListener mListener = null;
94
95     // ---------------------------------------------------------- Public methods
96

97     /**
98      * Initialize the servlet.
99      * @param pConfig See HttpServlet
100      * @throws ServletException
101      */

102     public void init(ServletConfig JavaDoc pConfig) throws ServletException JavaDoc {
103         super.init(pConfig);
104         // Initialize variables
105
mListener = null;
106     }
107
108     // ---------------------------------------------------------- Protected
109
// methods
110

111     /**
112      * Response to the GET request.
113      * @param pRequest See HttpServlet
114      * @param pResponse See HttpServlet
115      * @throws IOException
116      * @throws ServletException
117      */

118     protected void doGet(HttpServletRequest JavaDoc pRequest, HttpServletResponse JavaDoc pResponse) throws IOException JavaDoc,
119             ServletException JavaDoc {
120         dispatch(pRequest, pResponse);
121     }
122
123     /**
124      * Response to the POST request.
125      * @param pRequest See HttpServlet
126      * @param pResponse See HttpServlet
127      * @throws IOException
128      * @throws ServletException
129      */

130     protected void doPost(HttpServletRequest JavaDoc pRequest, HttpServletResponse JavaDoc pResponse) throws IOException JavaDoc,
131             ServletException JavaDoc {
132         dispatch(pRequest, pResponse);
133     }
134
135     /**
136      * Dispatch the response.
137      * @param pRequest Request
138      * @param pResponse Response
139      * @throws IOException
140      */

141     protected void dispatch(HttpServletRequest JavaDoc pRequest, HttpServletResponse JavaDoc pResponse) throws IOException JavaDoc {
142
143         pResponse.setContentType("text/html");
144         PrintWriter JavaDoc out = pResponse.getWriter();
145
146         // Get parameters
147
String JavaDoc sParamDomain = pRequest.getParameter(PARAM_DOMAIN);
148         String JavaDoc sParamView = pRequest.getParameter(PARAM_VIEW);
149
150         // Dispatching
151
if ((sParamDomain == null) || (sParamDomain.length() == 0)) {
152             doViewError("Parameter domain not found", pRequest, out);
153         } else if ((sParamView == null) || (sParamView.length() == 0) || VIEW_INIT.equals(sParamView)) {
154             doViewInit(sParamDomain, pRequest, out);
155         } else if (VIEW_NOTIFICATIONS.equals(sParamView)) {
156             doViewNotifications(out);
157         } else if (VIEW_OTHER.equals(sParamView)) {
158             doViewOther(sParamDomain, pRequest, out);
159         } else {
160             doViewError("Unknown View", pRequest, out);
161         }
162
163     }
164
165     /**
166      * Header.
167      * @param pOut Printer
168      * @param pTitle Title to display
169      * @param pSubTitle Subtitle to display or null if not
170      */

171     protected void printHeader(PrintWriter JavaDoc pOut, String JavaDoc pTitle, String JavaDoc pSubTitle) {
172
173         pOut.println("<html>");
174         pOut.println("<head>");
175         pOut.println("<title>" + pTitle);
176         if (pSubTitle != null) {
177             pOut.println(" - " + pSubTitle);
178         }
179         pOut.println("</title>");
180         pOut.println("</head>");
181         pOut.println("<body style=\"background : white; color : black;\">");
182         printHeaderTitle(pOut, pTitle, pSubTitle);
183     }
184
185     /**
186      * Header.
187      * @param pOut Printer
188      * @param pTitle Title to display
189      * @param pSubTitle Subtitle to display or null if not
190      */

191     protected void printHeaderAutoRefresh(PrintWriter JavaDoc pOut, String JavaDoc pTitle, String JavaDoc pSubTitle) {
192
193         pOut.println("<html>");
194         pOut.println("<head>");
195         pOut.println("<title>" + pTitle + "</title>");
196         pOut.println("<script language=\"JavaScript\">");
197         pOut.println("function startRefresh(){ setTimeout(\"refresh()\",30000); self.focus(); }");
198         pOut.println("function refresh(){ window.location.reload(); }");
199         pOut.println("</script>");
200         pOut.println("</head>");
201         pOut.println("<body onload=\"startRefresh()\" style=\"background : white; color : black;\">");
202         printHeaderTitle(pOut, pTitle, pSubTitle);
203     }
204
205     /**
206      * Header title.
207      * @param pOut Printer
208      * @param pTitle Title to display
209      * @param pSubTitle Subtitle to display or null if not
210      */

211     protected void printHeaderTitle(PrintWriter JavaDoc pOut, String JavaDoc pTitle, String JavaDoc pSubTitle) {
212
213         pOut.println("<table width=\"100%\" border=\"0\">");
214         pOut.println("<tr>");
215         pOut.println("<td rowspan=\"2\"><img SRC=\"img/logo_jonas.jpg\" alt=\"JOnAS Logo\"></td>");
216         pOut.println("<td align=\"center\"><h1>" + pTitle + "</h1></td>");
217         pOut.println("</tr>");
218         pOut.println("<tr>");
219         if (pSubTitle != null) {
220             pOut.println("<td align=\"center\"><h1>" + pSubTitle + "</h1></td>");
221         }
222         pOut.println("</tr>");
223         pOut.println("</table>");
224     }
225
226     /**
227      * Footer.
228      * @param pOut Printer
229      */

230     protected void printFooter(PrintWriter JavaDoc pOut) {
231         pOut.println("</body>");
232         pOut.println("</html>");
233     }
234
235     /**
236      * Footer navigation.
237      * @param pDomainName Name of domain
238      * @param pRequest Http request
239      * @param pOut Printer
240      * @param pPrevious Previous view
241      * @param pNext Next view
242      */

243     protected void printNavigationFooter(String JavaDoc pDomainName, HttpServletRequest JavaDoc pRequest, PrintWriter JavaDoc pOut,
244             String JavaDoc pPrevious, String JavaDoc pNext) {
245
246         // index
247
String JavaDoc sViewIndex = pRequest.getContextPath();
248         // Notifications
249
String JavaDoc sViewNotifications = pRequest.getRequestURI() + "?" + PARAM_DOMAIN + "=" + pDomainName + "&"
250                 + PARAM_VIEW + "=" + VIEW_NOTIFICATIONS;
251         // Previous View
252
String JavaDoc sPrevView = null;
253         if (pPrevious != null) {
254             sPrevView = pRequest.getRequestURI() + "?" + PARAM_DOMAIN + "=" + pDomainName + "&" + PARAM_VIEW + "="
255                     + pPrevious;
256         }
257         // Next View
258
String JavaDoc sNextView = null;
259         if (pNext != null) {
260             sNextView = pRequest.getRequestURI() + "?" + PARAM_DOMAIN + "=" + pDomainName + "&" + PARAM_VIEW + "="
261                     + pNext;
262         }
263
264         // Display
265
pOut.print("<table border=\"1\" align=\"center\" cellspacing=\"0\" cellpadding=\"5\">");
266         pOut.print("<tr>");
267         pOut.print("<td align=\"center\"> <a HREF=\"" + sViewIndex + "\">Index</a> </td>");
268         pOut.print("<td align=\"center\"> <a HREF=\"" + sViewNotifications
269                 + "\" target=\"ViewNotifications\">Notifications</a> </td>");
270         if (sPrevView != null) {
271             pOut.println("<td align=\"center\"> <a HREF=\"" + sPrevView + "\">Previous</a> </td>");
272         }
273         if (sNextView != null) {
274             pOut.println("<td align=\"center\"> <a HREF=\"" + sNextView + "\">Next</a> </td>");
275         }
276         pOut.println("</tr></table>");
277         // Footer
278
printFooter(pOut);
279     }
280
281     /**
282      * Simply View error.
283      * @param pError Message error
284      * @param pRequest Http request
285      * @param pOut Printer
286      */

287     protected void doViewError(String JavaDoc pError, HttpServletRequest JavaDoc pRequest, PrintWriter JavaDoc pOut) {
288
289         // Header
290
printHeader(pOut, APP_TITLE, "Error");
291         // Error message
292
pOut.println("<h2>" + pError + "</h2>");
293         // Return
294
pOut.println("<a HREF=\"" + pRequest.getContextPath() + "\">Return</a>");
295         // Footer
296
printFooter(pOut);
297     }
298
299     /**
300      * View init MEJB and access to MBEans J2EEDomain and J2EEServer.
301      * @param pDomainName Name of domain to access
302      * @param pRequest Http request
303      * @param pOut Printer
304      */

305     protected void doViewInit(String JavaDoc pDomainName, HttpServletRequest JavaDoc pRequest, PrintWriter JavaDoc pOut) {
306
307         // Header
308
//printHeader(pOut, APP_TITLE_LARGE, "Init");
309

310         // -----------------------------
311
// Get initial context
312
// -----------------------------
313
pOut.println("<h2>Initial context</h2>");
314         pOut.println("<ul>");
315         Context JavaDoc initialContext = null;
316         try {
317             initialContext = new InitialContext JavaDoc();
318             pOut.println("<li>Initial context OK</li>");
319         } catch (Exception JavaDoc e) {
320             pOut.print("<li>Cannot get initial context for JNDI: ");
321             pOut.println(e + "</li>");
322             pOut.println("</ul>");
323             return;
324         }
325         pOut.println("</ul><br>");
326
327         // -----------------------------
328
// Access to the MEJB
329
// -----------------------------
330
pOut.println("<h2>Create MEJB</h2>");
331         pOut.println("<ul>");
332
333         // Connecting to the MEJB home through JNDI
334
ManagementHome JavaDoc mgmtHome = null;
335         DomainManagementHome domainMgmtHome = null;
336         try {
337             mgmtHome = (ManagementHome JavaDoc) PortableRemoteObject.narrow(initialContext.lookup("java:comp/env/ejb/mgmt/MEJB"),
338                     ManagementHome JavaDoc.class);
339         } catch (Exception JavaDoc e) {
340             pOut.println("<li>Cannot lookup java:comp/env/ejb/mgmt/MEJB: " + e + "</li>");
341             pOut.println("</ul>");
342             return;
343         }
344
345         pOut.println("<h2>Create DomainMEJB</h2>");
346         pOut.println("<ul>");
347
348         try {
349             domainMgmtHome = (DomainManagementHome) PortableRemoteObject.narrow(initialContext.lookup("java:comp/env/ejb/mgmt/DomainMEJB"),
350                     DomainManagementHome.class);
351         } catch (Exception JavaDoc e) {
352             pOut.println("<li>Cannot lookup java:comp/env/ejb/mgmt/DomainMEJB: " + e + "</li>");
353             pOut.println("</ul>");
354             //return;
355
}
356         // Management bean creation
357
Management JavaDoc mgmt = null;
358         // Domain management bean creation
359
DomainManagement dmgmt = null;
360
361         try {
362             mgmt = mgmtHome.create();
363             pOut.println("<li>MEJB created</li>");
364         } catch (Exception JavaDoc e) {
365             pOut.println("<li>Cannot create MEJB: " + e + "</li>");
366             return;
367         }
368
369         try {
370             dmgmt = domainMgmtHome.create();
371             pOut.println("<li>DomainMEJB created</li>");
372         } catch (Exception JavaDoc e) {
373             pOut.println("<li>Cannot create DomainMEJB: " + e + "</li>");
374             return;
375         }
376         pOut.println("</ul><br />");
377
378         // ------------------------------
379
// Access to the J2EEDomain MBean
380
// ------------------------------
381
ObjectName JavaDoc onDomain = accessJ2EEDomain(pDomainName, mgmt, pOut);
382         if (onDomain == null) {
383             return;
384         }
385         // -----------------------------------------------------
386
// Check for J2EEServer MBeans in the current J2EEDomain
387
// -----------------------------------------------------
388
ObjectName JavaDoc onServer = accessJ2EEServer(onDomain, mgmt, pOut);
389         if (onServer == null) {
390             return;
391         }
392
393         // -----------------------------------------------------
394
// Using the domain management EJB to list servers
395
// -----------------------------------------------------
396
String JavaDoc[] serverNames = null;
397         String JavaDoc[] serverNamesDom = null;
398         String JavaDoc[] servers = null;
399         try {
400             pOut.println("<h2>Getting list of servers with MEJB </h2>");
401             serverNames = (String JavaDoc[]) mgmt.getAttribute(onDomain, "serverNames");
402             servers = (String JavaDoc[]) mgmt.getAttribute(onDomain, "servers");
403             pOut.println("<ul>");
404             for(int i = 0; i < serverNames.length; i++){
405                 pOut.println("<li>" + serverNames[i] + "</li>");
406             }
407             pOut.println("</ul>");
408
409         } catch (Exception JavaDoc e){
410             pOut.println("<li>Could not make MEJB list servers. </li>" + e);
411             e.printStackTrace(pOut);
412             return;
413         }
414
415         try {
416             pOut.println("<h2>Getting list of servers with DomainMEJB </h2>");
417             serverNamesDom = (String JavaDoc[]) dmgmt.getAttribute(null, onDomain, "serverNames");
418             pOut.println("<ul>");
419             for(int i = 0; i < serverNamesDom.length; i++){
420                 pOut.println("<li>" + serverNamesDom[i] + "</li>");
421             }
422             pOut.println("</ul>");
423
424         } catch (Exception JavaDoc e){
425             pOut.println("<li>Could not make DomainMEJB list servers. </li>" + e);
426             e.printStackTrace(pOut);
427             return;
428         }
429
430         // ------------------------------
431
// Test domain management
432
// ------------------------------
433
if (servers.length > 1) {
434             pOut.println("<h2>Try to manage the current domain: deploy/undeploy on all servers in the domain</h2>");
435             pOut.println("<ul>");
436             pOut.println("<li>Use the J2EEDomain object name: \"" + onDomain.toString() + "\"</li>");
437
438             String JavaDoc fileName = null;
439             // Use deployJar, unDeployJar
440
// (suppose sb.jar was installed under JONAS_BASE/ejbjars directory)
441
fileName = "sb.jar";
442             deployModuleWithTarget(JAR, onDomain, servers, mgmt, fileName, pOut);
443
444             // Use deployEar, unDeployEar
445
// (suppose earsample.ear was installed under JONAS_BASE/apps directory)
446
fileName = "earsample.ear";
447             deployModuleWithTarget(EAR, onDomain, servers, mgmt, fileName, pOut);
448
449             pOut.println("</ul><br>");
450         }
451
452         // ------------------------------
453
// Test current server management
454
// ------------------------------
455
pOut.println("<h2>Try to manage the current server</h2>");
456         pOut.println("<ul>");
457         pOut.println("<li>Using the J2EEServer object name: \"" + onServer.toString() + "\"</li>");
458
459         // Use deployJar, unDeployJar, isJarDeployed management opearations
460
// (suppose sb.jar was installed under JONAS_BASE/ejbjars directory)
461
String JavaDoc jarFileName = "sb.jar";
462         deployJarModule(onServer, mgmt, jarFileName, pOut);
463
464         // Use deployEar, unDeployEar, isEarDeployed management opearations
465
// (suppose earsample.ear was installed under JONAS_BASE/apps directory)
466
String JavaDoc earFileName = "earsample.ear";
467         deployEarModule(onServer, mgmt, earFileName, pOut);
468
469         // (suppose autoload/earsample.ear was installed under JONAS_BASE/apps directory)
470
earFileName = "autoload/earsample.ear";
471         deployEarModule(onServer, mgmt, earFileName, pOut);
472
473         // Use deployRar, unDeployRar, isRarDeployed
474
// (suppose JDBC connection rar files installed under JONAS_BASE/rars directory)
475
String JavaDoc rarFileName = "autoload/JOnAS_jdbcCP.rar";
476         deployRarModule(onServer, mgmt, rarFileName, pOut);
477
478         pOut.println("</ul><br>");
479
480         // -----------------------------
481
// Test Event Listener support
482
// -----------------------------
483

484         pOut.println("<h2>Register a event listener</h2>");
485         pOut.println("<ul>");
486         if (mListener == null) {
487             mListener = new MyListener();
488             pOut.println("<li>MyListener created</li>");
489         }
490
491         // Get the ListenerRegistration object
492
try {
493             ListenerRegistration JavaDoc lr = mgmt.getListenerRegistry();
494             if (lr != null) {
495                 pOut.println("<li>Add listener on J2EEServer (" + onServer.toString() + ")</li>");
496                 String JavaDoc sHandler = "MEJBTester";
497                 lr.addNotificationListener(onServer, mListener, null, sHandler);
498                 pOut.println("<li>Notification Listener added</li>");
499                 String JavaDoc sViewNotifications = pRequest.getRequestURI() + "?" + PARAM_DOMAIN + "=" + pDomainName + "&"
500                 + PARAM_VIEW + "=" + VIEW_NOTIFICATIONS;
501                 pOut.println("<a HREF=\"" + sViewNotifications
502                         + "\" target=\"ViewNotifications\">See list of notifications</a>");
503                 pOut.println("</ul>");
504             } else {
505                 pOut.println("<li>Can't add remote listener for the moment</li>");
506                 pOut.println("</ul>");
507             }
508         } catch (Exception JavaDoc e) {
509             pOut.println("<li>Can't add notification listener on " + onServer.toString() + " : " + e + "</li>");
510         }
511
512         pOut.println("<h2>View Sample Init is OK </h2>");
513
514         // Footer
515
printNavigationFooter(pDomainName, pRequest, pOut, null, VIEW_OTHER);
516     }
517
518     /**
519      * View notifications.
520      * @param pOut Printer
521      */

522     protected void doViewNotifications(PrintWriter JavaDoc pOut) {
523
524         // Header
525
printHeaderAutoRefresh(pOut, APP_TITLE, "Notifications");
526         pOut.println("<h2>List of notifications</h2>");
527
528         // Verify listener
529
if (mListener == null) {
530             pOut.println("Listener not found !");
531         } else {
532             // Display notifications
533
List JavaDoc list = mListener.getlistNotifications();
534             if (list.size() > 0) {
535                 pOut.println("<ol>");
536                 for (int i = 0; i < list.size(); i++) {
537                     pOut.println("<li>" + list.get(i) + "</li>");
538                 }
539                 pOut.println("</ol>");
540             } else {
541                 pOut.println("No notifications, the list is empty");
542             }
543         }
544
545         // Footer
546
printFooter(pOut);
547     }
548
549     /**
550      * View example.
551      * @param pDomainName Name of domain to access
552      * @param pRequest Http request
553      * @param pOut Printer
554      */

555     protected void doViewOther(String JavaDoc pDomainName, HttpServletRequest JavaDoc pRequest, PrintWriter JavaDoc pOut) {
556
557         // Header
558
printHeader(pOut, APP_TITLE, "Other");
559
560         pOut.println("<h2>Other</h2>");
561
562         // Footer
563
printNavigationFooter(pDomainName, pRequest, pOut, VIEW_INIT, null);
564     }
565
566     /**
567      * Create J2EEDomain MBean's ObjectName and test if MBean registered
568      * @param pDomainName the name provided by the user
569      * @param mgmt MEJB
570      * @param pOut output stream
571      * @return true if management operation succeeded
572      */

573     private ObjectName JavaDoc accessJ2EEDomain(String JavaDoc pDomainName, Management JavaDoc mgmt, PrintWriter JavaDoc pOut) {
574         ObjectName JavaDoc onDomain = null;
575         pOut.println("<h2>Access the J2EEDomain MBean</h2>");
576         pOut.println("<ul>");
577
578         // Get the J2EEDomain MBean's ObjectName
579
try {
580             String JavaDoc name = pDomainName + ":j2eeType=J2EEDomain,name=" + pDomainName;
581             onDomain = ObjectName.getInstance(name);
582             pOut.println("<li>J2EEDomain object name created: \"" + name.toString() + "\"</li>");
583         } catch (Exception JavaDoc e) {
584             pOut.println("<li>Cannot create object name for J2EEDomain managed object: " + e + "</li>");
585             pOut.println("</ul>");
586             return null;
587         }
588         // Check that the J2EEDomain MBean registered
589
try {
590             boolean exists = mgmt.isRegistered(onDomain);
591             if (exists) {
592                 pOut.println("<li>Found this J2EEDomain managed object registered in the current MBean server</li>");
593                 pOut.println("</ul>");
594             } else {
595                 pOut.println("<li><b>Can't find this J2EEDomain managed object in the current MBean server</b></li>");
596                 pOut.println("</ul>");
597                 return null;
598             }
599         } catch (Exception JavaDoc e) {
600             pOut.println("<li>Error when using this J2EEDomain managed object: " + e + "</li>");
601             pOut.println("</ul>");
602             return null;
603         }
604         return onDomain;
605     }
606
607     /**
608      * Get a registered J2EEServer MBean
609      * @param onDomain J2EEDomain MBean's ObjectName
610      * @param mgmt MEJB
611      * @param pOut output stream
612      * @return true if management operation succeeded
613      */

614     private ObjectName JavaDoc accessJ2EEServer(ObjectName JavaDoc onDomain, Management JavaDoc mgmt, PrintWriter JavaDoc pOut) {
615         pOut.println("<h2>Access the J2EEServer MBeans</h2>");
616         pOut.println("<ul>");
617         ObjectName JavaDoc onServer = null;
618         try {
619             String JavaDoc[] listServers = (String JavaDoc[]) mgmt.getAttribute(onDomain, "servers");
620             int nbServers = listServers.length;
621             if (nbServers == 0) {
622                 pOut.println("<li>No J2EE servers belonging to this domain (problem with domain management !!)</li>");
623                 pOut.println("</ul><br>");
624                 return null;
625             } else {
626                 if (nbServers == 1) {
627                     String JavaDoc serverName = (String JavaDoc) listServers[0];
628                     onServer = ObjectName.getInstance(serverName);
629                     if (mgmt.isRegistered(onServer)) {
630                         pOut.println("<li>One J2EE server running in this domain. Its object name is: \"" + serverName + "\"</li>");
631                         pOut.println("</ul><br>");
632                         return onServer;
633                     } else {
634                         pOut.println("<li><b>Can't find the J2EEServer MBean having object name: " + serverName + " in the current MBean server</b></li>");
635                         pOut.println("</ul><br>");
636                         return null;
637                     }
638                 } else {
639                     pOut.println("<li>List of J2EE servers belonging to this domain (list of the associated MBean object names):</li>");
640                     pOut.println("<ol>");
641                     for (int i = 0; i < nbServers; i++) {
642                         String JavaDoc sServer = listServers[i];
643                         ObjectName JavaDoc on = ObjectName.getInstance(sServer);
644                         if (mgmt.isRegistered(on)) {
645                             pOut.print("<li>The MBean having object name \"" + sServer);
646                             pOut.println("\" is registered in the current MBean server.");
647                             pOut.println("</li>");
648                             onServer = on;
649                         } else {
650                             pOut.print("<li>Found object name \"" + sServer);
651                             pOut.println("\" in the severs list, but this name is not registered in the current MBean server.");
652                             String JavaDoc name = on.getKeyProperty("name");
653                             String JavaDoc[] signature = {"java.lang.String"};
654                             String JavaDoc[] params = new String JavaDoc[1];
655                             params[0] = name;
656                             String JavaDoc[] urls = (String JavaDoc[]) mgmt.invoke(onDomain, "getConnectorServerURLs", params, signature);
657                             pOut.println("<ul>");
658                             for (int j = 0; j < params.length; j++) {
659                                 String JavaDoc url = urls[j];
660                                 pOut.print("<li>Try to connect to server " + name + ". Use MBean server connector url: " + url + "</li>");
661                                 MBeanServerConnection JavaDoc connection = createConnection(url);
662                                 if (connection != null) {
663                                     // Use connection directly instead of using MEJB
664
try {
665                                         if (connection.isRegistered(on)) {
666                                             pOut.print("<li>Found MBean having object name \"" + sServer + "\" registered in the connected MBean server");
667                                         } else {
668                                             pOut.print("<li>The MBean having object name \"" + sServer + "\" is not registered in the connected MBean server");
669                                         }
670                                     } catch (IOException JavaDoc ioe) {
671                                         pOut.print("<li>Exception when trying to use connection: " + ioe.toString());
672                                     }
673                                 }
674                             }
675                             pOut.println("</ul>");
676                             pOut.println("</li>");
677                         }
678                     }
679                     pOut.println("</ol>");
680                     pOut.println("</ul><br>");
681                     return onServer;
682                 }
683             }
684         } catch (Exception JavaDoc e) {
685             pOut.println("<li>Error when managing the J2EEServer: " + e + "</li>");
686             pOut.println("</ul><br>");
687             return null;
688         }
689     }
690
691     private MBeanServerConnection JavaDoc createConnection(String JavaDoc connectorServerURL) {
692         MBeanServerConnection JavaDoc connection;
693         // create a connector client for the connector server at the given url
694
JMXConnector JavaDoc connector = null;
695         try {
696             JMXServiceURL JavaDoc url = new JMXServiceURL JavaDoc(connectorServerURL);
697             connector = JMXConnectorFactory.newJMXConnector(url, null);
698             connector.connect(null);
699             connection = connector.getMBeanServerConnection();
700         } catch (MalformedURLException JavaDoc e) {
701             // there is no provider for the protocol in url
702
connection = null;
703         } catch (IOException JavaDoc e) {
704             // connector client or connection cannot be made because of a communication problem.
705
connection = null;
706         } catch (java.lang.SecurityException JavaDoc e) {
707             // connection cannot be made for security reasons
708
connection = null;
709         }
710         return connection;
711     }
712
713     /**
714      * Deploy if not already deployed, undeploy otherwise, a given jar file.
715      * @param onServer ObjectName of the J2EEServer MBean on which the mananagement operation has to be applied
716      * @param mgmt MEJB
717      * @param jarFileName name of the file to be deployed/undeployed
718      * @param pOut output stream
719      */

720     private void deployJarModule(ObjectName JavaDoc onServer, Management JavaDoc mgmt, String JavaDoc jarFileName, PrintWriter JavaDoc pOut) {
721         String JavaDoc[] signature = {"java.lang.String"};
722         String JavaDoc[] params = new String JavaDoc[1];
723         params[0] = jarFileName;
724         pOut.println("<li>Test if " + jarFileName + " deployed</li>");
725         boolean isDeployed = false;
726         try {
727             isDeployed = ((Boolean JavaDoc) mgmt.invoke(onServer, "isJarDeployed", params, signature)).booleanValue();
728         } catch (Exception JavaDoc e) {
729             pOut.println("<li>Problem when invoking isJarDeployed management operation: " + e.toString());
730         }
731         if (!isDeployed) {
732             pOut.println("<li>Try to deploy " + jarFileName + " !</li>");
733             try {
734                 String JavaDoc deployedObjectName = (String JavaDoc) mgmt.invoke(onServer, "deployJar", params, signature);
735                 pOut.println("<li>The Object Name of the deployed J2EEModule is: \"" + deployedObjectName + "\". </li>");
736             } catch (MBeanException JavaDoc mbe) {
737                 pOut.println("<li>Could not deploy " + jarFileName + " because of exception: "
738                         + mbe.getTargetException().toString());
739             } catch (Exception JavaDoc e) {
740                 pOut.println("<li>Could not deploy " + jarFileName + " because of exception: " + e.toString() + " </li>");
741             }
742         } else {
743             pOut.println("<li>Try to un-deploy " + jarFileName + " !</li>");
744             try {
745                 mgmt.invoke(onServer, "unDeployJar", params, signature);
746                 pOut.println("<li>Done undeploy.>");
747             } catch (MBeanException JavaDoc mbe) {
748                 pOut.println("<li>Could not undeploy " + jarFileName + " because of exception: "
749                         + mbe.getTargetException().toString());
750             } catch (Exception JavaDoc e) {
751                 pOut.println("<li>Could not undeploy " + jarFileName + " because of exception: " + e.toString() + " </li>");
752             }
753         }
754     }
755     /**
756      * Deploy if not already deployed, undeploy otherwise, a given ear file.
757      * @param onServer ObjectName of the J2EEServer MBean on which the mananagement operation has to be applied
758      * @param mgmt MEJB
759      * @param earFileName name of the file to be deployed/undeployed
760      * @param pOut output stream
761      */

762     private void deployEarModule(ObjectName JavaDoc onServer, Management JavaDoc mgmt, String JavaDoc earFileName, PrintWriter JavaDoc pOut) {
763         String JavaDoc[] signature = {"java.lang.String"};
764         String JavaDoc[] params = new String JavaDoc[1];
765         params[0] = earFileName;
766         pOut.println("<li>Test if " + earFileName + " deployed</li>");
767         boolean isDeployed = false;
768         try {
769             isDeployed = ((Boolean JavaDoc) mgmt.invoke(onServer, "isEarDeployed", params, signature)).booleanValue();
770         } catch (Exception JavaDoc e) {
771             pOut.println("<li>Problem when invoking isEarDeployed management operation: " + e.toString());
772         }
773         if (!isDeployed) {
774             pOut.println("<li>Try to deploy " + earFileName + " !</li>");
775             try {
776                 String JavaDoc deployedObjectName = (String JavaDoc) mgmt.invoke(onServer, "deployEar", params, signature);
777                 pOut.println("<li>The Object Name of the deployed J2EEModule is: \"" + deployedObjectName + "\". </li>");
778             } catch (MBeanException JavaDoc mbe) {
779                 pOut.println("<li>Could not deploy " + earFileName + " because of exception: "
780                         + mbe.getTargetException().toString());
781             } catch (Exception JavaDoc e) {
782                 pOut.println("<li>Could not deploy " + earFileName + " because of exception: " + e.toString() + " </li>");
783             }
784         } else {
785             pOut.println("<li>Try to un-deploy " + earFileName + " !</li>");
786             try {
787                 mgmt.invoke(onServer, "unDeployEar", params, signature);
788                 pOut.println("<li>Done undeploy.>");
789             } catch (MBeanException JavaDoc mbe) {
790                 pOut.println("<li>Could not undeploy " + earFileName + " because of exception: "
791                         + mbe.getTargetException().toString());
792             } catch (Exception JavaDoc e) {
793                 pOut.println("<li>Could not undeploy " + earFileName + " because of exception: " + e.toString() + " </li>");
794             }
795         }
796     }
797
798     /**
799      * Deploy if not already deployed, undeploy otherwise, a given rar file.
800      * @param onServer ObjectName of the J2EEServer MBean on which the mananagement operation has to be applied
801      * @param mgmt MEJB
802      * @param rarFileName name of the file to be deployed/undeployed
803      * @param pOut output stream
804      */

805     private void deployRarModule(ObjectName JavaDoc onServer, Management JavaDoc mgmt, String JavaDoc rarFileName, PrintWriter JavaDoc pOut) {
806         String JavaDoc[] signature = {"java.lang.String"};
807         String JavaDoc[] params = new String JavaDoc[1];
808         params[0] = rarFileName;
809         pOut.println("<li>Test if " + rarFileName + " deployed</li>");
810         boolean isDeployed = false;
811         try {
812             isDeployed = ((Boolean JavaDoc) mgmt.invoke(onServer, "isRarDeployed", params, signature)).booleanValue();
813         } catch (Exception JavaDoc e) {
814             pOut.println("<li>Problem when invoking isRarDeployed management operation: " + e.toString());
815         }
816         if (!isDeployed) {
817             pOut.println("<li>Try to deploy " + rarFileName + " !</li>");
818             try {
819                 String JavaDoc deployedObjectName = (String JavaDoc) mgmt.invoke(onServer, "deployRar", params, signature);
820                 pOut.println("<li>The Object Name of the deployed J2EEModule is: \"" + deployedObjectName + "\". </li>");
821             } catch (MBeanException JavaDoc mbe) {
822                 pOut.println("<li>Could not deploy " + rarFileName + " because of exception: "
823                         + mbe.getTargetException().toString());
824             } catch (Exception JavaDoc e) {
825                 pOut.println("<li>Could not deploy " + rarFileName + " because of exception: " + e.toString() + " </li>");
826             }
827         } else {
828             pOut.println("<li>Try to un-deploy " + rarFileName + " !</li>");
829             try {
830                 mgmt.invoke(onServer, "unDeployRar", params, signature);
831                 pOut.println("<li>Done undeploy.>");
832             } catch (MBeanException JavaDoc mbe) {
833                 pOut.println("<li>Could not undeploy " + rarFileName + " because of exception: "
834                         + mbe.getTargetException().toString());
835             } catch (Exception JavaDoc e) {
836                 pOut.println("<li>Could not undeploy " + rarFileName + " because of exception: " + e.toString() + " </li>");
837             }
838         }
839     }
840
841     /**
842      * Deploy if not already deployed, undeploy otherwise, a given jar file.
843      * @param moduleType can be JAR, WAR, RAR or EAR
844      * @param taget OBJECT_NAMEs of the J2EEServer MBean on which the mananagement operation has to be applied
845      * @param mgmt MEJB
846      * @param fileName name of the file to be deployed/undeployed
847      * @param pOut output stream
848      */

849     private void deployModuleWithTarget(String JavaDoc moduleType, ObjectName JavaDoc onDomain, String JavaDoc[] target, Management JavaDoc mgmt, String JavaDoc fileName, PrintWriter JavaDoc pOut) {
850         String JavaDoc[] signature = {"[Ljava.lang.String;", "java.lang.String"};
851         Object JavaDoc[] params = new Object JavaDoc[2];
852         params[0] = target;
853         params[1] = fileName;
854             pOut.println("<li>Try to deploy " + fileName + " on target !</li>");
855             String JavaDoc operationNameSuf = null;
856             if (moduleType.equals(JAR)) {
857                 operationNameSuf = "eployJar";
858             } else if (moduleType.equals(WAR)) {
859                 operationNameSuf = "eployWar";
860             } else if (moduleType.equals(RAR)) {
861                 operationNameSuf = "eployWar";
862             } else if (moduleType.equals(EAR)) {
863                 operationNameSuf = "eployEar";
864             } else {
865                 return;
866             }
867             try {
868                 String JavaDoc operationName = "d" + operationNameSuf;
869                 Boolean JavaDoc done = (Boolean JavaDoc) mgmt.invoke(onDomain, operationName, params, signature);
870                 if (done.booleanValue()) {
871                     operationName = "unD" + operationNameSuf;
872                     mgmt.invoke(onDomain, operationName, params, signature);
873                 }
874             } catch (MBeanException JavaDoc mbe) {
875                 pOut.println("<li>Could not deploy/undeploy " + fileName + " because of exception: "
876                         + mbe.getTargetException().toString());
877             } catch (Exception JavaDoc e) {
878                 pOut.println("<li>Could not deploy " + fileName + " because of exception: " + e.toString() + " </li>");
879             }
880     }
881 }
Popular Tags