发生com.sun.jdi.InvocationException调用方法osgi aem read子服务

时间:2018-09-07 05:00:17

标签: java osgi aem cq5 jcr

当我使用子服务读取AEM 6.4中模板的详细信息时,我在错误日志中得到一个nullpointer异常

map.get提供了空指针异常

,错误消息为

  

com.adobe.cq.sightly.WCMUsePojo无法激活Use类   java.lang.IllegalArgumentException:javax.jcr.RepositoryException:   本次会议已经关闭。在   org.apache.sling.jcr.resource.internal.JcrValueMap.readFully(JcrValueMap.java:395)   [org.apache.sling.jcr.resource:3.0.8]在   org.apache.sling.jcr.resource.internal.JcrValueMap.entrySet(JcrValueMap.java:183)   [org.apache.sling.jcr.resource:3.0.8]在   com.mysite.core.helpers.AnalyticsHelper.activate(AnalyticsHelper.java:138)   [mysite-core:1.0.0.SNAPSHOT]位于   com.adobe.cq.sightly.WCMUsePojo.init(WCMUsePojo.java:86)   [com.adobe.cq.sightly.cq-wcm-sightly-extension:1.5.12]

它在AEM 6.3中运行良好,但是在AEM 6.4中,它提供了空指针异常

地图为null。当我删除关闭resourceresolver

时,效果很好
MysiteReadTemplateService mysiteReadTemplateService = getSlingScriptHelper()
                .getService(MysiteReadTemplateService .class);
        ValueMap map = mysiteReadTemplateService 
                .fetchTemplateDetails(templatePath);
        if (map != null) {
            templateName = map.get("jcr:title", "");
            templateDescription = map.get("jcr:description", "");
        }

内部fetchTemplateDetails方法-

Map<String,Object> paramMap = new HashMap<String,Object>();
          //Mention the subServiceName that is configured in the User Mapping
          paramMap.put(ResourceResolverFactory.SUBSERVICE, "mysitereadservice");
          log.info("After the param");
          ResourceResolver rr = null;
          ValueMap map =null;
          try{
                rr = resourceFactory.getServiceResourceResolver(paramMap);
                log.info("UserId : " + rr.getUserID());
                Resource templateResource = rr.getResource(templatePath);
                log.info("Resource : " + templateResource.getPath());
                map = templateResource.getValueMap();  
                rr.close();
          }catch(Exception e){
                log.error(e.getMessage());
          }
          return map;

注释rr.close时,analyticshelper类中的map不为null。

可能的解释在

中提供

com.sun.jdi.InvocationException occurred invoking method

但是,我们还是要关闭resourceresolver,因为不关闭它会创建许多未关闭的会话。

如果有人遇到相同的问题,请让我们知道您的想法,或者如何重构代码,以便我们能够关闭资源解析器,并且地图也不为空。

1 个答案:

答案 0 :(得分:0)

关闭sesssion / resourceResolver后,您将无法访问资源或属性(ValueMap)。将您的数据放入另一个数据结构(Collection,Map)并返回。或在会话打开时执行操作:

Map<String, Object> result = new HashMap<String, Object>;
ResourceResolver rr = null;

try{
    Map<String,Object> paramMap = new HashMap<String,Object>();
    //Mention the subServiceName that is configured in the User Mapping
    paramMap.put(ResourceResolverFactory.SUBSERVICE, "mysitereadservice");
    log.info("After the param");
    rr = resourceFactory.getServiceResourceResolver(paramMap);

    if (rr != null) {
        log.info("UserId : " + rr.getUserID());

        Resource templateResource = rr.getResource(templatePath);

        if (templateResource != null) {
            log.info("Resource : " + templateResource.getPath());
            ValueMap map = templateResource.getValueMap();  

            result.put("jcr:title", map.get("jcr:title",""))
            result.put(......);
            result.put(......);
            result.put(......);
            result.put(......);
            result.put(......);
        }
    }
}catch(LoginException e){
    log.error(e.getMessage(), e);
} finally {
    if (rr != null && !rr.isLive()) {
        rr.close();
    }
}

return result;

ResourceResulver应该在finally块中关闭,或者使用try-with-resources:

try (ResourceResolver rr = resourceFactory.getServiceResourceResolver(paramMap)) {
    //yourCode
} catch (LoginException e) {
    log.errror(e.getMessage(), e);
}