Monday 5 August 2013

From CODI to DeltaSpike

Introduction

CODI is my favourite CDI extension framework where you can find all kind of goodies when you are working in a CDI environment.
Some time ago, there was a decision to create a new CDI extension framework which brings together all the good features of CODI and Seam 3 and other features from other CDI extension libraries.

They have gone through the incubation process at Apache and moving along steadily. The question is, can you already switch from CODI to DeltaSpike for an application?

Not all features of CODI are implemented but my 2 favourites ones are
- The additional scopes like ViewAccessScope
- The fluent API for adding FacesMessages.
So can we create a DeltaSpike application with these 2 features.

Scopes

In the current version of the DeltaSpike framework, version 0.4, there is no support for something like the CODI's ViewAccessScope .
Therefor, Gerhard Petracek created an extension for DeltaSpike to have the missing scopes from CODI version 1.0.5 into DeltaSpike. See here.
So adding the required dependency to the POM


<repositories>
 <repository>
  <id>os890</id>
  <name>Gerhard personal repository</name>
  <url>http://os890-m2-repository.googlecode.com/svn/tags/os890/</url>
  <layout>default</layout>
 </repository>
</repositories>

<dependency>
    <groupId>org.os890.cdi.ext.scope.modules</groupId>
    <artifactId>os890-cdi-ext-jsf2-module-impl</artifactId>
    <version>1.0.5_0.4_01</version>
    <scope>runtime</scope>
</dependency>



The version of the extension is a concatenation of the 2 libraries it will bridge. So we have the structure  &CODI version&_&DS version&_&extension version&.

You only have to use the new package name to have the additional CODI scopes.  The API is identical so that existing code which works for CODI, will also work with DeltaSpike.


So for example for ViewAccessScope,

import org.apache.myfaces.extensions.cdi.core.api.scope.conversation.ViewAccessScoped;

becomes 
import org.os890.cdi.ext.scope.api.scope.conversation.ViewAccessScoped;
So for any of the scope packages, you have to replace org.apache.myfaces.extensions.cdi.core with org.os890.cdi.ext.scope.

Messaging


The support for a fluent messaging API is already present in DeltaSpike. So to have this kind of functionality, we don't need to use an extension as we did for having the scopes.

But here we have the issue that the API is changed and thus we need to adapt existing code.

CODI version

messageContext.message().text("{msgKey}").add();

DeltaSpike version

Message defitions.
@MessageBundle
public interface AppMessages {
    @MessageTemplate("{msgKey}")
    String buttonClicked();
}

Within CDI bean
    @Inject
    private JsfMessage jsfMessage;

     
    public void addTestMsg(ActionEvent actionEvent) {
        jsfMessage.addInfo().buttonClicked();
    }


Servers

 The DeltaSpike application was tested successful on Glassfish 3.1.2.2 and JBoss as 7.1.1.Final.

Also the original CODI version was running without a problem on the new Glassfish 4 version.

On this server, the DeltaSpike version had a problem.  There was an exception thrown in the extension as there is no problem with the DeltaSpike code itself. All the integrations tests of DeltaSpike are running also on a Weld 2 version (used in the Glassfish 4) so you can expect that it is working on it without any problem.

With the new WLS 12C server, version 12.1.2, there is also an issue running the DeltaSpike application.   The application deployment fails during validation where it can't inject an Extension class into another bean.

Conclusion

With the help of the DeltaSpike extension, you can create already applications that uses a lot of the features of the CODI framework.
So check what features you need and maybe you can already create your application with the DeltaSpike framework.