调用session.getWorkspace()。getQuery()会导致错误

时间:2012-12-17 13:34:23

标签: java osgi jackrabbit jcr sling

我正在实现一个包含搜索服务(AbstractService)的osgi包。 此服务包含一种将内容节点列表作为NodeIterator对象获取的方法。

 public NodeIterator getNodes(Session session, String query, long count) throws RepositoryException{
            NodeIterator nodeIterator = null;

            QueryManager queryManager = session.getWorkspace().getQueryManager();

            Query qry = queryManager.createQuery(query, Query.SQL);
            qry.setLimit(count);
            QueryResult result = qry.execute();
            nodeIterator = result.getNodes();


        return nodeIterator;
    }

我在线上有错误:

QueryManager queryManager = session.getWorkspace().getQueryManager();

错误

org.apache.sling.api.SlingException: An exception occurred processing JSP page /apps/IMEApp/Content/Category/../../General/html_marketCommentary.jsp at line 49
    at org.apache.sling.scripting.jsp.jasper.servlet.JspServletWrapper.handleJspExceptionInternal(JspServletWrapper.java:571)
    at org.apache.sling.scripting.jsp.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:496)
    at org.apache.sling.scripting.jsp.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:442)
    at org.apache.sling.scripting.jsp.JspServletWrapperAdapter.service(JspServletWrapperAdapter.java:59)
    at org.apache.sling.scripting.jsp.JspScriptEngineFactory.callJsp(JspScriptEngineFactory.java:173)
    at org.apache.sling.scripting.jsp.JspScriptEngineFactory.access$100(JspScriptEngineFactory.java:84)
    at org.apache.sling.scripting.jsp.JspScriptEngineFactory$JspScriptEngine.eval(JspScriptEngineFactory.java:388)
    at org.apache.sling.scripting.core.impl.DefaultSlingScript.call(DefaultSlingScript.java:358)
    at org.apache.sling.scripting.core.impl.DefaultSlingScript.eval(DefaultSlingScript.java:170)
    at org.apache.sling.scripting.core.impl.DefaultSlingScript.service(DefaultSlingScript.java:456)
    at org.apache.sling.engine.impl.request.RequestData.service(RequestData.java:529)
    at org.apache.sling.engine.impl.SlingRequestProcessorImpl.processComponent(SlingRequestProcessorImpl.java:274)
    at org.apache.sling.engine.impl.filter.RequestSlingFilterChain.render(RequestSlingFilterChain.java:49)
    at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:64)
    at org.apache.sling.engine.impl.debug.RequestProgressTrackerLogFilter.doFilter(RequestProgressTrackerLogFilter.java:59)
    at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60)
    at org.apache.sling.engine.impl.SlingRequestProcessorImpl.processRequest(SlingRequestProcessorImpl.java:161)
    at org.apache.sling.engine.impl.SlingMainServlet.service(SlingMainServlet.java:183)
    at org.apache.felix.http.base.internal.handler.ServletHandler.doHandle(ServletHandler.java:96)
    at org.apache.felix.http.base.internal.handler.ServletHandler.handle(ServletHandler.java:79)
    at org.apache.felix.http.base.internal.dispatch.ServletPipeline.handle(ServletPipeline.java:42)
    at org.apache.felix.http.base.internal.dispatch.InvocationFilterChain.doFilter(InvocationFilterChain.java:49)
    at org.apache.felix.http.base.internal.dispatch.HttpFilterChain.doFilter(HttpFilterChain.java:33)
    at org.apache.felix.http.base.internal.dispatch.FilterPipeline.dispatch(FilterPipeline.java:48)
    at org.apache.felix.http.base.internal.dispatch.Dispatcher.dispatch(Dispatcher.java:39)
    at org.apache.felix.http.base.internal.DispatcherServlet.service(DispatcherServlet.java:67)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
    at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
    at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:390)
    at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
    at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at org.mortbay.jetty.Server.handle(Server.java:326)
    at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
    at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:926)
    at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:549)
    at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:212)
    at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
    at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:410)
    at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)
Caused by: java.lang.LinkageError: loader constraint violation: when resolving interface method "javax.jcr.Workspace.getQueryManager()Ljavax/jcr/query/QueryManager;" the class loader (instance of org/apache/felix/framework/ModuleImpl$ModuleClassLoaderJava5) of the current class, org/dapa/ime/service/AbstractService, and the class loader (instance of org/apache/felix/framework/ModuleImpl$ModuleClassLoaderJava5) for resolved class, javax/jcr/Workspace, have different Class objects for the type javax/jcr/query/QueryManager used in the signature
    at org.dapa.ime.service.AbstractService.getNodes(AbstractService.java:43)
    at org.dapa.ime.service.NewsService.getListOfNews(NewsService.java:35)
    at org.dapa.ime.service.NewsService.getListOfTopNews(NewsService.java:57)
    at org.apache.jsp.apps.IMEApp.Content.Category.html_jsp._jspService(html_jsp.java:1995)
    at org.apache.sling.scripting.jsp.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
    at org.apache.sling.scripting.jsp.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
    ... 37 more

我无法理解由部分引起的:

Caused by: java.lang.LinkageError: loader constraint violation: when resolving interface method "javax.jcr.Workspace.getQueryManager()Ljavax/jcr/query/QueryManager;" the class loader (instance of org/apache/felix/framework/ModuleImpl$ModuleClassLoaderJava5) of the current class, org/dapa/ime/service/AbstractService, and the class loader (instance of org/apache/felix/framework/ModuleImpl$ModuleClassLoaderJava5) for resolved class, javax/jcr/Workspace, have different Class objects for the type javax/jcr/query/QueryManager used in the signature

身体有帮助吗? 提前谢谢。

1 个答案:

答案 0 :(得分:2)

什么?

此类LinkageError通常是由不完整的OSGi元数据引起的:在当前情况下,您允许两个包(一个加载AbstractService和一个加载Workspace)到有不同版本的QueryManager

现在?

你的系统中有这个QueryManager的太多副本,你需要除掉一个以外的所有副本,或者确保选择正确的副本。第一个是最有可能的,第二个只有在您手动构建捆绑包时才会发生。

一个QueryManager来统治他们

检查多个包是否包含此类。我不知道你是如何构建捆绑包的,但是你可能有一些过于雄心勃勃的工具将类放入需要它的每个包中。常见的原因是在Maven POM中使用<Embed-Transitive>true</Embed-Transitive>

让一个捆绑包提供此类,并将其导出(and import!)packge。

选择正确的

如果您确实有多个副本,请确保所有捆绑包都同意使用哪个副本。确保,

  • 当捆绑包导出javax.jcr.query包时,它也会导入它,因此解析器可以选择一个它认为最有用的包,也可以选择
  • 明确说明某些捆绑包使用javax.jcr.query时必须使用的javax.jcr.query副本。您可以通过在导出中放置uses constraint来实现此目的,但公平地说,您的工具应该自行完成。

如上所述,如果您需要使用此解决方案,您可能拥有手工制作的捆绑包。考虑使用bndtools之类的工具为您构建捆绑包。