Project GreenFire mentioned on Java Posse

On a recent episode of The Java Posse they mentioned a project I had never heard about before: project GreenFire.

From the project’s website:

GreenFire efficient manages and controls heating control of houses and saves energy. GreenFire is based on Java EE 5 (is tested with Glassfish v2), Scripting, RMI and Shoal. SunSpot integration is planed.

I applaude the effort to create a J2EE-based solution to cheap and efficient home automation. I’ve often felt that home automation was, and still is, the domain of do-it-yourselfers, with its attendant reliability problems. Running a home automation solution on a J2EE stack might solve many of these problems.

In short, kudos to the creator(s) of this project, I’ll definitely keep an eye on what you are doing.

Looking up an EJB from a Web Service under JBoss 4.x

EJB injection in Web Services does not work with JBoss (yet), so when you want to use an EJB from your @WebService annotated POJO you have no choice but to look it up yourself.

This can get a little tricky, because each J2EE container can use its own JNDI naming convention when registering the EJB in the global naming context. Here I document how I configured JBoss to register my EJB with a name that I choose, and how I mapped a proper EJB name to that JNDI name.

Naming the EJB

Give your EJB a reasonably unique name in its annotation:

@Stateless(name=”MyServiceBean”)
@Local(SomeInterface.class)
public class ServiceBean implements SomeInterface {

}

Tell JBoss what JNDI name to register it under

Include the following in your jboss.xml file:




MyServiceBean
myapp/MyJNDIName


Tell JBoss to map an EJB name to the JNDI name

In your WAR, configure your jboss-web.xml file to include this:



ejb/MyEJBName
myapp/MyJNDIName

Lookup the EJB from your Web Service

You can now lookup the EJB directly from your Web Service:

@WebService
public class MyWebService {
private SomeInterface getMyBean() {
try {
return (SomeInterface) new InitialContext().lookup(“java:comp/env/ejb/MyEJBName”);
} catch (NamingException e) {
throw new EJBException(e);
}
}
}