系列文章目录
第一章 初见 Apache Jackrabbit
第二章 Apache Jackrabbit 入门
第三章 Repository 配置文件
第四章 Apache Jackrabbit 文件存储
第五章 Apache Jackrabbit 版本管理
文章目录
- 系列文章目录
- 一、创建一个 Repository 配置文件
- 二、初始化 Repository
Apache Jackrabbit 是一个完全符合 Java 内容库 API (JCR) 的开源内容库。它可以作为一个持久化的仓库来存储和检索数据。下面讲解如何自定义Repository 配置文件配置 Apache Jackrabbit 作为持久化仓库,并且可指向仓库的存储位置。
一、创建一个 Repository 配置文件
在 Apache Jackrabbit 中,你需要创建一个 XML 文件来配置你的内容库。这个配置文件通常称为 repository.xml。下面是我的配置文件,配置文件中已设置它成为一个持久化的仓库并且指向仓库的存储位置在D盘:Jackrabbit目录下。
<?xml version="1.0"?>
<Repository>
<FileSystem class="org.apache.jackrabbit.core.fs.local.LocalFileSystem">
<param name="path" value="D:\\Jackrabbit/repository"/>
</FileSystem>
<DataStore class="org.apache.jackrabbit.core.data.FileDataStore"/>
<Security appName="Jackrabbit">
<SecurityManager class="org.apache.jackrabbit.core.DefaultSecurityManager" workspaceName="security">
</SecurityManager>
<AccessManager class="org.apache.jackrabbit.core.security.DefaultAccessManager">
</AccessManager>
<LoginModule class="org.apache.jackrabbit.core.security.authentication.DefaultLoginModule">
<param name="anonymousId" value="anonymous"/>
<param name="adminId" value="admin"/>
</LoginModule>
</Security>
<Workspaces rootPath="D:\\Jackrabbit/workspaces" defaultWorkspace="default"/>
<Workspace name="${wsp.name}">
<FileSystem class="org.apache.jackrabbit.core.fs.local.LocalFileSystem">
<param name="path" value="D:\\Jackrabbit/${wsp.name}"/>
</FileSystem>
<PersistenceManager class="org.apache.jackrabbit.core.persistence.pool.DerbyPersistenceManager">
<param name="url" value="jdbc:derby:D:\\Jackrabbit/${wsp.name}/db;create=true"/>
<param name="schemaObjectPrefix" value="${wsp.name}_"/>
</PersistenceManager>
<SearchIndex class="org.apache.jackrabbit.core.query.lucene.SearchIndex">
<param name="path" value="D:\\Jackrabbit/${wsp.name}/index"/>
<param name="supportHighlighting" value="true"/>
</SearchIndex>
</Workspace>
<Versioning rootPath="D:\\Jackrabbit/version">
<FileSystem class="org.apache.jackrabbit.core.fs.local.LocalFileSystem">
<param name="path" value="D:\\Jackrabbit/version" />
</FileSystem>
<PersistenceManager class="org.apache.jackrabbit.core.persistence.pool.DerbyPersistenceManager">
<param name="url" value="jdbc:derby:D:\\Jackrabbit/version/db;create=true"/>
<param name="schemaObjectPrefix" value="version_"/>
</PersistenceManager>
</Versioning>
<SearchIndex class="org.apache.jackrabbit.core.query.lucene.SearchIndex">
<param name="path" value="D:\\Jackrabbit/repository/index"/>
<param name="supportHighlighting" value="true"/>
</SearchIndex>
<Cluster id="node1">
<Journal class="org.apache.jackrabbit.core.journal.MemoryJournal"/>
</Cluster>
</Repository>
二、初始化 Repository
有了配置文件,你就可以使用以下代码来初始化 Repository:
本例中使用的是maven项目演示,项目结构如下:
代码如下(示例):
import org.apache.jackrabbit.core.RepositoryImpl;
import org.apache.jackrabbit.core.config.RepositoryConfig;
import javax.jcr.Repository;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.SimpleCredentials;
import java.io.File;
import java.net.URISyntaxException;
import java.net.URL;
public class Demo1 {
public static void main(String[] args) throws RepositoryException, URISyntaxException {
Demo1 demo1 = new Demo1();
demo1.create();
}
private void create() throws RepositoryException, URISyntaxException {
ClassLoader classLoader = getClass().getClassLoader();
// 获取资源的 URL
URL resourceUrl = classLoader.getResource("repository.xml");
File repositoryDirectory = new File(resourceUrl.toURI());
RepositoryConfig repositoryConfig = RepositoryConfig.create(repositoryDirectory.getParentFile());
Repository repository = RepositoryImpl.create(repositoryConfig);
// To login
Session session = repository.login(new SimpleCredentials("admin", "admin".toCharArray()));
}
}
运行后效果