JonasBaseAction.java

00001 
00026 package org.objectweb.jonas.webapp.jonasadmin;
00027 
00028 import java.io.IOException;
00029 import java.util.ArrayList;
00030 import java.util.List;
00031 import java.util.Properties;
00032 import java.util.StringTokenizer;
00033 
00034 import javax.management.ObjectName;
00035 import javax.servlet.ServletException;
00036 import javax.servlet.http.HttpServletRequest;
00037 import javax.servlet.http.HttpServletResponse;
00038 import javax.servlet.http.HttpSession;
00039 
00040 import org.apache.struts.Globals;
00041 import org.apache.struts.action.Action;
00042 import org.apache.struts.action.ActionError;
00043 import org.apache.struts.action.ActionErrors;
00044 import org.apache.struts.action.ActionForm;
00045 import org.apache.struts.action.ActionForward;
00046 import org.apache.struts.action.ActionMapping;
00047 import org.apache.struts.util.MessageResources;
00048 
00049 import org.objectweb.jonas.jmx.JonasManagementRepr;
00050 
00056 public abstract class JonasBaseAction extends Action {
00057 
00058 // ----------------------------------------------------- Constants
00059 
00060     public static final int DEPTH_DOMAIN = 1;
00061     public static final int DEPTH_SERVER = 2;
00062 
00063 // --------------------------------------------------------- Instance Variables
00064 
00068     protected MessageResources m_Resources = null;
00069     protected HttpSession m_Session = null;
00070     protected ActionErrors m_Errors = null;
00071     protected WhereAreYou m_WhereAreYou = null;
00072 
00073 // --------------------------------------------------------- Public Methods
00074 
00075     public abstract ActionForward executeAction(ActionMapping p_Mapping, ActionForm p_Form
00076         , HttpServletRequest p_Request, HttpServletResponse p_Response)
00077         throws IOException, ServletException;
00078 
00096     public ActionForward execute(ActionMapping p_Mapping, ActionForm p_Form
00097         , HttpServletRequest p_Request, HttpServletResponse p_Response)
00098         throws IOException, ServletException {
00099 
00100         ActionForward oActionForward = null;
00101 
00102         // Instance variables
00103         initialize(p_Request);
00104 
00105         // Verify that a instance of WhereAreYou object exists
00106         if (m_WhereAreYou == null) {
00107             oActionForward = (p_Mapping.findForward("Main Index"));
00108         }
00109         else {
00110             oActionForward = executeAction(p_Mapping, p_Form, p_Request, p_Response);
00111         }
00112         return oActionForward;
00113     }
00114 
00119     protected void initialize(HttpServletRequest p_Request) {
00120         // Acquire the resources that we need
00121         m_Session = p_Request.getSession();
00122         if (m_Resources == null) {
00123             m_Resources = (MessageResources) getServlet().getServletContext().getAttribute(Globals.MESSAGES_KEY);
00124         }
00125         m_Errors = new ActionErrors();
00126         m_WhereAreYou = (WhereAreYou) m_Session.getAttribute(WhereAreYou.SESSION_NAME);
00127 
00128     }
00129 
00136     protected String getTreeBranchName(int p_Width) {
00137         StringBuffer sb = new StringBuffer();
00138         try {
00139             StringTokenizer st = new StringTokenizer(m_WhereAreYou.getSelectedNameNode()
00140                 , WhereAreYou.NODE_SEPARATOR);
00141             for (int i = 0; (st.hasMoreTokens() && (i < p_Width)); i++) {
00142                 if (i > 0) {
00143                     sb.append(WhereAreYou.NODE_SEPARATOR);
00144                 }
00145                 sb.append(st.nextToken());
00146             }
00147         }
00148         catch (NullPointerException e) {
00149             // none action
00150         }
00151         return sb.toString();
00152     }
00153 
00160     protected void addGlobalError(Throwable p_Throwable) {
00161         String sMessResource;
00162         String sMessageError = p_Throwable.getMessage();
00163         if (sMessageError == null) {
00164             sMessResource = m_Resources.getMessage("error.global.log"
00165                 , p_Throwable.getClass().getName());
00166             getServlet().log(sMessResource, p_Throwable);
00167             m_Errors.add("error.global", new ActionError("error.global"
00168                 , p_Throwable.getClass().getName()));
00169         }
00170         else {
00171             sMessResource = m_Resources.getMessage("error.global.message.log"
00172                 , p_Throwable.getClass().getName(), sMessageError);
00173             getServlet().log(sMessResource, p_Throwable);
00174             m_Errors.add("error.global", new ActionError("error.global.message"
00175                 , p_Throwable.getClass().getName(), sMessageError));
00176         }
00177     }
00178 
00186     protected String getStringAttribute(ObjectName p_ObjectName, String ps_AttrName) {
00187         String s = (String) JonasManagementRepr.getAttribute(p_ObjectName, ps_AttrName);
00188         return s;
00189     }
00190 
00198     protected void setStringAttribute(ObjectName p_ObjectName, String ps_AttrName, String p_Value) {
00199         JonasManagementRepr.setAttribute(p_ObjectName, ps_AttrName, p_Value);
00200     }
00201 
00209     protected int getIntegerAttribute(ObjectName p_ObjectName, String ps_AttrName) {
00210         try {
00211             Integer o = (Integer) JonasManagementRepr.getAttribute(p_ObjectName, ps_AttrName);
00212             return o.intValue();
00213         }
00214         catch (Exception e) {
00215             // None
00216         }
00217         return 0;
00218     }
00219 
00227     protected String toStringIntegerAttribute(ObjectName p_ObjectName, String ps_AttrName) {
00228         try {
00229             Integer o = (Integer) JonasManagementRepr.getAttribute(p_ObjectName, ps_AttrName);
00230             return o.toString();
00231         }
00232         catch (Exception e) {
00233             // None
00234         }
00235         return null;
00236     }
00237 
00245     protected void setIntegerAttribute(ObjectName p_ObjectName, String ps_AttrName, int p_Value) {
00246         JonasManagementRepr.setAttribute(p_ObjectName, ps_AttrName, new Integer(p_Value));
00247     }
00248 
00256     protected void setIntegerAttribute(ObjectName p_ObjectName, String ps_AttrName, String p_Value) {
00257         if ((p_Value != null) && (p_Value.length() > 0)) {
00258             JonasManagementRepr.setAttribute(p_ObjectName, ps_AttrName, new Integer(p_Value));
00259         }
00260     }
00261 
00269     protected long getLongAttribute(ObjectName p_ObjectName, String ps_AttrName) {
00270         try {
00271             Long o = (Long) JonasManagementRepr.getAttribute(p_ObjectName, ps_AttrName);
00272             return o.longValue();
00273         }
00274         catch (Exception e) {
00275             // None
00276         }
00277         return 0;
00278     }
00279 
00287     protected String toStringLongAttribute(ObjectName p_ObjectName, String ps_AttrName) {
00288         try {
00289             Long o = (Long) JonasManagementRepr.getAttribute(p_ObjectName, ps_AttrName);
00290             return o.toString();
00291         }
00292         catch (Exception e) {
00293             // None
00294         }
00295         return null;
00296     }
00297 
00305     protected void setLongAttribute(ObjectName p_ObjectName, String ps_AttrName, long p_Value) {
00306         JonasManagementRepr.setAttribute(p_ObjectName, ps_AttrName, new Long(p_Value));
00307     }
00308 
00316     protected boolean getBooleanAttribute(ObjectName p_ObjectName, String ps_AttrName) {
00317         try {
00318             Boolean o = (Boolean) JonasManagementRepr.getAttribute(p_ObjectName, ps_AttrName);
00319             return o.booleanValue();
00320         }
00321         catch (Exception e) {
00322             // None
00323         }
00324         return false;
00325     }
00326 
00334     protected String toStringBooleanAttribute(ObjectName p_ObjectName, String ps_AttrName) {
00335         try {
00336             Boolean o = (Boolean) JonasManagementRepr.getAttribute(p_ObjectName, ps_AttrName);
00337             return o.toString();
00338         }
00339         catch (Exception e) {
00340             // None
00341         }
00342         return null;
00343     }
00344 
00352     protected void setBooleanAttribute(ObjectName p_ObjectName, String ps_AttrName, boolean p_Value) {
00353         JonasManagementRepr.setAttribute(p_ObjectName, ps_AttrName, new Boolean(p_Value));
00354     }
00355 
00363     protected List getListAttribute(ObjectName p_ObjectName, String ps_AttrName) {
00364         try {
00365             return ((List) JonasManagementRepr.getAttribute(p_ObjectName, ps_AttrName));
00366         }
00367         catch (Exception e) {
00368             // None
00369         }
00370         return new ArrayList();
00371     }
00372 
00380     protected String getStringAttribute(Properties p_Props, String ps_AttrName) {
00381         return p_Props.getProperty(ps_AttrName);
00382     }
00383 
00392     protected String getStringAttribute(Properties p_Props, String ps_AttrName, String p_Default) {
00393         return p_Props.getProperty(ps_AttrName, p_Default);
00394     }
00395 
00403     protected void setStringAttribute(Properties p_Props, String ps_AttrName, String p_Value) {
00404         if (p_Value != null) {
00405             p_Props.setProperty(ps_AttrName, p_Value);
00406         }
00407     }
00408 
00417     protected void setStringAttribute(Properties p_Props, String ps_AttrName, String p_Value
00418         , String p_Default) {
00419         if ((p_Value != null) && (p_Value.length() == 0)) {
00420             p_Props.setProperty(ps_AttrName, p_Default);
00421         }
00422         else {
00423             p_Props.setProperty(ps_AttrName, p_Value);
00424         }
00425     }
00426 
00434     protected int getIntegerAttribute(Properties p_Props, String ps_AttrName) {
00435         return getIntegerAttribute(p_Props, ps_AttrName, 0);
00436     }
00437 
00446     protected int getIntegerAttribute(Properties p_Props, String ps_AttrName, int p_Default) {
00447         try {
00448             String s = getStringAttribute(p_Props, ps_AttrName);
00449             if (s != null) {
00450                 return Integer.parseInt(s);
00451             }
00452         }
00453         catch (Exception e) {
00454             // None
00455         }
00456         return p_Default;
00457     }
00458 
00466     protected void setIntegerAttribute(Properties p_Props, String ps_AttrName, int p_Value) {
00467         p_Props.setProperty(ps_AttrName, String.valueOf(p_Value));
00468     }
00469 
00477     protected long getLongAttribute(Properties p_Props, String ps_AttrName) {
00478         return getLongAttribute(p_Props, ps_AttrName, 0L);
00479     }
00480 
00489     protected long getLongAttribute(Properties p_Props, String ps_AttrName, long p_Default) {
00490         try {
00491             String s = getStringAttribute(p_Props, ps_AttrName);
00492             if (s != null) {
00493                 return Long.parseLong(s);
00494             }
00495         }
00496         catch (Exception e) {
00497             // None
00498         }
00499         return p_Default;
00500     }
00501 
00509     protected void setLongAttribute(Properties p_Props, String ps_AttrName, long p_Value) {
00510         p_Props.setProperty(ps_AttrName, String.valueOf(p_Value));
00511     }
00512 
00520     protected boolean getBooleanAttribute(Properties p_Props, String ps_AttrName) {
00521         return getBooleanAttribute(p_Props, ps_AttrName, false);
00522     }
00523 
00532     protected boolean getBooleanAttribute(Properties p_Props, String ps_AttrName, boolean p_Default) {
00533         try {
00534             String s = getStringAttribute(p_Props, ps_AttrName);
00535             if (s != null) {
00536                 return Boolean.getBoolean(s);
00537             }
00538         }
00539         catch (Exception e) {
00540             // None
00541         }
00542         return p_Default;
00543     }
00544 
00552     protected void setBooleanAttribute(Properties p_Props, String ps_AttrName, boolean p_Value) {
00553         p_Props.setProperty(ps_AttrName, String.valueOf(p_Value));
00554     }
00555 
00556     protected Properties getPropsFromString(String ps_Props) {
00557         // Clean the string
00558         String sProps = ps_Props.trim();
00559         sProps = removeChar(sProps, '\r');
00560         sProps = removeChar(sProps, '\n');
00561         Properties sp = new Properties();
00562         StringTokenizer st = new StringTokenizer(sProps, ",");
00563         while (st.hasMoreTokens()) {
00564             String token = st.nextToken();
00565             int pos = token.indexOf("=");
00566             String propName = token.substring(0, pos);
00567             String propValue = token.substring(pos + 1);
00568             sp.setProperty(propName, propValue);
00569         }
00570         return sp;
00571     }
00572 
00580     protected static String removeChar(String string, char c) {
00581         StringBuffer sb = new StringBuffer();
00582         sb.setLength(string.length());
00583         int i = 0;
00584         for (int j = 0; j < string.length(); j++) {
00585             char cur = string.charAt(j);
00586             if (cur != c) {
00587                 sb.setCharAt(i++, cur);
00588             }
00589         }
00590         return sb.toString();
00591     }
00592 
00593 }

Generated on Tue Feb 15 15:05:26 2005 for JOnAS by  doxygen 1.3.9.1