繁体   English   中英

OSGi组件可通过Apache Felix进行配置

[英]OSGi component configurable via Apache Felix

我是一个完整的新手,所以我提前道歉。 我正在尝试创建一个OSGi组件,它只显示一个hello world消息,并且可以通过felix的输入进行配置。 然后在jsp页面上吐出来。 我正在使用scr注释来帮助完成此操作。 这是我的java代码

package com.training.cq5.trainingApp;

import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.osgi.service.component.ComponentContext;
import org.apache.sling.commons.osgi.PropertiesUtil;

@Component(label= "Welcome Message",
        description = "Welcome Message for the training excercise",
        immediate = true, enabled = true, metatype=true)
@Properties({
    @Property(name = "welcome.message", value = "WelcomeMessage")
})
@Service(WelcomeMessage.class)
public class WelcomeMessage {

    private static String welcome_message = "Welcome";

    @Activate
    protected void activate(ComponentContext ctx) {
        welcome_message = PropertiesUtil.toString(ctx.getProperties().get(welcome_message), welcome_message);
    }

    public static String getMessage() { 
        return welcome_message;
    }
}

以下是我在JSP中调用它:

<%@ page import="com.training.cq5.trainingApp.WelcomeMessage" %>

<h2><%= WelcomeMessage.getMessage() %></h2>

有没有理由说它没有从felix更新? 我得到的只是来自welcome_message字符串的“欢迎”文本。

您正在访问WelcomeMessage.getMessage()作为静态方法,但您想要的是实际服务。 当您使用@Service和@Component批注对类进行批注时,您向OSGI框架指明您希望将此类的实例注册为服务。 此服务实例由OSGI框架管理,在其生命周期(实例化时)或通过类加载器加载适当的类。

但是,为了使用@Component和@Service注释,您必须使用Apache Felix SCR插件 一旦有效,您的服务将被实例化。

然后你将不得不访问该服务。 您似乎正在使用的Sling中最简单的方法是SlingScriptHelper.getService() ,它允许您查找服务。

更新

在OSGI中,服务按其类型注册。 当您使用@Service(MyClass.class)声明服务时,该服务将在MyClass类型下注册。 要检索它,您将在服务注册表中查询给定类型的服务。 在Java代码中,您将使用getServiceReference(Class clazz) / getService(ServiceReference引用)@Reference注释。

在Sling系统上的JSP中,您可以使用SlingScriptHelper,如前所述。 这是一个简短的代码示例(假设正确的导入):

<%
SlingBindings bindings = (SlingBindings) req.getAttribute(SlingBindings.class.getName());
SlingScriptHelper scriptHelper = bindings.getSling();
MyService service = scriptHelper.getService(MyService.class);
// ... do stuff with service.
%>

如果您打算更多地使用OSGI,我强烈推荐OSGI规范 它可以免费下载并详细解释所有内容。

ilikeorangutans是正确的,你不需要在OSGi服务上使用静态方法 - 这个想法是服务实现一个接口,客户端从它们的OSGi上下文中检索它并通过它的服务接口使用它。

Apache Sling webloader示例使用此技术在其请求处理脚本中访问Webloader服务。 在这种情况下,脚本是ESP(服务器端javascript),但原则与JSP完全相同。

服务接口在Webloader.java中定义, WebLoaderImpl.java将 实现为OSGi服务。

然后, html.esp脚本使用sling.getService获取服务:

var loader = sling.getService(Packages.org.apache.sling.samples.webloader.Webloader);

更改此行: - welcome_message = PropertiesUtil.toString(ctx.getProperties().get(welcome_message), welcome_message);

to welcome_message = PropertiesUtil.toString(ctx.getProperties().get("welcome.message"), welcome_message);

注意区别:-ctx.getProperties()。get( welcome_message )vs ctx.getProperties()。get( “welcome.message”

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM