AEM 6:SlingBindings对象为null

时间:2015-05-14 20:45:52

标签: osgi aem sling

我创建了sling:配置文件夹中的OsgiConfig节点,如config.author,config.publish等。我试图通过这样的方式从这些节点获取属性:

public static List fetchTokenLinksFromOsgiConfig(final SlingHttpServletRequest slingRequest) throws IOException {
        List<String> tokenlinksList = new ArrayList<String>();
        SlingBindings bindings = (SlingBindings) slingRequest.getAttribute(SlingBindings.class.getName());
        log.info("=================inside fetchTokenLinksFromOsgiConfig======================"+bindings);
        SlingScriptHelper sling = bindings.getSling();
        Configuration conf = sling.getService(org.osgi.service.cm.ConfigurationAdmin.class).getConfiguration("com.xxxxx.TokenLinksConfig");
        log.info("=================inside fetchTokenLinksFromOsgiConfig:::taking configurations======================");
        String TokenId = (String) conf.getProperties().get("TokenId");
        String TokenSecret = (String) conf.getProperties().get("TokenSecret");
        String OAuthLink = (String) conf.getProperties().get("OAuthLink");
        log.info("=================TokenId:::TokenSecret:::OAuthLink======================"+TokenId +" "+TokenSecret+" "+OAuthLink);
        if(!StringUtil.isEmpty(TokenId)) {
            tokenlinksList.add(TokenId);
        }
        if(!StringUtil.isEmpty(TokenSecret)) {
            tokenlinksList.add(TokenSecret);
        }
        if(!StringUtil.isEmpty(OAuthLink)) {
            tokenlinksList.add(OAuthLink);
        }
        return tokenlinksList;
    }

我从这样的吊索servlet中调用这个方法:

List tokenList = OsgiConfigUtil.fetchTokenLinksFromOsgiConfig(slingRequest);

但SlingBindings类型的bindings对象为null。我不知道如何解决这个问题?

提前致谢

1 个答案:

答案 0 :(得分:1)

Sling servlet是一个OSGi组件,因此您可以使用SCR ConfigurationAdmin注释直接注入@Reference服务:

public MyServlet extends SlingSafeMethodServlet {

    @Reference
    private ConfigurationAdmin confAdmin;

    public doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) {
        confAdmin.getConfiguration("com.myuhc.TokenLinksConfig");
    }
}

无需使用SlingBindings对象,该对象旨在在JSP脚本中提供OSGi服务。