Posts

How to create a custom distribution of Apache Sling to run your Sling Application

Apache Sling is a web framework that uses a Java Content Repository, such as Apache Jackrabbit, to store and manage content. Sling applications use either scripts or Java servlets to process HTTP requests in a RESTful way, while the embedded Apache Felix OSGi framework and console provide a dynamic runtime environment, where code and content bundles can be loaded, unloaded and reconfigured at runtime.

The Sling Launchpad is a ready-to-run Sling configuration, providing an embedded JCR content repository and web server, a selection of Sling components, documentation and examples. The Launchpad makes it easy to get started with Sling and to develop script-based applications.

Apache Sling Provisioning Model and SlingStart

The Apache Sling Provisioning Model and Apache SlingStart makes it much easier to create custom distributions of Apache Sling.

Inspired by the Launchpad builder (source at Github), you can create your own provisioning model.

In the source code of the builder there is a directory under /src/main called provisioning, you can use the files in this folder to mix and match bundles and bundle versions for your custom build.

When Maven is used to build Apache Sling, these files are used to define content and  configuration of the sling build. For example, which version of Launchpad is used and which bundles are included.

The provisioning files in the Launchpad Builder folder for Apache Sling will build a basic version of Apache Sling. Any provisioning model files added to the provisioning folder will be used.

 

Simple Use Case: Sling Launchpad to extract informations from mysql and store them in JCR

Starting from provisioning files provided in Launchpad Builder folder, you can define a new text file, in which add:

  • Sling datasource bundle
  • mysql java connector bundle
  • bundle previously produced to Extract information from mysql and store them in JCR (extractor)

 

For example, your file will include this content:

 

Next, in the same file, you can configure repository initialization with service users and needed ACL definitions (see Repository Initializers and Repository Initialization Language)

 

Now you must configure your pom.xml, defining as packaging type slingstart, including and configuring slingstart-maven-plugin:

 

Note: <createWebapp>true</createWebapp> says to create also a minimal Web Application, which may simply be deployed into your favourite servlet container.  If you like to build only Sling Launchpad self-runnable jar, omit that row or set “false” value.

 

Start your Sling Application

Here three ways to start your Launchpad:

  1. compiling it and testing Sling Instance start, using maven from command line, starting from project root dir:

mvn clean post-integration-test -Dhttp.port=8080

It will execute, after package phase, slingstart:start and slingstart:stop

  1. compiling it and starting Sling Instance, using maven from command line, starting from project root dir:

mvn clean package slingstart:start -Dhttp.port=8080 -Dlaunchpad.keep.running=true

You can stop it with Ctrl + C

  1. starting Sling Instance directly by launchpad jar, you can launch it with:

java -jar launchpad-extractor-0.0.1.jar -Dorg.osgi.service.http.port=8080

 

If you don’t want to use default sling home folder, you can include option -Dsling.home=/my/path

Http.port is not mandatory, without this information, it uses one randomly

 

Docker

You can also create a Docker image and launch it exposing Sling instance on its http port.

Prepare a configuration file like this:

 

Build an image executing:

docker build -t sling-extractor

To launch a docker instance named “extractor-container” bound to port 8080 on the local machine, and with the /opt/sling/sling volume mounted at /srv/docker/sling in the local machine, execute:

docker run -ti -p 8080:8080 -v /srv/docker/sling:/opt/sling/sling –name extractor-container sling-extractor