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: NewServerMgmtServlet.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: import java.net.MalformedURLException;
030:
031: import javax.management.MBeanServerConnection;
032: import javax.management.MalformedObjectNameException;
033: import javax.management.ObjectName;
034: import javax.management.j2ee.Management;
035: import javax.management.remote.JMXConnector;
036: import javax.management.remote.JMXConnectorFactory;
037: import javax.management.remote.JMXServiceURL;
038: import javax.servlet.ServletConfig;
039: import javax.servlet.ServletException;
040: import javax.servlet.http.HttpServletRequest;
041: import javax.servlet.http.HttpServletResponse;
042:
043: /**
044: * This servlet is an example which shows how to access the MEJB from a servlet.
045: * @author JOnAS team
046: * @author Adriana Danes
047: */
048: public class NewServerMgmtServlet extends J2eemanagementBaseServlet {
049:
050: // ---------------------------------------------------------- Constants
051: /** Parameter */
052: static final String PARAM_DOMAIN = "domainName";
053: /** Parameter */
054: static final String PARAM_CLUSTER = "clusterName";
055: /** Parameter */
056: static final String PARAM_SERVER = "serverName";
057: /** Parameter */
058: static final String PARAM_SERVER_URL = "serverURL";
059: /** Parameter */
060: static final String PARAM_VIEW = "view";
061: /** Parameter */
062: static final String VIEW_INIT = "init";
063:
064: // ---------------------------------------------------------- Public methods
065:
066: /**
067: * Initialize the servlet.
068: * @param pConfig See HttpServlet
069: * @throws ServletException
070: */
071: public void init(ServletConfig pConfig) throws ServletException {
072: super .init(pConfig);
073: }
074:
075: // ---------------------------------------------------------- Protected
076: // methods
077:
078: /**
079: * Response to the GET request.
080: * @param pRequest See HttpServlet
081: * @param pResponse See HttpServlet
082: * @throws IOException
083: * @throws ServletException
084: */
085: protected void doGet(HttpServletRequest pRequest,
086: HttpServletResponse pResponse) throws IOException,
087: ServletException {
088: dispatch(pRequest, pResponse);
089: }
090:
091: /**
092: * Response to the POST request.
093: * @param pRequest See HttpServlet
094: * @param pResponse See HttpServlet
095: * @throws IOException
096: * @throws ServletException
097: */
098: protected void doPost(HttpServletRequest pRequest,
099: HttpServletResponse pResponse) throws IOException,
100: ServletException {
101: dispatch(pRequest, pResponse);
102: }
103:
104: /**
105: * Dispatch the response.
106: * @param pRequest Request
107: * @param pResponse Response
108: * @throws IOException
109: */
110: protected void dispatch(HttpServletRequest pRequest,
111: HttpServletResponse pResponse) throws IOException {
112:
113: pResponse.setContentType("text/html");
114: PrintWriter out = pResponse.getWriter();
115:
116: // Get parameters
117: String sParamDomain = pRequest.getParameter(PARAM_DOMAIN);
118: String sParamCluster = pRequest.getParameter(PARAM_CLUSTER);
119: String sParamView = pRequest.getParameter(PARAM_VIEW);
120: String sParamServer = pRequest.getParameter(PARAM_SERVER);
121: String sParamServerURL = pRequest
122: .getParameter(PARAM_SERVER_URL);
123:
124: // Dispatching
125: if ((sParamDomain == null) || (sParamDomain.length() == 0)) {
126: doViewError("Parameter <i>Domain name</i> not found",
127: pRequest, out);
128: } else if ((sParamCluster == null)
129: || (sParamCluster.length() == 0)) {
130: doViewError("Parameter <i>Cluster name</i> not found",
131: pRequest, out);
132: } else if ((sParamServer == null)
133: || (sParamServer.length() == 0)) {
134: doViewError("Parameter <i>Server name</i> not found",
135: pRequest, out);
136: } else if ((sParamServerURL == null)
137: || (sParamServerURL.length() == 0)) {
138: doViewError(
139: "Parameter <i>Connector server url</i> not found",
140: pRequest, out);
141: } else if ((sParamView == null) || (sParamView.length() == 0)
142: || VIEW_INIT.equals(sParamView)) {
143: doViewInit(pRequest, out);
144: doViewManagement(sParamDomain, sParamCluster, sParamServer,
145: sParamServerURL, pRequest, out);
146: } else {
147: doViewError("Unknown View", pRequest, out);
148: }
149:
150: }
151:
152: /**
153: * Do management operations in this view.
154: * @param pDomainName Name of domain to access
155: * @param pClusterName Name of the cluster to manage in the domain
156: * @param pServerName Name of the server to add in the domain
157: * @param pServerURL Address of the server's remote JMX connector
158: * @param pRequest Http request
159: * @param pOut Printer
160: */
161: protected void doViewManagement(String pDomainName,
162: String pClusterName, String pServerName, String pServerURL,
163: HttpServletRequest pRequest, PrintWriter pOut) {
164: Management mgmt = getMgmt();
165:
166: // ------------------------------
167: // Access to the J2EEDomain MBean
168: // ------------------------------
169: ObjectName onDomain = accessJ2EEDomain(pDomainName, mgmt, pOut);
170: if (onDomain == null) {
171: return;
172: }
173: // TO DO extend interface in order to read cluster daemon name
174: String clusterDaemonName = "cd";
175: // Create the new server
176: createServer(onDomain, pClusterName, clusterDaemonName,
177: pServerName, pServerURL, mgmt, pOut);
178:
179: // -------------------------------------------------------------
180: // Using the domain management EJB to list servers in the domain
181: // -------------------------------------------------------------
182: String[] serverNames = null;
183: String[] serverNamesDom = null;
184: String[] servers = null;
185: String domainName = onDomain.getKeyProperty("name");
186: try {
187: pOut.println("<h2>Getting list of servers in the domain "
188: + domainName + "</h2>");
189: serverNames = (String[]) mgmt.getAttribute(onDomain,
190: "serverNames");
191: servers = (String[]) mgmt.getAttribute(onDomain, "servers");
192: pOut.println("<ul>");
193: for (int i = 0; i < serverNames.length; i++) {
194: String name = serverNames[i];
195: /*
196: String[] signature = {"java.lang.String"};
197: String[] params = {name};
198: String state = (String) mgmt.invoke(onDomain, "getServerState", params, signature);
199: pOut.println("<li>" + name + " - " + state + "</li>");*/
200: pOut.println("<li>" + name + "</li>");
201: }
202: pOut.println("</ul>");
203:
204: } catch (Exception e) {
205: pOut.println("<li>Could not get list of servers in "
206: + domainName + "</li>" + e);
207: e.printStackTrace(pOut);
208: return;
209: }
210: // -------------------------------------------------------------
211: // Using the cluster management EJBs to list servers in clusters
212: // -------------------------------------------------------------
213: String[] clusters = null;
214: String clusterName = null;
215: try {
216: clusters = (String[]) mgmt.getAttribute(onDomain,
217: "clusters");
218: for (int i = 0; i < clusters.length; i++) {
219: ObjectName onCluster = ObjectName
220: .getInstance(clusters[i]);
221: clusterName = onCluster.getKeyProperty("name");
222: if (!domainName.equals(clusterName)) {
223: // this is a cluster created by the administrator
224: // or a physical cluster created transparently
225: pOut
226: .println("<h2>Getting list of servers in cluster "
227: + clusterName + "</h2>");
228: String[] signature = { "java.lang.String" };
229: String[] params = new String[1];
230: params[0] = clusterName;
231: String[] urls = null;
232: serverNames = (String[]) mgmt.invoke(onDomain,
233: "getServerNames", params, signature);
234: pOut.println("<ul>");
235: if (serverNames.length == 0) {
236: pOut.println("<li>There are no servers.</li>");
237: }
238: for (int j = 0; j < serverNames.length; j++) {
239: String name = serverNames[j];
240: /*
241: String[] signature = {"java.lang.String"};
242: String[] params = {name};
243: String state = (String) mgmt.invoke(onCluster, "getServerState", params, signature);
244: pOut.println("<li>" + name + " - " + state + "</li>");
245: */
246: pOut.println("<li>" + name + "</li>");
247: }
248: pOut.println("</ul>");
249: }
250: }
251: } catch (Exception e) {
252: pOut
253: .println("<li>Could not get list of servers in clusters with the MEJB. </li>"
254: + e);
255: e.printStackTrace(pOut);
256: return;
257: }
258:
259: pOut.println("<h2>Application is OK </h2>");
260:
261: // Footer
262: printNavigationFooter(pRequest, pOut);
263: }
264:
265: /**
266: * Create J2EEDomain MBean's ObjectName and test if MBean registered
267: * @param pDomainName the name provided by the user
268: * @param mgmt MEJB
269: * @param pOut output stream
270: * @return true if management operation succeeded
271: */
272: private ObjectName accessJ2EEDomain(String pDomainName,
273: Management mgmt, PrintWriter pOut) {
274: ObjectName onDomain = null;
275: pOut.println("<h2>Access the J2EEDomain MBean</h2>");
276: pOut.println("<ul>");
277:
278: // Get the J2EEDomain MBean's ObjectName
279: try {
280: String name = pDomainName + ":j2eeType=J2EEDomain,name="
281: + pDomainName;
282: onDomain = ObjectName.getInstance(name);
283: pOut.println("<li>J2EEDomain object name created: \""
284: + name.toString() + "\"</li>");
285: } catch (Exception e) {
286: pOut
287: .println("<li>Cannot create object name for J2EEDomain managed object: "
288: + e + "</li>");
289: pOut.println("</ul>");
290: return null;
291: }
292: // Check that the J2EEDomain MBean registered
293: try {
294: boolean exists = mgmt.isRegistered(onDomain);
295: if (exists) {
296: pOut
297: .println("<li>Found this J2EEDomain MBean in the current MBean server</li>");
298: pOut.println("</ul>");
299: } else {
300: pOut
301: .println("<li><b>Can't find this J2EEDomain MBean in the current MBean server</b></li>");
302: pOut.println("</ul>");
303: return null;
304: }
305: } catch (Exception e) {
306: pOut.println("<li>Error when using this J2EEDomain MBean: "
307: + e + "</li>");
308: pOut.println("</ul>");
309: return null;
310: }
311: return onDomain;
312: }
313:
314: /**
315: * Add a new server to the domain and possibly attach it to a cluster
316: * @param pOnDomain J2EDomain ObjectName
317: * @param pClusterName cluster name
318: * @param pServerName server name
319: * @param pServerURL server's JMX remote connection urls
320: * @param mgmt MEJB
321: * @param pOut output stream
322: */
323: private void createServer(ObjectName pOnDomain,
324: String pClusterName, String pClusterDaemonName,
325: String pServerName, String pServerURL, Management mgmt,
326: PrintWriter pOut) {
327: String domainName = pOnDomain.getDomain();
328: try {
329: pOut.println("<h2>Add a server named " + pServerName
330: + " to the domain " + domainName + "</h2>");
331: pOut.println("<ul>");
332:
333: String[] signature = new String[4];
334: Object[] params = new Object[4];
335: signature[0] = "java.lang.String";
336: signature[1] = "java.lang.String";
337: signature[2] = "java.lang.String";
338: signature[3] = "[Ljava.lang.String;";
339: params[0] = pServerName;
340: params[1] = pClusterName;
341: params[2] = pClusterDaemonName;
342: String[] urls = new String[1];
343: urls[0] = pServerURL;
344: params[3] = urls;
345: mgmt.invoke(pOnDomain, "addServer", params, signature);
346: pOut.println("</ul>");
347: } catch (Exception e) {
348: pOut.println("<li>Cannot add server " + pServerName + ": "
349: + e + "</li>");
350: pOut.println("</ul>");
351: }
352: }
353: }
|