KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > jmx > adaptor > html > HtmlAdaptorServlet


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.jmx.adaptor.html;
23
24 import java.io.IOException JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Enumeration JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import javax.management.AttributeList JavaDoc;
30 import javax.management.JMException JavaDoc;
31 import javax.servlet.RequestDispatcher JavaDoc;
32 import javax.servlet.ServletConfig JavaDoc;
33 import javax.servlet.ServletException JavaDoc;
34 import javax.servlet.http.HttpServlet JavaDoc;
35 import javax.servlet.http.HttpServletRequest JavaDoc;
36 import javax.servlet.http.HttpServletResponse JavaDoc;
37 import javax.servlet.http.HttpSession JavaDoc;
38
39 import org.jboss.logging.Logger;
40 import org.jboss.jmx.adaptor.control.OpResultInfo;
41 import org.jboss.jmx.adaptor.control.Server;
42 import org.jboss.jmx.adaptor.model.MBeanData;
43
44 /** The HTML adaptor controller servlet.
45  *
46  * @author Scott.Stark@jboss.org
47  * @version $Revision: 37459 $
48  */

49 public class HtmlAdaptorServlet extends HttpServlet JavaDoc
50 {
51    private static Logger log = Logger.getLogger(HtmlAdaptorServlet.class);
52    private static final String JavaDoc ACTION_PARAM = "action";
53    private static final String JavaDoc FILTER_PARAM = "filter";
54    private static final String JavaDoc DISPLAY_MBEANS_ACTION = "displayMBeans";
55    private static final String JavaDoc INSPECT_MBEAN_ACTION = "inspectMBean";
56    private static final String JavaDoc UPDATE_ATTRIBUTES_ACTION = "updateAttributes";
57    private static final String JavaDoc INVOKE_OP_ACTION = "invokeOp";
58    private static final String JavaDoc INVOKE_OP_BY_NAME_ACTION = "invokeOpByName";
59
60    /** Creates a new instance of HtmlAdaptor */
61    public HtmlAdaptorServlet()
62    {
63    }
64    
65    public void init(ServletConfig JavaDoc config) throws ServletException JavaDoc
66    {
67       super.init(config);
68    }
69
70    public void destroy()
71    {
72    }
73    
74    protected void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
75       throws ServletException JavaDoc, IOException JavaDoc
76    {
77       processRequest(request, response);
78    }
79    protected void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
80       throws ServletException JavaDoc, IOException JavaDoc
81    {
82       processRequest(request, response);
83    }
84
85    protected void processRequest(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
86       throws ServletException JavaDoc, IOException JavaDoc
87    {
88       String JavaDoc action = request.getParameter(ACTION_PARAM);
89
90       if( action == null )
91          action = DISPLAY_MBEANS_ACTION;
92
93       if( action.equals(DISPLAY_MBEANS_ACTION) )
94          displayMBeans(request, response);
95       else if( action.equals(INSPECT_MBEAN_ACTION) )
96          inspectMBean(request, response);
97       else if( action.equals(UPDATE_ATTRIBUTES_ACTION) )
98          updateAttributes(request, response);
99       else if( action.equals(INVOKE_OP_ACTION) )
100          invokeOp(request, response);
101       else if( action.equals(INVOKE_OP_BY_NAME_ACTION) )
102          invokeOpByName(request, response);
103    }
104
105    /** Display all mbeans categorized by domain
106     */

107    private void displayMBeans(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
108       throws ServletException JavaDoc, IOException JavaDoc
109    {
110       // get ObjectName filter from request or session context
111
HttpSession JavaDoc session = request.getSession(false);
112       String JavaDoc filter = request.getParameter(FILTER_PARAM);
113
114       if( filter == null && session != null)
115       {
116          // try using previously provided filter from session context
117
filter = (String JavaDoc) session.getAttribute(FILTER_PARAM);
118       }
119       
120       if( filter != null && filter.length() > 0 )
121       {
122          // Strip any enclosing quotes
123
if( filter.charAt(0) == '"' )
124             filter = filter.substring(1);
125          if( filter.charAt(filter.length()-1) == '"')
126             filter = filter.substring(0, filter.length()-2);
127
128          // be a litte it tolerant to user input
129
String JavaDoc domain = "*";
130          String JavaDoc props = "*,*";
131
132          int separator = filter.indexOf(':');
133          int assignment = filter.indexOf('=');
134
135          if (separator == -1 && assignment != -1)
136          {
137             // assume properties only
138
props = filter.trim();
139          }
140          else if (separator == -1 && assignment == -1)
141          {
142             // assume domain name only
143
domain = filter.trim();
144          }
145          else
146          {
147             // domain and properties
148
domain = filter.substring(0,separator).trim();
149             props = filter.substring(separator+1).trim();
150          }
151
152          if (domain.equals("")) domain = "*";
153
154          if ( props.equals("") ) props = "*,*";
155          if ( props.endsWith("," )) props += "*";
156          if (!props.endsWith(",*")) props += ",*";
157          if ( props.equals("*,*" )) props = "*";
158
159          filter = domain + ":" + props;;
160
161          if (filter.equals("*:*"))
162              filter = "";
163       }
164       else
165       {
166          filter = "";
167       }
168
169       // update request filter and store filter in session context,
170
// so it can be used when no filter has been submitted in
171
// current request
172
request.setAttribute(FILTER_PARAM, filter);
173
174       if( session != null )
175       {
176          session.setAttribute(FILTER_PARAM, filter);
177       }
178
179       try
180       {
181          Iterator JavaDoc mbeans = Server.getDomainData(filter);
182          request.setAttribute("mbeans", mbeans);
183          RequestDispatcher JavaDoc rd = this.getServletContext().getRequestDispatcher("/displayMBeans.jsp");
184          rd.forward(request, response);
185       }
186       catch(JMException JavaDoc e)
187       {
188          throw new ServletException JavaDoc("Failed to get MBeans", e);
189       }
190    }
191
192    /** Display an mbeans attributes and operations
193     */

194    private void inspectMBean(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
195       throws ServletException JavaDoc, IOException JavaDoc
196    {
197       String JavaDoc name = request.getParameter("name");
198       log.trace("inspectMBean, name="+name);
199       try
200       {
201          MBeanData data = Server.getMBeanData(name);
202          request.setAttribute("mbeanData", data);
203          RequestDispatcher JavaDoc rd = this.getServletContext().getRequestDispatcher("/inspectMBean.jsp");
204          rd.forward(request, response);
205       }
206       catch(JMException JavaDoc e)
207       {
208          throw new ServletException JavaDoc("Failed to get MBean data", e);
209       }
210    }
211    
212    /** Update the writable attributes of an mbean
213     */

214    private void updateAttributes(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
215       throws ServletException JavaDoc, IOException JavaDoc
216    {
217       String JavaDoc name = request.getParameter("name");
218       log.trace("updateAttributes, name="+name);
219       Enumeration JavaDoc paramNames = request.getParameterNames();
220       HashMap JavaDoc attributes = new HashMap JavaDoc();
221       while( paramNames.hasMoreElements() )
222       {
223          String JavaDoc param = (String JavaDoc) paramNames.nextElement();
224          if( param.equals("name") || param.equals("action") )
225             continue;
226          String JavaDoc value = request.getParameter(param);
227          log.trace("name="+param+", value='"+value+"'");
228          // Ignore null values, these are empty write-only fields
229
if( value == null || value.length() == 0 )
230             continue;
231          attributes.put(param, value);
232       }
233
234       try
235       {
236          AttributeList JavaDoc newAttributes = Server.setAttributes(name, attributes);
237          MBeanData data = Server.getMBeanData(name);
238          request.setAttribute("mbeanData", data);
239          RequestDispatcher JavaDoc rd = this.getServletContext().getRequestDispatcher("/inspectMBean.jsp");
240          rd.forward(request, response);
241       }
242       catch(JMException JavaDoc e)
243       {
244          throw new ServletException JavaDoc("Failed to update attributes", e);
245       }
246    }
247    /** Invoke an mbean operation given the index into the MBeanOperationInfo{}
248     array of the mbean.
249     */

250    private void invokeOp(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
251       throws ServletException JavaDoc, IOException JavaDoc
252    {
253       String JavaDoc name = request.getParameter("name");
254       log.trace("invokeOp, name="+name);
255       String JavaDoc[] args = getArgs(request);
256       String JavaDoc methodIndex = request.getParameter("methodIndex");
257       if( methodIndex == null || methodIndex.length() == 0 )
258          throw new ServletException JavaDoc("No methodIndex given in invokeOp form");
259       int index = Integer.parseInt(methodIndex);
260       try
261       {
262          OpResultInfo opResult = Server.invokeOp(name, index, args);
263          request.setAttribute("opResultInfo", opResult);
264          RequestDispatcher JavaDoc rd = this.getServletContext().getRequestDispatcher("/displayOpResult.jsp");
265          rd.forward(request, response);
266       }
267       catch(JMException JavaDoc e)
268       {
269          throw new ServletException JavaDoc("Failed to invoke operation", e);
270       }
271    }
272
273    /** Invoke an mbean operation given the method name and its signature.
274     */

275    private void invokeOpByName(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
276       throws ServletException JavaDoc, IOException JavaDoc
277    {
278       String JavaDoc name = request.getParameter("name");
279       log.trace("invokeOpByName, name="+name);
280       String JavaDoc[] argTypes = request.getParameterValues("argType");
281       String JavaDoc[] args = getArgs(request);
282       String JavaDoc methodName = request.getParameter("methodName");
283       if( methodName == null )
284          throw new ServletException JavaDoc("No methodName given in invokeOpByName form");
285       try
286       {
287          OpResultInfo opResult = Server.invokeOpByName(name, methodName, argTypes, args);
288          request.setAttribute("opResultInfo", opResult);
289          RequestDispatcher JavaDoc rd = this.getServletContext().getRequestDispatcher("/displayOpResult.jsp");
290          rd.forward(request, response);
291       }
292       catch(JMException JavaDoc e)
293       {
294          throw new ServletException JavaDoc("Failed to invoke operation", e);
295       }
296    }
297
298    /** Extract the argN values from the request into a String[]
299    */

300    private String JavaDoc[] getArgs(HttpServletRequest JavaDoc request)
301    {
302       ArrayList JavaDoc argList = new ArrayList JavaDoc();
303       for(int i = 0; true ; i++)
304       {
305          String JavaDoc name = "arg" + i;
306          String JavaDoc value = request.getParameter(name);
307          if( value == null )
308             break;
309          argList.add(value);
310          log.trace(name+"="+value);
311       }
312       String JavaDoc[] args = new String JavaDoc[argList.size()];
313       argList.toArray(args);
314       return args;
315    }
316 }
317
318
Popular Tags