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