You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Matías Blasi <ma...@gmail.com> on 2012/09/23 15:58:16 UTC

Advisin Tapestry Session

Hi all,

I'm trying to implement a mechanism to track each user session, at least
the login and logout events.
The login is easy, because it occurs in a defined place, but the logout can
occurs explicitly or because of a timeout, so I thought in advising the
Tapestry Session.invalidate() method:

    @SuppressWarnings("unchecked")
@Advise(serviceInterface=org.apache.tapestry5.services.Session.class)
    public static void adviseUserActivity(final UserService userService,
final MethodAdviceReceiver receiver)
    throws SecurityException, NoSuchMethodException {
    MethodAdvice advise = new MethodAdvice() {
@Override
public void advise(MethodInvocation invocation) {
userService.endSession(userService.getCurrentUser().getUserName());
}
    };


  receiver.adviseMethod(receiver.getInterface().getMethod("invalidate"),
advise);
    }

But oddly, I'm getting the following error when starting up the application:

Caused by: java.lang.RuntimeException: Error invoking method public static
void
com.app.services.AppModule.adviseUserActivity(com.app.core.services.UserService,org.apache.tapestry5.ioc.MethodAdviceReceiver)
throws java.lang.SecurityException,java.lang.NoSuchMethodException:
org.hibernate.Session.invalidate()
        at
org.apache.tapestry5.ioc.internal.util.MethodInvoker.invoke(MethodInvoker.java:61)
        at
org.apache.tapestry5.ioc.internal.util.LoggingInvokableWrapper.invoke(LoggingInvokableWrapper.java:42)
        at
org.apache.tapestry5.ioc.internal.OperationTrackerImpl.invoke(OperationTrackerImpl.java:74)
        ... 79 more
Caused by: java.lang.NoSuchMethodException:
org.hibernate.Session.invalidate()
        at java.lang.Class.getMethod(Class.java:1622)
        at com.app.services.AppModule.adviseUserActivity(AppModule.java:198)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:616)
        at
org.apache.tapestry5.ioc.internal.util.MethodInvoker.invoke(MethodInvoker.java:50)
        ... 81 more
2012-09-23 10:50:11.134::INFO:  Started SelectChannelConnector@0.0.0.0:8080
[INFO] Started Jetty Server

Why is it trying to advise the org.hibernate.Sesssion.invalidate() method
if I explicitly
annotated: @Advise(serviceInterface=org.apache.tapestry5.services.Session.class),
is it a bug?

Anyway, do you think this is the best way of doing this, or is there any
other better mechanism to intercept a finished session?

Best regards,
Matias.

Re: Advisin Tapestry Session

Posted by Lance Java <la...@googlemail.com>.
Whilst the Request is a (thread-local) tapestry service, the session is not. 

It seems that you are trying to advise the (non-existant) http-session
service and tapestry is getting confused and trying to advise the
hibernate-session which does exist. Since hibernate does not have an
invalidate() a NoSuchMethodException is being thrown.

I can only assume that tapestry is using the service id ("session") instead
of the interface (org.apache.tapestry5.services.Session) to lookup the
object to advise.



--
View this message in context: http://tapestry.1045711.n5.nabble.com/Advisin-Tapestry-Session-tp5716457p5716487.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Advisin Tapestry Session

Posted by Matías Blasi <ma...@gmail.com>.
Thank both of you!

I'll analyze these options.

Anyway, I am curious about why my approach is not working, I insist, is it
a bug?

Best regards,
Matias.


On Sun, Sep 23, 2012 at 10:58 AM, Matías Blasi <ma...@gmail.com>wrote:

> Hi all,
>
> I'm trying to implement a mechanism to track each user session, at least
> the login and logout events.
> The login is easy, because it occurs in a defined place, but the logout
> can occurs explicitly or because of a timeout, so I thought in advising the
> Tapestry Session.invalidate() method:
>
>     @SuppressWarnings("unchecked")
> @Advise(serviceInterface=org.apache.tapestry5.services.Session.class)
>     public static void adviseUserActivity(final UserService userService,
> final MethodAdviceReceiver receiver)
>     throws SecurityException, NoSuchMethodException {
>     MethodAdvice advise = new MethodAdvice() {
> @Override
> public void advise(MethodInvocation invocation) {
>  userService.endSession(userService.getCurrentUser().getUserName());
> }
>     };
>
>
>   receiver.adviseMethod(receiver.getInterface().getMethod("invalidate"),
> advise);
>     }
>
> But oddly, I'm getting the following error when starting up the
> application:
>
> Caused by: java.lang.RuntimeException: Error invoking method public static
> void
> com.app.services.AppModule.adviseUserActivity(com.app.core.services.UserService,org.apache.tapestry5.ioc.MethodAdviceReceiver)
> throws java.lang.SecurityException,java.lang.NoSuchMethodException:
> org.hibernate.Session.invalidate()
>         at
> org.apache.tapestry5.ioc.internal.util.MethodInvoker.invoke(MethodInvoker.java:61)
>         at
> org.apache.tapestry5.ioc.internal.util.LoggingInvokableWrapper.invoke(LoggingInvokableWrapper.java:42)
>         at
> org.apache.tapestry5.ioc.internal.OperationTrackerImpl.invoke(OperationTrackerImpl.java:74)
>         ... 79 more
> Caused by: java.lang.NoSuchMethodException:
> org.hibernate.Session.invalidate()
>         at java.lang.Class.getMethod(Class.java:1622)
>         at
> com.app.services.AppModule.adviseUserActivity(AppModule.java:198)
>         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>         at
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
>         at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
>         at java.lang.reflect.Method.invoke(Method.java:616)
>         at
> org.apache.tapestry5.ioc.internal.util.MethodInvoker.invoke(MethodInvoker.java:50)
>         ... 81 more
> 2012-09-23 10:50:11.134::INFO:  Started
> SelectChannelConnector@0.0.0.0:8080
> [INFO] Started Jetty Server
>
> Why is it trying to advise the org.hibernate.Sesssion.invalidate() method
> if I explicitly
> annotated: @Advise(serviceInterface=org.apache.tapestry5.services.Session.class),
> is it a bug?
>
> Anyway, do you think this is the best way of doing this, or is there any
> other better mechanism to intercept a finished session?
>
> Best regards,
> Matias.
>

Re: Advisin Tapestry Session

Posted by Lance Java <la...@googlemail.com>.
As bobharner said, there is a non-tapestry way of doing this with a
HttpSessionListener (although I would synchronize his solution to make it
thread-safe).

Another option is to override / decorate the TapestrySessionFactory [1] to
return a Session instance with the hooks that you want.

[1]
http://tapestry.apache.org/5.3.5/apidocs/org/apache/tapestry5/internal/services/TapestrySessionFactory.html





--
View this message in context: http://tapestry.1045711.n5.nabble.com/Advisin-Tapestry-Session-tp5716457p5716464.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Advisin Tapestry Session

Posted by Bob Harner <bo...@gmail.com>.
I can't answer your specific question, but as an alternative approach
(and not tied to Tapestry) you could implement an HttpSessionListener,
like this (and configure it in your web.xml):

import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import org.apache.log4j.Logger;

public class SessionListener implements HttpSessionListener
{
    private static final Logger logger =
Logger.getLogger(SessionListener.class);
    private static int currentSessions = 0;
    private static int totalSessions  = 0;

    public void sessionCreated(HttpSessionEvent httpSessionEvent) {
        currentSessions++;
        totalSessions++;
        logger.info("Session created: current sessions = " +
currentSessions + "; sessions since app started = " + totalSessions);
    }
    public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
        currentSessions--;
        logger.info("Session destroyed: current sessions = " +
currentSessions + "; sessions since app started = " + totalSessions);
    }
}

Your sessionDestroyed() method is called just *before* the session is
invalidated (e.g. from a timeout).


On Sun, Sep 23, 2012 at 9:58 AM, Matías Blasi <ma...@gmail.com> wrote:
> Hi all,
>
> I'm trying to implement a mechanism to track each user session, at least
> the login and logout events.
> The login is easy, because it occurs in a defined place, but the logout can
> occurs explicitly or because of a timeout, so I thought in advising the
> Tapestry Session.invalidate() method:
>
>     @SuppressWarnings("unchecked")
> @Advise(serviceInterface=org.apache.tapestry5.services.Session.class)
>     public static void adviseUserActivity(final UserService userService,
> final MethodAdviceReceiver receiver)
>     throws SecurityException, NoSuchMethodException {
>     MethodAdvice advise = new MethodAdvice() {
> @Override
> public void advise(MethodInvocation invocation) {
> userService.endSession(userService.getCurrentUser().getUserName());
> }
>     };
>
>
>   receiver.adviseMethod(receiver.getInterface().getMethod("invalidate"),
> advise);
>     }
>
> But oddly, I'm getting the following error when starting up the application:
>
> Caused by: java.lang.RuntimeException: Error invoking method public static
> void
> com.app.services.AppModule.adviseUserActivity(com.app.core.services.UserService,org.apache.tapestry5.ioc.MethodAdviceReceiver)
> throws java.lang.SecurityException,java.lang.NoSuchMethodException:
> org.hibernate.Session.invalidate()
>         at
> org.apache.tapestry5.ioc.internal.util.MethodInvoker.invoke(MethodInvoker.java:61)
>         at
> org.apache.tapestry5.ioc.internal.util.LoggingInvokableWrapper.invoke(LoggingInvokableWrapper.java:42)
>         at
> org.apache.tapestry5.ioc.internal.OperationTrackerImpl.invoke(OperationTrackerImpl.java:74)
>         ... 79 more
> Caused by: java.lang.NoSuchMethodException:
> org.hibernate.Session.invalidate()
>         at java.lang.Class.getMethod(Class.java:1622)
>         at com.app.services.AppModule.adviseUserActivity(AppModule.java:198)
>         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>         at
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
>         at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
>         at java.lang.reflect.Method.invoke(Method.java:616)
>         at
> org.apache.tapestry5.ioc.internal.util.MethodInvoker.invoke(MethodInvoker.java:50)
>         ... 81 more
> 2012-09-23 10:50:11.134::INFO:  Started SelectChannelConnector@0.0.0.0:8080
> [INFO] Started Jetty Server
>
> Why is it trying to advise the org.hibernate.Sesssion.invalidate() method
> if I explicitly
> annotated: @Advise(serviceInterface=org.apache.tapestry5.services.Session.class),
> is it a bug?
>
> Anyway, do you think this is the best way of doing this, or is there any
> other better mechanism to intercept a finished session?
>
> Best regards,
> Matias.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org