JACKRABBIT入门(1)

 代码示例

import org.apache.jackrabbit.commons.JcrUtils;
import org.apache.jackrabbit.core.RepositoryImpl;
import org.apache.jackrabbit.core.config.RepositoryConfig;
import org.apache.log4j.Logger;

import javax.jcr.*;
import java.net.URL;

public class FiveHop {
    private static final Logger LOGGER = Logger.getLogger(FiveHop.class);

    /***
     * 获取Repository,有两种方法。
     * 方法一:使用工具类JcrUtils.getRepository()
     * 方法二:使用方法RepositoryConfig.create(xml, dir);
     *
     * 结果:
     *  1:使用方法一会在工作空间中生成文件夹jackrabbit。在该文件夹中会有目配置文件和数据信息。
     *  2:使用方法二时会按照{xml}文件在目录{dir}中生成文件仓库
     * 说明:
     *  1:当第一次初始化时Jackrabbit repository将初始化一个用户,该用户的用户名是admin,密码是admin
     * @param type
     * @param dataDir
     * @return
     * @throws RepositoryException
     */
    public Repository getRepository(int type,String dataDir) throws RepositoryException {
        Repository repository = null;
        if(type == 1){
            repository = JcrUtils.getRepository();
        }else if(type == 2){
            ClassLoader classLoader = getClass().getClassLoader();
            URL url = classLoader.getResource("rep_configuration.xml");
            LOGGER.info("配置文件路径:"+url.getPath());
            String xml = url.getPath();
            String dir = dataDir;
            RepositoryConfig config = RepositoryConfig.create(xml, dir);
            repository = RepositoryImpl.create(config);
        }else{
            throw new RuntimeException("非支持的类型,请纠正参数或者扩展代码");
        }
        return repository;
    }

    public Session doLogin(Repository repository,String userName,String password ) throws RepositoryException {
        Session  session = repository.login(
                new SimpleCredentials(userName, password.toCharArray()));
        return session;
    }
    public static void main(String[] args) throws RepositoryException {

        FiveHop fiveHop = new FiveHop();
        Repository repository = fiveHop.getRepository(1,"");

        Session session = fiveHop.doLogin(repository,"admin","admin");

        //获取该会话的根节点
        Node root = session.getRootNode();
        //在该根节点中增加两个子节点,注意没调用保存方法前,数据都保存在会话中,没有持久化。
        Node hello = root.addNode("hello");
        Node world = hello.addNode("world");
        world.setProperty("message", "Hello, World!");
        //数据持久化
        session.save();

        //精确查找文件节点
        Node node = root.getNode("hello/world");
        //打印节点路径,使用方法getPath;
        LOGGER.info(node.getPath());
        //打印节点属性
        LOGGER.info(node.getProperty("message").getString());

        //删除子节点hello
        root.getNode("hello").remove();
        //手动持久化
        session.save();

        //判断节点是否存在,如果使用 root.getNode("hello");会报异常,所以换成下面的方法
        node  = JcrUtils.getNodeIfExists(root,"hello");
         if(node == null){
             LOGGER.info("节点hello已经被删除");
         }else{
             LOGGER.error("节点hello没有被删除");
         }

        //在该根节点中增加两个子节点,注意没调用保存方法前,数据都保存在会话中,没有持久化。
        Node hello2 = root.addNode("hello2");
        Node world2 = hello2.addNode("world2");
        world2.setProperty("message", "Hello, World!");
        //数据持久化
        session.save();


    }
}

参考资料

官方示例1http://jackrabbit.apache.org/jcr/first-hops.html

  • 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、付费专栏及课程。

余额充值