001 /* 002 * JBoss DNA (http://www.jboss.org/dna) 003 * See the COPYRIGHT.txt file distributed with this work for information 004 * regarding copyright ownership. Some portions may be licensed 005 * to Red Hat, Inc. under one or more contributor license agreements. 006 * See the AUTHORS.txt file in the distribution for a full listing of 007 * individual contributors. 008 * 009 * JBoss DNA is free software. Unless otherwise indicated, all code in JBoss DNA 010 * is licensed to you under the terms of the GNU Lesser General Public License as 011 * published by the Free Software Foundation; either version 2.1 of 012 * the License, or (at your option) any later version. 013 * 014 * JBoss DNA is distributed in the hope that it will be useful, 015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 017 * Lesser General Public License for more details. 018 * 019 * You should have received a copy of the GNU Lesser General Public 020 * License along with this software; if not, write to the Free 021 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 022 * 02110-1301 USA, or see the FSF site: http://www.fsf.org. 023 */ 024 025 package org.jboss.dna.repository.util; 026 027 import java.util.Map; 028 import java.util.concurrent.ConcurrentHashMap; 029 import javax.jcr.Credentials; 030 import javax.jcr.Repository; 031 import javax.jcr.Session; 032 import javax.naming.InitialContext; 033 import org.jboss.dna.common.SystemFailureException; 034 import org.jboss.dna.repository.RepositoryI18n; 035 036 /** 037 * A SessionFactory implementation that creates {@link Session} instances from a map of named {@link Repository} references 038 * managed by this factory. 039 * <p> 040 * By default, this factory creates an anonymous JCR session. To use sessions with specific {@link Credentials}, simply 041 * {@link #registerCredentials(String, Credentials) register} credentials for the appropriate repository/workspace name. For 042 * security reasons, it is not possible to retrieve the Credentials once registered with this factory. 043 * </p> 044 * 045 * @author Randall Hauch 046 */ 047 public class SimpleSessionFactory extends AbstractSessionFactory { 048 049 private final Map<String, Repository> repositories = new ConcurrentHashMap<String, Repository>(); 050 051 /** 052 * Create an instance of the factory by creating a new {@link InitialContext}. 053 */ 054 public SimpleSessionFactory() { 055 super(); 056 } 057 058 /** 059 * Create an instance of the factory by supplying the characters that may be used to delimit the workspace name from the 060 * repository name. 061 * 062 * @param workspaceDelimiters the delimiters, or null/empty if the default delimiter of '/' should be used. 063 */ 064 public SimpleSessionFactory( char... workspaceDelimiters ) { 065 super(workspaceDelimiters); 066 } 067 068 /** 069 * {@inheritDoc} 070 */ 071 @Override 072 protected void doRegisterRepository( String name, 073 Repository repository ) { 074 this.repositories.put(name, repository); 075 } 076 077 /** 078 * {@inheritDoc} 079 */ 080 @Override 081 protected void doUnregisterRepository( String name ) throws SystemFailureException { 082 if (this.repositories.remove(name) == null) { 083 throw new SystemFailureException(RepositoryI18n.unableToRemoveRepository.text(name)); 084 } 085 } 086 087 /** 088 * {@inheritDoc} 089 */ 090 @Override 091 protected Repository findRegisteredRepository( String name ) throws SystemFailureException { 092 Repository repository = this.repositories.get(name); 093 if (repository == null) { 094 throw new SystemFailureException(RepositoryI18n.unableToFindRepositoryWithName.text(name)); 095 } 096 return repository; 097 } 098 099 }