Hi Peter,
State objects are not the same as HiveMind services. So reg.getService
will return a service you have defined with <service-point /> in a
hivemodule.xml. However these are typically equivalent to singletons and
therefore shared across all sessions. A state object is managed by the
ApplicationStateManager and can be either a global object (in many ways
a HiveMind service is equivalent), or a session object which is per user
web session.
Note that you only need to get the ApplicationStateManager object once
(use lazy initialization), as it will last for the lifetime of the web
application, so something like:
class MySessionListener implements HttpSessionListener {
private ApplicationStateManager asm;
public void sessionCreated(HttpSessionEvent event) {
if (asm == null) {
Registry reg =
(Registry)event.getSession().getServletContext().getAttribute("org.apache.tapestry.Registry:IRM");
asm =
(ApplicationStateManager)reg.getService(ApplicationStateManager.class);
}
Visit visit = (Visit)asm.get("visit");
// do stuff
}
}
Cheers
Richard
Peter Stavrinides wrote:
Thanks Richard, this is truly a bit of magic, one question though
regarding the last step:
Is there any reason why I would need to use the
ApplicationStateManager object, can I rather access a service directly
using getService() ?
For instance, I get the registry like so:
Registry reg = (Registry)
event.getSession().getServletContext().getAttribute("org.apache.tapestry.Registry:IRM");
//where IRM is the name of the servlet
and then use something like:
reg.getService(Visit.class);
Thanks again,
Peter
Richard Kirby wrote:
Hi Peter,
One way of doing this is to access the HiveMind Registry object that
Tapestry creates, from within your listener class, so that you can
then access the ApplicationStateManager, and from that access your ASO.
However, you have to use a little magic to access the Registry object:
1. From the sessionCreated/sessionDestroyed method you have access to
the HttpSessionEvent object.
2. From the HttpSessionEvent object you have access to the
HttpSession object.
3. From the HttpSession object you have access to the ServletContext
4. From the ServletContext object you can look up the HiveMind
Registry using the getAttribute method with the key
"org.apache.tapestry.Registry:SERVLET_NAME" where SERVLET_NAME is the
name of the Tapestry application servlet you have specified in your
web.xml (this is the magic bit since it requires knowing how Tapestry
squirrels away the Registry object).
5. You can then get the ApplicationStateManager object from the
Registry, and finally your ASO.
Hope that helps
Richard.
Peter Stavrinides wrote:
What is the best approach for the following scenario:
I have a listener class that listens for session activity, its
configured only in my web.xml (not for instance in hivemind)
I have a state object that I need to inject into the listener class,
but since I cannot make the listener class abstract how will I
inject my state object? or what can I do otherwise?
Thanks
Peter
---------------------------------------------------------------------
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]
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]