11/28/2007

Calling JBoss EJB 3 from JRuby client

In Calling EJB 3 from JRuby client, I wrote how to call EJB3 deployed to glassfish from a JRuby script. This post will show how to do it with JBoss EJB3, reusing the same EJB3 as the previous post (A simple EJB 3 on JBoss application server). Steps 1-7 (creating EJB3 classes, starting JBoss server, deploying EJB, preparing jndi.properties and log4j.properties) are exactly the same as that post. Here I will show the client script code and how to run it.

The JRuby script (testEJB.rb) is almost identical to the one for glassfish, except the default jndi name. TODO: pass it in as parameter instead of hardcoding it.
require 'java'
include_class 'javax.naming.InitialContext'

ic = InitialContext.new
foo = ic.lookup("FooBean/remote")
result = foo.echo("This is foo!")
puts "Calling the EJB 3 : #{foo} returned #{result}"
Before running the script, we need to set the environment variable CLASSPATH, which is of course different from the one for glassfish.
set CLASSPATH=%JBOSS_HOME%\lib\*;%JBOSS_HOME%\client\*;%JBOSS_HOME%\server\default\lib\*;C:\simple-ejb3\classes
jruby testEJB.rb
The above classpath value works only with JDK 6 or later. If you are stuck with JDK 5, then:
set CLASSPATH=%JBOSS_HOME%\lib\jboss-aop-jdk50.jar;
%JBOSS_HOME%\client\jbossall-client.jar;
%JBOSS_HOME%\server\default\lib\jbosssx.jar;
C:\simple-ejb3\classes
Technorati Tags: , ,

6 comments:

Navjeet said...

I am trying to follow these steps to call an ejb method that has a role restriction but I am getting "javax.ejb.EJBAccessException: Authorization failure". I have added the following properties to jndi.properties:
java.naming.security.principal=myUsername
java.naming.security.credentials=myPassword
java.naming.security.protocol=com.acme.security.providers.authentication.DbLoginModuleImpl

In the jboss server log it displays the following message:
[java] 12:44:04,101 ERROR [RoleBasedAuthorizationInterceptor] Insufficient
permissions, principal=null, requiredRoles=[MY_ROLE], principalRoles=null


Is it possible to do this?

javahowto said...

It's a limitation of a standalone java/JRuby client. Because they are not managed by JavaEE containers and so don't have a clearly defined authentication mechanism.

Adding client credentials in jndi.properties won't help a bit since they are used for accessing server jndi service, nothing to do with the target EJB.

You can use some sort of programmatic login in your java client. JBoss may have such api. Another option is to rewrite your client as servlet or application client and let appserver handle security.

Navjeet said...

Well after doing some googling around I have found the solution to my problem.

I had to use a special Initial Context Factory class provided by JBoss and everything works like a charm. Here is my final jndi.properties:

java.naming.factory.initial=org.jboss.security.jndi.JndiLoginInitialContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost:1099
java.naming.security.principal=my_user_id
java.naming.security.credentials=my_password

I am using JBoss AS 4.2.1 on Windows XP.

JRuby ROCKS !!! I hope this helps somebody else.

Anonymous said...

txn :)

Anonymous said...

Thanks worked for me as well

Anna said...

Great and Useful Article.

Online Java Course

Java Online Training

Java Course Online

J2EE training

online J2EE training

Best Recommended books for Spring framework

Java Interview Questions












Java Training Institutes in Chennai

Java Training in Chennai

J2EE Training in Chennai

java j2ee training institutes in chennai