<%-- 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. --%><%@page import="org.apache.jackrabbit.util.Text"%><% request.setAttribute("title", "Local Repository Access"); %>

The content repository within this web application can be accessed locally by other web applications within the same servlet container. Local access is much faster than remote access.

The content repository is made available both through JNDI and the web application context.

Accessing the repository through JNDI

By default the repository is only made available in a dummy JNDI directory local to this web application. However, you can make the repository globally available if your servlet container allows a web application to modify the global JNDI directory or you are using some other JNDI directory that can manage unserializable Java objects.

To bind the the repository to such a JNDI directory, you need to modify the java.naming parameters in either the /WEB-INF/web.xml deployment descriptor or the jackrabbit/bootstrap.properties file. You need to redeploy this web application to activate the changes.

Use the following code to access a repository bound in a JNDI directory:

import javax.jcr.Repository;
import javax.naming.Context;
import javax.naming.InitialContext;

Context context = new InitialContext(...);
Repository repository = (Repository) context.lookup(...);

Accessing the repository through servlet context

This web application makes the repository available as the javax.jcr.Repository attribute in the application context. If your servlet container supports cross-context access, you can access the repository directly using that attribute.

For example in Apache Tomcat you can enable cross-context access by setting the crossContext attribute to true in the <Context/> configuration.

Use the following code to access a repository through the servlet context:

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

ServletContext context = ...; // context of your servlet
ServletContext jackrabbit =
    context.getContext("<%= Text.encodeIllegalXMLCharacters(request.getContextPath()) %>");
Repository repository = (Repository)
    context.getAttribute(Repository.class.getName()).

Using the jackrabbit-jcr-servlet component

The jackrabbit-jcr-servlet component contains utility classes for use within JCR web applications. With that component you can hide both the above and the remote access options from your code, and use just the following to access a repository:

import javax.jcr.Repository;
import org.apache.jackrabbit.servlet.ServletRepository;

public class MyServlet extends HttpServlet {

    private final Repository repository = new ServletRepository(this);

    // ...

}

See the jackrabbit-jcr-servlet documentation for more details.