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: JmsAdminServlet.java 8063 2006-02-27 12:59:05Z 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.jms.Topic;
031: import javax.management.MalformedObjectNameException;
032: import javax.management.ObjectName;
033: import javax.management.j2ee.Management;
034: import javax.servlet.ServletConfig;
035: import javax.servlet.ServletException;
036: import javax.servlet.http.HttpServletRequest;
037: import javax.servlet.http.HttpServletResponse;
038:
039: /**
040: * This servlet is an example which shows how to access the MEJB from a servlet.
041: * The MEJB is used to invoke JoramAdmin Bean operations.
042: * @author JOnAS team
043: * @author Adriana Danes
044: */
045: public class JmsAdminServlet extends J2eemanagementBaseServlet {
046: /** Parameter */
047: static final String PARAM_TOPIC = "topicName";
048: /** Parameter */
049: static final String PARAM_SERVER_ID = "serverId";
050: /** Parameter */
051: static final String PARAM_VIEW = "view";
052: /** Parameter */
053: static final String VIEW_INIT = "init";
054:
055: // ---------------------------------------------------------- Public methods
056:
057: /**
058: * Initialize the servlet.
059: * @param pConfig See HttpServlet
060: * @throws ServletException Could not initialize servlet
061: */
062: public void init(ServletConfig pConfig) throws ServletException {
063: super .init(pConfig);
064: }
065:
066: // ---------------------------------------------------------- Protected
067: // methods
068:
069: /**
070: * Response to the GET request.
071: * @param pRequest See HttpServlet
072: * @param pResponse See HttpServlet
073: * @throws IOException Could not execute request
074: * @throws ServletException An input or output error is detected when the servlet handles the request
075: */
076: protected void doGet(HttpServletRequest pRequest,
077: HttpServletResponse pResponse) throws IOException,
078: ServletException {
079: dispatch(pRequest, pResponse);
080: }
081:
082: /**
083: * Response to the POST request.
084: * @param pRequest See HttpServlet
085: * @param pResponse See HttpServlet
086: * @throws IOException An input or output error is detected when the servlet handles the request
087: * @throws ServletException Could not execute request
088: */
089: protected void doPost(HttpServletRequest pRequest,
090: HttpServletResponse pResponse) throws IOException,
091: ServletException {
092: dispatch(pRequest, pResponse);
093: }
094:
095: /**
096: * Dispatch the response.
097: * @param pRequest Request
098: * @param pResponse Response
099: * @throws IOException An input or output error is detected when the servlet handles the request
100: */
101: protected void dispatch(HttpServletRequest pRequest,
102: HttpServletResponse pResponse) throws IOException {
103:
104: pResponse.setContentType("text/html");
105: PrintWriter out = pResponse.getWriter();
106:
107: // Get parameters
108: String sParamTopic = pRequest.getParameter(PARAM_TOPIC);
109: String sParamServerId = pRequest.getParameter(PARAM_SERVER_ID);
110: String sParamView = pRequest.getParameter(PARAM_VIEW);
111:
112: // Dispatching
113: if ((sParamTopic == null) || (sParamTopic.length() == 0)) {
114: doViewError("Parameter <i>Topic name</i> not found",
115: pRequest, out);
116: } else if ((sParamServerId == null)
117: || (sParamServerId.length() == 0)) {
118: doViewError("Parameter <i>Joram server id</i> not found",
119: pRequest, out);
120: } else if ((sParamView == null) || (sParamView.length() == 0)
121: || VIEW_INIT.equals(sParamView)) {
122: doViewInit(pRequest, out);
123: doViewManagement(sParamTopic, sParamServerId, pRequest, out);
124: } else {
125: doViewError("Unknown View", pRequest, out);
126: }
127:
128: }
129:
130: /**
131: * View init MEJB and access to MBEans J2EEDomain and J2EEServer.
132: * @param pTopicName Name of topic to create
133: * @param pServerId Joram server Id
134: * @param pRequest Http request
135: * @param pOut Printer
136: */
137: protected void doViewManagement(String pTopicName,
138: String pServerId, HttpServletRequest pRequest,
139: PrintWriter pOut) {
140:
141: // -------------------------------------------------------------
142: // Use the Joram MBean via the MEJB to make jms admin operations
143: // -------------------------------------------------------------
144: pOut.println("<h2>Get Joram admin MBean</h2>");
145: pOut.println("<ul>");
146: Management mgmt = getMgmt();
147:
148: String joramON = getInitParameter("mbeanName");
149: if (joramON == null) {
150: joramON = "joramClient:type=JoramAdmin";
151: } else {
152: pOut.println("<li>Use servlet init param 'mbeanName': "
153: + joramON + "</li>");
154: }
155: ObjectName joramOn = null;
156: try {
157: joramOn = ObjectName.getInstance(joramON);
158: } catch (MalformedObjectNameException e1) {
159: pOut.println("<li>Couldn't get Joram MBean</li>" + e1);
160: pOut.println("</ul>");
161: printNavigationFooter(pRequest, pOut);
162: return;
163: }
164: pOut.println("</ul><br/>");
165: pOut.println("<h2>Use Joram admin MBean</h2>");
166: pOut.println("<ul>");
167:
168: try {
169: if (mgmt.isRegistered(joramOn)) {
170: Object[] asParam = { new Integer(pServerId), pTopicName };
171: String[] asSignature = { "int", "java.lang.String" };
172: String op = "createTopic";
173: Topic topic = (Topic) mgmt.invoke(joramOn, op, asParam,
174: asSignature);
175: getInitialContext().rebind(pTopicName, topic);
176: pOut.println("<li>Topic " + pTopicName
177: + " created on server " + pServerId + "</li>");
178: }
179: } catch (Exception e) {
180: pOut
181: .println("<li>Could not use use Joram MBean to administer jms</li>"
182: + e);
183: pOut.println("</ul>");
184: printNavigationFooter(pRequest, pOut);
185: return;
186: }
187:
188: pOut.println("</ul><br/>");
189:
190: pOut.println("<h2>Application is OK </h2>");
191:
192: // Footer
193: printNavigationFooter(pRequest, pOut);
194: }
195:
196: }
|