Woops, that's not J2SE 5.0-compatible... I have to get out of the habit of 
using the variable name enum for enumerations :)  No big deal, I'll fix it if 
this is used for anythign.

-- 
Frank W. Zammetti
Founder and Chief Software Architect
Omnytex Technologies
http://www.omnytex.com

On Tue, December 21, 2004 4:22 pm, [EMAIL PROTECTED] said:
> 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: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to