001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 2006 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: ClustMgmtServlet.java 9886 2006-12-04 09:56:04Z danesa $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.j2eemanagement.servlets;
025:
026: //import java
027: import java.io.IOException;
028: import java.io.PrintWriter;
029:
030: import javax.management.ObjectName;
031: import javax.management.j2ee.Management;
032: import javax.servlet.ServletConfig;
033: import javax.servlet.ServletException;
034: import javax.servlet.http.HttpServletRequest;
035: import javax.servlet.http.HttpServletResponse;
036:
037: /**
038: * This servlet is an example which shows how to access the MEJB from a servlet.
039: * @author JOnAS team
040: * @author Adriana Danes
041: */
042: public class ClustMgmtServlet extends J2eemanagementBaseServlet {
043:
044: // ---------------------------------------------------------- Constants
045: /** Parameter */
046: static final String PARAM_DOMAIN = "domainName";
047: /** Parameter */
048: static final String PARAM_CLUSTER = "clusterName";
049: /** Parameter */
050: static final String PARAM_VIEW = "view";
051: /** Parameter */
052: static final String VIEW_INIT = "init";
053:
054: // ---------------------------------------------------------- Public methods
055:
056: /**
057: * Initialize the servlet.
058: * @param pConfig See HttpServlet
059: * @throws ServletException Could not execute request
060: */
061: public void init(ServletConfig pConfig) throws ServletException {
062: super .init(pConfig);
063: }
064:
065: // ---------------------------------------------------------- Protected
066: // methods
067:
068: /**
069: * Response to the GET request.
070: * @param pRequest See HttpServlet
071: * @param pResponse See HttpServlet
072: * @throws IOException An input or output error is detected when the servlet handles the request
073: * @throws ServletException Could not execute request
074: */
075: protected void doGet(HttpServletRequest pRequest,
076: HttpServletResponse pResponse) throws IOException,
077: ServletException {
078: dispatch(pRequest, pResponse);
079: }
080:
081: /**
082: * Response to the POST request.
083: * @param pRequest See HttpServlet
084: * @param pResponse See HttpServlet
085: * @throws IOException An input or output error is detected when the servlet handles the request
086: * @throws ServletException Could not execute request
087: */
088: protected void doPost(HttpServletRequest pRequest,
089: HttpServletResponse pResponse) throws IOException,
090: ServletException {
091: dispatch(pRequest, pResponse);
092: }
093:
094: /**
095: * Dispatch the response.
096: * @param pRequest Request
097: * @param pResponse Response
098: * @throws IOException An input or output error is detected when the servlet handles the request
099: */
100: protected void dispatch(HttpServletRequest pRequest,
101: HttpServletResponse pResponse) throws IOException {
102:
103: pResponse.setContentType("text/html");
104: PrintWriter out = pResponse.getWriter();
105:
106: // Get parameters
107: String sParamDomain = pRequest.getParameter(PARAM_DOMAIN);
108: String sParamCluster = pRequest.getParameter(PARAM_CLUSTER);
109: String sParamView = pRequest.getParameter(PARAM_VIEW);
110:
111: // Dispatching
112: if ((sParamDomain == null) || (sParamDomain.length() == 0)) {
113: doViewError("Parameter <i>Domain name</i> not found",
114: pRequest, out);
115: } else if ((sParamCluster == null)
116: || (sParamCluster.length() == 0)) {
117: doViewError("Parameter <i>Cluster name</i> not found",
118: pRequest, out);
119: } else if ((sParamView == null) || (sParamView.length() == 0)
120: || VIEW_INIT.equals(sParamView)) {
121: doViewInit(pRequest, out);
122: doViewManagement(sParamDomain, sParamCluster, pRequest, out);
123: } else {
124: doViewError("Unknown View", pRequest, out);
125: }
126:
127: }
128:
129: /**
130: * Do management opeartions in this view.
131: * @param pDomainName Name of domain to access
132: * @param pClusterName Name of the cluster to manage in the domain
133: * @param pRequest Http request
134: * @param pOut Printer
135: */
136: protected void doViewManagement(String pDomainName,
137: String pClusterName, HttpServletRequest pRequest,
138: PrintWriter pOut) {
139: Management mgmt = getMgmt();
140:
141: // ------------------------------
142: // Access to the J2EEDomain MBean
143: // ------------------------------
144: ObjectName onDomain = accessJ2EEDomain(pDomainName, mgmt, pOut);
145: if (onDomain == null) {
146: return;
147: }
148:
149: // Create a cluster in the domain
150: // -------------------------------
151: ObjectName onCluster = createCluster(onDomain, pClusterName,
152: mgmt, pOut);
153: if (onCluster == null) {
154: return;
155: }
156:
157: // -----------------------------------------------------
158: // Using the domain management EJB to list servers and clusters
159: // -----------------------------------------------------
160: String[] serverNames = null;
161: String[] serverNamesDom = null;
162: String[] servers = null;
163: String[] clusters = null;
164: try {
165:
166: pOut.println("<h2>Getting the list of cluster MBeans</h2>");
167: clusters = (String[]) mgmt.getAttribute(onDomain,
168: "clusters");
169: pOut.println("<ul>");
170: for (int i = 0; i < clusters.length; i++) {
171: pOut.println("<li>" + clusters[i] + "</li>");
172: }
173: pOut.println("</ul>");
174: } catch (Exception e) {
175: pOut
176: .println("<li>Could not make MEJB list servers or clusters. </li>"
177: + e);
178: e.printStackTrace(pOut);
179: return;
180: }
181:
182: pOut.println("<h2>Application is OK </h2>");
183:
184: // Footer
185: printNavigationFooter(pRequest, pOut);
186: }
187:
188: /**
189: * Create J2EEDomain MBean's ObjectName and test if MBean registered
190: * @param pDomainName the name provided by the user
191: * @param mgmt MEJB
192: * @param pOut output stream
193: * @return true if management operation succeeded
194: */
195: private ObjectName accessJ2EEDomain(String pDomainName,
196: Management mgmt, PrintWriter pOut) {
197: ObjectName onDomain = null;
198: pOut.println("<h2>Access the J2EEDomain MBean</h2>");
199: pOut.println("<ul>");
200:
201: // Get the J2EEDomain MBean's ObjectName
202: try {
203: String name = pDomainName + ":j2eeType=J2EEDomain,name="
204: + pDomainName;
205: onDomain = ObjectName.getInstance(name);
206: pOut.println("<li>J2EEDomain object name \""
207: + name.toString() + "\" created.</li>");
208: } catch (Exception e) {
209: pOut
210: .println("<li>Cannot create object name for J2EEDomain managed object: "
211: + e + "</li>");
212: pOut.println("</ul>");
213: return null;
214: }
215: // Check that the J2EEDomain MBean registered
216: try {
217: boolean exists = mgmt.isRegistered(onDomain);
218: if (exists) {
219: pOut
220: .println("<li>Found this J2EEDomain MBean in the current MBean server</li>");
221: pOut.println("</ul><br/>");
222: } else {
223: pOut
224: .println("<li><b>Can't find this J2EEDomain MBean in the current MBean server</b></li>");
225: pOut.println("</ul>");
226: return null;
227: }
228: } catch (Exception e) {
229: pOut.println("<li>Error when using this J2EEDomain MBean: "
230: + e + "</li>");
231: pOut.println("</ul>");
232: return null;
233: }
234: return onDomain;
235: }
236:
237: private ObjectName createCluster(ObjectName pOnDomain,
238: String pClusterName, Management mgmt, PrintWriter pOut) {
239: ObjectName on = null;
240: String domainName = pOnDomain.getDomain();
241: pOut.println("<h2>Create cluster " + pClusterName
242: + " in domain " + domainName + "</h2>");
243: pOut.println("<ul>");
244: try {
245: String[] signature = new String[1];
246: signature[0] = "java.lang.String";
247: Object[] params = new Object[1];
248: params[0] = pClusterName;
249: String clusterOn = (String) mgmt.invoke(pOnDomain,
250: "createCluster", params, signature);
251: on = ObjectName.getInstance(clusterOn);
252: pOut.println("<li>Cluster " + pClusterName
253: + " created.</li>");
254: pOut.println("</ul><br/>");
255: } catch (Exception e) {
256: pOut.println("<li>Cannot create cluster " + pClusterName
257: + ": " + e + "</li>");
258: pOut.println("</ul>");
259: }
260: return on;
261: }
262: }
|