<?xml version="1.0"?>
<!--
   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.
  -->
<document>
    <properties>
        <title>Model 2 Deployment Notes</title>
    </properties>
    
    <body>
        <section name="Model 2 Basics">
            <p>
                A Model 2, or Shared, deployment allows Tomcat to manage the lifecycle of the
                repository and makes it available to all the webapps deployed on the server. When
                Tomcat starts, it will create the repository and initialize it if it does not
                already exist. When the server shuts down, it will also shut down the repository.
                To access a shared repository, you must configure it as a JNDI resource in the
                global context.
            </p>
        </section>
        <section name="Shared repository configuration">
            <p>
                For this webapp to work, you must add the following code to Tomcat's
                <code>$CATALINA_HOME/conf/server.xml</code> file under the
                <code>GlobalNamingResources</code> section.
            </p>
            <source><![CDATA[
<Server ...>
  ...
  <GlobalNamingResources>
    ...
    <Resource name="jcr/model2Repository"
        auth="Container"
        type="javax.jcr.Repository"
        factory="org.apache.jackrabbit.core.jndi.BindableRepositoryFactory"
        configFilePath="${catalina.home}/conf/repository_model2.xml"
        repHomeDir="${catalina.home}/shared/jackrabbit_model2" />
    ...
  </GlobalNamingResources>
  ...
</Server>
]]></source>
            <p>
                You can change the location of the repository's directory (the
                <code>repHomeDir</code> attribute) as long as you ensure it is not the same
                directory specified by any other repository in your system (embedded, shared,
                or otherwise). Trying to share the same repository between two instances of
                Jackrabbit will lead to corrupted data and other bad things.
            </p>
            <p>
                For a more in depth discussion of the Resource attributes, see the discussion in the
                <a href="model1notes.html#Jackrabbit_as_an_embedded_JNDI_Resources">Model 1
                Notes</a>.
            </p>
            <p>
                After creating the Resource in <code>server.xml</code>, you must add a link to
                it in Tomcat's <code>$CATALINA_HOME/conf/context.xml</code>. While you
                <em>could</em> put this line inside <code>server.xml</code> (a Context section
                is allowed inside the Host tag), using <code>server.xml</code> for context
                information is discouraged because when the information changes you must restart
                the server. By keeping the information in a separate file, Tomcat is able to
                simply redeploy the webapps with their new context information while still
                running.
            </p>
            <source><![CDATA[
<Context>
  ...
  <ResourceLink
      glabal="sharedJCRRepository"
      name="jcr/model2Repository"
      type="javax.jcr.Repository" />
  ...
</Context>
]]></source>
            <p>
                This link will give all the installed webapps access to the repository.
                If you don't want all webapps to have access to the repository automatically,
                you can place the <code>ResourceLink</code> tag in your webapp's private
                <code>context.xml</code> in the same manner the Model 1 repository is
                configured there. See the <a href="model1notes.html">Model 1 notes</a> for more
                information on your webapp's private <code>context.xml</code> file. Note that
                no matter the location, the value of the <code>global</code> attribute must be
                different than the value of the <code>name</code> attribute. Also, the
                <code>name</code> attribute must match the value of the Resource
                <code>name</code> attribute.
            </p>
            <p>
                You must also add a resource reference to your webapp's <code>WEB-INF/web.xml</code>
                file.
            </p>
            <source><![CDATA[
<web-app>
    ...
    <resource-env-ref>
        <description>Jackrabbit Model 2 Repository</description>
        <resource-env-ref-name>sharedJCRRepository</resource-env-ref-name>
        <resource-env-ref-type>javax.jcr.Repository</resource-env-ref-type>
    </resource-env-ref>
    ...
</web-app>
]]></source>
            <p>
                Note that the <code>resource-env-ref-name</code> value matches the
                <code>name</code> attribute of the ResourceLink in the Context, not the
                <code>name</code> attribute of the Resource in the
                <name>GlobalNamingResources</name>.
            </p>
        </section>
        <section name="Accessing your repository">
            <p>
                To access your repository within the servlet, you extract it from the context
                just like any other JNDI resource (and just like you would a Model 1 JNDI
                configured repository).
            </p>
            <source><![CDATA[
Context initialContext = new InitialContext();
Context environmentContext = (Context) initialContext.lookup( "java:comp/env" );
Repository sharedRepository =
    (Repository) environmentContext.lookup( "sharedJCRRepository" );
]]></source>
            <p>
                Note that the same string in the <code>resource-env-ref-name</code> value is
                passed to the environment context's <code>lookup()</code> method.
            </p>
        </section>
        <section name="Model 2 Considerations">
            <p>
                Currently, the Shared (Model 2) repository deployment seems to be the best approach
                to using Jackrabbit and JCR in your webapp. It is more flexible than directly coding
                the Repository creation in your code, and the shutdown process is managed by Tomcat.
                And when the load on your server becomes too great, you can migrate to a Server
                (Model 3) deployment somewhat more easily than you could from an Embedded
                deployment.
            </p>
            <p>
                A potential pitfall to this deployment model involves class loading. If you are
                accessing a shared repository, you must not include the <code>jcr-1.0.jar</code>
                or <code>jackrabbit-1.0-SNAPSHOT.jar</code> libraries in your webapp's
                <code>WEB-INF/lib/</code> directory. Instead both of those jars, and Jackrabbit's
                dependencies, should be placed in <code>$CATALINA_HOME/common/lib/</code> where
                they are made available to both Tomcat and web applications. If you include the
                jars in your webapp, you will cause a <code>ClassCastException</code> to be thrown
                when you try to cast the <code>Object</code> returned by the <code>lookup()</code>
                method to a <code>Repository</code>. This will happen because Tomcat created that
                object using the <code>javax/jcr/Repository.class</code> contained in
                <code>$CATALINA_HOME/common/lib/jcr-1.0.jar</code> and your webapp is trying to
                cast it to the <code>javax/jcr/Repository.class</code> in
                <code>WEB-INF/lib/jcr-1.0.jar</code>.
            </p>
        </section>
    </body>
</document>
