有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在sling中创建一个新的mixin

我在使用下面的代码为ApacheSling中的jackrabbit创建一个新的自定义类型时遇到困难。这在Jackrabbit上直接起作用,但在Apache Sling上不起作用。我这样做正确吗?谢谢

下面的代码给出了一个“javax.jcr.InvalidItemStateException:Conflict”。我使用一个独立的吊索,是唯一的用户,所以绝对没有冲突

Repository repository = JcrUtils.getRepository("http://localhost:8080/server");

Session session = repository.login(new SimpleCredentials("admin", "admin".toCharArray()));

NamespaceRegistry registry = session.getWorkspace().getNamespaceRegistry();
registry.registerNamespace("my", "http://my.com/v1.0");

CndImporter.registerNodeTypes(new FileReader("C:\\test.cnd"), session);

共 (2) 个答案

  1. # 1 楼答案

    如果您使用的是Sling,那么可以通过将CND文件放在OSGi包中(java代码应该驻留在这个包中)来避免所有这一切,该包的头告诉Sling在哪里可以找到它

    当绑定激活时,节点类型将自动注册。有关此示例,请参见event.cnd文件,该文件在该模块的pom.xml中设置的Sling NodeType捆绑包头中声明(或者在不使用Maven的情况下以任何其他方式声明)

    还要注意的是,你不应该需要芦丁。getRepository在Sling中无论如何,获取存储库的正确方法是通过SlingRepository OSGi服务,它以一致的方式为所有Sling组件提供存储库登录和配置。您可以通过对java代码中SlingRepository的@Reference来获取存储库,或者从Sling提供给servlet和脚本等请求处理程序的资源中获取JCR会话。{a3}同时使用这两种机制

  2. # 2 楼答案

    InvalidItemStateException的JavaDocs提供了以下线索:

    Exception thrown by the write methods of Node and Property and by save and refresh if an attempted change would conflict with a change to the persistent workspace made through another Session. Also thrown by methods of Node and Property if that object represents an item that has been removed from the workspace.

    您确定没有任何旧会话仍在运行,可能是守护进程未正确关闭吗