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 java.util.concurrent.atomic.AtomicInteger;
019
020import javax.jcr.AccessDeniedException;
021import javax.jcr.Credentials;
022import javax.jcr.LoginException;
023import javax.jcr.NoSuchWorkspaceException;
024import javax.jcr.RepositoryException;
025import javax.jcr.Session;
026import javax.security.auth.Subject;
027
028import org.apache.jackrabbit.core.RepositoryImpl;
029import org.apache.jackrabbit.core.SessionImpl;
030import org.apache.jackrabbit.core.config.RepositoryConfig;
031import org.apache.jackrabbit.core.config.WorkspaceConfig;
032import org.apache.jackrabbit.core.security.authentication.AuthContext;
033
034/**
035 * JCR Repository implementation, based on Jackrabbit and allowing to change the logout behaviour for Sessions
036 */
037public class AmetysRepository extends RepositoryImpl
038{
039    private LogoutManager _logoutManager;
040    private AtomicInteger _sessionCount = new AtomicInteger();
041    private boolean _isShuttingDown;
042    
043    /**
044     * Creates a Repository.
045     * @param config the repository configuration.
046     * @throws RepositoryException if an error occurs.
047     */
048    public AmetysRepository(RepositoryConfig config) throws RepositoryException
049    {
050        super(config);
051    }
052    
053    @Override
054    protected SessionImpl createSessionInstance(AuthContext loginContext, WorkspaceConfig wspConfig) throws AccessDeniedException, RepositoryException
055    {
056        return new AmetysSession(context, loginContext, wspConfig);
057    }
058    
059    @Override
060    protected SessionImpl createSessionInstance(Subject subject, WorkspaceConfig wspConfig) throws AccessDeniedException, RepositoryException
061    {
062        return new AmetysSession(context, subject, wspConfig);
063    }
064    
065    void setLogoutManager(LogoutManager logoutManager)
066    {
067        _logoutManager = logoutManager;
068    }
069    
070    LogoutManager getLogoutManager()
071    {
072        return _logoutManager;
073    }
074    
075    int getSessionCount()
076    {
077        return _sessionCount.get();
078    }
079    
080    @Override
081    protected void onSessionCreated(SessionImpl session)
082    {
083        super.onSessionCreated(session);
084        
085        if (session instanceof AmetysSession)
086        {
087            _sessionCount.incrementAndGet();
088        }
089    }
090    
091    @Override
092    public void loggedOut(SessionImpl session)
093    {
094        super.loggedOut(session);
095        
096        if (session instanceof AmetysSession)
097        {
098            _sessionCount.decrementAndGet();
099        }
100    }
101    
102    @Override
103    protected synchronized void doShutdown() 
104    {
105        _isShuttingDown = true;
106        super.doShutdown();
107    }
108    
109    @Override
110    public Session login(Credentials credentials, String workspaceName) throws LoginException, NoSuchWorkspaceException, RepositoryException
111    {
112        if (_isShuttingDown)
113        {
114            throw new RepositoryException("Cannot login as the repository is shutting down.");
115        }
116        return super.login(credentials, workspaceName);
117    }
118}