0

I'm trying to create a Java class that is configurable via the OSGi console. I've heard you can do this via SCR annotations but not entirely sure how. I've got the bulk of it but unsure what to get and post and how to reference it in the 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
}
}
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Delmon Young
  • 2,013
  • 5
  • 39
  • 51
  • This is sling specific and needs the sling osgi bundles to work. You do also need to process the annotations via scr maven plugin. If you do not need sling, look at the Apache Http Servlet bridge to get you going. – Sten Roger Sandvik Mar 29 '13 at 21:23
  • Thanks for replying. Sorry but I'm confused I'm unsure what you mean by processing the annotations via a maven plugin do you have a simple example? I've got maven installed but unsure what you mean by processing the annotations. Sorry, complete and I really mean complete newb to this. – Delmon Young Mar 30 '13 at 16:45

1 Answers1

3

To be able to process such annotations, you need to setup the Maven SCR Plugin (from Apache Felix). This plugin will process annotations and create metadata inside your resulting JAR file.

@SlingServlet annotation is Apache Sling specific and will need certain Apache Sling bundles to be able to register the servlet. @SlingServlet annotations are also processed by the Maven SCR Plugin.

Here is a sample on how to configure the SCR plugin in Maven.

<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.

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 Bundle plugin documentation is here: 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.

Sten Roger Sandvik
  • 2,526
  • 2
  • 19
  • 16