红联Linux门户
Linux帮助

JackRabbit基础入门

发布时间:2014-06-21 09:32:19来源:红联作者:velcbo
在一个月前,我们经理就让我研究一下jackrabbit,为后面的内容管理开发做准备,我也一直在找关于jackrabbit的学习资料,无奈,好的资料都是英文的,看不太懂。终于在不懈努力下,写出了第一段代码。现在我将其粘贴出,大家一块学习下,有什么不足的,还请各位指点。

所需jar包:

commons-collections-3.2.1.jar
commons-dbcp-1.2.2.jar
commons-io-1.4.jar
commons-pool-1.3.jar
concurrent-1.3.4.jar
derby-10.5.3.0_1.jar
jackrabbit-api-2.4.0.jar
jackrabbit-core-2.4.0.jar
jackrabbit-jcr-commons-2.4.0.jar
jackrabbit-spi-2.4.0.jar
jackrabbit-spi-commons-2.4.0.jar
jcr-2.0.jar
lucene-core-3.0.3.jar
slf4j-api-1.6.4.jar
tika-core-1.0.jar
tika-parsers-1.0.jar

repository.xml配置文件[code]
"http://jackrabbit.apache.org/dtd/repository-1.5.dtd">














































































[/code]java代码:[code]import java.io.File;
import java.io.FileInputStream;
import java.util.Calendar;
import java.util.Hashtable;

import javax.jcr.Binary;
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 javax.jcr.Workspace;
import javax.jcr.query.Query;
import javax.jcr.query.QueryManager;
import javax.jcr.query.QueryResult;
import javax.naming.Context;
import javax.naming.InitialContext;

import org.apache.jackrabbit.core.jndi.RegistryHelper;
import org.apache.jackrabbit.core.jndi.provider.DummyInitialContextFactory;
import org.apache.jackrabbit.value.BinaryImpl;
import org.apache.jackrabbit.value.StringValue;

import sun.net.www.MimeTable;

//import sun.net.www.MimeTable;

public class TestJackrabbit {
public static void main(String[] args) throws Exception {

// 初始化仓库
String configFile = "config/repository.xml";
String repHomeDir = "F:\\Company\\repository";
Hashtable hashTable = new Hashtable();
hashTable.put(Context.INITIAL_CONTEXT_FACTORY,
DummyInitialContextFactory.class.getName());
hashTable.put(Context.PROVIDER_URL, "127.0.0.1");
InitialContext ctx = new InitialContext(hashTable);
RegistryHelper.registerRepository(ctx, "repo", configFile, repHomeDir,
true);
Repository r = (Repository) ctx.lookup("repo");

// 登陆 .这里注意一点,需要以管理员的身份登陆,否则很多操作都没有权限,默认用户名和密码都是admin
SimpleCredentials cred = new SimpleCredentials("admin",
"admin".toCharArray());
Session session = r.login(cred, null);

// /////////////注册工作区命名空间 start//////////////////////////////////
// 根节点
Node rn = session.getRootNode();

// 注册命名空间
Workspace ws = session.getWorkspace();
if (!ws.getSession().isLive()) {
ws.getNamespaceRegistry().registerNamespace("wiki",
"http://127.0.0.1/wiki/1.0");
}
// /////////////注册工作区命名空间 end//////////////////////////////////


// 添加内容
Node encyclopedia = rn.addNode("wiki:encyclopedia");
Node p = encyclopedia.addNode("wiki:entry");
p.setProperty("wiki:title", toStringValue("rose"));
p.setProperty("wiki:content",
toStringValue("A rose is a flowering shrub."));
p.setProperty("wiki:category", new Value[] { toStringValue("flower"),
toStringValue("plant"), toStringValue("rose") });

Node n = encyclopedia.addNode("wiki:entry");
n.setProperty("wiki:title", toStringValue("Shakespeare"));
n.setProperty("wiki:content",
toStringValue("A famous poet who likes roses."));
n.setProperty("wiki:category", toStringValue("poet"));
// 保存
session.save();

// 查找
QueryManager qm = ws.getQueryManager();
Query q = qm.createQuery(
"//wiki:encyclopedia/wiki:entry[@wiki:title = 'rose']",
Query.XPATH);// deprecated
QueryResult result = q.execute();// 执行查询
NodeIterator it = result.getNodes();
// 然后就可以了遍历了
while (it.hasNext()) {
Node entry = it.nextNode();
// 简单的输出,后面会有输出详细内容的方法
System.out.println(entry.toString());
}

// 加入文件
File file = new File("F:\\我的文档\\图片素材\\非主流\\30.gif");
MimeTable mt = MimeTable.getDefaultTable();
String mimeType = mt.getContentTypeFor(file.getName());
if (mimeType == null) {
mimeType = "application/octet-stream";
}
Node fileNode = rn.addNode(file.getName(), "nt:file");
Node resNode = fileNode.addNode("jcr:content", "nt:resource");
resNode.setProperty("jcr:mimeType", mimeType);
resNode.setProperty("jcr:encoding", "");

//这里--用流加入
Binary fileBinary = new BinaryImpl(new FileInputStream(file));
resNode.setProperty("jcr:data", fileBinary);

Calendar lastModified = Calendar.getInstance();
lastModified.setTimeInMillis(file.lastModified());
resNode.setProperty("jcr:lastModified", lastModified);
// 保存
session.save();

// 打印
printAll(rn);

}

private static StringValue toStringValue(String str) {
return new StringValue(str);
}

/**
* 打印
*/
private static void printAll(Node node) throws RepositoryException {
printFormat(node.toString());
PropertyIterator propertys = node.getProperties();
while (propertys.hasNext()) {
Property entry = propertys.nextProperty();
if (entry.isMultiple()) {
Value[] values = entry.getValues();
if (values == null) {
continue;
}
for (Value v : values) {
printFormat(v.getString());
}
} else {
printFormat(entry.getValue().getString());
}
}

NodeIterator entries = node.getNodes();
while (entries.hasNext()) {
Node entry = entries.nextNode();
printAll(entry);
}
}

private static void printFormat(Object str) {
System.out.println("####################:" + str);
}
}[/code]这写就是完整的代码。

来源:Linux社区
文章评论

共有 0 条评论