1 12 13 package org.ejtools.jmx.browser.web.action; 14 15 16 17 import java.io.IOException ; 18 19 import java.util.Iterator ; 20 21 import java.util.Locale ; 22 23 24 25 import javax.management.MBeanAttributeInfo ; 26 27 import javax.management.MBeanInfo ; 28 29 import javax.management.MBeanOperationInfo ; 30 31 import javax.management.ObjectName ; 32 33 import javax.servlet.ServletContext ; 34 35 import javax.servlet.ServletException ; 36 37 import javax.servlet.http.HttpServletRequest ; 38 39 import javax.servlet.http.HttpServletResponse ; 40 41 42 43 import org.apache.log4j.Logger; 44 45 import org.apache.struts.action.Action; 46 47 import org.apache.struts.action.ActionError; 48 49 import org.apache.struts.action.ActionErrors; 50 51 import org.apache.struts.action.ActionForm; 52 53 import org.apache.struts.action.ActionForward; 54 55 import org.apache.struts.action.ActionMapping; 56 57 import org.apache.struts.util.MessageResources; 58 59 import org.ejtools.jmx.browser.mbean.View; 60 61 import org.ejtools.jmx.browser.mbean.ViewLine; 62 63 import org.ejtools.jmx.browser.model.Resource; 64 65 import org.ejtools.jmx.browser.web.Constants; 66 67 import org.ejtools.jmx.browser.web.JMXContainer; 68 69 70 71 86 87 public class CustomViewAction extends Action 88 89 { 90 91 92 93 private static Logger logger = Logger.getLogger(CustomViewAction.class); 94 95 96 97 98 99 100 101 public CustomViewAction() { } 102 103 104 105 106 107 128 129 public ActionForward perform(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 130 131 throws IOException , ServletException 132 133 { 134 135 JMXContainer tree = null; 136 137 138 139 141 Locale locale = getLocale(request); 142 143 MessageResources messages = getResources(); 144 145 146 147 149 ActionErrors errors = new ActionErrors(); 150 151 152 153 logger.debug("Connecting to JMX Server"); 154 155 156 157 ServletContext context = this.getServlet().getServletContext(); 158 159 tree = (JMXContainer) context.getAttribute(Constants.TREE); 160 161 162 163 if (tree != null) 164 165 { 166 167 logger.debug("Tree root found => " + tree); 168 169 170 171 try 172 173 { 174 175 177 int number = Integer.parseInt(request.getParameter("view")); 178 179 180 181 183 View view = tree.getView(number); 184 185 logger.debug("Get View " + view); 186 187 188 189 191 context.setAttribute(Constants.CUSTOM_VIEW, view); 192 193 context.setAttribute(Constants.CUSTOM_VIEW_INDEX, new String ("" + number)); 194 195 196 197 if (view != null) 198 199 { 200 201 Iterator iterator = null; 202 203 View resultView = new View(); 204 205 206 207 209 iterator = view.getAttributeLines().iterator(); 210 211 while (iterator.hasNext()) 212 213 { 214 215 ViewLine query = (ViewLine) iterator.next(); 216 217 218 219 String name = query.getName(); 220 221 Iterator it = tree.query(query.getObjectName(), null).iterator(); 222 223 224 225 while (it.hasNext()) 226 227 { 228 229 ObjectName on = (ObjectName ) it.next(); 230 231 Resource res = (Resource) tree.searchObjectName(on.getCanonicalName()); 232 233 234 235 MBeanInfo info = res.getMBeanInfo(); 236 237 MBeanAttributeInfo [] attrs = info.getAttributes(); 238 239 240 241 for (int i = 0; i < attrs.length; i++) 242 243 { 244 245 if (attrs[i].getName().equals(name)) 246 247 { 248 249 resultView.addAttributeResult(res, attrs[i]); 250 251 logger.debug("Add new attribute " + res + " => " + name); 252 253 break; 254 255 } 256 257 } 258 259 } 260 261 } 262 263 264 265 267 iterator = view.getOperationLines().iterator(); 268 269 while (iterator.hasNext()) 270 271 { 272 273 ViewLine query = (ViewLine) iterator.next(); 274 275 276 277 String name = query.getName(); 278 279 Iterator it = tree.query(query.getObjectName(), null).iterator(); 280 281 282 283 while (it.hasNext()) 284 285 { 286 287 ObjectName on = (ObjectName ) it.next(); 288 289 Resource res = (Resource) tree.searchObjectName(on.getCanonicalName()); 290 291 292 293 MBeanInfo info = res.getMBeanInfo(); 294 295 MBeanOperationInfo [] opers = info.getOperations(); 296 297 298 299 for (int i = 0; i < opers.length; i++) 300 301 { 302 303 if (opers[i].getName().equals(name)) 304 305 { 306 307 resultView.addOperationResult(res, opers[i]); 308 309 logger.debug("Add new attribute " + res + " => " + name); 310 311 break; 312 313 } 314 315 } 316 317 } 318 319 } 320 321 322 323 context.setAttribute(Constants.CUSTOM_VIEW_ATTRIBUTES, resultView.getAttributeResults()); 324 325 context.setAttribute(Constants.CUSTOM_VIEW_OPERATIONS, resultView.getOperationResults()); 326 327 } 328 329 else 330 331 { 332 333 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("web.error.cannot.process.view")); 334 335 } 336 337 } 338 339 catch (Exception e) 340 341 { 342 343 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("web.error.cannot.process.view")); 344 345 } 346 347 } 348 349 else 350 351 { 352 353 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("web.error.cannot.connect")); 354 355 } 356 357 358 359 361 if (!errors.empty()) 362 363 { 364 365 saveErrors(request, errors); 366 367 return (mapping.findForward("error")); 368 369 } 370 371 372 373 return (mapping.findForward("view")); 374 375 } 376 377 } 378 379 | Popular Tags |