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: $
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 ClusterDaemonServlet extends J2eemanagementBaseServlet {
043:
044: // ---------------------------------------------------------- Constants
045: /** Parameter */
046: static final String PARAM_DOMAIN = "domainName";
047: /** Parameter */
048: static final String PARAM_DAEMON = "daemonName";
049: /** Parameter which gives the name of the server to be started by the daemon */
050: static final String PARAM_SERVER = "serverName";
051: /** Parameter */
052: static final String PARAM_VIEW = "view";
053: /** Parameter */
054: static final String VIEW_INIT = "init";
055:
056: // ---------------------------------------------------------- Public methods
057:
058: /**
059: * Initialize the servlet.
060: * @param pConfig See HttpServlet
061: * @throws ServletException Could not execute request
062: */
063: public void init(ServletConfig pConfig) throws ServletException {
064: super .init(pConfig);
065: }
066:
067: // ---------------------------------------------------------- Protected
068: // methods
069:
070: /**
071: * Response to the GET request.
072: * @param pRequest See HttpServlet
073: * @param pResponse See HttpServlet
074: * @throws IOException An input or output error is detected when the servlet handles the request
075: * @throws ServletException Could not execute request
076: */
077: protected void doGet(HttpServletRequest pRequest,
078: HttpServletResponse pResponse) throws IOException,
079: ServletException {
080: dispatch(pRequest, pResponse);
081: }
082:
083: /**
084: * Response to the POST request.
085: * @param pRequest See HttpServlet
086: * @param pResponse See HttpServlet
087: * @throws IOException An input or output error is detected when the servlet handles the request
088: * @throws ServletException Could not execute request
089: */
090: protected void doPost(HttpServletRequest pRequest,
091: HttpServletResponse pResponse) throws IOException,
092: ServletException {
093: dispatch(pRequest, pResponse);
094: }
095:
096: /**
097: * Dispatch the response.
098: * @param pRequest Request
099: * @param pResponse Response
100: * @throws IOException An input or output error is detected when the servlet handles the request
101: */
102: protected void dispatch(HttpServletRequest pRequest,
103: HttpServletResponse pResponse) throws IOException {
104:
105: pResponse.setContentType("text/html");
106: PrintWriter out = pResponse.getWriter();
107:
108: // Get parameters
109: String sParamDomain = pRequest.getParameter(PARAM_DOMAIN);
110: String sParamDaemon = pRequest.getParameter(PARAM_DAEMON);
111: String sParamServer = pRequest.getParameter(PARAM_SERVER);
112: String sParamView = pRequest.getParameter(PARAM_VIEW);
113:
114: // Dispatching
115: if ((sParamDomain == null) || (sParamDomain.length() == 0)) {
116: doViewError("Parameter <i>Domain name</i> not found",
117: pRequest, out);
118: } else if ((sParamDaemon == null)
119: || (sParamDaemon.length() == 0)) {
120: doViewError("Parameter <i>Daemon name</i> not found",
121: pRequest, out);
122: } else if ((sParamServer == null)
123: || (sParamServer.length() == 0)) {
124: doViewError("Parameter <i>Server name</i> not found",
125: pRequest, out);
126: } else if ((sParamView == null) || (sParamView.length() == 0)
127: || VIEW_INIT.equals(sParamView)) {
128: doViewInit(pRequest, out);
129: doViewManagement(sParamDomain, sParamDaemon, sParamServer,
130: pRequest, out);
131: } else {
132: doViewError("Unknown View", pRequest, out);
133: }
134:
135: }
136:
137: /**
138: * Do management opeartions in this view.
139: * @param pDomainName Name of domain to access
140: * @param pDaemonName Name of the deamon
141: * @param pServerName Name of the server to be started by the daemon
142: * @param pRequest Http request
143: * @param pOut Printer
144: */
145: protected void doViewManagement(String pDomainName,
146: String pDaemonName, String pServerName,
147: HttpServletRequest pRequest, PrintWriter pOut) {
148: Management mgmt = getMgmt();
149:
150: // ------------------------------
151: // Access to the J2EEDomain MBean
152: // ------------------------------
153: ObjectName onDomain = accessJ2EEDomain(pDomainName, mgmt, pOut);
154: if (onDomain == null) {
155: return;
156: }
157:
158: startServer(onDomain, pDaemonName, pServerName, mgmt, pOut);
159: pOut.println("<h2>Application is OK </h2>");
160:
161: // Footer
162: printNavigationFooter(pRequest, pOut);
163: }
164:
165: /**
166: * Create J2EEDomain MBean's ObjectName and test if MBean registered
167: * @param pDomainName the name provided by the user
168: * @param mgmt MEJB
169: * @param pOut output stream
170: * @return true if management operation succeeded
171: */
172: private ObjectName accessJ2EEDomain(String pDomainName,
173: Management mgmt, PrintWriter pOut) {
174: ObjectName onDomain = null;
175: pOut.println("<h2>Access the J2EEDomain MBean</h2>");
176: pOut.println("<ul>");
177:
178: // Get the J2EEDomain MBean's ObjectName
179: try {
180: String name = pDomainName + ":j2eeType=J2EEDomain,name="
181: + pDomainName;
182: onDomain = ObjectName.getInstance(name);
183: pOut.println("<li>J2EEDomain object name \""
184: + name.toString() + "\" created.</li>");
185: } catch (Exception e) {
186: pOut
187: .println("<li>Cannot create object name for J2EEDomain managed object: "
188: + e + "</li>");
189: pOut.println("</ul>");
190: return null;
191: }
192: // Check that the J2EEDomain MBean registered
193: try {
194: boolean exists = mgmt.isRegistered(onDomain);
195: if (exists) {
196: pOut
197: .println("<li>Found this J2EEDomain MBean in the current MBean server</li>");
198: pOut.println("</ul><br/>");
199: } else {
200: pOut
201: .println("<li><b>Can't find this J2EEDomain MBean in the current MBean server</b></li>");
202: pOut.println("</ul>");
203: return null;
204: }
205: } catch (Exception e) {
206: pOut.println("<li>Error when using this J2EEDomain MBean: "
207: + e + "</li>");
208: pOut.println("</ul>");
209: return null;
210: }
211: return onDomain;
212: }
213:
214: /**
215: * Start a server in domain
216: * @param pOnDomain J2EEDomain MBean ObjectName
217: * @param pDaemonName daemon name - currently not used
218: * @param pServerName server name
219: * @param mgmt MEJB
220: * @param pOut output stream
221: */
222: private void startServer(ObjectName pOnDomain, String pDaemonName,
223: String pServerName, Management mgmt, PrintWriter pOut) {
224: pOut.println("<h2>Start the server " + pServerName + "</h2>");
225: pOut.println("<ul>");
226: try {
227: String[] signature = { "java.lang.String" };
228: String[] params = { pServerName };
229: mgmt.invoke(pOnDomain, "startServer", params, signature);
230: pOut
231: .println("<li>Server " + pServerName
232: + " started.</li>");
233: pOut.println("</ul><br/>");
234: } catch (Exception e) {
235: pOut
236: .println("<li>Cannot create cluster start the server</li>");
237: pOut.println("</ul>");
238: }
239: }
240: }
|