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());
							}
package conf;
/*
* #%L
* Osgiliath integration tests JaxRS CDI
* %%
* Copyright (C) 2013 - 2015 Osgiliath
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import helpers.cxf.exception.handling.jaxrs.mapper.ExceptionXmlMapper;
import java.util.Set;
import javax.inject.Inject;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
import net.osgiliath.features.karaf.jaxrs.cdi.HelloServiceJaxRS;
import org.apache.cxf.jaxrs.provider.JAXBElementProvider;
import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
import com.google.common.collect.Sets;
import com.wordnik.swagger.jaxrs.listing.ApiDeclarationProvider;
import com.wordnik.swagger.jaxrs.listing.ApiListingResourceJSON;
import com.wordnik.swagger.jaxrs.listing.ResourceListingProvider;
@ApplicationPath("/helloService")
public class CXFApplication extends Application {
@Inject private HelloServiceJaxRS helloService;
@Inject private ApiListingResourceJSON swaggerService;
@Override
public Set< Object > getSingletons() {
return Sets.< Object >newHashSet(
helloService,
swaggerService,
new JAXBElementProvider(),
new ExceptionXmlMapper(),
new ResourceListingProvider(),
new ApiDeclarationProvider(),
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):
@Slf4j
@ApplicationScoped
@ContextName
public class HelloServiceJMS extends RouteBuilder implements HelloService {
private final transient DataFormat helloObjectJSonFormat = new JacksonDataFormat(
HelloEntity.class, Hellos.class);
/**
* The repository.
*/
@Inject
@OsgiService
private transient HelloRepository helloObjectRepository;
/**
* JMS producer.
*/
@Inject
@Uri("jms:topic:helloServiceQueueOut")
private transient ProducerTemplate producer;
/**
* saves element
*
* @param helloObject
* element to save
*/
@Override
public void persistHello(@NotNull @Valid HelloEntity helloObject) {
log.info("****************** Save on JMS Service **********************");
log.info("persisting new message with jms: " + helloObject.getHelloMessage());
this.helloObjectRepository.save(helloObject);
ObjectMapper mapper = new ObjectMapper();
try {
this.producer.sendBody( mapper.writeValueAsString(getHellos()));
} catch (CamelExecutionException | JsonProcessingException e) {
log.error("exception marshalling messages", e);
}
}
/**
* Returns all elements.
*
* @return all elements
*/
@Override
public Hellos getHellos() {
final Collection<HelloEntity> helloObjects = this.helloObjectRepository.findAll();
if (helloObjects.isEmpty()) {
throw new UnsupportedOperationException("You could not call this method when the list is empty");
}
return Hellos.builder()
.helloCollection(helloObjects.stream().map(x -> x.getHelloMessage()).collect(Collectors.toList())).build();
}
/**
* Deletes all elements.
*/
@Override
public void deleteHellos() {
this.helloObjectRepository.deleteAll();
}
/**
* receives JMS message.
*
* @throws Exception
* Persistence error
*/
@Override
public void configure() throws Exception {
from("properties:{{helloApp.inQueueJMS}}"/*"jms:queue:helloServiceQueueIn"*/)
.log(LoggingLevel.INFO, "received jms message: ${body}").unmarshal(this.helloObjectJSonFormat).process(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
HelloServiceJMS.this.persistHello((HelloEntity) exchange.getIn().getBody());
}
});
}
}
view raw JMSSample.java hosted with ❤ by GitHub

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