天天看點

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

繼續閱讀