Context:
I need to audit changes in my business entities, setting the username in a userLog field.
And I don't want to mix the auditing data and the business data.
I mean, I don't wan't to pass the user (or username) as a parameter of my business method just because it is needed for audit purpose.
Solution:
In this case, the simplest way is to pass this metadata during the JNDI lookup.
The EJB container will create a security context for my invocation with the received username.
Then, every EJB would obtain the principal (and thus, the username of the authenticated user in the remote client context) with a simple EJBContext.getCallerPrincipal().
Here is a simple example with JBoss:
In the client:
In my entity:
Restrictions:
In order to reduce the lookups to Stateless EJB, the lookup can be done only once and the proxy can be kept in a cache (having a Business Delegate as Singleton, for example).
If we want to pass the username with the JNDI invocation, we have to maintain an instance of the Business Delegate for user.
Tuesday, December 23, 2008
Friday, December 19, 2008
Why Threadlocal musn't be used with EJB...
There is nothing in the EJB specifications that prohibits the use of ThreadLocal.
It only says in the Programming Restrictions (21.1.2):
But nor does the EJB specifications ensure that all local invocations in the EJB container will run in the same thread.
Generally application servers use a single thread for local invocations: I have tested it on OC4J and JBoss.
But I think it's really dangerous to rely on this feature as it can change without warning.
So, my recommendation is "Dont't use ThreadLocal with EJB" !
It only says in the Programming Restrictions (21.1.2):
The enterprise bean must not attempt to manage threads. The enterprise bean must not attempt to start, stop, suspend, or resume a thread, or to change a thread’s priority or name. The enterprise bean must not attempt to manage thread groups.
But nor does the EJB specifications ensure that all local invocations in the EJB container will run in the same thread.
Generally application servers use a single thread for local invocations: I have tested it on OC4J and JBoss.
But I think it's really dangerous to rely on this feature as it can change without warning.
So, my recommendation is "Dont't use ThreadLocal with EJB" !
Thursday, December 11, 2008
Web Service Client in a J2EE application, using JAX-WS
In my J2EE application, I use an external web service.
I have generated a client of this WS using the JAX-WS tool wsimport.
Constraints:
- This web service won’t always be up.
- As my web service client has been generated from the WSDL, I do not want each invocation of the web service to go over the network to accesses the WSDL.
- The URL of the WS must be obtained from the configuration.
Tuesday, December 09, 2008
System Quality Attribute
From the SAF Framework:
- End User’s view
- Performance
- Availability
- Usability
- Security
- Developer’s view
- Maintainability
- Portability
- Reusability
- Testability
- Business Community view
- Time To Market
- Cost and Benefits
- Projected life time
- Targeted Market
- Integration with Legacy System
- Roll back Schedule
A simple code revison for a JSF / EJB3.0 project
In my current J2EE project (JSF with EJB 3.0) , we did a simple code revision.
The points of verification were:
- Presentation tier (JSF)
- JSP mustn’t contain Java scriptlets.
- JSP must contain references to backing beans via JSF binding.
- Catalogs must be cached in a bean with scope “application”.
- Backing beans must use the Business Delegate in order to communicate with the business tier.
- When a Business Delegates helper invokes a Stateless Session Bean, the Business Delegates helper can be a singleton and the lookup can be done in the constructor. This way, there is only one Proxy and one JNDI lookup.
- Business tier (EJB 3.0)
- There must be only one invocation to the business tier per request (with this restriction, we try to ensure that EJB services are coarse-grained business components).
- All the database access must be delegated to DAO classes.
- DAO mustn’t contain business logic.