1 22 package org.jboss.jmx.adaptor.html; 23 24 import java.io.IOException ; 25 import java.util.ArrayList ; 26 import java.util.Enumeration ; 27 import java.util.HashMap ; 28 import java.util.Iterator ; 29 import javax.management.AttributeList ; 30 import javax.management.JMException ; 31 import javax.servlet.RequestDispatcher ; 32 import javax.servlet.ServletConfig ; 33 import javax.servlet.ServletException ; 34 import javax.servlet.http.HttpServlet ; 35 import javax.servlet.http.HttpServletRequest ; 36 import javax.servlet.http.HttpServletResponse ; 37 import javax.servlet.http.HttpSession ; 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 49 public class HtmlAdaptorServlet extends HttpServlet 50 { 51 private static Logger log = Logger.getLogger(HtmlAdaptorServlet.class); 52 private static final String ACTION_PARAM = "action"; 53 private static final String FILTER_PARAM = "filter"; 54 private static final String DISPLAY_MBEANS_ACTION = "displayMBeans"; 55 private static final String INSPECT_MBEAN_ACTION = "inspectMBean"; 56 private static final String UPDATE_ATTRIBUTES_ACTION = "updateAttributes"; 57 private static final String INVOKE_OP_ACTION = "invokeOp"; 58 private static final String INVOKE_OP_BY_NAME_ACTION = "invokeOpByName"; 59 60 61 public HtmlAdaptorServlet() 62 { 63 } 64 65 public void init(ServletConfig config) throws ServletException 66 { 67 super.init(config); 68 } 69 70 public void destroy() 71 { 72 } 73 74 protected void doGet(HttpServletRequest request, HttpServletResponse response) 75 throws ServletException , IOException 76 { 77 processRequest(request, response); 78 } 79 protected void doPost(HttpServletRequest request, HttpServletResponse response) 80 throws ServletException , IOException 81 { 82 processRequest(request, response); 83 } 84 85 protected void processRequest(HttpServletRequest request, HttpServletResponse response) 86 throws ServletException , IOException 87 { 88 String 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 107 private void displayMBeans(HttpServletRequest request, HttpServletResponse response) 108 throws ServletException , IOException 109 { 110 HttpSession session = request.getSession(false); 112 String filter = request.getParameter(FILTER_PARAM); 113 114 if( filter == null && session != null) 115 { 116 filter = (String ) session.getAttribute(FILTER_PARAM); 118 } 119 120 if( filter != null && filter.length() > 0 ) 121 { 122 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 String domain = "*"; 130 String props = "*,*"; 131 132 int separator = filter.indexOf(':'); 133 int assignment = filter.indexOf('='); 134 135 if (separator == -1 && assignment != -1) 136 { 137 props = filter.trim(); 139 } 140 else if (separator == -1 && assignment == -1) 141 { 142 domain = filter.trim(); 144 } 145 else 146 { 147 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 request.setAttribute(FILTER_PARAM, filter); 173 174 if( session != null ) 175 { 176 session.setAttribute(FILTER_PARAM, filter); 177 } 178 179 try 180 { 181 Iterator mbeans = Server.getDomainData(filter); 182 request.setAttribute("mbeans", mbeans); 183 RequestDispatcher rd = this.getServletContext().getRequestDispatcher("/displayMBeans.jsp"); 184 rd.forward(request, response); 185 } 186 catch(JMException e) 187 { 188 throw new ServletException ("Failed to get MBeans", e); 189 } 190 } 191 192 194 private void inspectMBean(HttpServletRequest request, HttpServletResponse response) 195 throws ServletException , IOException 196 { 197 String 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 rd = this.getServletContext().getRequestDispatcher("/inspectMBean.jsp"); 204 rd.forward(request, response); 205 } 206 catch(JMException e) 207 { 208 throw new ServletException ("Failed to get MBean data", e); 209 } 210 } 211 212 214 private void updateAttributes(HttpServletRequest request, HttpServletResponse response) 215 throws ServletException , IOException 216 { 217 String name = request.getParameter("name"); 218 log.trace("updateAttributes, name="+name); 219 Enumeration paramNames = request.getParameterNames(); 220 HashMap attributes = new HashMap (); 221 while( paramNames.hasMoreElements() ) 222 { 223 String param = (String ) paramNames.nextElement(); 224 if( param.equals("name") || param.equals("action") ) 225 continue; 226 String value = request.getParameter(param); 227 log.trace("name="+param+", value='"+value+"'"); 228 if( value == null || value.length() == 0 ) 230 continue; 231 attributes.put(param, value); 232 } 233 234 try 235 { 236 AttributeList newAttributes = Server.setAttributes(name, attributes); 237 MBeanData data = Server.getMBeanData(name); 238 request.setAttribute("mbeanData", data); 239 RequestDispatcher rd = this.getServletContext().getRequestDispatcher("/inspectMBean.jsp"); 240 rd.forward(request, response); 241 } 242 catch(JMException e) 243 { 244 throw new ServletException ("Failed to update attributes", e); 245 } 246 } 247 250 private void invokeOp(HttpServletRequest request, HttpServletResponse response) 251 throws ServletException , IOException 252 { 253 String name = request.getParameter("name"); 254 log.trace("invokeOp, name="+name); 255 String [] args = getArgs(request); 256 String methodIndex = request.getParameter("methodIndex"); 257 if( methodIndex == null || methodIndex.length() == 0 ) 258 throw new ServletException ("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 rd = this.getServletContext().getRequestDispatcher("/displayOpResult.jsp"); 265 rd.forward(request, response); 266 } 267 catch(JMException e) 268 { 269 throw new ServletException ("Failed to invoke operation", e); 270 } 271 } 272 273 275 private void invokeOpByName(HttpServletRequest request, HttpServletResponse response) 276 throws ServletException , IOException 277 { 278 String name = request.getParameter("name"); 279 log.trace("invokeOpByName, name="+name); 280 String [] argTypes = request.getParameterValues("argType"); 281 String [] args = getArgs(request); 282 String methodName = request.getParameter("methodName"); 283 if( methodName == null ) 284 throw new ServletException ("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 rd = this.getServletContext().getRequestDispatcher("/displayOpResult.jsp"); 290 rd.forward(request, response); 291 } 292 catch(JMException e) 293 { 294 throw new ServletException ("Failed to invoke operation", e); 295 } 296 } 297 298 300 private String [] getArgs(HttpServletRequest request) 301 { 302 ArrayList argList = new ArrayList (); 303 for(int i = 0; true ; i++) 304 { 305 String name = "arg" + i; 306 String value = request.getParameter(name); 307 if( value == null ) 308 break; 309 argList.add(value); 310 log.trace(name+"="+value); 311 } 312 String [] args = new String [argList.size()]; 313 argList.toArray(args); 314 return args; 315 } 316 } 317 318 | Popular Tags |