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 javax.jcr.Credentials;
028 import javax.jcr.Repository;
029 import javax.jcr.Session;
030 import javax.naming.Context;
031 import javax.naming.InitialContext;
032 import javax.naming.NamingException;
033 import org.jboss.dna.common.SystemFailureException;
034 import org.jboss.dna.common.util.CheckArg;
035 import org.jboss.dna.repository.RepositoryI18n;
036
037 /**
038 * A SessionFactory implementation that creates {@link Session} instances using {@link Repository} instances registered in JNDI.
039 * <p>
040 * This factory using a naming convention where the name supplied to the {@link #createSession(String)} contains both the name of
041 * the repository and the name of the workspace. Typically, this is <i><code>repositoryName/workspaceName</code></i>, where
042 * <code>repositoryName</code> is the JNDI name under which the Repository instance was bound, and <code>workspaceName</code>
043 * is the name of the workspace. Note that this method looks for the last delimiter in the whole name to distinguish between the
044 * repository and workspace names.
045 * </p>
046 * <p>
047 * For example, if "<code>java:comp/env/repository/dataRepository/myWorkspace</code>" is passed to the
048 * {@link #createSession(String)} method, this factory will look for a {@link Repository} instance registered in JDNI with the
049 * name "<code>java:comp/env/repository/dataRepository</code>" and use it to {@link Repository#login(String) create a session}
050 * to the workspace named "<code>myWorkspace</code>".
051 * </p>
052 * <p>
053 * By default, this factory creates an anonymous JCR session. To use sessions with specific {@link Credentials}, simply
054 * {@link #registerCredentials(String, Credentials) register} credentials for the appropriate repository/workspace name. For
055 * security reasons, it is not possible to retrieve the Credentials once registered with this factory.
056 * </p>
057 * @author Randall Hauch
058 */
059 public class JndiSessionFactory extends AbstractSessionFactory {
060
061 private final Context context;
062
063 /**
064 * Create an instance of the factory by creating a new {@link InitialContext}. This is equivalent to calling
065 * <code>new JndiSessionFactory(new InitialContext())</code>.
066 * @throws NamingException if there is a problem creating the InitialContext.
067 */
068 public JndiSessionFactory() throws NamingException {
069 this(new InitialContext());
070 }
071
072 /**
073 * Create an instance of the factory by supplying the characters that may be used to delimit the workspace name from the
074 * repository name. This constructor initializes the factory with a new {@link InitialContext}, and is equivalent to calling
075 * <code>new JndiSessionFactory(new InitialContext(),workspaceDelimiters)</code>.
076 * @param workspaceDelimiters the delimiters, or null/empty if the default delimiter of '/' should be used.
077 * @throws NamingException if there is a problem creating the InitialContext.
078 */
079 public JndiSessionFactory( char... workspaceDelimiters ) throws NamingException {
080 this(new InitialContext(), workspaceDelimiters);
081 }
082
083 /**
084 * Create an instance of the factory using the supplied JNDI context.
085 * @param context the naming context
086 * @throws IllegalArgumentException if the context parameter is null
087 */
088 public JndiSessionFactory( Context context ) {
089 this(context, DEFAULT_DELIMITERS);
090 }
091
092 /**
093 * Create an instance of the factory by supplying naming context and the characters that may be used to delimit the workspace
094 * name from the repository name.
095 * @param context the naming context
096 * @param workspaceDelimiters the delimiters, or null/empty if the default delimiter of '/' should be used.
097 * @throws IllegalArgumentException if the context parameter is null
098 */
099 public JndiSessionFactory( Context context, char... workspaceDelimiters ) {
100 super(workspaceDelimiters);
101 CheckArg.isNotNull(context, "initial context");
102 this.context = context;
103 }
104
105 /**
106 * {@inheritDoc}
107 */
108 @Override
109 protected void doRegisterRepository( String name, Repository repository ) throws SystemFailureException {
110 try {
111 this.context.bind(name, repository);
112 } catch (NamingException e) {
113 throw new SystemFailureException(RepositoryI18n.unableToRegisterRepositoryInJndi.text(name));
114 }
115 }
116
117 /**
118 * {@inheritDoc}
119 */
120 @Override
121 protected void doUnregisterRepository( String name ) throws SystemFailureException {
122 try {
123 this.context.unbind(name);
124 } catch (NamingException e) {
125 throw new SystemFailureException(RepositoryI18n.unableToUnregisterRepositoryInJndi.text(name));
126 }
127 }
128
129 /**
130 * {@inheritDoc}
131 */
132 @Override
133 protected Repository findRegisteredRepository( String name ) throws SystemFailureException {
134 try {
135 return (Repository)this.context.lookup(name);
136 } catch (NamingException e) {
137 throw new SystemFailureException(RepositoryI18n.unableToFindRepositoryInJndi.text(name));
138 }
139 }
140 }