【深入浅出Apache Jackrabbit】第五章 Apache Jackrabbit 版本管理

系列文章目录

第一章 初见 Apache Jackrabbit
第二章 Apache Jackrabbit 入门
第三章 Repository 配置文件
第四章 Apache Jackrabbit 文件存储
第五章 Apache Jackrabbit 版本管理


前言

Jackrabbit 是一种符合 Java Content Repository (JCR) 2.0 规范的开源内容库。版本管理是 JCR 2.0 规范的一部分,Apache Jackrabbit 完全支持版本管理。


在 Jackrabbit 中,你可以创建、恢复和管理内容节点的不同版本。这意味着你可以将特定的节点标记为"versionable",然后对其进行更改和存储,而不会覆盖原始数据。这使得你可以回滚到以前的版本,如果需要的话。

例如,你可以使用addMixin(“mix:versionable”)方法将节点设为可版本化。然后,使用checkin()方法为特定节点创建新的版本,使用checkout()方法使节点变为可编辑状态。你也可以使用getVersionHistory()方法获取节点的版本历史记录。


import org.apache.jackrabbit.commons.JcrUtils;

import javax.jcr.*;
import javax.jcr.version.Version;
import javax.jcr.version.VersionHistory;
import javax.jcr.version.VersionIterator;
import javax.jcr.version.VersionManager;

import java.io.*;

public class VersionDemo {

    private static Repository repository;
    private static Session session;

    public static void main(String[] args) {
        try {

            repository = JcrUtils.getRepository();
            session = repository.login(new SimpleCredentials("admin", "admin".toCharArray()));
        } catch (RepositoryException e) {
            e.printStackTrace();
        }

        addFile();
        createNewVersion();
        downloadAllVersions();
    }


    public static void downloadAllVersions() {
        try {
            Node fileNode = session.getRootNode().getNode("myFile");

            VersionManager vm = session.getWorkspace().getVersionManager();
            VersionHistory versionHistory = vm.getVersionHistory(fileNode.getPath());
            VersionIterator versions = versionHistory.getAllVersions();

            //如果下载指定的版本可以用
            // Version version = versionHistory.getVersion("1.0");

            //如果遍历所有的版本下载可以用
            while (versions.hasNext()) {
                Version version = versions.nextVersion();

                if ("jcr:rootVersion".equals(version.getName())) {
                    continue;
                }

                Node node = version.getFrozenNode();


                // Get 'jcr:data' from 'jcr:content' under frozenNode
                Binary bin = node.getProperty("jcr:content/jcr:data").getBinary();

                FileOutputStream fos = new FileOutputStream("D:\\1\\file" + version.getName() + ".pdf");
                OutputStream out = new BufferedOutputStream(fos);
                byte[] buff = new byte[8192];
                long position = 0;
                int len = 0;
                while ((len = bin.read(buff, position)) != -1) {
                    out.write(buff, 0, len);
                    position += len;
                }
                out.flush();
                out.close();

                System.out.println("Version " + version.getName() + " is downloaded.");

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void createNewVersion() {
        try {
            Node fileNode = session.getRootNode().getNode("myFile");

            // Enable versioning
            if (!fileNode.isNodeType("mix:versionable")) {
                fileNode.addMixin("mix:versionable");
                session.save();
            }

            // Create a checkpoint that represents a specific version
            VersionManager vm = session.getWorkspace().getVersionManager();

            vm.checkout(fileNode.getPath()); // Checkout the 'myFile' node
            Version version = vm.checkin(fileNode.getPath());

            System.out.println("New version is created: " + version.getName());
        } catch (RepositoryException e) {
            e.printStackTrace();
        }
    }

    public static void addFile() {
        try {
            Node root = session.getRootNode();

            // 创建一个新的node来存储文件
            Node fileNode = root.addNode("myFile", "nt:file");

            // 创建一个'jcr:content' node来存储文件的内容
            Node resNode = fileNode.addNode("jcr:content", "nt:resource");
            resNode.setProperty("jcr:mimeType", "application/pdf");

            File file = new File("D:\\aaa.pdf");
            FileInputStream fis = new FileInputStream(file);
            Binary binary = session.getValueFactory().createBinary(fis);
            resNode.setProperty("jcr:data", binary);
            session.save();
            System.out.println("File is uploaded.");
        } catch (RepositoryException | FileNotFoundException e) {
            e.printStackTrace();
        }
    }


}

代码逻辑讲解:

  • main 方法是程序的入口点。它首先获取一个 Jackrabbit 存储库并进行登录,然后调用 addFile 方法上传文件,createNewVersion 方法创建一个新的文件版本,最后调用 downloadAllVersions 方法下载所有文件版本。

  • addFile 方法用于上传文件到 Jackrabbit 存储库中。它首先获取存储库的根节点,然后在根节点下创建一个名为 “myFile” 的节点,用于存储文件。接着在 “myFile” 节点下创建一个 “jcr:content” 节点,用于存储文件的内容。最后,它将文件的内容设置到 “jcr:data” 属性,并保存会话。如果文件上传成功,将打印 “File is uploaded.”。

  • createNewVersion 方法用于为文件创建新的版本。它首先获取 “myFile” 节点,然后检查该节点是否启用了版本管理,如果没有,则添加 “mix:versionable” 混入以启用版本管理,并保存会话。接着,它调用 checkout 方法在 “myFile” 节点上执行检出操作,然后调用 checkin 方法在 “myFile” 节点上执行检入操作,这将创建一个新的版本。如果新版本创建成功,将打印 "New version is created: " 加上版本名。

  • downloadAllVersions 方法用于下载文件的所有版本。它首先获取 “myFile” 节点,然后获取该节点的版本历史记录,并遍历所有版本。对于每个版本,它会获取冻结节点,并从 “jcr:content” 下的 “jcr:data” 属性中获取二进制内容,然后写入到一个 PDF 文件中。如果版本下载成功,将打印 “Version " 加上版本名 " is downloaded.”。


总结

虽然版本管理功能对于内容库来说很有用,但是它也会增加存储和性能开销,因此应适当使用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提示错误[ERROR] [ERROR] Some problems were encountered while processing the POMs: [ERROR] Unresolveable build extension: Plugin org.apache.maven.wagon:wagon-webdav-jackrabbit:1.0-beta-6 or one of its dependencies could not be resolved: The following artifacts could not be resolved: commons-httpclient:commons-httpclient:jar:3.1 (absent): Could not transfer artifact commons-httpclient:commons-httpclient:jar:3.1 from/to central (https://repo.maven.apache.org/maven2): Connect to repo.maven.apache.org:443 [repo.maven.apache.org/146.75.112.215] failed: connect timed out @ @ [ERROR] The build could not read 1 project -> [Help 1] [ERROR] [ERROR] The project org.drools:droolsjbpm-integration:7.74.0-SNAPSHOT (D:\droolsjbpm-integration-main\droolsjbpm-integration-main\pom.xml) has 1 error [ERROR] Unresolveable build extension: Plugin org.apache.maven.wagon:wagon-webdav-jackrabbit:1.0-beta-6 or one of its dependencies could not be resolved: The following artifacts could not be resolved: commons-httpclient:commons-httpclient:jar:3.1 (absent): Could not transfer artifact commons-httpclient:commons-httpclient:jar:3.1 from/to central (https://repo.maven.apache.org/maven2): Connect to repo.maven.apache.org:443 [repo.maven.apache.org/146.75.112.215] failed: connect timed out -> [Help 2] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/ProjectBuildingException [ERROR] [Help 2] http://cwiki.apache.org/confluence/display/MAVEN/PluginManagerException
06-09

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值