Purpose

The business module is done to make computation and processing on your datas. You’ll be able to test it (unitary or in its full environment), export its services (synchronously or asynchronously), use functional programing.

Referencing services

You can reference OSGI services via two ways: via annotations or xml.

Service reference via annotation

You inject your services via Annotations (Pax-CDI). In order to do this, you’ve to tweak you pom.xml adding this feature:
1
2
3
4
5
6
7
<dependency>
			<groupId>net.osgiliath.framework</groupId>
			<version>${osgiliath.framework.maven.version}</version>
			<artifactId>net.osgiliath.features.karaf-features-cdi</artifactId>
			<type>xml</type>
			<classifier>features</classifier>
		</dependency>
And add a beans.xml file in your src/main/resources/META-INF:
1
2
3
4
5
6
7
<beans
   xmlns="http://java.sun.com/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="
      http://java.sun.com/xml/ns/javaee
      http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
    </beans>
You can now expose and consume your services (and other cdi beans) this way:
1
2
3
4
5
6
7
8
9
@ApplicationScoped
public class Provider implements IProvider {}
@Default
@OsgiServiceProvider(classes= {IConsumer.class})
public class Consumer implements IConsumer{
@Inject
@OsgiService
private IProvider provider;
}

REST

You can expose REST web services easily. Using the REST API, you have to tell with path to access your services:
1
2
3
4
5
6
7
8
9
@Path("/hello")
public interface HelloRepository {
	@POST
	@Consumes(MediaType.APPLICATION_XML)
	public <S extends HelloEntity> S save(S entity);
	@GET
	@Produces(MediaType.APPLICATION_XML)
	public List<HelloEntity> findAll();
}
And finally expose the service via annotations:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
		@Eager
		@ApplicationPath("/helloService")
		@Slf4j
		public class CXFApplication extends Application {

		@Inject
		private transient HelloService helloService;
		@Inject
		private transient SwaggerAPIAccessService swagger;

		@Override
		public Set<Object> getSingletons() {

					return Sets.<Object> newHashSet(helloService
							new SwaggerSerializers(),
							new ValidationExceptionMapper(),
							new JacksonJsonProvider());
							}

Messaging

You can also consume or produce asynchronous messages. You can consume queued messages and send to with the Camel JMS template (take care, with CDI, @Consume doesn’t work):

Validation

You can use JSR303 bean validation to validate your fields.
1
2
3
4
@Data
public class HelloObject {
	@NotNull
	private String message;}
Finally, use the injected validator to validate your instances:
1
2
3
4
Set<ConstraintViolation<HelloObject>> validationResults = validator
				.validate(helloObject_p);
		String errors = "";
		if (!validationResults.isEmpty()) {....}
You can also bootstrap validator factory via CDI and reference the interceptor in your beans.xml to have validation

Integration testing

I will pass through the unit testing case (you can use Mockito…) to jump to the integration tests part. First, you have to define an integration test feature on the feature module (available from the parent archetype) referencing all the underlying dependencies:
1
2
3
4
5
6
7
8
9
10
<feature name="mybundle.business.impl.itests" version="${project.version}">
	<feature version='${osgiliath.framework.maven.version}'>osgiliath-business</feature>
	<feature version="${project.version}" prerequisite = "true">osgiliath-validation-cdi</feature>
	<feature version="${project.version}" prerequisite = "true">osgiliath-rest-cdi-web</feature>
	<feature version="${project.version}" prerequisite = "true">osgiliath-messaging-cdi</feature>
	<feature version="${project.version}" prerequisite = "true">osgiliath-routes-xmlandjson</feature>
	<feature version="${project.version}" prerequisite = "true">osgiliath-messaging-default-broker</feature>
	<bundle>mvn:${project.parent.groupId}/mybundle.business.spi/${project.version}/jar</bundle>
    <bundle>file:${project.parent.basedir}/${project.groupId}.business/mybundle.business.impl/target/mybundle.business.impl-${project.version}.jar</bundle>
	</feature>
You can now make your integration test prefixing it by IT (failsafe convention).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@RunWith(PaxExam.class)
@ExamReactorStrategy(PerClass.class)
public class ITHelloServiceJaxRS extends AbstractKarafPaxExamConfiguration {
@Inject//Waiting for the OSGI service before starting the tests
	@Filter(timeout = 40000)
	private HelloService helloService;
@Test
	public void testSayHello() throws Exception {
helloService.sayHello("bob");
}
    @Override
    protected Option featureToTest() {
    return features(maven().groupId(System.getProperty(MODULE_GROUP_ID))
    .artifactId(System.getProperty(MODULE_GROUP_ID) + ".features")
    .type("xml").classifier("features").versionAsInProject(),
    "mybundle.business.impl.itests");
    }

    }

Comments