...
So now, it is time to create the interface that we will use in this project. Open Eclipse environment if not already done and create a new folder "service" in src/main/java/demo
tree. Add the interface TransformService.java
and copy paste the code below:
Code Block |
---|
| java |
---|
| java |
---|
title | TransformService.javajava |
---|
|
package demo.service;
public interface TransformService {
public Object transform(Object obj);
}
|
...
Next, we will create the class TransformServiceImpl
implementing the interface TransformService
in the folder "impl" in src/main/java/demo/service/impl
.
Code Block |
---|
| java |
---|
| java |
---|
title | TransformServiceImpl.javajava |
---|
|
package demo.service.impl;
import java.util.Date;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class TransformServiceImpl implements TransformService {
private static final transient Log LOG = LogFactory.getLog(TransformServiceImpl.class);
private boolean verbose = true;
private String prefix = "MyTransform";
public Object transform(Object body) {
String answer = prefix + " set body: " + new Date();
if (verbose) {
System.out.println(">> call >> " + answer);
}
LOG.info(">> call >>" + answer);
return answer;
}
public boolean isVerbose() {
return verbose;
}
public void setVerbose(boolean verbose) {
this.verbose = verbose;
}
public String getPrefix() {
return prefix;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
}
|
...
Create the file demo-service-bundle-contxt.xml
in the folder src/main/resources/META-INF/spring
Code Block |
---|
| xml |
---|
| xml |
---|
title | demo-service-bundle-contxt.xml | xml |
---|
|
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- regular spring configuration file defining the beans for this
bundle. The configuration of OSGi definitions is kept in a separate
configuration file so that this file can easily be used
for integration testing outside of an OSGi environment -->
<bean id="transformService" class="demo.service.impl.TransformServiceImpl">
</bean>
</beans>
|
...
Create the file demo-service-bundle-contxt-osgi.xml
in the folder src/main/resources/META-INF/spring
Code Block |
---|
| xml |
---|
| xml |
---|
title | demo-service-bundle-contxt-osgi.xmlxml |
---|
|
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:osgi="http://www.springframework.org/schema/osgi"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/osgi
http://www.springframework.org/schema/osgi/spring-osgi.xsd">
<osgi:service ref="transformService">
<osgi:interfaces>
<value>demo.service.TransformService</value>
</osgi:interfaces>
</osgi:service>
</beans>
|
...
- For the routing, create the following file in the folder
src/main/resources/META-INF/spring
of the project demo.camel-bundle
:
Code Block |
---|
| xml |
---|
| xml |
---|
title | CamelContext.xml | xml |
---|
|
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://camel.apache.org/schema/spring
http://camel.apache.org/schema/spring/camel-spring-2.0.0.xsd">
<camelContext xmlns="http://camel.apache.org/schema/spring">
<route>
<from uri="timer://myTimer?fixedRate=true&period=10000"/>
<bean ref="myTransform" method="transform"/>
<to uri="log:ExampleRouter"/>
</route>
</camelContext>
</beans>
|
...
- To inject the dependency, we will create a second file named
bundle-context-osgi.xml
in the same folder:
Code Block |
---|
| xml |
---|
| xml |
---|
title | bundle-context-osgi.xmlxml |
---|
|
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:osgi="http://www.springframework.org/schema/osgi"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/osgi
http://www.springframework.org/schema/osgi/spring-osgi.xsd">
<osgi:reference id="myTransform" interface="demo.service.TransformService"/>
</beans>
|
...
Prior to run/launch the command generating the MANIFEST file, you must modify the pom.xml file in order to allow the OSGI bundle to import/use the services classes : demo.service
Code Block |
---|
| xml |
---|
| xml |
---|
title | pom demo.camel-bundlexml |
---|
|
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<properties>
<bundle.symbolicName>demo.camel-bundle</bundle.symbolicName>
<bundle.namespace>demo.camel</bundle.namespace>
</properties>
<modelVersion>4.0.0</modelVersion>
<groupId>demo.camel</groupId>
<artifactId>demo.camel-bundle</artifactId>
<version>0.1</version>
<name>${bundle.symbolicName} [${bundle.namespace}]</name>
<packaging>bundle</packaging>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
<!--
| example additional resource entries, useful when building Eclipse RCP applications
-->
<resource>
<directory>.</directory>
<includes>
<include>plugin.xml</include>
<include>plugin.properties</include>
<include>icons/**</include>
<include>META-INF/*</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.ops4j</groupId>
<artifactId>maven-pax-plugin</artifactId>
<version>1.4</version>
<!--
| enable improved OSGi compilation support for the bundle life-cycle.
| to switch back to the standard bundle life-cycle, move this setting
| down to the maven-bundle-plugin section
-->
<extensions>true</extensions>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>1.4.3</version>
<!--
| the following instructions build a simple set of public/private classes into an OSGi bundle
-->
<configuration>
<instructions>
<Bundle-SymbolicName>${bundle.symbolicName}</Bundle-SymbolicName>
<Bundle-Version>${pom.version}</Bundle-Version>
<Import-Package>demo.service;version="${pom.version}"</Import-Package>
<Include-Resource>src/main/resources</Include-Resource>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>osgi_R4_core</artifactId>
<version>1.0</version>
<scope>provided</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>osgi_R4_compendium</artifactId>
<version>1.0</version>
<scope>provided</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>demo.service</groupId>
<artifactId>demo.service-bundle</artifactId>
<version>${pom.version}</version>
</dependency>
</dependencies>
</project>
|
...