0001: /**
0002: * JOnAS: Java(TM) Open Application Server
0003: * Copyright (C) 1999 Bull S.A.
0004: * Contact: jonas-team@objectweb.org
0005: *
0006: * This library is free software; you can redistribute it and/or
0007: * modify it under the terms of the GNU Lesser General Public
0008: * License as published by the Free Software Foundation; either
0009: * version 2.1 of the License, or any later version.
0010: *
0011: * This library is distributed in the hope that it will be useful,
0012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0014: * Lesser General Public License for more details.
0015: *
0016: * You should have received a copy of the GNU Lesser General Public
0017: * License along with this library; if not, write to the Free Software
0018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
0019: * USA
0020: *
0021: * --------------------------------------------------------------------------
0022: * $Id: MgmtServlet.java 9886 2006-12-04 09:56:04Z danesa $
0023: * --------------------------------------------------------------------------
0024: */package org.objectweb.j2eemanagement.servlets;
0025:
0026: //import java
0027: import java.io.IOException;
0028: import java.io.PrintWriter;
0029: import java.net.MalformedURLException;
0030: import java.rmi.RemoteException;
0031: import java.util.List;
0032:
0033: import javax.management.MBeanException;
0034: import javax.management.MBeanServerConnection;
0035: import javax.management.MalformedObjectNameException;
0036: import javax.management.ObjectName;
0037: import javax.management.j2ee.ListenerRegistration;
0038: import javax.management.j2ee.Management;
0039: import javax.management.j2ee.ManagementHome;
0040: import javax.management.remote.JMXConnector;
0041: import javax.management.remote.JMXConnectorFactory;
0042: import javax.management.remote.JMXServiceURL;
0043: import javax.naming.Context;
0044: import javax.naming.InitialContext;
0045: import javax.rmi.PortableRemoteObject;
0046: import javax.servlet.ServletConfig;
0047: import javax.servlet.ServletException;
0048: import javax.servlet.http.HttpServletRequest;
0049: import javax.servlet.http.HttpServletResponse;
0050:
0051: import org.objectweb.jonas.mejb.DomainManagement;
0052: import org.objectweb.jonas.mejb.DomainManagementHome;
0053:
0054: /**
0055: * This servlet is an example which shows how to access the MEJB from a servlet.
0056: * @author JOnAS team
0057: * @author Adriana Danes
0058: * @author Michel-Ange Anton
0059: */
0060: public class MgmtServlet extends J2eemanagementBaseServlet {
0061:
0062: // ---------------------------------------------------------- Constants
0063: /** Printing constant*/
0064: static final String APP_TITLE_LARGE = "J2EE Management sample with Servlet accessing the MEJB";
0065: /** Printing constant*/
0066: static final String APP_TITLE = "J2EE Management sample";
0067: /** Parameter */
0068: static final String PARAM_DOMAIN = "domainName";
0069: /** Parameter */
0070: static final String PARAM_VIEW = "view";
0071: /** Parameter */
0072: static final String VIEW_INIT = "init";
0073: /** Parameter */
0074: static final String VIEW_NOTIFICATIONS = "notifications";
0075: /** Parameter */
0076: static final String VIEW_OTHER = "other";
0077: /** Deploable module types */
0078: static final String JAR = "jar";
0079: /** Deploable module types */
0080: static final String WAR = "war";
0081: /** Deploable module types */
0082: static final String RAR = "rar";
0083: /** Deploable module types */
0084: static final String EAR = "ear";
0085: /**
0086: * Listener object
0087: */
0088: private MyListener mListener = null;
0089:
0090: // ---------------------------------------------------------- Public methods
0091:
0092: /**
0093: * Initialize the servlet.
0094: * @param pConfig See HttpServlet
0095: * @throws ServletException
0096: */
0097: public void init(ServletConfig pConfig) throws ServletException {
0098: super .init(pConfig);
0099: // Initialize variables
0100: mListener = null;
0101: }
0102:
0103: // ---------------------------------------------------------- Protected
0104: // methods
0105:
0106: /**
0107: * Response to the GET request.
0108: * @param pRequest See HttpServlet
0109: * @param pResponse See HttpServlet
0110: * @throws IOException
0111: * @throws ServletException
0112: */
0113: protected void doGet(HttpServletRequest pRequest,
0114: HttpServletResponse pResponse) throws IOException,
0115: ServletException {
0116: dispatch(pRequest, pResponse);
0117: }
0118:
0119: /**
0120: * Response to the POST request.
0121: * @param pRequest See HttpServlet
0122: * @param pResponse See HttpServlet
0123: * @throws IOException
0124: * @throws ServletException
0125: */
0126: protected void doPost(HttpServletRequest pRequest,
0127: HttpServletResponse pResponse) throws IOException,
0128: ServletException {
0129: dispatch(pRequest, pResponse);
0130: }
0131:
0132: /**
0133: * Dispatch the response.
0134: * @param pRequest Request
0135: * @param pResponse Response
0136: * @throws IOException
0137: */
0138: protected void dispatch(HttpServletRequest pRequest,
0139: HttpServletResponse pResponse) throws IOException {
0140:
0141: pResponse.setContentType("text/html");
0142: PrintWriter out = pResponse.getWriter();
0143:
0144: // Get parameters
0145: String sParamDomain = pRequest.getParameter(PARAM_DOMAIN);
0146: String sParamView = pRequest.getParameter(PARAM_VIEW);
0147:
0148: // Dispatching
0149: if ((sParamDomain == null) || (sParamDomain.length() == 0)) {
0150: doViewError("Parameter domain not found", pRequest, out);
0151: } else if ((sParamView == null) || (sParamView.length() == 0)
0152: || VIEW_INIT.equals(sParamView)) {
0153: doViewInit(sParamDomain, pRequest, out);
0154: } else if (VIEW_NOTIFICATIONS.equals(sParamView)) {
0155: doViewNotifications(out);
0156: } else if (VIEW_OTHER.equals(sParamView)) {
0157: doViewOther(sParamDomain, pRequest, out);
0158: } else {
0159: doViewError("Unknown View", pRequest, out);
0160: }
0161:
0162: }
0163:
0164: /**
0165: * Footer navigation.
0166: * @param pDomainName Name of domain
0167: * @param pRequest Http request
0168: * @param pOut Printer
0169: * @param pPrevious Previous view
0170: * @param pNext Next view
0171: */
0172: protected void printNavigationFooter(String pDomainName,
0173: HttpServletRequest pRequest, PrintWriter pOut,
0174: String pPrevious, String pNext) {
0175:
0176: // index
0177: String sViewIndex = pRequest.getContextPath();
0178: // Notifications
0179: String sViewNotifications = pRequest.getRequestURI() + "?"
0180: + PARAM_DOMAIN + "=" + pDomainName + "&" + PARAM_VIEW
0181: + "=" + VIEW_NOTIFICATIONS;
0182: // Previous View
0183: String sPrevView = null;
0184: if (pPrevious != null) {
0185: sPrevView = pRequest.getRequestURI() + "?" + PARAM_DOMAIN
0186: + "=" + pDomainName + "&" + PARAM_VIEW + "="
0187: + pPrevious;
0188: }
0189: // Next View
0190: String sNextView = null;
0191: if (pNext != null) {
0192: sNextView = pRequest.getRequestURI() + "?" + PARAM_DOMAIN
0193: + "=" + pDomainName + "&" + PARAM_VIEW + "="
0194: + pNext;
0195: }
0196:
0197: // Display
0198: pOut
0199: .print("<table border=\"1\" align=\"center\" cellspacing=\"0\" cellpadding=\"5\">");
0200: pOut.print("<tr>");
0201: pOut.print("<td align=\"center\"> <a href=\"" + sViewIndex
0202: + "\">Index</a> </td>");
0203: pOut
0204: .print("<td align=\"center\"> <a href=\""
0205: + sViewNotifications
0206: + "\" target=\"ViewNotifications\">Notifications</a> </td>");
0207: if (sPrevView != null) {
0208: pOut.println("<td align=\"center\"> <a href=\"" + sPrevView
0209: + "\">Previous</a> </td>");
0210: }
0211: if (sNextView != null) {
0212: pOut.println("<td align=\"center\"> <a href=\"" + sNextView
0213: + "\">Next</a> </td>");
0214: }
0215: pOut.println("</tr></table>");
0216: // Footer
0217: printFooter(pOut);
0218: }
0219:
0220: /**
0221: * View init MEJB and access to MBEans J2EEDomain and J2EEServer.
0222: * @param pDomainName Name of domain to access
0223: * @param pRequest Http request
0224: * @param pOut Printer
0225: */
0226: protected void doViewInit(String pDomainName,
0227: HttpServletRequest pRequest, PrintWriter pOut) {
0228:
0229: // Header
0230: //printHeader(pOut, APP_TITLE_LARGE, "Init");
0231:
0232: // -----------------------------
0233: // Get initial context
0234: // -----------------------------
0235: pOut.println("<h2>Initial context</h2>");
0236: pOut.println("<ul>");
0237: Context initialContext = null;
0238: try {
0239: initialContext = new InitialContext();
0240: pOut.println("<li>Initial context OK</li>");
0241: } catch (Exception e) {
0242: pOut.print("<li>Cannot get initial context for JNDI: ");
0243: pOut.println(e + "</li>");
0244: pOut.println("</ul>");
0245: return;
0246: }
0247: pOut.println("</ul><br>");
0248:
0249: // -----------------------------
0250: // Access to the MEJB
0251: // -----------------------------
0252: pOut.println("<h2>Create MEJB</h2>");
0253: pOut.println("<ul>");
0254:
0255: // Connecting to the MEJB home through JNDI
0256: ManagementHome mgmtHome = null;
0257: try {
0258: mgmtHome = (ManagementHome) PortableRemoteObject.narrow(
0259: initialContext
0260: .lookup("java:comp/env/ejb/mgmt/MEJB"),
0261: ManagementHome.class);
0262: } catch (Exception e) {
0263: pOut
0264: .println("<li>Cannot lookup java:comp/env/ejb/mgmt/MEJB: "
0265: + e + "</li>");
0266: pOut.println("</ul>");
0267: return;
0268: }
0269:
0270: // Management bean creation
0271: Management mgmt = null;
0272:
0273: try {
0274: mgmt = mgmtHome.create();
0275: pOut.println("<li>MEJB created</li>");
0276: } catch (Exception e) {
0277: pOut.println("<li>Cannot create MEJB: " + e + "</li>");
0278: return;
0279: }
0280: pOut.println("</ul><br/>");
0281:
0282: pOut.println("<h2>Create DomainMEJB</h2>");
0283: pOut.println("<ul>");
0284:
0285: DomainManagementHome domainMgmtHome = null;
0286: try {
0287: domainMgmtHome = (DomainManagementHome) PortableRemoteObject
0288: .narrow(
0289: initialContext
0290: .lookup("java:comp/env/ejb/mgmt/DomainMEJB"),
0291: DomainManagementHome.class);
0292: } catch (Exception e) {
0293: pOut
0294: .println("<li>Cannot lookup java:comp/env/ejb/mgmt/DomainMEJB: "
0295: + e + "</li>");
0296: pOut.println("</ul>");
0297: //return;
0298: }
0299:
0300: // Domain management bean creation
0301: DomainManagement dmgmt = null;
0302:
0303: try {
0304: dmgmt = domainMgmtHome.create();
0305: pOut.println("<li>DomainMEJB created</li>");
0306: } catch (Exception e) {
0307: pOut
0308: .println("<li>Cannot create DomainMEJB: " + e
0309: + "</li>");
0310: return;
0311: }
0312: pOut.println("</ul><br/>");
0313:
0314: // ------------------------------
0315: // Access to the J2EEDomain MBean
0316: // ------------------------------
0317:
0318: ObjectName onDomain = accessJ2EEDomain(pDomainName, mgmt, pOut);
0319: if (onDomain == null) {
0320: return;
0321: }
0322:
0323: boolean master = checkIfMaster(onDomain, mgmt, pOut);
0324:
0325: // -----------------------------------------------------
0326: // Check for J2EEServer MBeans in the current J2EEDomain
0327: // -----------------------------------------------------
0328: ObjectName onServer = accessJ2EEServer(onDomain, mgmt, pOut);
0329: if (onServer == null) {
0330: return;
0331: }
0332:
0333: // ------------------------------
0334: // Test domain deployment
0335: // ------------------------------
0336: if (master) {
0337: pOut
0338: .println("<h2>Try to manage the current domain: deploy/undeploy on all servers in the domain</h2>");
0339: pOut.println("<ul>");
0340: pOut.println("<li>Use the J2EEDomain object name: \""
0341: + onDomain.toString() + "\"</li>");
0342: String[] serverNames = null;
0343: try {
0344: serverNames = (String[]) mgmt.getAttribute(onDomain,
0345: "serverNames");
0346: } catch (Exception e) {
0347: pOut.println("<li>Can't get serverNames attribute: "
0348: + e + "</li>");
0349: }
0350: if (serverNames.length > 1) {
0351: String fileName = null;
0352: // Use deployJar, unDeployJar
0353: // (suppose sb.jar was installed under JONAS_BASE/ejbjars directory)
0354: fileName = "sb.jar";
0355: deployModuleWithTarget(JAR, onDomain, serverNames,
0356: mgmt, fileName, pOut);
0357:
0358: // Use deployEar, unDeployEar
0359: // (suppose earsample.ear was installed under JONAS_BASE/apps directory)
0360: fileName = "earsample.ear";
0361: deployModuleWithTarget(EAR, onDomain, serverNames,
0362: mgmt, fileName, pOut);
0363: } else {
0364: pOut
0365: .println("<li>Can't find server names in this domain</li>");
0366: }
0367: pOut.println("</ul><br>");
0368: } else {
0369: // ------------------------------
0370: // Test current server management
0371: // ------------------------------
0372: pOut.println("<h2>Try to manage the current server</h2>");
0373: pOut.println("<ul>");
0374: pOut.println("<li>Using the J2EEServer object name: \""
0375: + onServer.toString() + "\"</li>");
0376:
0377: // Use deployJar, unDeployJar, isJarDeployed management opearations
0378: // (suppose sb.jar was installed under JONAS_BASE/ejbjars directory)
0379: String jarFileName = "sb.jar";
0380: deployJarModule(onServer, mgmt, jarFileName, pOut);
0381:
0382: // Use deployEar, unDeployEar, isEarDeployed management opearations
0383: // (suppose earsample.ear was installed under JONAS_BASE/apps directory)
0384: String earFileName = "earsample.ear";
0385: deployEarModule(onServer, mgmt, earFileName, pOut);
0386:
0387: // (suppose autoload/earsample.ear was installed under JONAS_BASE/apps directory)
0388: earFileName = "autoload/earsample.ear";
0389: deployEarModule(onServer, mgmt, earFileName, pOut);
0390:
0391: // Use deployRar, unDeployRar, isRarDeployed
0392: // (suppose JDBC connection rar files installed under JONAS_BASE/rars directory)
0393: /*
0394: String rarFileName = "autoload/JOnAS_jdbcCP.rar";
0395: deployRarModule(onServer, mgmt, rarFileName, pOut);
0396: */
0397: pOut.println("</ul><br>");
0398: }
0399: // -----------------------------
0400: // Test Event Listener support
0401: // -----------------------------
0402:
0403: pOut.println("<h2>Register an event listener</h2>");
0404: pOut.println("<ul>");
0405: if (mListener == null) {
0406: mListener = new MyListener();
0407: pOut.println("<li>MyListener created</li>");
0408: }
0409:
0410: // Get the ListenerRegistration object
0411: try {
0412: ListenerRegistration lr = mgmt.getListenerRegistry();
0413: if (lr != null) {
0414: pOut.println("<li>Add listener on J2EEServer ("
0415: + onServer.toString() + ")</li>");
0416: String sHandler = "MEJBTester";
0417: lr.addNotificationListener(onServer, mListener, null,
0418: sHandler);
0419: pOut.println("<li>Notification Listener added</li>");
0420: String sViewNotifications = pRequest.getRequestURI()
0421: + "?" + PARAM_DOMAIN + "=" + pDomainName + "&"
0422: + PARAM_VIEW + "=" + VIEW_NOTIFICATIONS;
0423: pOut
0424: .println("<a href=\""
0425: + sViewNotifications
0426: + "\" target=\"ViewNotifications\">See list of notifications</a>");
0427: pOut.println("</ul>");
0428: } else {
0429: pOut
0430: .println("<li>Can't add remote listener for the moment</li>");
0431: pOut.println("</ul>");
0432: }
0433: } catch (Exception e) {
0434: pOut.println("<li>Can't add notification listener on "
0435: + onServer.toString() + " : " + e + "</li>");
0436: }
0437:
0438: pOut.println("<h2>Application is OK </h2>");
0439:
0440: // Footer
0441: printNavigationFooter(pDomainName, pRequest, pOut, null,
0442: VIEW_OTHER);
0443: }
0444:
0445: /**
0446: * Header.
0447: * @param pOut Printer
0448: * @param pTitle Title to display
0449: * @param pSubTitle Subtitle to display or null if not
0450: */
0451: protected void printHeaderAutoRefresh(PrintWriter pOut,
0452: String pTitle, String pSubTitle) {
0453:
0454: pOut.println("<html>");
0455: pOut.println("<head>");
0456: pOut.println("<title>" + pTitle + "</title>");
0457: pOut.println("<script language=\"JavaScript\">");
0458: pOut
0459: .println("function startRefresh(){ setTimeout(\"refresh()\",30000); self.focus(); }");
0460: pOut.println("function refresh(){ window.location.reload(); }");
0461: pOut.println("</script>");
0462: pOut.println("</head>");
0463: pOut
0464: .println("<body onload=\"startRefresh()\" style=\"background : white; color : black;\">");
0465: printHeaderTitle(pOut, pTitle, pSubTitle);
0466: }
0467:
0468: /**
0469: * View notifications.
0470: * @param pOut Printer
0471: */
0472: protected void doViewNotifications(PrintWriter pOut) {
0473:
0474: // Header
0475: printHeaderAutoRefresh(pOut, APP_TITLE, "Notifications");
0476: pOut.println("<h2>List of notifications</h2>");
0477:
0478: // Verify listener
0479: if (mListener == null) {
0480: pOut.println("Listener not found !");
0481: } else {
0482: // Display notifications
0483: List list = mListener.getlistNotifications();
0484: if (list.size() > 0) {
0485: pOut.println("<ol>");
0486: for (int i = 0; i < list.size(); i++) {
0487: pOut.println("<li>" + list.get(i) + "</li>");
0488: }
0489: pOut.println("</ol>");
0490: } else {
0491: pOut.println("No notifications, the list is empty");
0492: }
0493: }
0494:
0495: // Footer
0496: printFooter(pOut);
0497: }
0498:
0499: /**
0500: * View example.
0501: * @param pDomainName Name of domain to access
0502: * @param pRequest Http request
0503: * @param pOut Printer
0504: */
0505: protected void doViewOther(String pDomainName,
0506: HttpServletRequest pRequest, PrintWriter pOut) {
0507:
0508: // Header
0509: printHeader(pOut, APP_TITLE, "Other");
0510:
0511: pOut.println("<h2>Other</h2>");
0512:
0513: // Footer
0514: printNavigationFooter(pDomainName, pRequest, pOut, VIEW_INIT,
0515: null);
0516: }
0517:
0518: /**
0519: * Create J2EEDomain MBean's ObjectName and test if MBean registered
0520: * @param pDomainName the name provided by the user
0521: * @param mgmt MEJB
0522: * @param pOut output stream
0523: * @return true if management operation succeeded
0524: */
0525: private ObjectName accessJ2EEDomain(String pDomainName,
0526: Management mgmt, PrintWriter pOut) {
0527: ObjectName onDomain = null;
0528: pOut.println("<h2>Access the J2EEDomain MBean</h2>");
0529: pOut.println("<ul>");
0530:
0531: // Get the J2EEDomain MBean's ObjectName
0532: try {
0533: String name = pDomainName + ":j2eeType=J2EEDomain,name="
0534: + pDomainName;
0535: onDomain = ObjectName.getInstance(name);
0536: pOut.println("<li>J2EEDomain object name created: \""
0537: + name.toString() + "\"</li>");
0538: } catch (Exception e) {
0539: pOut
0540: .println("<li>Cannot create object name for J2EEDomain managed object: "
0541: + e + "</li>");
0542: pOut.println("</ul>");
0543: return null;
0544: }
0545: // Check that the J2EEDomain MBean registered
0546: try {
0547: boolean exists = mgmt.isRegistered(onDomain);
0548: if (exists) {
0549: pOut
0550: .println("<li>Found this J2EEDomain MBean in the current MBean server</li>");
0551: } else {
0552: pOut
0553: .println("<li><b>Can't find this J2EEDomain MBean in the current MBean server</b></li>");
0554: pOut.println("</ul>");
0555: return null;
0556: }
0557: } catch (Exception e) {
0558: pOut.println("<li>Error when using this J2EEDomain MBean: "
0559: + e + "</li>");
0560: pOut.println("</ul>");
0561: return null;
0562: }
0563: return onDomain;
0564: }
0565:
0566: /**
0567: *
0568: * @param onDomain J2EEDomain MBean ObjectName
0569: * @param mgmt Management EJB
0570: * @param pOut output stream
0571: * @return true if the current server is a master
0572: */
0573: private boolean checkIfMaster(ObjectName onDomain, Management mgmt,
0574: PrintWriter pOut) {
0575: try {
0576: Boolean master = (Boolean) mgmt.getAttribute(onDomain,
0577: "master");
0578: if (master.booleanValue()) {
0579: pOut
0580: .println("<li>The current server is a management master</li>");
0581: }
0582: pOut.println("</ul>");
0583: return master.booleanValue();
0584: } catch (Exception e) {
0585: pOut.println("<li>Error when using this J2EEDomain MBean: "
0586: + e + "</li>");
0587: }
0588: pOut.println("</ul>");
0589: return false;
0590: }
0591:
0592: /**
0593: * Get a registered J2EEServer MBean
0594: * @param onDomain J2EEDomain MBean's ObjectName
0595: * @param mgmt MEJB
0596: * @param pOut output stream
0597: * @return true if management operation succeeded
0598: */
0599: private ObjectName accessJ2EEServer(ObjectName onDomain,
0600: Management mgmt, PrintWriter pOut) {
0601: pOut.println("<h2>Access the J2EEServer MBeans</h2>");
0602: pOut.println("<ul>");
0603: ObjectName onServer = null;
0604: String[] listServers = null;
0605: try {
0606: listServers = (String[]) mgmt.getAttribute(onDomain,
0607: "servers");
0608: } catch (Exception e) {
0609: pOut.println("<li>Cant' access the " + onDomain
0610: + " MBean's <i>servers</i> attribute</li>");
0611: pOut.println("</ul><br>");
0612: return null;
0613: }
0614: int nbServers = listServers.length;
0615: if (nbServers == 0) {
0616: pOut
0617: .println("<li>No J2EEServer MBeans in the "
0618: + onDomain
0619: + " MBean's <i>servers</i> list (problem with domain management !!)</li>");
0620: pOut.println("</ul><br>");
0621: return null;
0622: } else {
0623: if (nbServers == 1) {
0624: String serverOn = (String) listServers[0];
0625: try {
0626: onServer = ObjectName.getInstance(serverOn);
0627: if (mgmt.isRegistered(onServer)) {
0628: pOut
0629: .println("<li>Found one J2EEServer MBean registered in the current MBean server. Its OBJECT_NAME is: \""
0630: + serverOn + "\"</li>");
0631: pOut.println("</ul><br>");
0632: return onServer;
0633: } else {
0634: pOut
0635: .println("<li><b>Can't find the J2EEServer MBean having OBJECT_NAME: "
0636: + serverOn
0637: + " in the current MBean server</b></li>");
0638: pOut.println("</ul><br>");
0639: return null;
0640: }
0641: } catch (Exception e) {
0642: pOut.println("<li>Error witht OBJECT_NAME "
0643: + serverOn + ": " + e + "</li>");
0644: pOut.println("</ul><br>");
0645: return null;
0646: }
0647: } else {
0648: pOut.println("<li>List of J2EEServer MBeans in the \""
0649: + onDomain
0650: + "\" MBean's <i>servers</i> list:</li>");
0651: pOut.println("<ol>");
0652: for (int i = 0; i < nbServers; i++) {
0653: String serverOn = listServers[i];
0654: ObjectName on = null;
0655: try {
0656: on = ObjectName.getInstance(serverOn);
0657: } catch (MalformedObjectNameException e) {
0658: pOut.print("<li>Error witht OBJECT_NAME "
0659: + serverOn + ": " + e + "</li>");
0660: continue;
0661: }
0662: pOut.print("<li>\"" + serverOn + "\". ");
0663: boolean isRegisterdServer = false;
0664: try {
0665: isRegisterdServer = mgmt.isRegistered(on);
0666: } catch (RemoteException e) {
0667: pOut.println("<ul>");
0668: pOut.println("<li>Error witht OBJECT_NAME "
0669: + serverOn + ": " + e + "</li>");
0670: pOut.println("</ul>");
0671: pOut.println("</li>");
0672: continue;
0673: }
0674: if (isRegisterdServer) {
0675: pOut
0676: .print("This MBean is registered in the current MBean server. It should correspond to the current server.");
0677: pOut.println("</li>");
0678: onServer = on;
0679: } else {
0680: pOut
0681: .println("This MBean is not registered in the current MBean server. It could correspond to a remote server.");
0682: pOut.println("</li>");
0683: pOut.println("<ul>");
0684: String name = on.getKeyProperty("name");
0685: String[] signature = { "java.lang.String" };
0686: String[] params = new String[1];
0687: params[0] = name;
0688: String[] urls = null;
0689: try {
0690: urls = (String[]) mgmt.invoke(onDomain,
0691: "getConnectorServerURLs", params,
0692: signature);
0693: } catch (Exception e) {
0694: pOut
0695: .println("<li>Could not found a connector server URL for server "
0696: + name + ".</li>");
0697: pOut.println("</ul>");
0698: pOut.println("</li>");
0699: continue;
0700: }
0701: if (urls == null) {
0702: pOut
0703: .println("<li>Could not found a connector server URL for server "
0704: + name + "</li>");
0705: pOut.println("</ul>");
0706: pOut.println("</li>");
0707: continue;
0708: }
0709: for (int j = 0; j < urls.length; j++) {
0710: String url = urls[j];
0711: pOut.print("<li>Try to connect to server "
0712: + name
0713: + " using connector server URL: "
0714: + url + "</li>");
0715: MBeanServerConnection connection = createConnection(url);
0716: if (connection != null) {
0717: // Use connection directly instead of using MEJB
0718: try {
0719: if (connection.isRegistered(on)) {
0720: pOut
0721: .print("<li>Found MBean having OBJECT_NAME \""
0722: + serverOn
0723: + "\" registered in the connected MBean server.");
0724: } else {
0725: pOut
0726: .print("<li>The MBean having OBJECT_NAME \""
0727: + serverOn
0728: + "\" is not registered in the connected MBean server.");
0729: }
0730: } catch (IOException ioe) {
0731: pOut
0732: .print("<li>Exception when trying to use connection: "
0733: + ioe.toString());
0734: }
0735: } else {
0736: pOut
0737: .print("<li>Could not establish connection with server "
0738: + name
0739: + ". Server may be stopped or URL not valid.</li>");
0740: }
0741: }
0742: pOut.println("</ul>");
0743: pOut.println("</li>");
0744: }
0745: }
0746: pOut.println("</ol>");
0747: pOut.println("</ul><br>");
0748: return onServer;
0749: }
0750: }
0751: }
0752:
0753: private MBeanServerConnection createConnection(
0754: String connectorServerURL) {
0755: MBeanServerConnection connection;
0756: // create a connector client for the connector server at the given url
0757: JMXConnector connector = null;
0758: try {
0759: JMXServiceURL url = new JMXServiceURL(connectorServerURL);
0760: connector = JMXConnectorFactory.newJMXConnector(url, null);
0761: connector.connect(null);
0762: connection = connector.getMBeanServerConnection();
0763: } catch (MalformedURLException e) {
0764: // there is no provider for the protocol in url
0765: connection = null;
0766: } catch (IOException e) {
0767: // connector client or connection cannot be made because of a communication problem.
0768: connection = null;
0769: } catch (java.lang.SecurityException e) {
0770: // connection cannot be made for security reasons
0771: connection = null;
0772: }
0773: return connection;
0774: }
0775:
0776: /**
0777: * Deploy if not already deployed, undeploy otherwise, a given jar file.
0778: * @param onServer ObjectName of the J2EEServer MBean on which the mananagement operation has to be applied
0779: * @param mgmt MEJB
0780: * @param jarFileName name of the file to be deployed/undeployed
0781: * @param pOut output stream
0782: */
0783: private void deployJarModule(ObjectName onServer, Management mgmt,
0784: String jarFileName, PrintWriter pOut) {
0785: String[] signature = { "java.lang.String" };
0786: String[] params = new String[1];
0787: params[0] = jarFileName;
0788: pOut.println("<li>Test if " + jarFileName + " deployed</li>");
0789: boolean isDeployed = false;
0790: try {
0791: isDeployed = ((Boolean) mgmt.invoke(onServer,
0792: "isJarDeployed", params, signature)).booleanValue();
0793: } catch (Exception e) {
0794: pOut
0795: .println("<li>Problem when invoking isJarDeployed management operation: "
0796: + e.toString());
0797: }
0798: if (!isDeployed) {
0799: pOut
0800: .println("<li>Try to deploy " + jarFileName
0801: + " !</li>");
0802: try {
0803: String deployedObjectName = (String) mgmt.invoke(
0804: onServer, "deployJar", params, signature);
0805: pOut
0806: .println("<li>The Object Name of the deployed J2EEModule is: \""
0807: + deployedObjectName + "\". </li>");
0808: } catch (MBeanException mbe) {
0809: pOut.println("<li>Could not deploy " + jarFileName
0810: + " because of exception: "
0811: + mbe.getTargetException().toString());
0812: } catch (Exception e) {
0813: pOut.println("<li>Could not deploy " + jarFileName
0814: + " because of exception: " + e.toString()
0815: + " </li>");
0816: }
0817: } else {
0818: pOut.println("<li>Try to un-deploy " + jarFileName
0819: + " !</li>");
0820: try {
0821: mgmt.invoke(onServer, "unDeployJar", params, signature);
0822: pOut.println("<li>Done undeploy.>");
0823: } catch (MBeanException mbe) {
0824: pOut.println("<li>Could not undeploy " + jarFileName
0825: + " because of exception: "
0826: + mbe.getTargetException().toString());
0827: } catch (Exception e) {
0828: pOut.println("<li>Could not undeploy " + jarFileName
0829: + " because of exception: " + e.toString()
0830: + " </li>");
0831: }
0832: }
0833: }
0834:
0835: /**
0836: * Deploy if not already deployed, undeploy otherwise, a given ear file.
0837: * @param onServer ObjectName of the J2EEServer MBean on which the mananagement operation has to be applied
0838: * @param mgmt MEJB
0839: * @param earFileName name of the file to be deployed/undeployed
0840: * @param pOut output stream
0841: */
0842: private void deployEarModule(ObjectName onServer, Management mgmt,
0843: String earFileName, PrintWriter pOut) {
0844: String[] signature = { "java.lang.String" };
0845: String[] params = new String[1];
0846: params[0] = earFileName;
0847: pOut.println("<li>Test if " + earFileName + " deployed</li>");
0848: boolean isDeployed = false;
0849: try {
0850: isDeployed = ((Boolean) mgmt.invoke(onServer,
0851: "isEarDeployed", params, signature)).booleanValue();
0852: } catch (Exception e) {
0853: pOut
0854: .println("<li>Problem when invoking isEarDeployed management operation: "
0855: + e.toString());
0856: }
0857: if (!isDeployed) {
0858: pOut
0859: .println("<li>Try to deploy " + earFileName
0860: + " !</li>");
0861: try {
0862: String deployedObjectName = (String) mgmt.invoke(
0863: onServer, "deployEar", params, signature);
0864: pOut
0865: .println("<li>The Object Name of the deployed J2EEModule is: \""
0866: + deployedObjectName + "\". </li>");
0867: } catch (MBeanException mbe) {
0868: pOut.println("<li>Could not deploy " + earFileName
0869: + " because of exception: "
0870: + mbe.getTargetException().toString());
0871: } catch (Exception e) {
0872: pOut.println("<li>Could not deploy " + earFileName
0873: + " because of exception: " + e.toString()
0874: + " </li>");
0875: }
0876: } else {
0877: pOut.println("<li>Try to un-deploy " + earFileName
0878: + " !</li>");
0879: try {
0880: mgmt.invoke(onServer, "unDeployEar", params, signature);
0881: pOut.println("<li>Done undeploy.>");
0882: } catch (MBeanException mbe) {
0883: pOut.println("<li>Could not undeploy " + earFileName
0884: + " because of exception: "
0885: + mbe.getTargetException().toString());
0886: } catch (Exception e) {
0887: pOut.println("<li>Could not undeploy " + earFileName
0888: + " because of exception: " + e.toString()
0889: + " </li>");
0890: }
0891: }
0892: }
0893:
0894: /**
0895: * Deploy if not already deployed, undeploy otherwise, a given rar file.
0896: * @param onServer ObjectName of the J2EEServer MBean on which the mananagement operation has to be applied
0897: * @param mgmt MEJB
0898: * @param rarFileName name of the file to be deployed/undeployed
0899: * @param pOut output stream
0900: */
0901: private void deployRarModule(ObjectName onServer, Management mgmt,
0902: String rarFileName, PrintWriter pOut) {
0903: String[] signature = { "java.lang.String" };
0904: String[] params = new String[1];
0905: params[0] = rarFileName;
0906: pOut.println("<li>Test if " + rarFileName + " deployed</li>");
0907: boolean isDeployed = false;
0908: try {
0909: isDeployed = ((Boolean) mgmt.invoke(onServer,
0910: "isRarDeployed", params, signature)).booleanValue();
0911: } catch (Exception e) {
0912: pOut
0913: .println("<li>Problem when invoking isRarDeployed management operation: "
0914: + e.toString());
0915: }
0916: if (!isDeployed) {
0917: pOut
0918: .println("<li>Try to deploy " + rarFileName
0919: + " !</li>");
0920: try {
0921: String deployedObjectName = (String) mgmt.invoke(
0922: onServer, "deployRar", params, signature);
0923: pOut
0924: .println("<li>The Object Name of the deployed J2EEModule is: \""
0925: + deployedObjectName + "\". </li>");
0926: } catch (MBeanException mbe) {
0927: pOut.println("<li>Could not deploy " + rarFileName
0928: + " because of exception: "
0929: + mbe.getTargetException().toString());
0930: } catch (Exception e) {
0931: pOut.println("<li>Could not deploy " + rarFileName
0932: + " because of exception: " + e.toString()
0933: + " </li>");
0934: }
0935: } else {
0936: pOut.println("<li>Try to un-deploy " + rarFileName
0937: + " !</li>");
0938: try {
0939: mgmt.invoke(onServer, "unDeployRar", params, signature);
0940: pOut.println("<li>Done undeploy.>");
0941: } catch (MBeanException mbe) {
0942: pOut.println("<li>Could not undeploy " + rarFileName
0943: + " because of exception: "
0944: + mbe.getTargetException().toString());
0945: } catch (Exception e) {
0946: pOut.println("<li>Could not undeploy " + rarFileName
0947: + " because of exception: " + e.toString()
0948: + " </li>");
0949: }
0950: }
0951: }
0952:
0953: /**
0954: * Deploy if not already deployed, undeploy otherwise, a given jar file.
0955: * @param moduleType can be JAR, WAR, RAR or EAR
0956: * @param onDomain J2EEDomain MBean ObjectName
0957: * @param target names of the servers on which the mananagement operation has to be applied
0958: * @param mgmt MEJB
0959: * @param fileName name of the file to be deployed/undeployed
0960: * @param pOut output stream
0961: */
0962: private void deployModuleWithTarget(String moduleType,
0963: ObjectName onDomain, String[] target, Management mgmt,
0964: String fileName, PrintWriter pOut) {
0965: String[] signature = { "[Ljava.lang.String;",
0966: "java.lang.String" };
0967: Object[] params = new Object[2];
0968: params[0] = target;
0969: params[1] = fileName;
0970: String operationNameSuf = null;
0971: if (moduleType.equals(JAR)) {
0972: operationNameSuf = "eployJar";
0973: } else if (moduleType.equals(WAR)) {
0974: operationNameSuf = "eployWar";
0975: } else if (moduleType.equals(RAR)) {
0976: operationNameSuf = "eployWar";
0977: } else if (moduleType.equals(EAR)) {
0978: operationNameSuf = "eployEar";
0979: } else {
0980: return;
0981: }
0982: try {
0983: String operationName = "d" + operationNameSuf;
0984: pOut.println("<li>Try to " + operationName + " " + fileName
0985: + " on multiple target !</li>");
0986: mgmt.invoke(onDomain, operationName, params, signature);
0987: operationName = "unD" + operationNameSuf;
0988: pOut.println("<li>Try to " + operationName + " " + fileName
0989: + " on multiple target !</li>");
0990: mgmt.invoke(onDomain, operationName, params, signature);
0991: } catch (MBeanException mbe) {
0992: pOut.println("<li>Could not deploy/undeploy " + fileName
0993: + " because of exception: "
0994: + mbe.getTargetException().toString());
0995: } catch (Exception e) {
0996: pOut.println("<li>Could not deploy " + fileName
0997: + " because of exception: " + e.toString()
0998: + " </li>");
0999: }
1000: }
1001: }
|