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 package org.jboss.dna.web.jcr.rest;
025
026 import java.util.Collection;
027 import javax.jcr.RepositoryException;
028 import javax.jcr.Session;
029 import javax.servlet.ServletContext;
030 import javax.servlet.http.HttpServletRequest;
031 import net.jcip.annotations.ThreadSafe;
032 import org.jboss.dna.common.util.CheckArg;
033 import org.jboss.dna.web.jcr.rest.spi.RepositoryProvider;
034
035 /**
036 * Factory that provides implementations of the {@link RepositoryProvider repository provider SPI} by wrapping a
037 * {@link RepositoryProvider}.
038 * <p>
039 * The repository factory implements a lifecycle for the repository providers. It is first {@link #initialize(ServletContext)
040 * initialized} by {@link DnaJcrDeployer}, a servlet context listener that must be configured in the DNA JCR REST web
041 * configuration (web.xml). The repository factory looks in the context for a parameter with the name of {@link #PROVIDER_KEY}.
042 * This is assumed to be the FQN of the {@link RepositoryProvider repository provider}, which the factory will then instantiate.
043 * </p>
044 * <p>
045 * The repository factory is then able to respond to multiple requests to {@link #getJcrRepositoryNames() list the repository
046 * names} and {@link #getSession(HttpServletRequest, String, String) return active JCR sessions} until the {@link #shutdown()
047 * shutdown method} is called.
048 * </p>
049 * <p>
050 * The {@link #shutdown() shutdown method} is a simple proxy to the {@link RepositoryProvider#shutdown()} repository provider's
051 * shutdown method}.
052 * </p>
053 */
054 @ThreadSafe
055 public class RepositoryFactory {
056
057 /** The FQN of the repository provider class. Currently set to {@value} . */
058 public static final String PROVIDER_KEY = "org.jboss.dna.web.jcr.rest.REPOSITORY_PROVIDER";
059
060 private static RepositoryProvider provider;
061
062 private RepositoryFactory() {
063
064 }
065
066 /**
067 * Initializes the repository factory. For more details, please see the {@link RepositoryFactory class-level documentation}.
068 *
069 * @param context the servlet context; may not be null
070 * @see RepositoryFactory
071 */
072 static void initialize( ServletContext context ) {
073 CheckArg.isNotNull(context, "context");
074 String className = context.getInitParameter(PROVIDER_KEY);
075
076 try {
077 Class<? extends RepositoryProvider> providerClass = Class.forName(className).asSubclass(RepositoryProvider.class);
078 provider = providerClass.newInstance();
079
080 } catch (Exception ex) {
081 throw new IllegalStateException(ex);
082 }
083
084 provider.startup(context);
085 }
086
087 public static Session getSession( HttpServletRequest request,
088 String repositoryName,
089 String workspaceName ) throws RepositoryException {
090 return provider.getSession(request, repositoryName, workspaceName);
091 }
092
093 public static Collection<String> getJcrRepositoryNames() {
094 return provider.getJcrRepositoryNames();
095 }
096
097 static void shutdown() {
098 provider.shutdown();
099 }
100 }