1 12 13 package org.ejtools.jmx.browser.web.action; 14 15 16 17 import java.io.IOException ; 18 19 import java.io.PrintWriter ; 20 21 import java.io.StringWriter ; 22 23 import java.util.Locale ; 24 25 import java.util.Set ; 26 27 import java.util.Vector ; 28 29 30 31 import javax.management.AttributeValueExp ; 32 33 import javax.management.ObjectName ; 34 35 import javax.management.Query ; 36 37 import javax.management.QueryExp ; 38 39 import javax.management.ValueExp ; 40 41 import javax.servlet.ServletContext ; 42 43 import javax.servlet.ServletException ; 44 45 import javax.servlet.http.HttpServletRequest ; 46 47 import javax.servlet.http.HttpServletResponse ; 48 49 50 51 import org.apache.log4j.Logger; 52 53 import org.apache.struts.action.Action; 54 55 import org.apache.struts.action.ActionError; 56 57 import org.apache.struts.action.ActionErrors; 58 59 import org.apache.struts.action.ActionForm; 60 61 import org.apache.struts.action.ActionForward; 62 63 import org.apache.struts.action.ActionMapping; 64 65 import org.apache.struts.util.MessageResources; 66 67 import org.ejtools.jmx.browser.web.Constants; 68 69 import org.ejtools.jmx.browser.web.JMXContainer; 70 71 import org.ejtools.jmx.browser.web.form.SearchForm; 72 73 74 75 90 91 public class SearchAction extends Action 92 93 { 94 95 96 97 private static Logger logger = Logger.getLogger(SearchAction.class); 98 99 100 101 102 103 104 105 public SearchAction() { } 106 107 108 109 110 111 132 133 public ActionForward perform(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 134 135 throws IOException , ServletException 136 137 { 138 139 JMXContainer tree = null; 140 141 SearchForm searchForm = null; 142 143 144 145 147 Locale locale = getLocale(request); 148 149 MessageResources messages = getResources(); 150 151 152 153 155 ActionErrors errors = new ActionErrors(); 156 157 158 159 if (form instanceof SearchForm) 160 161 { 162 163 searchForm = (SearchForm) form; 164 165 } 166 167 else 168 169 { 170 171 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("web.error.no.form")); 172 173 } 174 175 176 177 logger.debug("Connecting to JMX Server"); 178 179 180 181 ServletContext context = this.getServlet().getServletContext(); 182 183 tree = (JMXContainer) context.getAttribute(Constants.TREE); 184 185 186 187 context.setAttribute(Constants.SEARCH_RESULT, new Vector ()); 188 189 190 191 if (tree != null) 192 193 { 194 195 String filter = searchForm.getFilter(); 196 197 String attribute = searchForm.getAttribute(); 198 199 String query = searchForm.getQuery(); 200 201 String value = searchForm.getValue(); 202 203 String type = searchForm.getType(); 204 205 206 207 try 208 209 { 210 211 ObjectName name = null; 212 213 QueryExp qExp = null; 214 215 216 217 if (!("".equals(filter))) 218 219 { 220 221 name = new ObjectName (filter); 222 223 } 224 225 226 227 logger.debug("ObjectName " + name); 228 229 230 231 if (!("".equals(attribute))) 232 233 { 234 235 AttributeValueExp attrExp = Query.attr(attribute); 236 237 ValueExp valExp = computeValue(type, value); 238 239 240 241 qExp = computeQuery(query, attrExp, valExp); 242 243 } 244 245 246 247 logger.debug("QueryExp " + qExp); 248 249 250 251 Set result = tree.query(name, qExp); 252 253 254 255 if (result != null) 256 257 { 258 259 logger.debug("Result " + result); 260 261 262 263 context.setAttribute(Constants.SEARCH_RESULT, result); 264 265 } 266 267 268 269 } 270 271 catch (Exception e) 272 273 { 274 275 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("web.error.query.filter", filter)); 276 277 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("web.error.exception.message", e.getMessage())); 278 279 280 281 StringWriter w = new StringWriter (); 282 283 PrintWriter pw = new PrintWriter (w); 284 285 e.printStackTrace(pw); 286 287 pw.close(); 288 289 290 291 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("web.error.exception.stack", w.toString())); 292 293 } 294 295 } 296 297 else 298 299 { 300 301 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("web.error.cannot.connect")); 302 303 } 304 305 306 307 309 if (!errors.empty()) 310 311 { 312 313 saveErrors(request, errors); 314 315 return (new ActionForward(mapping.getInput())); 316 317 } 318 319 320 321 return (mapping.findForward("view")); 322 323 } 324 325 326 327 328 329 344 345 protected QueryExp computeQuery(String op, ValueExp value1, ValueExp value2) 346 347 { 348 349 if ("=".equals(op)) 350 351 { 352 353 return Query.eq(value1, value2); 354 355 } 356 357 if (">".equals(op)) 358 359 { 360 361 return Query.gt(value1, value2); 362 363 } 364 365 if (">=".equals(op)) 366 367 { 368 369 return Query.geq(value1, value2); 370 371 } 372 373 if ("<".equals(op)) 374 375 { 376 377 return Query.lt(value1, value2); 378 379 } 380 381 if ("<=".equals(op)) 382 383 { 384 385 return Query.leq(value1, value2); 386 387 } 388 389 return null; 390 391 } 392 393 394 395 396 397 412 413 protected ValueExp computeValue(String type, String value) 414 415 throws Exception 416 417 { 418 419 logger.debug("Converting " + value + " of type " + type); 420 421 422 423 if ("java.lang.String".equals(type)) 424 425 { 426 427 return Query.value(value); 428 429 } 430 431 432 433 if ("java.lang.Integer".equals(type)) 434 435 { 436 437 return Query.value(new Integer (value)); 438 439 } 440 441 if ("java.lang.Long".equals(type)) 442 443 { 444 445 return Query.value(new Long (value)); 446 447 } 448 449 if ("java.lang.Float".equals(type)) 450 451 { 452 453 return Query.value(new Float (value)); 454 455 } 456 457 if ("java.lang.Double".equals(type)) 458 459 { 460 461 return Query.value(new Double (value)); 462 463 } 464 465 if ("java.lang.Boolean".equals(type)) 466 467 { 468 469 return Query.value((new Boolean (value)).booleanValue()); 470 471 } 472 473 474 475 if ("byte".equals(type)) 476 477 { 478 479 return Query.value(Byte.parseByte(value)); 480 481 } 482 483 if ("short".equals(type)) 484 485 { 486 487 return Query.value(Short.parseShort(value)); 488 489 } 490 491 if ("int".equals(type)) 492 493 { 494 495 return Query.value(Integer.parseInt(value)); 496 497 } 498 499 if ("long".equals(type)) 500 501 { 502 503 return Query.value(Long.parseLong(value)); 504 505 } 506 507 if ("float".equals(type)) 508 509 { 510 511 return Query.value(Float.parseFloat(value)); 512 513 } 514 515 if ("double".equals(type)) 516 517 { 518 519 return Query.value(Double.parseDouble(value)); 520 521 } 522 523 if ("boolean".equals(type)) 524 525 { 526 527 return Query.value((new Boolean (value)).booleanValue()); 528 529 } 530 531 return null; 532 533 } 534 535 } 536 537 | Popular Tags |