<?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="disk-connector">
  <title>Disk Connector</title>
  <para>
		This connector stores content in a ModeShape-specific file format on disk.  Although this may seem similar in concept to the <link linkend="file-system-connector">File System Connector</link>,
		this connector actually serves a much different purpose.  While the File System Connector is designed to expose existing files and folders on the 
		disk and allow ModeShape users to create content that can be read directly by other applications, the Disk Connector is designed for efficiency and
		stores content in a serialized representation that is not readily accessible to other applications.  Conversely, the Disk Connector supports referenceable
		nodes and can efficiently access nodes by UUID, unlike the File System Connector.    
 	</para>
	<para>
		The &DiskSource; class provides a number of JavaBean properties that control its behavior:
	</para>
  <variablelist>
    <varlistentry>
      <term>cachePolicy</term>
      <listitem>
        <para>
          Optional property that, if used, defines the cache policy for this repository source.  When not used, this source will not define a specific
			    duration for caching information.
				</para>
      </listitem>
    </varlistentry>
    <varlistentry>
      <term>creatingWorkspaceAllowed</term>
      <listitem>
        <para>
          Optional property that defines whether clients can create additional workspaces.  The default value is "true".
        </para>
      </listitem>
    </varlistentry>
    <varlistentry>
      <term>defaultWorkspaceName</term>
      <listitem>
        <para>
          Optional property that is initialized to <code>"default"</code> and which defines the name for the workspace that will be used by default
					if none is specified.
				</para>
      </listitem>
    </varlistentry>
    <varlistentry>
      <term>largeValuePath</term>
      <listitem>
        <para>
          Optional, advanced property that, if specified, specifies the path to the large value area.  <emphasis>This path is relative to the value of the <code>repositoryRootPath</code> property.</emphasis>
					The default value for this property is <code>"largeValues"</code> and it only needs to be changed if there will be a workspace named <code>"largeValues"</code>. 
				</para>
      </listitem>
    </varlistentry>
    <varlistentry>
      <term>largeValueSizeInBytes</term>
      <listitem>
        <para>
          Optional property that, if specified, sets the threshold for large values.  Binary property values that exceed this size will be copied into the large
				  value area for this repository, where they can be shared between nodes and lazily loaded to improve node retrieval time.  The default value is "8192".
				</para>
      </listitem>
    </varlistentry>
    <varlistentry>
      <term>lockFileUsed</term>
      <listitem>
        <para>
          An advanced property that, if set to "true", indicates that repository read and write locks should be synchronized with file lock options 
				  on a file in the on-disk storage.  This causes a performance penalty, but allows disk sources in different JVMS (e.g., clustered disk sources) to
				  coordinate their locks <emphasis>as long as all cluster members share the same disk</emphasis>.  This approach uses Java NIO file locking and is 
				  subject to the limitations of the Java NIO file locking for the current
				  JVM implementation.
				</para>
				<para>
				  The default value is "false", but this should always be set to "true" when used in a clustered environment.
		    </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>nodeCachePolicy</term>
      <listitem>
        <para>
          Optional property that, if used, defines the cache policy to use for caching nodes within the connector.
        </para>
      </listitem>
    </varlistentry>
    <varlistentry>
      <term>predefinedWorkspaceNames</term>
      <listitem>
        <para>
          Optional property that, if used, defines names of the workspaces that are predefined and need not be created before being used.
					This can be coupled with a "false" value for the "creatingWorkspaceAllowed" property to allow only the use of only predefined workspaces.
        </para>
      </listitem>
    </varlistentry>
    <varlistentry>
      <term>repositoryRootPath</term>
      <listitem>
        <para>
          Optional property that specifies a path on the local file system to the root of all workspaces.  The connector will use this as the root
					for a file and folder structure for storing content.  The default value is "/tmp", so setting this property to a more logical value is strongly 
					recommended.
        </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>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 default UUID is used.
        </para>
      </listitem>
    </varlistentry>
    <varlistentry>
      <term>updatesAllowed</term>
      <listitem>
        <para>
          Optional property that determines whether the content in the file system can be updated ("true"), or if the content may only be read ("false").
					The default value is "true".
        </para>
      </listitem>
    </varlistentry>
  </variablelist>
	<para>
		One way to configure the file system connector is to create &JcrConfiguration; instance with a repository source that uses the &DiskSource; class.
		For example:
	</para>
  <programlisting language="JAVA" role="JAVA"><![CDATA[
JcrConfiguration config = ...
config.repositorySource("Disk Store")
      .usingClass(DiskSource.class)
      .setDescription("The repository for our content")
      .setProperty("repositoryRootPath", "/home/content/someApp")
      .setProperty("defaultWorkspaceName", "prod")
      .setProperty("predefinedWorkspaceNames", new String[] { "staging", "dev"})
      .setProperty("rootNodeUuid", UUID.fromString("fd129c12-81a8-42ed-aa4b-820dba49e6f0")
      .setProperty("updatesAllowed", "true")
      .setProperty("creatingWorkspaceAllowed", "false");
 ]]></programlisting>
	<para>
		Another way to configure the file system connector is to create &JcrConfiguration; instance and load an XML configuration file that contains a repository source that 
		uses the &DiskSource; 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 'Disk Store' repository is a disk source with a three predefined workspaces 
        ("prod", "staging", and "dev").
        -->
        <mode:source jcr:name="Disk Store" 
        	mode:classname="org.modeshape.connector.disk.DiskSource"
        	mode:description="The repository for our content"
        	mode:repositoryRootPath="/home/content/someApp"
        	mode:defaultWorkspaceName="prod"
        	mode:creatingWorkspacesAllowed="false"
        	mode:rootNodeUuid="fd129c12-81a8-42ed-aa4b-820dba49e6f0"
        	mode:updatesAllowed="true" >
            <mode:predefinedWorkspaceNames>staging</mode:predefinedWorkspaceNames>
            <mode:predefinedWorkspaceNames>dev</mode:predefinedWorkspaceNames>
      	    <!-- 
      	    If desired, specify a cache policy that caches items in memory for 5 minutes (300 s).
      	    This fragment can be left out if the connector should not cache any content.
      	    -->
      	    <mode:cachePolicy jcr:name="nodeCachePolicy" 
      	      mode:classname="org.modeshape.graph.connector.base.cache.InMemoryNodeCache$MapCachePolicy"
      	      mode:timeToLive="300" />
        </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>

