1 25 26 package org.objectweb.jonas.webapp.jonasadmin; 27 28 import java.io.IOException ; 29 import java.util.ArrayList ; 30 import java.util.List ; 31 import java.util.Properties ; 32 import java.util.StringTokenizer ; 33 34 import javax.management.ObjectName ; 35 import javax.servlet.ServletException ; 36 import javax.servlet.http.HttpServletRequest ; 37 import javax.servlet.http.HttpServletResponse ; 38 import javax.servlet.http.HttpSession ; 39 40 import org.apache.struts.Globals; 41 import org.apache.struts.action.Action; 42 import org.apache.struts.action.ActionMessage; 43 import org.apache.struts.action.ActionMessages; 44 import org.apache.struts.action.ActionForm; 45 import org.apache.struts.action.ActionForward; 46 import org.apache.struts.action.ActionMapping; 47 import org.apache.struts.util.MessageResources; 48 49 import org.objectweb.jonas.common.Log; 50 import org.objectweb.jonas.jmx.JonasManagementRepr; 51 import org.objectweb.jonas.webapp.taglib.TreeControl; 52 import org.objectweb.jonas.webapp.taglib.TreeControlNode; 53 import org.objectweb.util.monolog.api.BasicLevel; 54 import org.objectweb.util.monolog.api.Logger; 55 56 60 61 public abstract class JonasBaseAction extends Action { 62 63 65 public static final int DEPTH_DOMAIN = 1; 66 public static final int DEPTH_SERVER = 2; 67 68 70 73 protected MessageResources m_Resources = null; 74 protected HttpSession m_Session = null; 75 protected ActionMessages m_Errors = null; 76 protected WhereAreYou m_WhereAreYou = null; 77 78 80 public abstract ActionForward executeAction(ActionMapping p_Mapping, ActionForm p_Form 81 , HttpServletRequest p_Request, HttpServletResponse p_Response) 82 throws IOException , ServletException ; 83 84 101 public ActionForward execute(ActionMapping p_Mapping, ActionForm p_Form 102 , HttpServletRequest p_Request, HttpServletResponse p_Response) 103 throws IOException , ServletException { 104 105 ActionForward oActionForward = null; 106 107 initialize(p_Request); 109 110 if (m_WhereAreYou == null) { 112 oActionForward = (p_Mapping.findForward("Main Index")); 113 } else { 114 oActionForward = executeAction(p_Mapping, p_Form, p_Request, p_Response); 115 } 116 return oActionForward; 117 } 118 119 123 protected void initialize(HttpServletRequest p_Request) { 124 m_Session = p_Request.getSession(); 126 if (m_Resources == null) { 127 m_Resources = (MessageResources) getServlet().getServletContext().getAttribute(Globals.MESSAGES_KEY); 128 } 129 m_Errors = new ActionMessages(); 130 m_WhereAreYou = (WhereAreYou) m_Session.getAttribute(WhereAreYou.SESSION_NAME); 131 132 } 133 134 140 protected String getTreeBranchName(int p_Width) { 141 StringBuffer sb = new StringBuffer (); 142 try { 143 StringTokenizer st = new StringTokenizer (m_WhereAreYou.getSelectedNameNode() 144 , WhereAreYou.NODE_SEPARATOR); 145 for (int i = 0; (st.hasMoreTokens() && (i < p_Width)); i++) { 146 if (i > 0) { 147 sb.append(WhereAreYou.NODE_SEPARATOR); 148 } 149 sb.append(st.nextToken()); 150 } 151 } catch (NullPointerException e) { 152 } 154 return sb.toString(); 155 } 156 157 163 protected void addGlobalError(Throwable p_Throwable) { 164 String sMessResource; 165 String sMessageError = p_Throwable.getMessage(); 166 if (sMessageError == null) { 167 sMessResource = m_Resources.getMessage("error.global.log" 168 , p_Throwable.getClass().getName()); 169 Logger logger = Log.getLogger(Log.JONAS_ADMIN_PREFIX); 171 if (logger.isLoggable(BasicLevel.DEBUG)) { 172 logger.log(BasicLevel.DEBUG, sMessResource); 173 } 174 m_Errors.add("error.global", new ActionMessage("error.global" 175 , p_Throwable.getClass().getName())); 176 } else { 177 sMessResource = m_Resources.getMessage("error.global.message.log" 178 , p_Throwable.getClass().getName(), sMessageError); 179 Logger logger = Log.getLogger(Log.JONAS_ADMIN_PREFIX); 181 if (logger.isLoggable(BasicLevel.DEBUG)) { 182 logger.log(BasicLevel.DEBUG, sMessResource); 183 } 184 m_Errors.add("error.global", new ActionMessage("error.global.message" 185 , p_Throwable.getClass().getName(), sMessageError)); 186 } 187 } 188 189 196 protected String getStringAttribute(ObjectName p_ObjectName, String ps_AttrName) { 197 String s = (String ) JonasManagementRepr.getAttribute(p_ObjectName, ps_AttrName); 198 return s; 199 } 200 201 208 protected String [] getStringArrayAttribute(ObjectName p_ObjectName, String ps_AttrName) { 209 String [] s = (String []) JonasManagementRepr.getAttribute(p_ObjectName, ps_AttrName); 210 return s; 211 } 212 213 220 protected void setStringAttribute(ObjectName p_ObjectName, String ps_AttrName, String p_Value) { 221 JonasManagementRepr.setAttribute(p_ObjectName, ps_AttrName, p_Value); 222 } 223 224 231 protected int getIntegerAttribute(ObjectName p_ObjectName, String ps_AttrName) { 232 try { 233 Integer o = (Integer ) JonasManagementRepr.getAttribute(p_ObjectName, ps_AttrName); 234 return o.intValue(); 235 } catch (Exception e) { 236 } 238 return 0; 239 } 240 241 248 protected String toStringIntegerAttribute(ObjectName p_ObjectName, String ps_AttrName) { 249 try { 250 Integer o = (Integer ) JonasManagementRepr.getAttribute(p_ObjectName, ps_AttrName); 251 return o.toString(); 252 } catch (Exception e) { 253 } 255 return null; 256 } 257 258 265 protected void setIntegerAttribute(ObjectName p_ObjectName, String ps_AttrName, int p_Value) { 266 JonasManagementRepr.setAttribute(p_ObjectName, ps_AttrName, new Integer (p_Value)); 267 } 268 269 276 protected void setIntegerAttribute(ObjectName p_ObjectName, String ps_AttrName, String p_Value) { 277 if ((p_Value != null) && (p_Value.length() > 0)) { 278 JonasManagementRepr.setAttribute(p_ObjectName, ps_AttrName, new Integer (p_Value)); 279 } 280 } 281 282 289 protected long getLongAttribute(ObjectName p_ObjectName, String ps_AttrName) { 290 try { 291 Long o = (Long ) JonasManagementRepr.getAttribute(p_ObjectName, ps_AttrName); 292 return o.longValue(); 293 } catch (Exception e) { 294 } 296 return 0; 297 } 298 299 306 protected String toStringLongAttribute(ObjectName p_ObjectName, String ps_AttrName) { 307 try { 308 Long o = (Long ) JonasManagementRepr.getAttribute(p_ObjectName, ps_AttrName); 309 return o.toString(); 310 } catch (Exception e) { 311 } 313 return null; 314 } 315 316 323 protected void setLongAttribute(ObjectName p_ObjectName, String ps_AttrName, long p_Value) { 324 JonasManagementRepr.setAttribute(p_ObjectName, ps_AttrName, new Long (p_Value)); 325 } 326 327 334 protected long getShortAttribute(ObjectName p_ObjectName, String ps_AttrName) { 335 try { 336 Short o = (Short ) JonasManagementRepr.getAttribute(p_ObjectName, ps_AttrName); 337 return o.longValue(); 338 } catch (Exception e) { 339 } 341 return 0; 342 } 343 344 351 protected String toStringShortAttribute(ObjectName p_ObjectName, String ps_AttrName) { 352 try { 353 Short o = (Short ) JonasManagementRepr.getAttribute(p_ObjectName, ps_AttrName); 354 return o.toString(); 355 } catch (Exception e) { 356 } 358 return null; 359 } 360 361 368 protected void setShortAttribute(ObjectName p_ObjectName, String ps_AttrName, long p_Value) { 369 JonasManagementRepr.setAttribute(p_ObjectName, ps_AttrName, new Long (p_Value)); 370 } 371 372 379 protected boolean getBooleanAttribute(ObjectName p_ObjectName, String ps_AttrName) { 380 try { 381 Boolean o = (Boolean ) JonasManagementRepr.getAttribute(p_ObjectName, ps_AttrName); 382 return o.booleanValue(); 383 } catch (Exception e) { 384 } 386 return false; 387 } 388 389 396 protected String toStringBooleanAttribute(ObjectName p_ObjectName, String ps_AttrName) { 397 try { 398 Boolean o = (Boolean ) JonasManagementRepr.getAttribute(p_ObjectName, ps_AttrName); 399 return o.toString(); 400 } catch (Exception e) { 401 } 403 return null; 404 } 405 406 413 protected void setBooleanAttribute(ObjectName p_ObjectName, String ps_AttrName, boolean p_Value) { 414 JonasManagementRepr.setAttribute(p_ObjectName, ps_AttrName, new Boolean (p_Value)); 415 } 416 417 424 protected List getListAttribute(ObjectName p_ObjectName, String ps_AttrName) { 425 try { 426 return ((List ) JonasManagementRepr.getAttribute(p_ObjectName, ps_AttrName)); 427 } catch (Exception e) { 428 } 430 return new ArrayList (); 431 } 432 433 440 protected String getStringAttribute(Properties p_Props, String ps_AttrName) { 441 return p_Props.getProperty(ps_AttrName); 442 } 443 444 452 protected String getStringAttribute(Properties p_Props, String ps_AttrName, String p_Default) { 453 return p_Props.getProperty(ps_AttrName, p_Default); 454 } 455 456 463 protected void setStringAttribute(Properties p_Props, String ps_AttrName, String p_Value) { 464 if (p_Value != null) { 465 p_Props.setProperty(ps_AttrName, p_Value); 466 } 467 } 468 469 477 protected void setStringAttribute(Properties p_Props, String ps_AttrName, String p_Value 478 , String p_Default) { 479 if ((p_Value != null) && (p_Value.length() == 0)) { 480 p_Props.setProperty(ps_AttrName, p_Default); 481 } else { 482 p_Props.setProperty(ps_AttrName, p_Value); 483 } 484 } 485 486 493 protected int getIntegerAttribute(Properties p_Props, String ps_AttrName) { 494 return getIntegerAttribute(p_Props, ps_AttrName, 0); 495 } 496 497 505 protected int getIntegerAttribute(Properties p_Props, String ps_AttrName, int p_Default) { 506 try { 507 String s = getStringAttribute(p_Props, ps_AttrName); 508 if (s != null) { 509 return Integer.parseInt(s); 510 } 511 } catch (Exception e) { 512 } 514 return p_Default; 515 } 516 517 524 protected void setIntegerAttribute(Properties p_Props, String ps_AttrName, int p_Value) { 525 p_Props.setProperty(ps_AttrName, String.valueOf(p_Value)); 526 } 527 528 535 protected long getLongAttribute(Properties p_Props, String ps_AttrName) { 536 return getLongAttribute(p_Props, ps_AttrName, 0L); 537 } 538 539 547 protected long getLongAttribute(Properties p_Props, String ps_AttrName, long p_Default) { 548 try { 549 String s = getStringAttribute(p_Props, ps_AttrName); 550 if (s != null) { 551 return Long.parseLong(s); 552 } 553 } catch (Exception e) { 554 } 556 return p_Default; 557 } 558 559 566 protected void setLongAttribute(Properties p_Props, String ps_AttrName, long p_Value) { 567 p_Props.setProperty(ps_AttrName, String.valueOf(p_Value)); 568 } 569 570 577 protected boolean getBooleanAttribute(Properties p_Props, String ps_AttrName) { 578 return getBooleanAttribute(p_Props, ps_AttrName, false); 579 } 580 581 589 protected boolean getBooleanAttribute(Properties p_Props, String ps_AttrName, boolean p_Default) { 590 try { 591 String s = getStringAttribute(p_Props, ps_AttrName); 592 if (s != null) { 593 return Boolean.getBoolean(s); 594 } 595 } catch (Exception e) { 596 } 598 return p_Default; 599 } 600 601 608 protected void setBooleanAttribute(Properties p_Props, String ps_AttrName, boolean p_Value) { 609 p_Props.setProperty(ps_AttrName, String.valueOf(p_Value)); 610 } 611 612 protected Properties getPropsFromString(String ps_Props) { 613 String sProps = ps_Props.trim(); 615 sProps = removeChar(sProps, '\r'); 616 sProps = removeChar(sProps, '\n'); 617 Properties sp = new Properties (); 618 StringTokenizer st = new StringTokenizer (sProps, ","); 619 while (st.hasMoreTokens()) { 620 String token = st.nextToken(); 621 int pos = token.indexOf("="); 622 String propName = token.substring(0, pos); 623 String propValue = token.substring(pos + 1); 624 sp.setProperty(propName, propValue); 625 } 626 return sp; 627 } 628 629 636 protected static String removeChar(String string, char c) { 637 StringBuffer sb = new StringBuffer (); 638 sb.setLength(string.length()); 639 int i = 0; 640 for (int j = 0; j < string.length(); j++) { 641 char cur = string.charAt(j); 642 if (cur != c) { 643 sb.setCharAt(i++, cur); 644 } 645 } 646 return sb.toString(); 647 } 648 653 protected void refreshServerTree(HttpServletRequest p_Request) throws Exception { 654 TreeControl oControl = m_WhereAreYou.getTreeControl(); 656 TreeControlNode domainNode = oControl.findNode("domain"); 658 if (domainNode != null) { 659 oControl.enableAutoRefresh(); 661 domainNode.remove(); 663 JonasTreeBuilder oBuilder = new JonasTreeBuilder(); 665 oBuilder.getDomain(oControl.getRoot(), m_Resources, p_Request); 666 oControl.disableAutoRefresh(); 668 } 669 } 670 671 676 protected void refreshDomainDeployTree(HttpServletRequest p_Request) throws Exception { 677 TreeControl oControl = m_WhereAreYou.getTreeControl(); 679 TreeControlNode domainNode = oControl.findNode("domain"); 680 if (domainNode != null) { 681 oControl.enableAutoRefresh(); 683 JonasTreeBuilder oBuilder = new JonasTreeBuilder(); 686 oBuilder.getDomainDeploy(oControl.findNode("domain"), m_Resources, p_Request); 687 oControl.disableAutoRefresh(); 689 } 690 691 } 692 693 } 694 | Popular Tags |