View Javadoc

1   /*
2    * ModeShape (http://www.modeshape.org)
3    * See the COPYRIGHT.txt file distributed with this work for information
4    * regarding copyright ownership.  Some portions may be licensed
5    * to Red Hat, Inc. under one or more contributor license agreements.
6    * See the AUTHORS.txt file in the distribution for a full listing of 
7    * individual contributors. 
8    *
9    * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
10   * is licensed to you under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation; either version 2.1 of
12   * the License, or (at your option) any later version.
13   *
14   * ModeShape is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this software; if not, write to the Free
21   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23   */
24  package org.modeshape.web.jcr;
25  
26  import java.util.Collection;
27  import javax.jcr.RepositoryException;
28  import javax.jcr.Session;
29  import javax.servlet.ServletContext;
30  import javax.servlet.http.HttpServletRequest;
31  import net.jcip.annotations.ThreadSafe;
32  import org.modeshape.common.util.CheckArg;
33  import org.modeshape.web.jcr.spi.RepositoryProvider;
34  
35  /**
36   * Factory that provides implementations of the {@link RepositoryProvider repository provider SPI} by wrapping a
37   * {@link RepositoryProvider}.
38   * <p>
39   * The repository factory implements a lifecycle for the repository providers. It is first {@link #initialize(ServletContext)
40   * initialized} by {@link ModeShapeJcrDeployer}, a servlet context listener that must be configured in the ModeShape JCR REST web
41   * configuration (web.xml). The repository factory looks in the context for a parameter with the name of {@link #PROVIDER_KEY}.
42   * This is assumed to be the FQN of the {@link RepositoryProvider repository provider}, which the factory will then instantiate.
43   * </p>
44   * <p>
45   * The repository factory is then able to respond to multiple requests to {@link #getJcrRepositoryNames() list the repository
46   * names} and {@link #getSession(HttpServletRequest, String, String) return active JCR sessions} until the {@link #shutdown()
47   * shutdown method} is called.
48   * </p>
49   * <p>
50   * The {@link #shutdown() shutdown method} is a simple proxy to the {@link RepositoryProvider#shutdown()} repository provider's
51   * shutdown method}.
52   * </p>
53   */
54  @ThreadSafe
55  public class RepositoryFactory {
56  
57      /** The FQN of the repository provider class. Currently set to {@value} . */
58      public static final String PROVIDER_KEY = "org.modeshape.web.jcr.REPOSITORY_PROVIDER";
59  
60      private static RepositoryProvider provider;
61  
62      private RepositoryFactory() {
63  
64      }
65  
66      /**
67       * Initializes the repository factory. For more details, please see the {@link RepositoryFactory class-level documentation}.
68       * 
69       * @param context the servlet context; may not be null
70       * @see RepositoryFactory
71       */
72      static void initialize( ServletContext context ) {
73          CheckArg.isNotNull(context, "context");
74          String className = context.getInitParameter(PROVIDER_KEY);
75  
76          try {
77              Class<? extends RepositoryProvider> providerClass = Class.forName(className).asSubclass(RepositoryProvider.class);
78              provider = providerClass.newInstance();
79  
80          } catch (Exception ex) {
81              throw new IllegalStateException(ex);
82          }
83  
84          provider.startup(context);
85      }
86  
87      public static Session getSession( HttpServletRequest request,
88                                        String repositoryName,
89                                        String workspaceName ) throws RepositoryException {
90          return provider.getSession(request, repositoryName, workspaceName);
91      }
92  
93      public static Collection<String> getJcrRepositoryNames() {
94          return provider.getJcrRepositoryNames();
95      }
96  
97      static void shutdown() {
98          provider.shutdown();
99      }
100 }