Simplifying the management of a multi-workspace application
(one-shot logout for all opened sessions)
Use org.exoplatform.services.jcr.ext.common.SessionProvider
which
is responsible for caching/obtaining your JCR Sessions and closing all
opened sessions at once.
public class SessionProvider implements SessionLifecycleListener {
/**
* Creates a SessionProvider for a certain identity
* @param cred
*/
public SessionProvider(Credentials cred)
/**
* Gets the session from internal cache or creates and caches a new one
*/
public Session getSession(String workspaceName, ManageableRepository repository)
throws LoginException, NoSuchWorkspaceException, RepositoryException
/**
* Calls a logout() method for all cached sessions
*/
public void close()
/**
* a Helper for creating a System session provider
* @return System session
*/
public static SessionProvider createSystemProvider()
/**
* a Helper for creating an Anonimous session provider
* @return System session
*/
public static SessionProvider createAnonimProvider()
/**
* Helper for creating session provider from AccessControlEntry.
*
* @return System session
*/
SessionProvider createProvider(List<AccessControlEntry> accessList)
/**
* Remove the session from the cache
*/
void onCloseSession(ExtendedSession session)
/**
* Gets the current repository used
*/
ManageableRepository getCurrentRepository()
/**
* Gets the current workspace used
*/
String getCurrentWorkspace()
/**
* Set the current repository to use
*/
void setCurrentRepository(ManageableRepository currentRepository)
/**
* Set the current workspace to use
*/
void setCurrentWorkspace(String currentWorkspace)
}
The SessionProvider is a request or user object, depending on your policy. Create it with your application before performing JCR operations, then use it to obtain the Sessions and close at the end of an application session (request). See the following example:
// (1) obtain current javax.jcr.Credentials, for example get it from AuthenticationService
Credentials cred = ....
// (2) create SessionProvider for current user
SessionProvider sessionProvider = new SessionProvider(ConversationState.getCurrent());
// NOTE: for creating an Anonymous or System Session use the corresponding static SessionProvider.create...() method
// Get appropriate Repository as described in "Obtaining Repository object" section for example
ManageableRepository repository = (ManageableRepository) ctx.lookup("repository");
// get an appropriate workspace's session
Session session = sessionProvider.getSession("collaboration", repository);
.........
// your JCR code
.........
// Close the session provider
sessionProvider.close();
As shown above, creating the SessionProvider involves multiple
steps and you may not want to repeat them each time you need to get a
JCR session. To avoid the plumbing code, SessionProviderService
is provided that aims at helping you get a
SessionProvider
object.
The org.exoplatform.services.jcr.ext.app.SessionProviderService
interface is defined as follows:
public interface SessionProviderService {
void setSessionProvider(Object key, SessionProvider sessionProvider);
SessionProvider getSessionProvider(Object key);
void removeSessionProvider(Object key);
}
Using this service is pretty straightforward, the main contract of an implemented component is getting a SessionProvider by key. eXo Platform provides the following implementation:
Implementation | Description | Typical Use |
---|---|---|
org.exoplatform.services.jcr.ext.app.ThreadLocalSessionProviderService | per-request style: Keep a single SessionProvider in a static ThreadLocal variable. | Always use null for the key. |
For the implementation, your code should follow the following sequence:
Call SessionProviderService.setSessionProvider(Object key, SessionProvider sessionProvider)
at the beginning of a business
request for Stateless application or application's session for the Statefull policy.
Call SessionProviderService.getSessionProvider(Object key)
for obtaining a SessionProvider
object.
Call SessionProviderService.removeSessionProvider(Object key)
at the end of a business request for Stateless application or
application's session for the Statefull policy.