Cleaning up that last message...
Ignore the last sentence; Tapestry doesn't store the username in the
session; my login method does that.
The "new way" code could be cleaned up, doesn't need the
ApplicationStateManager at all.
String key = "state:" + appName + ":visit";
Visit visit = (Visit) session.getAttribute(key);
Bryan Lewis wrote:
>That's what I've been doing, keeping a static map in my Engine to
>associate sessions with visits.
>
>My login method does:
>
> // After a successful login, associate the session with the visit.
> WebSession session =
>cycle.getInfrastructure().getRequest().getSession(false);
> String sessionId = session.getId();
> mapSessionsToVisits.put(sessionId, visit);
>
>In the sessionDestroyed() listener:
>
> Visit visit = (Visit) mapSessionsToVisits.get(sessionId);
> if (visit != null) {
> String userName = visit.getUserName();
> }
> mapSessionsToVisits.remove(sessionId);
>
>But I've been trying out your suggestion of getting the visit from the
>ApplicationStateManager via the ServletContext:
>
> ServletContext sc = session.getServletContext();
> String appName = getAppName(); // basically same as
>sc.getServletContextName()
> Registry registry = (Registry)
>sc.getAttribute("org.apache.tapestry.Registry:" + appName);
> ApplicationStateManager asm = (ApplicationStateManager)
>
>registry.getService("tapestry.state.ApplicationStateManager",
> ApplicationStateManager.class);
>
> // This way doesn't work; runs into NPE because the SessionScopeManager
> // tries to go through the request which doesn't exist at this point:
> //Visit visit = (Visit) asm.get("visit");
> // So get it directly from the session.
> String key = "state:" + appName + ":visit";
> Visit visit = (Visit) session.getAttribute(key);
>
>It works... returns the same Visit as the old way. I thought the new
>way might be better because it would eliminate the map, but it's more
>dependent on Tapestry internals and private strings.
>
>By the way, if you only need to get the user's name (and don't mind one
>private string), Tapestry stores it in the session:
>
> String username = session.getAttribute("tapestry.engine.username");
>
>
>
>
>
>
>James Carman wrote:
>
>
>
>>Perhaps you can key your logged in user list (make it a map) by the session
>>id? Wouldn't that work?
>>
>>-----Original Message-----
>>From: Oscar Picasso [mailto:[EMAIL PROTECTED]
>>Sent: Thursday, May 04, 2006 12:54 PM
>>To: Tapestry users
>>Subject: Re: List of logged users
>>
>>I have tried to implement the HttpSessionListerner solution.
>>
>>I have a slight problem. In the HttpSessionListener.destroy method I need to
>>get a session scoped "user" state object.
>>
>>This "user" state object is then removed from the list of currently logged
>>users. It works fine except I get an IllegalStateException warning.
>>
>>I guess that's because when reaching the destroy method, the session has
>>already been invalidated.
>>
>>How to avoid this warning ?
>>
>>
>>---------------------------------
>>New Yahoo! Messenger with Voice. Call regular phones from your PC and save
>>big.
>>
>>
>>
>>---------------------------------------------------------------------
>>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]
>
>
>