不会健身的程序员
不是好作家

Apache Jackrabbit 示例代码

下面记录工作中用到jackrabbit用到的一些代码片段。

导入文件

public Node importFile (Node folderNode, File file, String mimeType,
            String encoding) throws RepositoryException, IOException {
    Node fileNode = folderNode.addNode (file.getName (), "nt:file");
    Node resNode = fileNode.addNode ("jcr:content", "nt:resource");
    resNode.setProperty ("jcr:mimeType", mimeType);
    resNode.setProperty ("jcr:encoding", encoding);
    resNode.setProperty ("jcr:data", new FileInputStream (file));
    Calendar lastModified = Calendar.getInstance ();
    lastModified.setTimeInMillis (file.lastModified ());
    resNode.setProperty ("jcr:lastModified", lastModified);

    return fileNode;
}

重命名结点

public void rename(Node node, String newName) throws RepositoryException {
    node.getSession().move(node.getPath(), node.getParent().getPath() + "/" + newName);
    // 下面这代码不是必须的
    // node.getSession().save();
}

注册结点类型

示例一:

public void registerNodeType(NodeTypeDef nodeTypeDef, Session session) 
                   throws RepositoryException  {
    //NodeTypeRegistry object
    Workspace wsp = session.getWorkspace();
    NodeTypeManager ntMgr = wsp.getNodeTypeManager();

    //non-JSR 170 - jackrabbit specific
    NodeTypeRegistry ntReg =
            ((NodeTypeManagerImpl) ntMgr).getNodeTypeRegistry();
    ntReg.registerNodeType(nodeTypeDef);
}

示例二:

/* Retrieve node type manager from the session */
NodeTypeManager nodeTypeManager = session.workspace.nodeTypeManager

/* Create node type */
NodeTypeTemplate nodeType = nodeTypeManager.createNodeTypeTemplate()
nodeType.name = "my_custom_node_type"

/* Create a new property */
PropertyDefinitionTemplate customProperty = nodeTypeManager.createPropertyDefinitionTemplate()
customProperty.name = "my_custom_property"
customProperty.requiredType = PropertyType.LONG
/* Add property to node type */
nodeType.propertyDefinitionTemplates << customProperty

/* Register node type */
nodeTypeManager.registerNodeType(nodeType, false)

Versioning Basics


/*** * most of the code below is deprecated as of jcr-2.0 * please see the javax.jcr.version.VersionManager javadocs - * * http://www.day.com/maven/jsr170/javadocs/jcr-2.0/index.html */ public void versioningBasics (Node parentNode, Session session) throws RepositoryException { //create versionable node Node n = parentNode.addNode("childNode", "nt:unstructured"); n.addMixin("mix:versionable"); n.setProperty("anyProperty", "Blah"); session.save(); Version firstVersion = n.checkin(); //add new version Node child = parentNode.getNode("childNode"); child.checkout(); child.setProperty("anyProperty", "Blah2"); session.save(); child.checkin(); //print version history VersionHistory history = child.getVersionHistory(); for (VersionIterator it = history.getAllVersions(); it.hasNext();) { Version version = (Version) it.next(); System.out.println(version.getCreated().getTime()); } //restoring old version child.checkout(); child.restore(firstVersion, true); }

创建一个工作区

((org.apache.jackrabbit.core.WorkspaceImpl)workspace).createWorkspace(name);

删除一个工作区

((org.apache.jackrabbit.core.WorkspaceImpl)workspace).deleteWorkspace(name);

注:该方法是没有实现的,总是抛出UnsupportedRepositoryOperationException错误,所以无法用编码的方式删除workspace

赞(0)
转载请注明出处:林俊龙博客 » Apache Jackrabbit 示例代码

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

年年岁岁花相识,岁岁年年人不同

给我留言网站地图

谢主隆恩

支付宝扫一扫打赏

微信扫一扫打赏