<?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 1 Deployment Notes</title>
    </properties>
    
    <body>
        <section name="Model 1 Basics">
            <p>
                A Model 1, or Embedded, deployment gives your webapp sole access and control
                of the repository. You are responsible for starting the repository and for shutting
                it down. Other webapps in the application server will not have access to the
                repository.
            </p>
            <p>
                There are two methods of accessing the repository: coding it directly into your
                application, and using a JNDI Resource configuration for your webapp. As the first
                approach ties your code very tightly to Jackrabbit (as opposed to the JCR API), we
                will not cover it here and instead focus on using JNDI to configure and access the
                repository. If you want more information about coding the repository's creation
                and management directly, see the
                <a href="http://jackrabbit.apache.org/doc/firststeps.html">First Hops</a>
                tutorial in the main Jackrabbit documentation.
            </p>
        </section>
        
        <section name="Context locations">
            <p>
                There are three locations where you can place webapp specific context information.
                The first, which is strongly discouraged, is inside Tomcat's <code>server.xml</code>
                file under the Host tag; this approach is far from ideal because you must restart
                the whole server for changes to your context to take effect. The second location
                is in <code>$CATALINA_HOME/conf/Catalina/localhost/[webapp].xml</code>, where
                <code>[webapp]</code> is the name of the root directory of your web application
                (i.e. the name of your webapp's war file, without the <code>.war</code> extension).
                The third location is within your war's directory structure, in a file called
                <code>META-INF/context.xml</code> (note this is not <code>WEB-INF</code>).
            </p>
            <p>
                Putting the context information inside your application's war is the preferred
                method because it is the easiest to manage. Tomcat will copy that file to
                <code>$CATALINA_HOME/conf/Catalina/localhost/[webapp].xml</code> when it deploys
                your application. This is the approach taken in this example.
            </p>
        </section>
        <section name="Jackrabbit as an embedded JNDI Resources">
            <p>
                The Resource specification for a Model 1 deployment is relatively simple.
            </p>
            <source><![CDATA[
<Context>
    
    <Resource name="jcr/model1Repository"
        auth="Container"
        type="javax.jcr.Repository"
        factory="org.apache.jackrabbit.core.jndi.BindableRepositoryFactory"
        configFilePath="${catalina.home}/webapps/jackrabbit_deployment_test/META-INF/repository_model1.xml"
        repHomeDir="${catalina.home}/shared/jackrabbit_model1" />

</Context>
]]></source>
            <p>
                The Resource is first given a <code>name</code>, which we will use inside the
                webapp to access the repository. Jackrabbit uses container authorization, specified
                in the <code>auth</code> attribute. When you access the repository, you will be
                getting back an object that implements the <code>javax.jcr.Repository</code>
                interface. The <code>factory</code> attribute gives the fully qualified class name
                of the repository factory object, which creates the Repository instance you will
                be using. You specify the location of your repository's configuration file in the
                <code>configFilePath</code> attribute; for this example, the repository
                configuration lives in the <code>META-INF</code> directory, alongside
                <code>context.xml</code>. The location of the repository within the file system
                is specified with the <code>repHomeDir</code> directory. Remember that Tomcat
                must have full permissions on this location.
            </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-ref>
        <description>Jackrabbit Model 1 Repository</description>
        <res-ref-name>jcr/model1Repository</res-ref-name>
        <res-type>javax.jcr.Repository</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>
    ...
</web-app>
]]></source>
            <p>
                You'll notice that many of the values here are the same as in the
                <code>META-INF/context.xml</code>. They are also less likely to change. You can
                change how the repository is created (the factory used or the parameters used by
                the factory) and you won't need to change anything in your
                <code>WEB-INF/web.xml</code> or the source code.
            </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.
            </p>
            <source><![CDATA[
Context initialContext = new InitialContext();
Context environmentContext = (Context) initialContext.lookup( "java:comp/env" );
Repository embeddedRepository =
    (Repository) environmentContext.lookup( "jcr/model1Repository" );
]]></source>
            <p>
                Note that the same string used in the <code>name</code> attribute is passed to the
                environment context's <code>lookup()</code> method.
            </p>
        </section>
        <section name="Model 1 Considerations">
            <p>
                The
                <a href="http://jackrabbit.apache.org/doc/deploy/howto-model1.html">Model
                1 documentation</a> stresses the importance of shutting down the repository when
                your webapp is un-deployed. Unfortunately, this is currently impossible. The
                object created by the <code>BindableRepositoryFactory</code> is an instance of
                <code>BindableRepository</code> which is scoped to its package and therefore cannot
                be instantiated; you cannot even cast the repository object to its type within your
                webapp. The <code>javax.jcr.Repository</code> interface does not have a
                <code>shutdown()</code> method. This means that, currently, there is no way for
                a webapp to shut down a repository created using the JNDI mechanism detailed here.
                If you directly code the creation of your repository, this is not a problem (you'll
                create a <code>TransientRepository</code> instead of a
                <code>BindableRepository</code>). Fortunately, one of the benefits of using the
                JNDI mechanism to configure and access your repository is that its easy to switch
                between an embedded (Model 1) and shared (Model 2) repository configuration.
                Therefore, until the issue with shutting down a repository is addressed (the
                developers are aware of it), I recommend configuring your repository as a shared
                repository as described in the <a href="model2notes.html">Model 2 Notes</a>.
            </p>
        </section>
    </body>
</document>
