代码示例
逐行阅读,该例子的目标是遍历节点
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 SixHop {
private static final Logger LOGGER = Logger.getLogger(SixHop.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 {
SixHop fiveHop = new SixHop();
Repository repository = fiveHop.getRepository(1,"");
Session session = fiveHop.doLogin(repository,"admin","admin");
//获取该会话的根节点
Node root = session.getRootNode();
Node hello2 = JcrUtils.getNodeIfExists(root,"hello2");
if(hello2 == null){
//在该根节点中增加两个子节点,注意没调用保存方法前,数据都保存在会话中,没有持久化。
hello2 = root.addNode("hello2");
Node world2 = hello2.addNode("world2");
world2.setProperty("message", "Hello, World!");
}else {
Node world2 = JcrUtils.getNodeIfExists(hello2,"world2");
if(world2 == null){
world2 = hello2.addNode("world2");
world2.setProperty("message", "Hello, World!");
}else{
world2.setProperty("message", "Hello, World!");
}
}
//数据持久化
session.save();
//遍历root节点
fiveHop.dumpNode(root,1);
}
public void dumpNode(Node node,int level) throws RepositoryException {
String pre = "";
for(int i=0;i<level;i++){
pre += "-";
}
LOGGER.info(pre+node.getPath());
// Then output the properties
PropertyIterator properties = node.getProperties();
while (properties.hasNext()) {
Property property = properties.nextProperty();
if (property.getDefinition().isMultiple()) {
// A multi-valued property, print all values
Value[] values = property.getValues();
for (int i = 0; i < values.length; i++) {
LOGGER.info(pre+
property.getPath() + " = " + values[i] .getString());
}
} else {
// A single-valued property
LOGGER.info(pre+
property.getPath() + " = " + property.getString());
}
}
// Finally output all the child nodes recursively
NodeIterator nodes = node.getNodes();
while (nodes.hasNext()) {
dumpNode(nodes.nextNode(),(level+1));
}
}
}