Jackrabbit学习笔记1
最近软件测评中心有一个项目要做,Ext方面的人不够用,正好Ext我自学过一些,就去做了兼职,正好就聊起了他们那个项目,感觉还是蛮大的,主要是一个公司的内部系统,好像是什么平台管理的还有一个CRM还有其他的子系统,反正听他讲还是蛮复杂的,他们现在在做需求,正式编码要到12月份的样子,现在的时间我们正好去熟悉下jackrabbit。
以前从来没有接触过jackrabbit这个项目,今天搞了好久,看了很多学习jackrabbit的博客,但是进展很小,最后无奈又回到官网上了,慢慢发现其实官网上面的介绍还是蛮好的。就把这个学习笔记记录了下来,以后如果有可能看的话,就再来看。
好了废话少说,还是一步步讲步骤吧。首先需要下载jackrabbit相关的资源,去官网的话当然没有问题,可是实际操作的时候总是会发现有些文件下载失败,所以推荐去人人网下载:http://labs.renren.com/apache-mirror//jackrabbit/
在这个链接下面选择一个下载,新版本的话可能会好一些,所以我下载的是2.2.9(2.3.0现在还不稳定)。
下载完之后在MyEclipse下面新建一个Web项目,如下图所示:
注意添加Maven的支持,并且这是官网推荐的,需要说明的是第一次加载该项目的时候可能会很慢,MyEclipse会去官网上面下载一些文件,这个我们不用去关心。
然后就将上面下载的一个jackrabbit-standalone-2.2.9.jar文件拷贝到lib下面,然后引入该包,就可以使用jackrabbit的东西了。
新建一个测试的类,如下图所示
代码如下:
package com.jackrabbit.demo;
import javax.jcr.Repository;
import javax.jcr.Session;
import org.apache.jackrabbit.core.TransientRepository;
/** * First hop example. Logs in to a content repository and prints a status
* message.
*/
public class FirstHop {
public static void main(String[] args) throws Exception {
Repository repository = new TransientRepository();
Session session = repository.login();
try {
String user = session.getUserID();
String name = repository.getDescriptor(Repository.REP_NAME_DESC);
System.out.println("Logged in as " + user + " to a " + name
+ " repository.");
System.out.println("登录仓库身份:"+user+",登录名为:"+name);
} finally {
session.logout();
}
}
}
运行该程序,可见到结果:
Logged in as anonymous to a Jackrabbit repository.
登录仓库身份:anonymous,登录名为:Jackrabbit
这就表明我们成功使用jackrabbit实现了第一个程序!
然后继续:新建第二个测试类SecondHop.java,代码如下:
package com.jackrabbit.demo;
import javax.jcr.Node;
import javax.jcr.Repository;
import javax.jcr.Session;
import javax.jcr.SimpleCredentials;
import org.apache.jackrabbit.core.TransientRepository;
public class SecondHop {
public static void main(String[] args) throws Exception {
Repository repository = new TransientRepository();
Session session = repository.login(new SimpleCredentials("username",
"password".toCharArray()));
try {
Node root = session.getRootNode();
// Store content
Node hello = root.addNode("hello");
Node world = hello.addNode("world");
world.setProperty("message", "Hello, World!");
session.save();
// Retrieve content
Node node = root.getNode("hello/world");
System.out.println(node.getPath());
System.out.println(node.getProperty("message").getString());
// Remove content
root.getNode("hello").remove();
session.save();
} finally {
session.logout();
}
}
}
运行结果为:
/hello/world
Hello, World!
新建第三个测试类:ThirdHop.java,代码如下:
package com.jackrabbit.demo;
import java.io.FileInputStream;
import javax.jcr.ImportUUIDBehavior;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Property;
import javax.jcr.PropertyIterator;
import javax.jcr.Repository;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.SimpleCredentials;
import javax.jcr.Value;
import org.apache.jackrabbit.core.TransientRepository;
public class ThirdHop {
public static void main(String[] args) throws Exception {
Repository repository = new TransientRepository();
Session session = repository.login(new SimpleCredentials("username",
"password".toCharArray()));
try {
Node root = session.getRootNode();
// Import the XML file unless already imported
if (!root.hasNode("importxml")) {
System.out.print("Importing xml... ");
// Create an unstructured node under which to import the XML
Node node = root.addNode("importxml", "nt:unstructured");
// Import the file "test.xml" under the created node
FileInputStream xml = new FileInputStream("test.xml");
session.importXML(node.getPath(), xml,
ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW);
xml.close();
session.save();
System.out.println("done.");
}
// output the repository content
dump(root);
} finally {
session.logout();
}
}
/** Recursively outputs the contents of the given node. */
private static void dump(Node node) throws RepositoryException {
// First output the node path
System.out.println(node.getPath());
// Skip the virtual (and large!) jcr:system subtree
if (node.getName().equals("jcr:system")) {
return;
}
// 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++) {
System.out.println(property.getPath() + " = "
+ values[i].getString());
}
} else {
// A single-valued property
System.out.println(property.getPath() + " = "
+ property.getString());
}
}
// Finally output all the child nodes recursively
NodeIterator nodes = node.getNodes();
while (nodes.hasNext()) {
dump(nodes.nextNode());
}
}
}
同时新建一个test.xml文件:放在和src平行的目录下,内容如下:
<?xml version="1.0" encoding="UTF-8"?> <xhtml:html xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:mathml="http://www.w3.org/1998/Math/MathML"> <xhtml:head> <xhtml:title>Three Namespaces</xhtml:title> </xhtml:head> <xhtml:body> <xhtml:h1 align="center">An Ellipse and a Rectangle</xhtml:h1> <svg:svg xmlns:svg="http://www.w3.org/2000/svg" width="12cm" height="10cm"> <svg:ellipse rx="110" ry="130" /> <svg:rect x="4cm" y="1cm" width="3cm" height="6cm" /> </svg:svg> <xhtml:p>The equation for ellipses</xhtml:p> <mathml:math> <mathml:apply> <mathml:eq /> <mathml:cn> 1 </mathml:cn> <mathml:apply> <mathml:plus /> <mathml:apply> <mathml:divide /> <mathml:apply> <mathml:power /> <mathml:ci> x </mathml:ci> <mathml:cn> 2 </mathml:cn> </mathml:apply> <mathml:apply> <mathml:power /> <mathml:ci> a </mathml:ci> <mathml:cn> 2 </mathml:cn> </mathml:apply> </mathml:apply> <mathml:apply> <mathml:divide /> <mathml:apply> <mathml:power /> <mathml:ci> y </mathml:ci> <mathml:cn> 2 </mathml:cn> </mathml:apply> <mathml:apply> <mathml:power /> <mathml:ci> b </mathml:ci> <mathml:cn> 2 </mathml:cn> </mathml:apply> </mathml:apply> </mathml:apply> </mathml:apply> </mathml:math> <xhtml:hr /> <xhtml:p>Last Modified January 10, 2002</xhtml:p> </xhtml:body> </xhtml:html>
然后运行该程序,结果如下:
Importing xml... done.
/
/jcr:primaryType = rep:root
/jcr:system
/importxml
/importxml/jcr:primaryType = nt:unstructured
/importxml/xhtml:html
/importxml/xhtml:html/jcr:primaryType = nt:unstructured
/importxml/xhtml:html/xhtml:head
/importxml/xhtml:html/xhtml:head/jcr:primaryType = nt:unstructured
/importxml/xhtml:html/xhtml:head/xhtml:title
/importxml/xhtml:html/xhtml:head/xhtml:title/jcr:primaryType = nt:unstructured
/importxml/xhtml:html/xhtml:head/xhtml:title/jcr:xmltext
/importxml/xhtml:html/xhtml:head/xhtml:title/jcr:xmltext/jcr:xmlcharacters = Three Namespaces
/importxml/xhtml:html/xhtml:head/xhtml:title/jcr:xmltext/jcr:primaryType = nt:unstructured
/importxml/xhtml:html/xhtml:body
/importxml/xhtml:html/xhtml:body/jcr:primaryType = nt:unstructured
/importxml/xhtml:html/xhtml:body/xhtml:h1
/importxml/xhtml:html/xhtml:body/xhtml:h1/align = center
/importxml/xhtml:html/xhtml:body/xhtml:h1/jcr:primaryType = nt:unstructured
/importxml/xhtml:html/xhtml:body/xhtml:h1/jcr:xmltext
/importxml/xhtml:html/xhtml:body/xhtml:h1/jcr:xmltext/jcr:xmlcharacters = An Ellipse and a Rectangle
/importxml/xhtml:html/xhtml:body/xhtml:h1/jcr:xmltext/jcr:primaryType = nt:unstructured
/importxml/xhtml:html/xhtml:body/svg:svg
/importxml/xhtml:html/xhtml:body/svg:svg/width = 12cm
/importxml/xhtml:html/xhtml:body/svg:svg/height = 10cm
/importxml/xhtml:html/xhtml:body/svg:svg/jcr:primaryType = nt:unstructured
/importxml/xhtml:html/xhtml:body/svg:svg/svg:ellipse
/importxml/xhtml:html/xhtml:body/svg:svg/svg:ellipse/ry = 130
/importxml/xhtml:html/xhtml:body/svg:svg/svg:ellipse/rx = 110
/importxml/xhtml:html/xhtml:body/svg:svg/svg:ellipse/jcr:primaryType = nt:unstructured
/importxml/xhtml:html/xhtml:body/svg:svg/svg:rect
/importxml/xhtml:html/xhtml:body/svg:svg/svg:rect/x = 4cm
/importxml/xhtml:html/xhtml:body/svg:svg/svg:rect/y = 1cm
/importxml/xhtml:html/xhtml:body/svg:svg/svg:rect/width = 3cm
/importxml/xhtml:html/xhtml:body/svg:svg/svg:rect/height = 6cm
/importxml/xhtml:html/xhtml:body/svg:svg/svg:rect/jcr:primaryType = nt:unstructured
/importxml/xhtml:html/xhtml:body/xhtml:p
/importxml/xhtml:html/xhtml:body/xhtml:p/jcr:primaryType = nt:unstructured
/importxml/xhtml:html/xhtml:body/xhtml:p/jcr:xmltext
/importxml/xhtml:html/xhtml:body/xhtml:p/jcr:xmltext/jcr:xmlcharacters = The equation for ellipses
/importxml/xhtml:html/xhtml:body/xhtml:p/jcr:xmltext/jcr:primaryType = nt:unstructured
/importxml/xhtml:html/xhtml:body/mathml:math
/importxml/xhtml:html/xhtml:body/mathml:math/jcr:primaryType = nt:unstructured
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/jcr:primaryType = nt:unstructured
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:eq
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:eq/jcr:primaryType = nt:unstructured
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:cn
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:cn/jcr:primaryType = nt:unstructured
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:cn/jcr:xmltext
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:cn/jcr:xmltext/jcr:xmlcharacters = 1
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:cn/jcr:xmltext/jcr:primaryType = nt:unstructured
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:apply
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:apply/jcr:primaryType = nt:unstructured
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:apply/mathml:plus
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:apply/mathml:plus/jcr:primaryType = nt:unstructured
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:apply/mathml:apply
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:apply/mathml:apply/jcr:primaryType = nt:unstructured
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:apply/mathml:apply/mathml:divide
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:apply/mathml:apply/mathml:divide/jcr:primaryType = nt:unstructured
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:apply/mathml:apply/mathml:apply
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:apply/mathml:apply/mathml:apply/jcr:primaryType = nt:unstructured
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:apply/mathml:apply/mathml:apply/mathml:power
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:apply/mathml:apply/mathml:apply/mathml:power/jcr:primaryType = nt:unstructured
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:apply/mathml:apply/mathml:apply/mathml:ci
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:apply/mathml:apply/mathml:apply/mathml:ci/jcr:primaryType = nt:unstructured
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:apply/mathml:apply/mathml:apply/mathml:ci/jcr:xmltext
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:apply/mathml:apply/mathml:apply/mathml:ci/jcr:xmltext/jcr:xmlcharacters = x
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:apply/mathml:apply/mathml:apply/mathml:ci/jcr:xmltext/jcr:primaryType = nt:unstructured
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:apply/mathml:apply/mathml:apply/mathml:cn
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:apply/mathml:apply/mathml:apply/mathml:cn/jcr:primaryType = nt:unstructured
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:apply/mathml:apply/mathml:apply/mathml:cn/jcr:xmltext
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:apply/mathml:apply/mathml:apply/mathml:cn/jcr:xmltext/jcr:xmlcharacters = 2
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:apply/mathml:apply/mathml:apply/mathml:cn/jcr:xmltext/jcr:primaryType = nt:unstructured
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:apply/mathml:apply/mathml:apply[2]
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:apply/mathml:apply/mathml:apply[2]/jcr:primaryType = nt:unstructured
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:apply/mathml:apply/mathml:apply[2]/mathml:power
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:apply/mathml:apply/mathml:apply[2]/mathml:power/jcr:primaryType = nt:unstructured
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:apply/mathml:apply/mathml:apply[2]/mathml:ci
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:apply/mathml:apply/mathml:apply[2]/mathml:ci/jcr:primaryType = nt:unstructured
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:apply/mathml:apply/mathml:apply[2]/mathml:ci/jcr:xmltext
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:apply/mathml:apply/mathml:apply[2]/mathml:ci/jcr:xmltext/jcr:xmlcharacters = a
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:apply/mathml:apply/mathml:apply[2]/mathml:ci/jcr:xmltext/jcr:primaryType = nt:unstructured
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:apply/mathml:apply/mathml:apply[2]/mathml:cn
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:apply/mathml:apply/mathml:apply[2]/mathml:cn/jcr:primaryType = nt:unstructured
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:apply/mathml:apply/mathml:apply[2]/mathml:cn/jcr:xmltext
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:apply/mathml:apply/mathml:apply[2]/mathml:cn/jcr:xmltext/jcr:xmlcharacters = 2
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:apply/mathml:apply/mathml:apply[2]/mathml:cn/jcr:xmltext/jcr:primaryType = nt:unstructured
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:apply/mathml:apply[2]
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:apply/mathml:apply[2]/jcr:primaryType = nt:unstructured
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:apply/mathml:apply[2]/mathml:divide
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:apply/mathml:apply[2]/mathml:divide/jcr:primaryType = nt:unstructured
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:apply/mathml:apply[2]/mathml:apply
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:apply/mathml:apply[2]/mathml:apply/jcr:primaryType = nt:unstructured
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:apply/mathml:apply[2]/mathml:apply/mathml:power
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:apply/mathml:apply[2]/mathml:apply/mathml:power/jcr:primaryType = nt:unstructured
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:apply/mathml:apply[2]/mathml:apply/mathml:ci
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:apply/mathml:apply[2]/mathml:apply/mathml:ci/jcr:primaryType = nt:unstructured
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:apply/mathml:apply[2]/mathml:apply/mathml:ci/jcr:xmltext
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:apply/mathml:apply[2]/mathml:apply/mathml:ci/jcr:xmltext/jcr:xmlcharacters = y
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:apply/mathml:apply[2]/mathml:apply/mathml:ci/jcr:xmltext/jcr:primaryType = nt:unstructured
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:apply/mathml:apply[2]/mathml:apply/mathml:cn
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:apply/mathml:apply[2]/mathml:apply/mathml:cn/jcr:primaryType = nt:unstructured
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:apply/mathml:apply[2]/mathml:apply/mathml:cn/jcr:xmltext
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:apply/mathml:apply[2]/mathml:apply/mathml:cn/jcr:xmltext/jcr:xmlcharacters = 2
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:apply/mathml:apply[2]/mathml:apply/mathml:cn/jcr:xmltext/jcr:primaryType = nt:unstructured
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:apply/mathml:apply[2]/mathml:apply[2]
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:apply/mathml:apply[2]/mathml:apply[2]/jcr:primaryType = nt:unstructured
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:apply/mathml:apply[2]/mathml:apply[2]/mathml:power
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:apply/mathml:apply[2]/mathml:apply[2]/mathml:power/jcr:primaryType = nt:unstructured
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:apply/mathml:apply[2]/mathml:apply[2]/mathml:ci
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:apply/mathml:apply[2]/mathml:apply[2]/mathml:ci/jcr:primaryType = nt:unstructured
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:apply/mathml:apply[2]/mathml:apply[2]/mathml:ci/jcr:xmltext
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:apply/mathml:apply[2]/mathml:apply[2]/mathml:ci/jcr:xmltext/jcr:xmlcharacters = b
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:apply/mathml:apply[2]/mathml:apply[2]/mathml:ci/jcr:xmltext/jcr:primaryType = nt:unstructured
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:apply/mathml:apply[2]/mathml:apply[2]/mathml:cn
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:apply/mathml:apply[2]/mathml:apply[2]/mathml:cn/jcr:primaryType = nt:unstructured
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:apply/mathml:apply[2]/mathml:apply[2]/mathml:cn/jcr:xmltext
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:apply/mathml:apply[2]/mathml:apply[2]/mathml:cn/jcr:xmltext/jcr:xmlcharacters = 2
/importxml/xhtml:html/xhtml:body/mathml:math/mathml:apply/mathml:apply/mathml:apply[2]/mathml:apply[2]/mathml:cn/jcr:xmltext/jcr:primaryType = nt:unstructured
/importxml/xhtml:html/xhtml:body/xhtml:hr
/importxml/xhtml:html/xhtml:body/xhtml:hr/jcr:primaryType = nt:unstructured
/importxml/xhtml:html/xhtml:body/xhtml:p[2]
/importxml/xhtml:html/xhtml:body/xhtml:p[2]/jcr:primaryType = nt:unstructured
/importxml/xhtml:html/xhtml:body/xhtml:p[2]/jcr:xmltext
/importxml/xhtml:html/xhtml:body/xhtml:p[2]/jcr:xmltext/jcr:xmlcharacters = Last Modified January 10, 2002
/importxml/xhtml:html/xhtml:body/xhtml:p[2]/jcr:xmltext/jcr:primaryType = nt:unstructured
至此,jackrabbit的3个测试类已经完成,需要用到高级的东西时候就要参考其他的文件,这些内容以后学到了再写。