All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.jackrabbit.servlet.ServletRepositoryFactory Maven / Gradle / Ivy

Go to download

Servlets and related classes for easy use of JCR content repositories in web applications.

There is a newer version: 2.13.4
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.jackrabbit.servlet;

import javax.jcr.Repository;
import javax.jcr.RepositoryException;
import javax.servlet.GenericServlet;
import javax.servlet.ServletContext;

import org.apache.jackrabbit.commons.repository.RepositoryFactory;

/**
 * Factory that looks up a repository from the context of a given servlet.
 * 

* The default name of the repository attribute is * "javax.jcr.Repository", but it can be changed by specifying * an init parameter with the same name: *

 * <servlet>
 *   <init-param>
 *     <param-name>javax.jcr.Repository</param-name>
 *     <param-value>my.repository.attribute</param-value>
 *     <description>
 *       This init parameter causes the repository to be looked up from
 *       the "my.repository.attribute" attribute instead of the default
 *       "javax.jcr.Repository".
 *     </description>
 *   </init-param>
 * </servlet>
 * 
* * @since 1.4 */ public class ServletRepositoryFactory implements RepositoryFactory { /** * The servlet whose context contains the repository. */ private final GenericServlet servlet; /** * Creates a factory for looking up a repository from the context of * the given servlet. * * @param servlet servlet whose context contains the repository */ public ServletRepositoryFactory(GenericServlet servlet) { this.servlet = servlet; } /** * Looks up and returns a repository bound in the configured servlet * context attribute. * * @return repository from servlet context * @throws RepositoryException if the repository is not available */ public Repository getRepository() throws RepositoryException { String name = servlet.getInitParameter(Repository.class.getName()); if (name == null) { name = Repository.class.getName(); } ServletContext context = servlet.getServletContext(); Object repository = context.getAttribute(name); if (repository instanceof Repository) { return (Repository) repository; } else if (repository != null) { throw new RepositoryException( "Invalid repository: Attribute " + name + " in servlet context " + context.getServletContextName() + " is an instance of " + repository.getClass().getName()); } else { throw new RepositoryException( "Repository not found: Attribute " + name + " does not exist in servlet context " + context.getServletContextName()); } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy