Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2009 eXo Platform SAS.
   *
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
   * the License, or (at your option) any later version.
   *
   * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
 package org.exoplatform.services.jcr.ext.common;
 
 
 import java.util.List;
 import java.util.Map;
 
Created by The eXo Platform SAS .
Provides JCR Session for client program. Usually it is per client thread object Session creates with Repository.login(..) method and then can be stored in some cache if neccessary.

Author(s):
Gennady Azarenkov
Version:
$Id: SessionProvider.java 34445 2009-07-24 07:51:18Z dkatayev $
 
 
 public class SessionProvider implements SessionLifecycleListener
 {

   
Constant for handlers.
 
    public final static String SESSION_PROVIDER = "JCRsessionProvider";

   
Sessions cache.
 
    private final Map<StringExtendedSessioncache;

   
System session marker.
 
    private boolean isSystem;
 
 
    private String currentWorkspace;
 
    private boolean closed;
    
    private ConversationState conversationState;

   
Creates SessionProvider for certain identity.

Parameters:
userState
 
    public SessionProvider(ConversationState userState)
    {
       this(false);
       if (userState.getAttribute() == null)
          userState.setAttribute(this);
    }

   
Creates SessionProvider for a dynamic identity.

Parameters:
membershipEntries the expected memberships
 
    private SessionProvider(HashSet<MembershipEntrymembershipEntries)
    {
       this(false);
       Identity id = new Identity(.membershipEntries);
       this. = new ConversationState(id);
    }

   
Internal constructor.

Parameters:
isSystem
   private SessionProvider(boolean isSystem)
   {
      this. = isSystem;
      this. = new HashMap<StringExtendedSession>();
      this. = false;
   }

   
Helper for creating System session provider.

Returns:
System session
   public static SessionProvider createSystemProvider()
   {
      return new SessionProvider(true);
   }

   
Helper for creating Anonymous session provider.

Returns:
System session
   public static SessionProvider createAnonimProvider()
   {
      return new SessionProvider(new ConversationState(id));
   }
    
Return SessionProvider for a given list of AccessControlEntry.

Parameters:
accessList list of AccessControlEntry
Returns:
SessionProvider
   public static SessionProvider createProvider(List<AccessControlEntryaccessList)
   {
      if (accessList == null || accessList.isEmpty())
      {
         return createAnonimProvider();
      }
      else
      {
         HashSet<MembershipEntrymembershipEntries = new HashSet<MembershipEntry>();
         for (AccessControlEntry ace : accessList)
         {
            membershipEntries.add(ace.getMembershipEntry());
         }
         return new SessionProvider(membershipEntries);
      }
   }

   
Gets the session from internal cache or creates and caches new one.

Parameters:
workspaceName
repository
Returns:
session
Throws:
javax.jcr.LoginException
javax.jcr.NoSuchWorkspaceException
javax.jcr.RepositoryException
   public synchronized Session getSession(String workspaceNameManageableRepository repositorythrows LoginException,
   {
      if ()
      {
         throw new IllegalStateException("Session provider already closed");
      }
      if (workspaceName == null)
      {
         throw new IllegalArgumentException("Workspace Name is null");
      }
      ExtendedSession session = .get(key(repositoryworkspaceName));
      // create and cache new session
      if (session == null)
      {
         if ( != null)
         {
            session =
                     (ExtendedSessionrepository.getDynamicSession(workspaceName.getIdentity()
                              .getMemberships());
         }
         else if (!)
         {
            session = (ExtendedSession)repository.login(workspaceName);
         }
         else
         {
            session = (ExtendedSession)repository.getSystemSession(workspaceName);
         }
         session.registerLifecycleListener(this);
         .put(key(repositoryworkspaceName), session);
      }
      return session;
   }

   
Calls logout() method for all cached sessions. Session will be removed from cache by the listener (this provider) via ExtendedSession.logout().
   public synchronized void close()
   {
      if ()
      {
         throw new IllegalStateException("Session provider already closed");
      }
       = true;
      for (ExtendedSession session : (ExtendedSession[]).values().toArray(
         new ExtendedSession[.values().size()]))
         session.logout();
      // the cache already empty (logout listener work, see onCloseSession())
      // just to be sure
      .clear();
   }
   /*
    * (non-Javadoc)
    * @see
    * org.exoplatform.services.jcr.core.SessionLifecycleListener#onCloseSession(org.exoplatform.services
    * .jcr.core.ExtendedSession)
    */
   public synchronized void onCloseSession(ExtendedSession session)
   {
      this..remove(key((ManageableRepository)session.getRepository(), session.getWorkspace().getName()));
   }

   
Key generator for sessions cache.

Parameters:
repository
workspaceName
Returns:
   private String key(ManageableRepository repositoryString workspaceName)
   {
      String repositoryName = repository.getConfiguration().getName();
      return repositoryName + workspaceName;
   }
    
Return Current Repository.

Returns:
ManageableRepository
   {
      return ;
   }
    
Return Current Workspace used.

Returns:
String
   {
      return ;
   }
    
Set Repository.

Parameters:
currentRepository
   public void setCurrentRepository(ManageableRepository currentRepository)
   {
      this. = currentRepository;
   }
    
Set Workspace.

Parameters:
currentWorkspace
   public void setCurrentWorkspace(String currentWorkspace)
   {
      this. = currentWorkspace;
   }
New to GrepCode? Check out our FAQ X