001/*
002 *  Copyright 2010 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.plugins.repository.provider;
017
018import javax.jcr.AccessDeniedException;
019import javax.jcr.RepositoryException;
020import javax.security.auth.Subject;
021
022import org.apache.jackrabbit.core.RepositoryContext;
023import org.apache.jackrabbit.core.XASessionImpl;
024import org.apache.jackrabbit.core.config.WorkspaceConfig;
025import org.apache.jackrabbit.core.security.authentication.AuthContext;
026
027/**
028 * Implementation of a JCR Session that is poolable.<br>
029 * The logout method actually bring the Session back into the pool.
030 */
031public class AmetysSession extends XASessionImpl
032{
033    private AmetysRepository _repository;
034    
035    /**
036     * Create a new Session.
037     * @param repositoryCtx repository context
038     * @param loginCtx login context containing authenticated subject
039     * @param wspConfig workspace configuration
040     * @throws AccessDeniedException if the given subject is not granted access
041     *                               to the specified workspace
042     * @throws RepositoryException   if another error occurs
043     */
044    protected AmetysSession(RepositoryContext repositoryCtx, AuthContext loginCtx, WorkspaceConfig wspConfig) throws AccessDeniedException, RepositoryException
045    {
046        super(repositoryCtx, loginCtx, wspConfig);
047        _repository = (AmetysRepository) repositoryContext.getRepository();
048    }
049    
050    /**
051     * Create a new Session.
052     * @param repositoryCtx repository context
053     * @param sub authenticated subject
054     * @param wspConfig workspace configuration
055     * @throws AccessDeniedException if the given subject is not granted access
056     *                               to the specified workspace
057     * @throws RepositoryException   if another error occurs
058     */
059    public AmetysSession(RepositoryContext repositoryCtx, Subject sub, WorkspaceConfig wspConfig) throws AccessDeniedException, RepositoryException
060    {
061        super(repositoryCtx, sub, wspConfig);
062        _repository = (AmetysRepository) repositoryContext.getRepository();
063    }
064    
065    @Override
066    public synchronized void logout()
067    {
068        LogoutManager logoutManager = _repository.getLogoutManager();
069        
070        if (logoutManager != null)
071        {
072            logoutManager.logout(this);
073        }
074        else
075        {
076            super.logout();
077        }
078    }
079    
080    /**
081     * Logout this Session without calling the LogoutManager.
082     */
083    public synchronized void forceLogout()
084    {
085        super.logout();
086    }
087}