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