<?xml version="1.0" encoding="UTF-8"?>
<!--
  ~ ModeShape (http://www.modeshape.org)
  ~
  ~ See the COPYRIGHT.txt file distributed with this work for information
  ~ regarding copyright ownership.  Some portions may be licensed
  ~ to Red Hat, Inc. under one or more contributor license agreements.
  ~ See the AUTHORS.txt file in the distribution for a full listing of 
  ~ individual contributors.
  ~
  ~ ModeShape is free software. Unless otherwise indicated, all code in ModeShape
  ~ is licensed to you under the terms of the GNU Lesser General Public License as
  ~ published by the Free Software Foundation; either version 2.1 of
  ~ the License, or (at your option) any later version.
  ~
  ~ ModeShape is distributed in the hope that it will be useful,
  ~ but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  ~ or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
  ~ for more details.
  ~
  ~ You should have received a copy of the GNU Lesser General Public License
  ~ along with this distribution; if not, write to:
  ~ Free Software Foundation, Inc.
  ~ 51 Franklin Street, Fifth Floor
  ~ Boston, MA  02110-1301  USA
  -->
<!DOCTYPE preface PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN" "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd"	[
<!ENTITY % CustomDTD SYSTEM "../../custom.dtd">
%CustomDTD;
]>
<chapter id="jboss-cache-connector">
  <title>JBoss Cache Connector</title>
  <para>
  	The JBoss Cache repository connector allows a <ulink url="http://www.jboss.org/jbosscache/">JBoss Cache</ulink> instance to be 
		used as a ModeShape (and thus JCR) repository.  This provides a repository that is an effective, scalable, and distributed cache,
		and can be <link linkend="federation-connector">federated</link> with other repository sources to provide a distributed repository.
  </para>
	<para>
		The &JBossCacheSource; class provides a number of JavaBean properties that control its behavior:
	</para>
  <variablelist>
    <varlistentry>
      <term>cacheConfigurationName</term>
      <listitem>
				<para>
				  Optional property that, if used, specifies the name of the configuration that is supplied to the cache factory
					when creating a new JBoss Cache instance.
				</para>
      </listitem>
    </varlistentry>
    <varlistentry>
      <term>cacheFactoryJndiName</term>
      <listitem>
				<para>
				  Optional property that, if used, specifies the name in JNDI where an existing JBoss Cache Factory instance can be found.
					That factory would then be used if needed to create a JBoss Cache instance.  If no value is provided, then the
					JBoss Cache <code>DefaultCacheFactory</code> class is used.
				</para>
      </listitem>
    </varlistentry>
    <varlistentry>
      <term>cacheJndiName</term>
      <listitem>
				<para>
				  Optional property that, if used, specifies the name in JNDI where an existing JBoss Cache instance can be found.
					This should be used if your application already defines a cache, or if you need to configure the cache in
					a special way.
				</para>
      </listitem>
    </varlistentry>
    <varlistentry>
      <term>creatingWorkspacesAllowed</term>
      <listitem>
				<para>
				  Optional property that is by default 'true' that defines whether clients can create new workspaces.
				</para>
      </listitem>
    </varlistentry>
    <varlistentry>
      <term>defaultCachePolicy</term>
      <listitem>
        <para>
          Optional property that, if used, defines the default for how long this information provided by this source may to be 
					cached by other, higher-level components.  The default value is an empty string (or null) and implies that this source 
					does not define a specific duration for caching information provided by this repository source.
				</para>
      </listitem>
    </varlistentry>
    <varlistentry>
      <term>defaultWorkspaceName</term>
      <listitem>
        <para>
          Optional property that defines the name for the workspace that will be used in cases when clients do not explicitly specify
          the workspace name. If not specified, "<code>default</code>" will be used.
				</para>
      </listitem>
    </varlistentry>
    <varlistentry>
      <term>name</term>
      <listitem>
        <para>
          Required property that specifies the name of the repository source, which is used by the &RepositoryService; 
          when obtaining a &RepositoryConnection; by name.
				</para>
      </listitem>
    </varlistentry>
    <varlistentry>
      <term>predefinedWorkspaceNames</term>
      <listitem>
        <para>
          Optional property that defines the names of the workspaces that exist and that are available for use without having to create them.
				</para>
      </listitem>
    </varlistentry>
    <varlistentry>
      <term>rootNodeUuid</term>
      <listitem>
        <para>
          Optional property that, if used, specifies the UUID that should be used for the root node of each workspace.  If no value is
					specified, a random UUID is generated each time that the repository is started.
				</para>
      </listitem>
    </varlistentry>
    <varlistentry>
      <term>retryLimit</term>
      <listitem>
        <para>
          Optional property that, if used, defines the number of times that any single operation on a &RepositoryConnection; to this source should be retried
					following a communication failure. The default value is '0'.
				</para>
      </listitem>
    </varlistentry>
    <varlistentry>
      <term>updatesAllowed</term>
      <listitem>
        <para>
          Optional property that determines whether the content in the connector is can be updated ("true"), or if the content may only be read ("false").
					The default value is "true".
				</para>
      </listitem>
    </varlistentry>
    <varlistentry>
      <term>uuidPropertyName</term>
      <listitem>
        <para>
          Optional property that, if used, defines the property that should be used to find the UUID value for each node
					in the cache.  "<code>mode:uuid</code>" is the default.
				</para>
      </listitem>
    </varlistentry>
  </variablelist>
	<para>
		One way to configure the JBoss Cache connector is to create &JcrConfiguration; instance with a repository source that uses the &JBossCacheSource; class.
		For example:
	</para>
  <programlisting language="JAVA" role="JAVA"><![CDATA[
JcrConfiguration config = ...
config.repositorySource("Store")
      .usingClass(JBossCacheSource.class)
      .setDescription("The repository for our content")      
      .setProperty("defaultWorkspaceName", "prod")
      .setProperty("rootNodeUuid", UUID.fromString("12083e7e-2b55-4c8d-954d-627a9f5c45c2"))
      .setProperty("predefinedWorkspaceNames", new String[] { "staging", "dev"});
 ]]></programlisting>
	<para>
		Another way to configure the JBoss Cache connector is to create &JcrConfiguration; instance and load an XML configuration file that contains a repository source that 
		uses the &JBossCacheSource; class.
		For example a file named configRepository.xml can be created with these contents:
	</para>
	
  <programlisting language="XML" role="XML"><![CDATA[
<?xml version="1.0" encoding="UTF-8"?>
<configuration xmlns:mode="http://www.modeshape.org/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0">
    <!-- 
    Define the sources for the content.  These sources are directly accessible using the 
    ModeShape-specific Graph API.  In fact, this is how the ModeShape JCR implementation works.  You 
    can think of these as being similar to JDBC DataSource objects, except that they expose 
    graph content via the Graph API instead of records via SQL or JDBC. 
    -->
    <mode:sources jcr:primaryType="nt:unstructured">
        <!-- 
        The 'Store' repository is a JBoss Cache repository with a single default workspace (though 
        others could be created, too).
        -->
        <mode:source jcr:name="Store" 
                    mode:classname="org.modeshape.graph.connector.jbosscache.JBossCacheSource" 
                    mode:description="The repository for our content"
                    mode:defaultworkspaceName="prod"
                    mode:rootNodeUuid="12083e7e-2b55-4c8d-954d-627a9f5c45c2">
           <mode:predefinedWorkspaceNames>staging</mode:predefinedWorkspaceNames>
           <mode:predefinedWorkspaceNames>dev</mode:predefinedWorkspaceNames>
        </mode:source>
    </mode:sources>
    
	<!-- MIME type detectors and JCR repositories would be defined below --> 
</configuration>
 ]]></programlisting>
	<para>
		The configuration can then be loaded from Java like this:
	</para>
	
	<programlisting language="JAVA" role="JAVA"><![CDATA[
JcrConfiguration config = new JcrConfiguration().loadFrom("/configRepository.xml");
 ]]></programlisting>
 </chapter>

