You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by John <jo...@quivinco.com> on 2013/05/01 10:49:40 UTC

SessionListener interacting with Tapestry services?

I configure a SessionListener in web.xml:

<listener>

<listener-class>epulse.audit.manager.SessionListener</listener-class>

</listener>

And in the listener I have directly wired the Logger so:

public class SessionListener implements HttpSessionListener {


/** The log. */

private static Logger log = Logger.getLogger(SessionListener.class);


/** The audit log. */

@Inject

private AuditDAO auditDAO;    


I now want to inject a DAO to perform auditing but of course the SessionListener is managed by the web container, what to do?

As it happens at present the audit log is just a wrapper service for SLF4j so I can directly instantiate the audit logging the same as the regular log.

John

Re: SessionListener interacting with Tapestry services?

Posted by John <jo...@quivinco.com>.
Of course I'm seeing that my idea can't hook up the logger to the calling class type which I dare say the Tapestry injection performs.

Is there another way? I don't want to wire the logger directly.
  ----- Original Message ----- 
  From: John 
  To: Tapestry users 
  Sent: Thursday, May 02, 2013 10:15 AM
  Subject: Re: SessionListener interacting with Tapestry services?


  this works 
      auditDAO = registry.getService(AuditDAO.class);

  but this does not
      log = registry.getService(Logger.class);

  [ERROR] TapestryModule.RequestExceptionHandler Processing of request failed with
   uncaught exception: No service implements the interface org.slf4j.Logger.
  java.lang.RuntimeException: No service implements the interface org.slf4j.Logger
    ----- Original Message ----- 
    From: Taha Hafeez Siddiqi 
    To: Tapestry users 
    Sent: Wednesday, May 01, 2013 9:55 AM
    Subject: Re: SessionListener interacting with Tapestry services?


    Hi John

    You can directly extract the registry from the ServetContext 

    using http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/TapestryFilter.html#REGISTRY_CONTEXT_NAME


    Note: NOT TESTED

    public class MyListener implements HttpServletListener {
       
       public void sessionCreated(HttpServletRequest e){
          Registry registry = (Registry)e.getSession().getServletContext().getAttribute(TapestryFilter.REGISTRY_CONTEXT_NAME);
          MyDAO dao = registry.getService(MyDAO.class);
       }

    }

    regards
    Taha



    On 01-May-2013, at 2:19 PM, John <jo...@quivinco.com> wrote:

    > I configure a SessionListener in web.xml:
    > 
    > <listener>
    > 
    > <listener-class>epulse.audit.manager.SessionListener</listener-class>
    > 
    > </listener>
    > 
    > And in the listener I have directly wired the Logger so:
    > 
    > public class SessionListener implements HttpSessionListener {
    > 
    > 
    > /** The log. */
    > 
    > private static Logger log = Logger.getLogger(SessionListener.class);
    > 
    > 
    > /** The audit log. */
    > 
    > @Inject
    > 
    > private AuditDAO auditDAO;    
    > 
    > 
    > I now want to inject a DAO to perform auditing but of course the SessionListener is managed by the web container, what to do?
    > 
    > As it happens at present the audit log is just a wrapper service for SLF4j so I can directly instantiate the audit logging the same as the regular log.
    > 
    > John


Re: SessionListener interacting with Tapestry services?

Posted by John <jo...@quivinco.com>.
this works 
    auditDAO = registry.getService(AuditDAO.class);

but this does not
    log = registry.getService(Logger.class);

[ERROR] TapestryModule.RequestExceptionHandler Processing of request failed with
 uncaught exception: No service implements the interface org.slf4j.Logger.
java.lang.RuntimeException: No service implements the interface org.slf4j.Logger
  ----- Original Message ----- 
  From: Taha Hafeez Siddiqi 
  To: Tapestry users 
  Sent: Wednesday, May 01, 2013 9:55 AM
  Subject: Re: SessionListener interacting with Tapestry services?


  Hi John

  You can directly extract the registry from the ServetContext 

  using http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/TapestryFilter.html#REGISTRY_CONTEXT_NAME


  Note: NOT TESTED

  public class MyListener implements HttpServletListener {
     
     public void sessionCreated(HttpServletRequest e){
        Registry registry = (Registry)e.getSession().getServletContext().getAttribute(TapestryFilter.REGISTRY_CONTEXT_NAME);
        MyDAO dao = registry.getService(MyDAO.class);
     }

  }

  regards
  Taha



  On 01-May-2013, at 2:19 PM, John <jo...@quivinco.com> wrote:

  > I configure a SessionListener in web.xml:
  > 
  > <listener>
  > 
  > <listener-class>epulse.audit.manager.SessionListener</listener-class>
  > 
  > </listener>
  > 
  > And in the listener I have directly wired the Logger so:
  > 
  > public class SessionListener implements HttpSessionListener {
  > 
  > 
  > /** The log. */
  > 
  > private static Logger log = Logger.getLogger(SessionListener.class);
  > 
  > 
  > /** The audit log. */
  > 
  > @Inject
  > 
  > private AuditDAO auditDAO;    
  > 
  > 
  > I now want to inject a DAO to perform auditing but of course the SessionListener is managed by the web container, what to do?
  > 
  > As it happens at present the audit log is just a wrapper service for SLF4j so I can directly instantiate the audit logging the same as the regular log.
  > 
  > John


Re: SessionListener interacting with Tapestry services?

Posted by Lance Java <la...@googlemail.com>.
public class HttpServletListenerHelperImpl implements
HttpServletListenerHelper {

                private SomeDao someDao;



                public SessionListenerHelperImpl(SomeDao someDao) {

                                doSomeFunkyInitializationWithDao(someDao);

                                this.someDao = someDao;

                }



                @Override

                public void sessionCreated(HttpSession session) {

                                // TODO: implement

                }

}



AppModule.java

                public static void bind(ServiceBinder binder) {


binder.bind(HttpServletListenerHelper.class,
HttpServletListenerHelperImpl.class);

                }



public class MyHttpSessionListener implements HttpSessionListener {

                public void sessionCreated(HttpSessionEvent event) {

                                Registry registry = (Registry)
event.getSession().getServletContext().getAttribute(TapestryFilter.REGISTRY_CONTEXT_NAME);

                                HttpServletListenerHelper helper =
registry.getService(HttpServletListenerHelper.class);

                                helper.sessionCreated(event.getSession());

                }



                public void sessionDestroyed(HttpSessionEvent event) {}

}

Re: SessionListener interacting with Tapestry services?

Posted by John <jo...@quivinco.com>.
What would that look like in code Lance?

Can the solution you invisage work around the need to locate the Tapestry registry via the servlet context in the session listener?

John
  ----- Original Message ----- 
  From: Lance Java 
  To: Tapestry users 
  Sent: Wednesday, May 01, 2013 12:38 PM
  Subject: Re: SessionListener interacting with Tapestry services?


  I think I might've answered a question you didn't ask!

  If you want to do something complex in your listeners constructor with the
  DAO, don't!

  Instead, create a new IOC service and delegate your methods through to it
  in your session listener

Re: SessionListener interacting with Tapestry services?

Posted by John <jo...@quivinco.com>.
I'm using session listener to have a single point for destroying objects I placed in the session and for logging the time the user was logged in for.

John
  ----- Original Message ----- 
  From: Taha Siddiqi 
  To: Tapestry users 
  Sent: Wednesday, May 01, 2013 1:05 PM
  Subject: Re: SessionListener interacting with Tapestry services?


  I agree with Lance. 

  BTW what is your use case ? May be we can suggest a way  within the IOC container.


  On May 1, 2013, at 5:08 PM, Lance Java wrote:

  > I think I might've answered a question you didn't ask!
  > 
  > If you want to do something complex in your listeners constructor with the
  > DAO, don't!
  > 
  > Instead, create a new IOC service and delegate your methods through to it
  > in your session listener


Re: SessionListener interacting with Tapestry services?

Posted by Taha Siddiqi <ta...@gmail.com>.
I agree with Lance. 

BTW what is your use case ? May be we can suggest a way  within the IOC container.


On May 1, 2013, at 5:08 PM, Lance Java wrote:

> I think I might've answered a question you didn't ask!
> 
> If you want to do something complex in your listeners constructor with the
> DAO, don't!
> 
> Instead, create a new IOC service and delegate your methods through to it
> in your session listener


Re: SessionListener interacting with Tapestry services?

Posted by Lance Java <la...@googlemail.com>.
I think I might've answered a question you didn't ask!

If you want to do something complex in your listeners constructor with the
DAO, don't!

Instead, create a new IOC service and delegate your methods through to it
in your session listener

Re: SessionListener interacting with Tapestry services?

Posted by Lance Java <la...@googlemail.com>.
The DAO reference will never be null. It might be a proxy that points to a
null implementation. The proxy will instantiate the DAO implementation the
first time a method is invoked on the proxy.

You can use @EagerLoad to load your DAO at app startup to avoid lazy
loading.
On 1 May 2013 11:07, "John" <jo...@quivinco.com> wrote:

> Thanks for this, it does work. :)
>
> I'm disliking that I have to have a request to get the servlet context
> though, this makes the instantiation a bit messy as I have to check the dao
> reference is not null.
>
> John
>   ----- Original Message -----
>   From: Taha Hafeez Siddiqi
>   To: Tapestry users
>   Sent: Wednesday, May 01, 2013 9:55 AM
>   Subject: Re: SessionListener interacting with Tapestry services?
>
>
>   Hi John
>
>   You can directly extract the registry from the ServetContext
>
>   using
> http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/TapestryFilter.html#REGISTRY_CONTEXT_NAME
>
>
>   Note: NOT TESTED
>
>   public class MyListener implements HttpServletListener {
>
>      public void sessionCreated(HttpServletRequest e){
>         Registry registry =
> (Registry)e.getSession().getServletContext().getAttribute(TapestryFilter.REGISTRY_CONTEXT_NAME);
>         MyDAO dao = registry.getService(MyDAO.class);
>      }
>
>   }
>
>   regards
>   Taha
>
>
>
>   On 01-May-2013, at 2:19 PM, John <jo...@quivinco.com> wrote:
>
>   > I configure a SessionListener in web.xml:
>   >
>   > <listener>
>   >
>   > <listener-class>epulse.audit.manager.SessionListener</listener-class>
>   >
>   > </listener>
>   >
>   > And in the listener I have directly wired the Logger so:
>   >
>   > public class SessionListener implements HttpSessionListener {
>   >
>   >
>   > /** The log. */
>   >
>   > private static Logger log = Logger.getLogger(SessionListener.class);
>   >
>   >
>   > /** The audit log. */
>   >
>   > @Inject
>   >
>   > private AuditDAO auditDAO;
>   >
>   >
>   > I now want to inject a DAO to perform auditing but of course the
> SessionListener is managed by the web container, what to do?
>   >
>   > As it happens at present the audit log is just a wrapper service for
> SLF4j so I can directly instantiate the audit logging the same as the
> regular log.
>   >
>   > John
>
>

Re: SessionListener interacting with Tapestry services?

Posted by John <jo...@quivinco.com>.
Thanks for this, it does work. :)

I'm disliking that I have to have a request to get the servlet context though, this makes the instantiation a bit messy as I have to check the dao reference is not null.

John
  ----- Original Message ----- 
  From: Taha Hafeez Siddiqi 
  To: Tapestry users 
  Sent: Wednesday, May 01, 2013 9:55 AM
  Subject: Re: SessionListener interacting with Tapestry services?


  Hi John

  You can directly extract the registry from the ServetContext 

  using http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/TapestryFilter.html#REGISTRY_CONTEXT_NAME


  Note: NOT TESTED

  public class MyListener implements HttpServletListener {
     
     public void sessionCreated(HttpServletRequest e){
        Registry registry = (Registry)e.getSession().getServletContext().getAttribute(TapestryFilter.REGISTRY_CONTEXT_NAME);
        MyDAO dao = registry.getService(MyDAO.class);
     }

  }

  regards
  Taha



  On 01-May-2013, at 2:19 PM, John <jo...@quivinco.com> wrote:

  > I configure a SessionListener in web.xml:
  > 
  > <listener>
  > 
  > <listener-class>epulse.audit.manager.SessionListener</listener-class>
  > 
  > </listener>
  > 
  > And in the listener I have directly wired the Logger so:
  > 
  > public class SessionListener implements HttpSessionListener {
  > 
  > 
  > /** The log. */
  > 
  > private static Logger log = Logger.getLogger(SessionListener.class);
  > 
  > 
  > /** The audit log. */
  > 
  > @Inject
  > 
  > private AuditDAO auditDAO;    
  > 
  > 
  > I now want to inject a DAO to perform auditing but of course the SessionListener is managed by the web container, what to do?
  > 
  > As it happens at present the audit log is just a wrapper service for SLF4j so I can directly instantiate the audit logging the same as the regular log.
  > 
  > John


Re: SessionListener interacting with Tapestry services?

Posted by Taha Hafeez Siddiqi <ta...@gmail.com>.
Hi John

You can directly extract the registry from the ServetContext 

using http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/TapestryFilter.html#REGISTRY_CONTEXT_NAME


Note: NOT TESTED

public class MyListener implements HttpServletListener {
   
   public void sessionCreated(HttpServletRequest e){
      Registry registry = (Registry)e.getSession().getServletContext().getAttribute(TapestryFilter.REGISTRY_CONTEXT_NAME);
      MyDAO dao = registry.getService(MyDAO.class);
   }

}

regards
Taha



On 01-May-2013, at 2:19 PM, John <jo...@quivinco.com> wrote:

> I configure a SessionListener in web.xml:
> 
> <listener>
> 
> <listener-class>epulse.audit.manager.SessionListener</listener-class>
> 
> </listener>
> 
> And in the listener I have directly wired the Logger so:
> 
> public class SessionListener implements HttpSessionListener {
> 
> 
> /** The log. */
> 
> private static Logger log = Logger.getLogger(SessionListener.class);
> 
> 
> /** The audit log. */
> 
> @Inject
> 
> private AuditDAO auditDAO;    
> 
> 
> I now want to inject a DAO to perform auditing but of course the SessionListener is managed by the web container, what to do?
> 
> As it happens at present the audit log is just a wrapper service for SLF4j so I can directly instantiate the audit logging the same as the regular log.
> 
> John