SessionBindingListenerBridge.java :  » REST » alc-rest » org » allcolor » alc » webapp » Java Open Source

Java Open Source » REST » alc rest 
alc rest » org » allcolor » alc » webapp » SessionBindingListenerBridge.java
package org.allcolor.alc.webapp;

import java.io.Serializable;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;


/**
 * @author (Author)
 * @version $Revision$
  */
public final class SessionBindingListenerBridge
  extends SerializableObject
  implements HttpSessionBindingListener,
    Serializable {
  //~ Static fields/initializers -----------------------------------------------

  /**
   * DOCUMENT ME!
   */
  public static final long serialVersionUID = 1L;

  //~ Instance fields ----------------------------------------------------------

  /**
   * DOCUMENT ME!
   */
  private final String name;

  /**
   * DOCUMENT ME!
   */
  private transient Constructor<?> httpSessionBindingEvent = null;

  /**
   * DOCUMENT ME!
   */
  private transient Method valueBound = null;

  /**
   * DOCUMENT ME!
   */
  private transient Method valueUnbound = null;

  //~ Constructors -------------------------------------------------------------

  /**
   * Creates a new SessionBindingListenerBridge object.
   *
   * @param name DOCUMENT ME!
   * @param value DOCUMENT ME!
   */
  public SessionBindingListenerBridge (
    final String name,
    final Object value) {
    super(value);
    this.name = name;
  } // end SessionBindingListenerBridge()

  //~ Methods ------------------------------------------------------------------

  /**
   * DOCUMENT ME!
   *
   * @param evt DOCUMENT ME!
   */
  public void valueBound (final HttpSessionBindingEvent evt) {
    try {
      final Object session = this._GetSession (evt.getSession ());

      if (session == null) {
        return;
      } // end if

      if (
        (this.valueBound == null)
          || (this.httpSessionBindingEvent == null)
          || (this.valueUnbound == null)) {
        this.setupMethods (session);
      } // end if

      final Object event = this.httpSessionBindingEvent.newInstance (
          new Object [] { session, this.name, this.getObj () });
      this.valueBound.invoke (
        this.getObj (),
        new Object [] { event });
    } // end try
    catch (final Exception ignore) {}
  } // end valueBound()

  /**
   * DOCUMENT ME!
   *
   * @param evt DOCUMENT ME!
   */
  public void valueUnbound (final HttpSessionBindingEvent evt) {
    try {
      final Object session = this._GetSession (evt.getSession ());

      if (session == null) {
        return;
      } // end if

      if (
        (this.valueBound == null)
          || (this.httpSessionBindingEvent == null)
          || (this.valueUnbound == null)) {
        this.setupMethods (session);
      } // end if

      final Object event = this.httpSessionBindingEvent.newInstance (
          new Object [] { session, this.name, this.getObj () });
      this.valueUnbound.invoke (
        this.getObj (),
        new Object [] { event });
    } // end try
    catch (final Exception ignore) {}
  } // end valueUnbound()

  /**
   * DOCUMENT ME!
   *
   * @param sessionDelegate DOCUMENT ME!
   *
   * @return DOCUMENT ME!
   *
   * @throws Exception DOCUMENT ME!
   */
  private Object _GetSession (final HttpSession sessionDelegate)
    throws Exception {
    final SerializableObject obj = ( SerializableObject ) sessionDelegate
        .getAttribute ("org.allcolor.alc.filter.CSessionWrapper");

    return (obj != null)
    ? obj.getObj ()
    : null;
  } // end _GetSession()

  /**
   * DOCUMENT ME!
   *
   * @param loader DOCUMENT ME!
   *
   * @return DOCUMENT ME!
   */
  private Constructor<?> getHttpSessionBindingEventCons (final ClassLoader loader) {
    try {
      final Class<?> clazz = loader.loadClass (
          "javax.servlet.http.HttpSessionBindingEvent");
      final Constructor<?> cons[] = clazz.getConstructors ();

      for (final Constructor<?> element : cons) {
        if (
          (element.getParameterTypes () != null)
            && (element.getParameterTypes ().length == 3)) {
          return element;
        } // end if
      } // end for

      return null;
    } // end try
    catch (final Exception ignore) {
      return null;
    } // end catch
  } // end getHttpSessionBindingEventCons()

  /**
   * DOCUMENT ME!
   *
   * @param session DOCUMENT ME!
   */
  private void setupMethods (final Object session) {
    try {
      this.httpSessionBindingEvent = this.getHttpSessionBindingEventCons (
          session.getClass ().getClassLoader ());

      final Method array[] = this.getObj ()
          .getClass ()
          .getMethods ();

      for (final Method element : array) {
        if (element.getName ()
            .equals ("valueBound")) {
          if (
            (element.getParameterTypes () != null)
              && (element.getParameterTypes ().length == 1)) {
            if (
              element
                .getParameterTypes () [0].getName ()
                .endsWith ("HttpSessionBindingEvent")) {
              this.valueBound = element;
            } // end if
          } // end if
        } // end if

        if (element.getName ()
            .equals ("valueUnbound")) {
          if (
            (element.getParameterTypes () != null)
              && (element.getParameterTypes ().length == 1)) {
            if (
              element
                .getParameterTypes () [0].getName ()
                .endsWith ("HttpSessionBindingEvent")) {
              this.valueUnbound = element;
            } // end if
          } // end if
        } // end if

        if ((this.valueBound != null) && (this.valueUnbound != null)) {
          break;
        } // end if
      } // end for
    } // end try
    catch (final Exception ignore) {}
  } // end setupMethods()
} // end SessionBindingListenerBridge
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.