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.

src
   main
      provisioning
         boot.txt
         composum.txt
         launchpad.txt
         oak.txt
         repoinit.txt
         scripting.txt
         sling-discovery.txt
         sling-event.txt
         sling.txt
         standalone.txt

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:

[feature name=extractor]
 
[artifacts startLevel=15]
  org.apache.sling/org.apache.sling.datasource/1.0.2
  mysql/mysql-connector-java/6.0.5
  it.altereos.labs/extractor/0.0.1

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

[:repoinit]
  create service user extractionAdmin
  create path (sling:Folder) /var/sling
  set ACL for extractionAdmin
    allow   jcr:all  on  /var/sling
  end

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

...
<packaging>slingstart</packaging>
...
<build>
   <plugins>
      …
      <plugin>
         <groupId>org.apache.sling</groupId>
         <artifactId>slingstart-maven-plugin</artifactId>
         <version>1.7.0</version>
         <extensions>true</extensions>
         <executions>
            <execution>
               <id>start-container</id>
               <goals>
                  <goal>start</goal>
                  <goal>stop</goal>
               </goals>
            </execution>
         </executions>
         <configuration>
            <createWebapp>true</createWebapp>
            <servers>
               <server>
                  <port>${http.port}</port>
                  <controlPort>${sling.control.port}</controlPort>
                  <runmode>${runmodes}</runmode>
                  <folder>${folder}</folder>
                  <debug>${debug}</debug>
               </server>
            </servers>
         </configuration>
      </plugin>

Note: true 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
  2. 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
  3. 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:

FROM java:8
MAINTAINER your@email.com
COPY launchpad-extractor-0.0.1.jar /opt/sling/
WORKDIR /opt/sling/
EXPOSE 8080
VOLUME /opt/sling/sling
ENV JAVA_OPTS -Xmx512m
ENV SLING_OPTS -Dsling.home=/opt/sling/extractor
CMD exec java $JAVA_OPTS -jar launchpad-extractor-0.0.1.jar $SLING_OPTS

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

https://altereos.anteprimaprogetto.it/wp-content/uploads/2023/02/logo-altereos.png

Copyright 2023 © Altèreos | All rights reserved

Copyright 2023 © Altèreos | All rights reserved

bt_bb_section_top_section_coverage_image
bt_bb_section_bottom_section_coverage_image