Currently, I am moving this blog onto the latest version of Sling. Part of this effort is the migration of the comment spam checker into an OSGi bundle (mostly, that means wrangling with Maven). Here's two little bits of information I encountered along the way. Maybe they can be useful to someone.

The actual backend services that are used for comment verification are Akismet and Typepad's Anti Spam. I took David Czarnecki's Akismet-Java library that wraps the respectice REST APIs of these services (both service providers actually use the same API).

The trouble with David's code is that it uses commons-httpclient which depends on commons-logging. That clashes with Sling's use of log4j (note to the Java community: how could we get into this logging mess?). I found the solution for this annoying problem in Sling's parent pom.xml. Here's the relevant bit:

<dependency>
  <groupId>commons-httpclient</groupId>
  <artifactId>commons-httpclient</artifactId>
  <version>3.1</version>
  <scope>provided</scope>
  <exclusions>
    <exclusion>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
    </exclusion>
  </exclusions>
</dependency>

The second thing I would like to point out: it is quite simple to make OSGi bundles configurable through the web console (at http://localhost:7402/system/console).

This is useful e.g. for configuring the API key of the above-mentioned service providers. In order to expose a property in the console use the annotation @scr.property:

/** @scr.property */
public static final String PARAM_API_KEY = "akismet.service.api.key"; 

Other types like integer or boolean can also be used:

/** @scr.property value="0" type="Integer" options 0="Akismet" 1="Typepad" */
public static final String PARAM_SERVICE_PROVIDER = "akismet.service.provider";  

The values are read in the service's setup method:

Object key = configuration.get(PARAM_API_KEY);
if (key != null) {
    this.apiKey = key.toString();
}

As usual, the full sources are attached.


Related Posts


Files

one file attached

  1. antispam.zip



Comments

3 comments

  1. Michael on 15/10/2008

    > (note to the Java community: how could
    > we get into this logging mess?).

    Probably we should write our own logging framework to resolve the mess... ;-)

  2. Felix on 15/10/2008

    Just a small clarification with respect to logging support in Sling: Sling has its own logging support bundle (org.apache.sling.commons.log) which exports the Jakarta Commons Logging API 1.1.1, the Log4J API 1.2.15 and the SLF4J API 1.5.2. In addition the bundle implements the OSGi LogService. So this is your one-stop-shop for logging in Sling.

    Internally the Sling log bundle uses SLF4J, that is JCL, Log4J and OSGi LogService are implemented on top of SLF4J. In addition, we have our own SLF4J SPI implementation.

    So using httpclient is really no big deal in Sling because the required JCL library is already present.

  3. Christian Sprecher on 16/10/2008

    Very nice non-trivial example of an OSGI-bundle, thx!

Add a comment

Name

URL

  • Print version

    Printer-friendly version
  • Recent Discussions

  • PlanetDay

    The latest posts on PlanetDay

  • Links on Daigg