B4J Code Snippet [Server] Session and session attribute events

Place the code below into a code module (eg. SessionListener) and after you start the server you call : SessionListener.AddToServer(yourServerObject, True, True).

B4X:
'Session Listener
Sub Process_Globals
    Private myPackage As String = ""
    Private joSessionEvents As JavaObject
    Private joSessionAttributeEvents As JavaObject
    Private SessionEventsAddedToServer As Boolean = False
    Private SessionAttributeEventsAddedToServer As Boolean = False
End Sub

'Adds a Listener to the server
'Session events : SessionListener_SessionCreated and SessionListener_SessionCreated
'Attribute events : SessionListener_AttributeAdded, SessionListener_AttributeRemoved and SessionListener_AttributeReplaced
Public Sub AddToServer(ServerObject As Server, AddSessionEvents As Boolean, AddSessionAttributeEvents As Boolean)
    If ServerObject <> Null And (Not(SessionEventsAddedToServer) Or Not(SessionAttributeEventsAddedToServer)) Then
        If myPackage.Length = 0 Then
            myPackage = Me
            myPackage = myPackage.SubString("class ".Length)         
        End If
        Dim joServer As JavaObject = ServerObject
        If AddSessionEvents And Not(SessionEventsAddedToServer) Then
            joSessionEvents = joSessionEvents.InitializeNewInstance(myPackage & ".SessionEvents", Null)
            joServer.GetFieldJO("context").RunMethodJO("getSessionHandler", Null).RunMethodJO("addEventListener", Array(joSessionEvents))
            SessionEventsAddedToServer = True
        End If
        If AddSessionAttributeEvents And Not(SessionAttributeEventsAddedToServer) Then
            joSessionAttributeEvents = joSessionAttributeEvents.InitializeNewInstance(myPackage & ".SessionAttributeEvents", Null)
            joServer.GetFieldJO("context").RunMethodJO("getSessionHandler", Null).RunMethodJO("addEventListener", Array(joSessionAttributeEvents))
            SessionAttributeEventsAddedToServer = True
        End If 
    End If
End Sub

'Removes a Listener from the server. If you remove a listener no events will be generated
Public Sub RemoveFromServer(ServerObject As Server, RemoveSessionEvents As Boolean, RemoveSessionAttributeEvents As Boolean)
    If ServerObject <> Null And (SessionEventsAddedToServer Or SessionAttributeEventsAddedToServer) Then
        Dim joServer As JavaObject = ServerObject
        If RemoveSessionEvents And SessionEventsAddedToServer Then
            joServer.GetFieldJO("context").RunMethodJO("getSessionHandler", Null).RunMethodJO("removeEventListener", Array(joSessionEvents))
            SessionEventsAddedToServer = False
        End If
        If RemoveSessionAttributeEvents And SessionAttributeEventsAddedToServer Then
            joServer.GetFieldJO("context").RunMethodJO("getSessionHandler", Null).RunMethodJO("removeEventListener", Array(joSessionAttributeEvents)) 
            SessionAttributeEventsAddedToServer = False
        End If
    End If
End Sub

'This sub gets fired when a session is created
'SessionObject = The session itself to access it: Dim session as HttpSession = SessionObject
Private Sub SessionListener_SessionCreated(SessionObject As Object)
    Dim Session As HttpSession = SessionObject
    Log("SessionCreated : " & Session.Id)
End Sub

'This sub gets fired when after a session is destroyed (session has been invalidated or has expired)
'SessionObject = The session itself to access it: Dim session as HttpSession = SessionObject
Private Sub SessionListener_SessionDestroyed(SessionObject As Object)
    Dim Session As HttpSession = SessionObject
    Log("SessionDestroyed : " & Session.Id)
End Sub

'This sub gets fired when an attribute is added to a session
'SessionObject = The session itself to access it: Dim session as HttpSession = SessionObject
'AttributeName is the name of the attribute that was added to the session
'AttributeValue is the value of the attribute that was added to the session
Private Sub SessionListener_AttributeAdded(SessionObject As Object, AttributeName As String, AttributeValue As Object)
    Dim Session As HttpSession = SessionObject
    Log("AttributeAdded : " & Session.Id & " Name : " & AttributeName & " Value : " & AttributeValue)
End Sub

'This sub gets fired when an attribute is removed from session (session has been invalidated or has expired or you removed the attribute)
'SessionObject = The session itself to access it: Dim session as HttpSession = SessionObject
'AttributeName is the name of the attribute that was removed to the session
'AttributeValue is the value of the attribute before the removal from session
Private Sub SessionListener_AttributeRemoved(SessionObject As Object, AttributeName As String, AttributeValue As Object)
    Dim Session As HttpSession = SessionObject
    Log("AttributeRemoved : " & Session.Id & " Name : " & AttributeName & " Value : " & AttributeValue) 
End Sub

'This sub gets fired when an attribute is value is replaced
'SessionObject = The session itself to access it: Dim session as HttpSession = SessionObject
'AttributeName is the name of the attribute which value was replaced
'AttributeValue is the OLD value of the attribute that was replaced
Private Sub SessionListener_AttributeReplaced(SessionObject As Object, AttributeName As String, AttributeOldValue As Object, AttributeNewValue As Object)
    Dim Session As HttpSession = SessionObject
    Log("AttributeReplaced : " & Session.Id & " Name : " & AttributeName & " Old Value : " & AttributeOldValue & " New Value : " & AttributeNewValue)
End Sub

#IF JAVA
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.annotation.WebListener;

@WebListener
public static class SessionEvents implements HttpSessionListener {
    @Override
    public void sessionCreated(final HttpSessionEvent event) {
        ba.raiseEvent(null, "sessionlistener_sessioncreated", event.getSession());
    }
    @Override
    public void sessionDestroyed(final HttpSessionEvent event) {
        ba.raiseEvent(null, "sessionlistener_sessiondestroyed", event.getSession());
    }
}

@WebListener
public static class SessionAttributeEvents implements HttpSessionAttributeListener {
    @Override
    public void attributeAdded(final HttpSessionBindingEvent event) {
        ba.raiseEvent(null, "sessionlistener_attributeadded", event.getSession(), event.getName(), event.getValue());
    }
    @Override
    public void attributeRemoved(final HttpSessionBindingEvent event) {
        ba.raiseEvent(null, "sessionlistener_attributeremoved", event.getSession(), event.getName(), event.getValue());
    }
    @Override
    public void attributeReplaced(final HttpSessionBindingEvent event) {
        ba.raiseEvent(null, "sessionlistener_attributereplaced", event.getSession(), event.getName(), event.getValue(), event.getSession().getAttribute(event.getName()));
    }
}
#END IF


This is my first inline java ... so I don't know if this was the correct way of implementing this events but it works for me! I tried making a library but it seems I have more to learn to do this so maybe another time in the future I will try to make a library, until then use it as is ;)
 
Top