Yearly Archives: 2011

12 posts

JPA, EJB and Spring

Today I stumbled over a rather interesting issue in combination with JBoss, Hibernate, Spring, JPA and EJB.

First of all, we have mixed environment of EJB3 and Spring 3 using JPA2. All in JBoss 6 AS, with default Hibernate 3.6 as JPA provider. Now for a long time everything works fine until we re-organize the dependencies of our modules. Starting with that we got some strange JPA behavior like “class is not an entity” and some rather weird getter issues.

Altogether it looked like some classloader issue. But it took some time to find that one.

In the beginning there was a JPA persistence unit called “someModel”. The problem with spring was, that it needs to be imported using the “jee:jndi-lookup” tag, which requires a JNDI name. That one was provided by JBoss itself using the “jboss.entity.manager.factory.jndi.name” property in the “persistence.xml” file. This triggers JBoss/Hibernate to bind the persistence unit with that specified name. Of course the name was “persistence/someModel”.

Now everything grew, EJB was added in order to gain modularity. And now that the dependencies and the start order was changed, the same persistence unit was used multiple times in multiple EJBs beside the spring application. While EJB seems not to have any problems finding its persistence unit, spring needs a bit more information and requires the already discusses “jee:jndi-lookup” tag in order to find its persistence unit.

As it turned out, the multiple persistence units all registered with the same name, leaving spring with the first (or last) registration, but not necessarily the correct registration. So spring accesses “some” persistence unit with that name and possible used the wrong classloader.

The solution was quite and and lot cleaner than using the “jboss.entity.manager.factory.jndi.name” property. But, less intuitive I have to say.

In the “web.xml” of the servlet that starts the application context a reference has to be added to the persistence unit:

[sourcecode language=”xml”]
<persistence-unit-ref>
<persistence-unit-ref-name>persistence/someModel</persistence-unit-ref-name>
<persistence-unit-name>someModel</persistence-unit-name>
</persistence-unit-ref>
[/sourcecode]

This will register the local (in the EAR file hosting the WAR) peristence unit “someModel” in the local JNDI space as “persistence/someModel” and you are done. Spring will can still import it as “persistence/someModel”.

Struggling with Acceleo 3.0 after Eclipse 3.7 upgrade

While the upgrade from Eclipse 3.6.2 to 3.7 went without nearly any trouble the upgrade of the Acceleo plugins to the versions provided with Eclipse Indigo was “a little bit more” problematic.

Starting the generator after the upgrade simply brought up:

Exception in thread "Main Thread" org.eclipse.acceleo.engine.AcceleoEvaluationException: The type of the first parameter of the main template named 'myTemplate' is a proxy.
    at org.eclipse.acceleo.engine.service.AcceleoService.doGenerate(AcceleoService.java:507)
    at org.eclipse.acceleo.engine.service.AbstractAcceleoGenerator.generate(AbstractAcceleoGenerator.java:175)
    at org.eclipse.acceleo.engine.service.AbstractAcceleoGenerator.doGenerate(AbstractAcceleoGenerator.java:154)
...

The main template did not accept the model anymore. Although everything was registered and working with the previous version.

Googling a bit I found out that Acceleo provides a different serialization model which stores models in binary form instead of XMI. Since all my models I flipped the switch in the project settings:

Acceleo Project Settings

Which worked at first until I stumbled over the next problem a few hours later:

org.eclipse.acceleo.engine.AcceleoEvaluationException: Invalid loop iteration at line 18 in Module myModule for block for (myObject.null). Last recorded value of self was com.thfour.config.model.model.impl.MyObjectImpl@81b69bf (....).
    at subModule.subModule(null)(subModule.mtl:18)
    at subModule.subModule(null)(subModule.mtl:6)
    at subModule.subModule(null)(subModule.mtl:4)
    at module.module(MyObject)(module.mtl:0)
    at module.module(MyObject)(module.mtl:7)

After some debugging it seemed as if the main module could not pass a model object to a template that is located in another bundle. We have structured our model generator to have a common part and a project specific bundle. At first it looked like as it this stopped working by an unknown reason.

After some testing, flipping switches, debugging, restructuring I found out that the solution was rather simple. The same switch (Binary, XMI) has to be set to the common generator module as well. All modules must agree to a model format (either binary or XMI).

While I can understand this for the generator that is launched, I cannot understand this for submodules that provide common generating templates and should not even care about the serialization form. But it is as it is ;-)

Some thoughts on software testing

If you are working as a software developer in a project based development environment you will, hopefully, encounter the day the customer wants to see the result that was promised to him. The worst thing that can happen is that after months of development you finally end up in a scenario of it is not working or it is not what we need. Sure there are numerous reasons of why this happened and what types development process you could have used. But often quality management and software development definitions are written once and never lived as described. developers consider it a burden that is unnecessary and is blocking them in their daily task of creating new functionality.

Continue reading

Access to WMI in Java using Eclipse SWT OLE integration

Today I ran into a problem which could easily solved using a short WMI query. The problem was that the query must be executed within a Java UI application. Googling for a solution I came only up with either quite some ugly workarounds (like generating a VBScript fragment, forking off the VBScript runtime and parsing the result) or some full blown COM/DCOM interfaces (like J-Integra or J-Interop). Although I really like J-Interop (we are using it for DCOM when accessing OPC server in OpenSCADA Utgard) it has some drawbacks. For J-Interop every access (even local access) is a network based access. Since J-Interop only supports DCOM it is free of any platform specific code but required the machine to be accessible using “remoting” functionality (DCOM). Since I wanted to query the WMI from a UI application and I am sure that the WMI query will stay on the Win32 version of the application I was not keen on adding “remoting” as a requirement to the UI application.

After some digging I remembered that SWT brings an OLE interface which provides direct COM access. So I started poking around and finally come up with a solution that works quite well.

For the impatient: The full source code is available from github https://github.com/ctron/wmisample and the screenshot is here.

Continue reading

Eclipse Install Issue

I just stumbled over a strange issue when installing an additional plugin into a fresh Eclipse installation:
An error occurred while collecting items to be installed
session context was:(profile=SDKProfile, phase=org.eclipse.equinox.internal.p2.engine.phases.Collect, operand=, action=).
No repository found containing: osgi.bundle,org.eclipse.team.cvs.ssh,3.2.100.I20090508-2000

I was unable to add any new plugin and searching Google for help was not successfull. There was an issue somewhere in the Eclipse Bugzilla that the plugin org.eclipse.team.cvs.ssh was optional and no longer installed or required. Seems that P2 thinks the somewhat required for the installation process.

ButI found one hint that deactivating the option “Contact all update sites during install to find required software” helps. And it did.

Screenshot of Eclipse Dialog

Workaround for LoadTimeWeaver issue with JBoss 6 and Spring 3

A possible workaround for the LTW issue that appeared in JBoss 6 using Spring 3 (SPR-7887) is to add an empty jboss-scanning.xml file to EAR and WAR files.

The reason to this issue as explained by one comment that JBoss pre-loads classes when scanning for annotations. Which triggers the class loader before Spring has a chance to add the AspectJ transformer to the class loader. The LTW support of Spring loads fine but after all classes are loaded.

Adding the empty jboss-scanning.xml to EAR and WAR files will skip the scanning for the modules and trigger the transformer later.

Example for jboss-scanning.xml:

[sourcecode language=”xml”]
<?xml version="1.0" encoding="UTF-8"?>

<scanning xmlns="urn:jboss:scanning:1.0">
</scanning>
[/sourcecode]

Thanks to Marius Bogoevici and Costin Leau for looking into this issue.

Name that pattern

In my day to day work I encounter several interesting programming pattern that I don’t have a name for. Maybe you have?! So name the following pattern:

[sourcecode language=”java”]
if ( !trans.getWeights ().isEmpty () )
{
final Collection<Weight> weightList = trans.getWeights ();
WeightVo weightVo = null;
for ( final Weight weight : weightList )
{
weightVo = getWeightDao ().toWeightVo ( weight );
break;
}
truckVo.setWeightVo ( weightVo );
}
[/sourcecode]

While one can argue if this one-liner is more readable and understandable:

[sourcecode language=”java”]
truckVo.setWeightVo ( trans.getWeights().isEmpty() ? null : getWeightDao ().toWeightVo ( trans.getWeights().get ( 0 ) ) );
[/sourcecode]

The following definitely is:

[sourcecode language=”java”]
if ( !trans.getWeights ().isEmpty () )
{
truckVo.setWeightVo ( getWeightDao ().toWeightVo ( trans.getWeights().get ( 0 ) ) );
}
[/sourcecode]