简体   繁体   English

OSGI的可配置Java Servlet

[英]Configurable Java Servlet from OSGI

I'm trying to create a Java class that is configurable via the OSGi console. 我正在尝试创建一个可通过OSGi控制台配置的Java类。 I've heard you can do this via SCR annotations but not entirely sure how. 我听说您可以通过SCR注释执行此操作,但不完全确定该怎么做。 I've got the bulk of it but unsure what to get and post and how to reference it in the JSP. 我已经掌握了大部分内容,但不确定要获取和发布的内容以及如何在JSP中引用它。 Here is what I have so far: 这是我到目前为止的内容:

import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;

import javax.servlet.ServletException;
import java.io.IOException;

@SlingServlet(
paths={"/somepath/"}
)
@Properties({
@Property(name="email.add", value="Email Info",propertyPrivate=false),
@Property(name="user.info",value="User Info", propertyPrivate=false)
})
public class WelcomeMessage extends SlingAllMethodsServlet
{
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse                                 response) throws ServletException, IOException
{
    //Do something here
}

@Override
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException
{
    //Do something here
}
}

To be able to process such annotations, you need to setup the Maven SCR Plugin (from Apache Felix). 为了能够处理此类注释,您需要设置Maven SCR插件(来自Apache Felix)。 This plugin will process annotations and create metadata inside your resulting JAR file. 该插件将处理注释并在生成的JAR文件中创建元数据。

@SlingServlet annotation is Apache Sling specific and will need certain Apache Sling bundles to be able to register the servlet. @SlingServlet注释特定于Apache Sling,将需要某些Apache Sling捆绑包才能注册servlet。 @SlingServlet annotations are also processed by the Maven SCR Plugin. @SlingServlet批注也由Maven SCR插件处理。

Here is a sample on how to configure the SCR plugin in Maven. 这是有关如何在Maven中配置SCR插件的示例。

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.felix</groupId>
      <artifactId>maven-scr-plugin</artifactId>
      <version>1.9.0</version>
      <executions>
        <execution>
          <id>generate-scr-scrdescriptor</id>
          <goals>
            <goal>scr</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

Also, to be able to create an OSGi bundle (Jar with OSGi metadata), you will need to setup the Maven Bundle Plugin. 另外,为了能够创建OSGi捆绑包(带有OSGi元数据的JAR),您将需要设置Maven捆绑包插件。

You can find a brief documentation on the Maven SCR Plugin here: http://felix.apache.org/documentation/subprojects/apache-felix-maven-scr-plugin.html . 您可以在以下位置找到有关Maven SCR插件的简短文档: http : //felix.apache.org/documentation/subprojects/apache-felix-maven-scr-plugin.html

Maven Bundle plugin documentation is here: http://felix.apache.org/site/apache-felix-maven-bundle-plugin-bnd.html . Maven Bundle插件文档在这里: http : //felix.apache.org/site/apache-felix-maven-bundle-plugin-bnd.html

But, the best way to understand this is by looking at examples in the Sling bundles here: https://github.com/apache/sling/tree/trunk/bundles . 但是,了解这一点的最好方法是在此处查看Sling捆绑包中的示例: https : //github.com/apache/sling/tree/trunk/bundles

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

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