You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by Alan Mehio <al...@yahoo.co.uk> on 2005/01/03 22:37:44 UTC
RE: Addition to struts.utils packes
Thanks. I think it is useful since the user session size needs to be
controlled in some big portal applications. It would be nice to include it
into the struts utility package
> -----Original Message-----
> From: fzlists@omnytex.com [mailto:fzlists@omnytex.com]
> Sent: 21 December 2004 21:22
> To: dev@struts.apache.org
> Subject: Addition to struts.utils packes
>
>
> I recently had a need to determine the size of a user's current
> session, and I didn't find any available option, so I rolled my
> own. If I in fact didn't miss something and this really doesn't
> exist somewhere already, then I'd like to offer it up for
> inclusion in the struts.utils package. I can see this being a
> new SessionUtils class.
>
> I wasn't sure how logging should be handled, so I converted what
> I use over to simple System.out's. Obviously that needs to be
> changed, but for now it's fine. It will only work with
> serializable objects in session, and it probably has other
> shortcomings that I'm not aware of.
>
> In any case, just calling getSessionSize(), passing it a valid
> HTTPSession object, and off you go. As written, it dumps
> information to stdout about all the objects encountered, but it
> might be sufficient to remove all that and just let it return a
> final value. I tend to prefer more information though.
>
> Here you go, hope it's useful...
>
> /**
> * This method is used to get the total size of a current,
> valid HTTPSession
> * object it is passed.
> *
> * @param session A valid HTTPSession object
> * @return The total size of session in bytes
> */
> public static int getSessionSize(HttpSession session) {
>
> System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
> System.out.println("!!!!!!!!!! getSessionSize() !!!!!!!!!!");
> Enumeration enum = session.getAttributeNames();
> String name = null;
> Object obj = null;
> String serialOut;
> String SIZE_DELIMITER = "size=";
> int sizeIndex;
> int objSize;
> int totalSize = 0;
> while (enum.hasMoreElements()) {
> name = (String)enum.nextElement();
> obj = session.getAttribute(name);
> serialOut = AppHelpers.serializiableTest(obj);
> if ((sizeIndex = serialOut.lastIndexOf(SIZE_DELIMITER)) > 0) {
> objSize = Integer.parseInt(serialOut.substring(sizeIndex
> + SIZE_DELIMITER.length(), serialOut.lastIndexOf(')')));
> totalSize += objSize;
> }
> System.out.println("Attribute " + name + " - " + serialOut);
> }
> System.out.println("Current total session size = " + totalSize);
> System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
> return totalSize;
>
> }
>
>
> /**
> * This method is used by the getSessionSize() method to determine if a
> * given object is serializable.
> *
> * @param Object The object to test
> * @return String A response string detailing the outcome of the test
> */
> private static String serializiableTest(Object obj) {
>
> String ans = "ok";
> if (obj == null) {
> return "Object is null";
> } else {
> try {
> ByteArrayOutputStream bastream = new ByteArrayOutputStream();
> ObjectOutputStream p = new ObjectOutputStream(bastream);
> p.writeObject(obj);
> ans = "OK (size=" + bastream.size() + ")";
> } catch (NotSerializableException ex) {
> Field[] fields = obj.getClass().getDeclaredFields();
> ans = "NOT SERIALIZABLE (fields=" + fields + ")";
> ex.printStackTrace();
> Object fldObj = null;
> if (fields != null && (fields.length != 0)) {
> StringBuffer sb = new StringBuffer("\n" + ans + "[");
> for (int i = 0; i < fields.length; i++) {
> sb.append(fields[i].getName());
> try {
> if (obj != null) {
> fldObj = getFieldWithPrivilege(fields[i], obj);
> }
> sb.append("::");
> if (fldObj == null) {
> sb.append("<field null>");
> } else {
> sb.append(serializiableTest(fldObj));
> }
> } catch (IllegalArgumentException aex) {
> sb.append("::");
> sb.append("ILLEGAL ARGUMENT EXCEPTION");
> }
> if (i != fields.length - 1) {
> sb.append('\n');
> }
> }
> sb.append("]");
> ans = sb.toString();
> }
> } catch (Exception ex) {
> ans = "EXCEPTION: " + ex.getMessage();
> }
> }
> return obj.getClass().getName() + " is " + ans;
>
> }
>
>
> /**
> * This method is used by the serializiableTest() method to get the
> * needed priveleges on a given field of a given object needed to
> * perform the serializable test.
> *
> * @param Field The field to get priveleges on
> Object The object to test
> * @return Object A Priveleged reference to the field essentially
> */
> private static Object getFieldWithPrivilege(Field fld, Object obj) {
>
> final Object obj2 = obj;
> final Field fld2 = fld;
> return AccessController.doPrivileged(
> new PrivilegedAction() {
> public Object run() {
> try {
> return fld2.get(obj2);
> } catch (IllegalAccessException ex) {
> ex.printStackTrace();
> return null;
> }
> }
> }
> );
>
> } // End getFieldWithPrivilege()
>
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org