-
Notifications
You must be signed in to change notification settings - Fork 2
Hello World part I
| Tutorial Level | Goals | Prerequisites |
|---|---|---|
| Beginners |
|
|
In this tutorial, you will learn to create and run a very simple ApAM Component.
Now available with the new ApAM in 5 minutes video tutorial !.
Firstly, you create a regular maven project as usual, with the basic project descriptor (pom.xml) :
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>fr.imag.adele.apam.tutorials.helloworld</groupId>
<artifactId>HelloWorld-ApAM</artifactId>
<version>0.0.1-SNAPSHOT</version>
<description>A very Simple Hello World Implementation with ApAM</description>
<project>It is very simple thanks to Apache Felix maven bundle plugin. You need to change the packaging of you artifact :
<project>
...
<packaging>bundle</packaging>
...
<project><project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>2.3.7</version>
<extensions>true</extensions>
</plugin>
</plugins>
</build>
...
<project>This is also a matter of plugin. As ApAM is evolving rapidly, let's add a custom property indicating the current version (please change to the latest stable) :
<project>
...
<properties>
<apam-version>0.0.3</apam-version>
</properties>
...
<project><project>
...
<build>
<plugins>
...
<plugin>
<groupId>fr.imag.adele.apam</groupId>
<artifactId>apam-maven-plugin</artifactId>
<version>${apam-version}</version>
<executions>
<execution>
<goals>
<goal>apam-bundle</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
...
<project>We need to get this particular plugin from ApAM repository :
<project>
...
<pluginRepositories>
<pluginRepository>
<id>apam-plugin-repository</id>
<url>https://repository-apam.forge.cloudbees.com/release/repository/</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
...
<project><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>fr.imag.adele.apam.tutorials.helloworld</groupId>
<artifactId>HelloWorld-ApAM</artifactId>
<version>0.0.1-SNAPSHOT</version>
<description>A very Simple Hello World Implementation with ApAM</description>
<packaging>bundle</packaging>
<!-- Check Latest ApAM Version -->
<properties>
<apam-version>0.0.4-SNAPSHOT</apam-version>
</properties>
<!-- Repositories List -->
<pluginRepositories>
<pluginRepository>
<id>apam-plugin-repository</id>
<url>https://repository-apam.forge.cloudbees.com/release/repository/</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>2.3.7</version>
<extensions>true</extensions>
</plugin>
<plugin>
<groupId>fr.imag.adele.apam</groupId>
<artifactId>apam-maven-plugin</artifactId>
<version>${apam-version}</version>
<executions>
<execution>
<goals>
<goal>apam-bundle</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>Nothing particular with this. No ApAM dependencies or libraries. Create a class in the src/main/java directory :
package fr.imag.adele.apam.tutorials.helloworld;
/**
* A simple POJO implementation of Hello World with ApAM and a callback function
*
*/
public class HelloWorldApAM {
public void sayHello() {
System.out.println("Hello World, from an ApAM component !");
}
}- The package for our component main class : fr.imag.adele.apam.tutorials.helloworld
- The name of our component main class : HelloWorldApAM
- The name of the function that we want to be called when our component start : public void sayHello()
The ApAM descriptor is an XML file named metadata.xml in src/main/resources.
Our first component is a java class from which we can create objects, it will the be an ApAM Implementation (from which we will create ApAM Instances).
We give the name HelloWorld-ApAM to our component and we indicate the corresponding java class fr.imag.adele.apam.tutorials.helloworld.HelloWorldApAM created previously.
<apam xmlns="fr.imag.adele.apam" xmlns:ipojo="org.apache.felix.ipojo"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="fr.imag.adele.apam http://repository-apam.forge.cloudbees.com/release/schema/ApamCore.xsd" >
<implementation name="HelloWorld-ApAM"
classname="fr.imag.adele.apam.tutorials.helloworld.HelloWorldApAM">
</implementation>
</apam>sayHello(). This is done using a callback :
<callback onInit="sayHello"/><apam xmlns="fr.imag.adele.apam" xmlns:ipojo="org.apache.felix.ipojo"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="fr.imag.adele.apam http://repository-apam.forge.cloudbees.com/release/schema/ApamCore.xsd" >
<implementation name="HelloWorld-ApAM"
classname="fr.imag.adele.apam.tutorials.helloworld.HelloWorldApAM">
<callback onInit="sayHello"/>
</implementation>
</apam>To compile and install our component, simply use :
mvn clean install
The build goes through several steps : resolve dependencies -> compile -> test -> OSGi bundle packaging -> ApAM static checks (according to metadata.xml) -> Description of ApAM capabilities of the component through obr.xml file -> deployment in the local maven repository.
Check the result :
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------According to the download of the prepackaged ApAM distribution, open a terminal at the root directory : apam-basic-distribution-VERSION. and launch :
java -jar bin/felix.jar
You can check the OSGi Bundle already installed and running,
just type lb in the console. It contains several utilities and the ApAM core bundles. All these bundles should be in Active state before going further.
Notice that our HelloWorld-ApAM bundle isn't installed nor stated yet.
You can also check all the running ApAM Instance components,
just type inst. All the core ApAM Manager are already started.
OBRMAN-Instance resolves dependencies using OSGi bundle repository. By default, it uses the local maven repository.
As we have seen previously, our HelloWorld-ApAM bundle has been deployed in the local maven repository. We can try to create an instance from our implementation.
Just type l HelloWorld-ApAM. (1)It will resolve using the implementation name described in the local repository, (2)install and start the bundle on the platform, (3)create the ApAM instance and (4)call the sayHello method as a callback.
Congratulation, your first ApAM component is up and running !!!