How inject EJB into abstract CDI class? - java-ee-6

I have a problem with injecting an EJB into an abstract class which is the parent of my JSF CDI beans. In my project I am using MyFaces CODI 1.0.5 (ViewScope), Omnifaces 1.3, PrimeFaces 3.4.2 and GlassFish 3.1.2.
The application is an EAR, the abstract class is in an EJB module and the JSF CDI beans are in a WAR module:
webframework-demo.ear
|__ webframework-war.war -> concrete JSF CDI bean
|__ webframework-ejb-lib.jar -> abstract class with EJB injection
|__ lib\
|__ shared libs
My abstract class:
public abstract class AbstractListPageAction<T extends AbstractViewBean<K>, K extends Serializable> {
...
#EJB
private CriteriaFacadeLocal<K> facade;
#PostConstruct
public void create() {
...
facade.setEntityClass(getEntityClass());
...
}
...
public abstract Class<K> getEntityClass();
}
My CDI bean:
import org.apache.myfaces.extensions.cdi.core.api.scope.conversation.ViewAccessScoped;
#Named
#ViewAccessScoped
public class UserListAction extends AbstractListPageAction<UserViewBean, UserEntity>
implements Serializable {
private static final long serialVersionUID = -1178878323047991855L;
...
#Override
public Class<UserEntity> getEntityClass() {
return UserEntity.class;
}
...
}
When I deploy the application and access a JSF page, UserListAction is created but CriteriaFacadeLocal is not injected and I end with a NullPointerException in the #PostConstruct method.
When I change UserListAction and add an empty #PostConstruct method then CriteriaFacade is injected and everything works fine:
#Named
#ViewAccessScoped
public class UserListAction extends AbstractListPageAction<UserViewBean, UserEntity>
implements Serializable {
private static final long serialVersionUID = -1178878323047991855L;
...
#PostConstruct
public void init() {
}
#Override
public Class<UserEntity> getEntityClass() {
return UserEntity.class;
}
...
}
I have beans.xml in every module. But why must I have an empty #PostConstruct method in my CDI bean? Is there a problem with an abstract class placed in a EJB module?

Using generics with EJB can be somewhat problematic.
See Use of generics in EJB 3.1.
Or you can make a qualified EJB in your hierarchy. See javax.inject.Qualifier.

I Created answer from edited question:
I figured that it was probably a problem with the EAR/EAR modules classloaders. I moved webframework-ejb-lib.jar into the webframework-war.war WEB-INF/lib folder:
webframework-demo.ear
|__ webframework-war.war -> concrete JSF CDI bean
| |__ WEB-INF
| |__lib
| |__ webframework-ejb-lib.jar -> abstract class with EJB injection
|__ ... (other ejb modules)
|__ lib\
|__ shared libs
and suddenly everything is fine.

Related

Injecting one ejb bean inside another within the same jar module not working

I have two beans in my jar module ->
bean A:
#Singleton
public final class A {
public String getS() {
return"";
}
and a second bean B within the same jar module:
#Stateless
public final class B {
#Inject
private A a;
public String getAS() {
a.getS() // -> NullPointerException
}
}
I have the file beans.xml in
src/main/resources/META-INF/
<groupId>javaee</groupId>
<artifactId>javaee-api</artifactId>
<scope>provided</scope>
is added to pom.xml, but still injected A is null.
In a generated jar file beans.xml is present
whether I missed something?
The default place for the beans.xml for building jars with maven would be in
src/main/resources/META-INF/

Jersey EJB injection

I read a lot about the possibility of injection with jax rs 2.0 and in particular with jersey.
I even read that ejb injection is expected in jax rs 2.0 spec. But i still haven't found a unique solution among the variety of posts i read over the net.
In my use case i'm working with:
WildFly 9.0 and Jersey 2.x
I have a webapplication exposing my REST services and importing a jar implementing my model data.
This is the CDI approach:
#RequestScoped
#Path("/myPath")
public class ModelRetriever {
#Context
SecurityContext securityContext;
#Inject
private IMyModel MyModel;
#Path("{i}")
#GET
#Produces("application/json")
public Response countries(#PathParam("i") String countryId)
throws JSONException, Failure, IOException {
MyModel.doSomething();
}
This is my IMyModel interface
public interface IKasPrincipal extends Principal {
public void doSomething();
}
And this is MyModel implementation:
#RequestScope
public class MyModelImpl implements IMyModel {
public void doSomehting() {
doSomething();
}
}
Another method i tried is to use EJB injection changing my previous annotations like this:
#Stateless
#Path("/myPath")
public class ModelRetriever {
#EJB
private IMyModel MyModel;
#Path("{i}")
#GET
#Produces("application/json")
public Response countries(#PathParam("i") String countryId)
throws JSONException, Failure, IOException {
MyModel.doSomething();
}
This is my IMyModel interface
#Local
public interface IKasPrincipal extends Principal {
public void doSomething();
}
And this is MyModel implementation:
#Stateless
public class MyModelImpl implements IMyModel {
public void doSomehting() {
doSomething();
}
}
i get a null object using EJB approach and i get this exception using CDI
Caused by: org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(requiredType=IMyModel,parent=ModelRetriever,qualifiers={},position=-1,optional=false,self=false,unqualified=null,616459318)
at org.jvnet.hk2.internal.ThreeThirtyResolver.resolve(ThreeThirtyResolver.java:75)
at org.jvnet.hk2.internal.ClazzCreator.resolve(ClazzCreator.java:211)
at org.jvnet.hk2.internal.ClazzCreator.resolveAllDependencies(ClazzCreator.java:234)
So is there anything i'm missing?
see other Stack Overflow related posts:
Dependency injection with Jersey 2.0
HK2 Jersey EJB 3 injection
The problem you are having is that HK2 does not know about anything that was not registered directly into it, and HK2 tries to to satisfy all dependency in your Jersey aware class.
I has this issue a while back. Then I discovered that Jersey uses HK2 internally. HK2 is a JSR-330 implementation (CDI).
One would think that a open-source project would declares it's CDI beans and use them regardless of the CDI implementation, but it looks like its not that way.
see : https://java.net/jira/browse/JERSEY-1933
see : https://hk2.java.net/integration.html
You can register your components into HK2...
see : https://jersey.java.net/documentation/latest/ioc.html
For all I know, you cannot inject CDI components (or anything else, as EJB) into Jersey's classes using your own (or your container's) CDI implementation, unless you use Glassfish (which personally I would never use) which in turn uses HK2 as its CDI implementation.
To me, this is a major draw back. But the only(?) draw back of Jersey.
-Maybe I missed something (which is very possible)
-Maybe this is a trick from Oracle so that you can't use Jersey in, let's say, your Websphere app which uses OpenWeb Beans as CDI implementation.
-Maybe they hardwired it to HK2, and just don't care that Jersey can't be used as a drop in component in your application, which relies on CDI or EJB
I'm not aware of why #EJB not worked, but, you can use #Produces/#Disposes bean.
#ApplicationScoped // or some other scoped
public class MyModelProducer {
#Produces public MyModel produceMyModel() {
}
public void disposeMyModel#Disposes final MyModel model) {
}
}

#Inject not working in AttributeConverter

I have a simple AttributeConverter implementation in which I try to inject an object which have to provide the conversion logic, but #Inject seem not to work for this case. The converter class looks like this:
#Converter(autoApply=false)
public class String2ByteArrayConverter implements AttributeConverter<String, byte[]>
{
#Inject
private Crypto crypto;
#Override
public byte[] convertToDatabaseColumn(String usrReadable)
{
return crypto.pg_encrypt(usrReadable);
}
#Override
public String convertToEntityAttribute(byte[] dbType)
{
return crypto.pg_decrypt(dbType);
}
}
When the #Converter is triggered it throws an NullPointerException because the property crypto is not being initialized from the container. Why is that?
I'm using Glassfish 4 and in all other cases #Inject works just fine.
Is it not possible to use CDI on converters?
Any help will be appreciated :)
The accent of my question is more the AttributeConverter part. I understand that for the CDI to work a bean must meet the conditions described here http://docs.oracle.com/javaee/6/tutorial/doc/gjfzi.html.
I also have tried to force the CDI to work by implementing the following constructor:
#Inject
public String2ByteArrayConverter(Crypto crypto)
{
this.crypto = crypto;
}
And now I got the following exception which doesn't give me any clue:
2015-07-23T01:03:24.835+0200|Severe: Exception during life cycle processing
org.glassfish.deployment.common.DeploymentException: Exception [EclipseLink-28019] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.EntityManagerSetupException
Exception Description: Deployment of PersistenceUnit [PU_VMA] failed. Close all factories for this PersistenceUnit.
Internal Exception: Exception [EclipseLink-7172] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.ValidationException
Exception Description: Error encountered when instantiating the class [class model.converter.String2ByteArrayConverter].
Internal Exception: java.lang.InstantiationException: model.converter.String2ByteArrayConverter
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.createDeployFailedPersistenceException(EntityManagerSetupImpl.java:820)
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:760)
...
I even tried using #Producer or #Decorator in order to have the CDI working on that place, but I still think there is something specific with the AttributeConverter which doesn't allow CDI. So problem not solved yet.
Unfortunately you can't inject CDI beans into a JPA converter, however in CDI 1.1 you can inject your Crypto programmatically :
Crypto crypto = javax.enterprise.inject.spi.CDI.current().select(Crypto.class).get()
For reference, JPA 2.2 will allow CDI to be used with AttributeConverter, and some vendors already support this (EclipseLink, DataNucleus JPA are the ones I know of that do it).
You're trying to combine two different worlds, as CDI doesn't know about JPA Stuff and vice-versa. (One annotation parser of course doesn't know about the other)
What you CAN do, is this:
/**
* #author Jakob Galbavy <code>jg#chex.at</code>
*/
#Converter
#Singleton
#Startup
public class UserConverter implements AttributeConverter<User, Long> {
#Inject
private UserRepository userRepository;
private static UserRepository staticUserRepository;
#PostConstruct
public void init() {
staticUserRepository = this.userRepository;
}
#Override
public Long convertToDatabaseColumn(User attribute) {
if (null == attribute) {
return null;
}
return attribute.getId();
}
#Override
public User convertToEntityAttribute(Long dbData) {
if (null == dbData) {
return null;
}
return staticUserRepository.findById(dbData);
}
}
This way, you would create a Singleton EJB, that is created on boot of the container, setting the static class attribute in the PostConstruct phase. You then just use the static Repository instead of the injected field (which will still be NULL, when used as a JPA Converter).
Well, CDI still doesn't work for AttributeConverter, which would be the most elegant solution, but I have found a satisfying workaround. The workaround is using #FacesConverter. Unfortunately per default CDI doesn't work in faces converters and validators either, but thanks to the Apache MyFaces CODI API you can make it work unsing the #Advaced annotation :) So I came up with an implementation like this:
#Advanced
#FacesConverter("cryptoConverter")
public class CryptoJSFConverter implements Converter
{
private CryptoController crypto = new CryptoController();
#Inject
PatientController ptCtrl;
public Object getAsObject(FacesContext fc, UIComponent uic, String value)
{
if(value != null)
return crypto.pg_encrypt(value, ptCtrl.getSecretKey());
else
return null;
}
public String getAsString(FacesContext fc, UIComponent uic, Object object)
{
String res = crypto.pg_decrypt((byte[]) object, ptCtrl.getSecretKey());
return res;
}
}
The injected managed bean has to be explicitly annotated with #Named and some scope definition. A declaration in faces-config.xml doesn't work! In my solution it looks like this:
#Named
#SessionScoped
public class PatientController extends PersistanceManager
{
...
}
Now one has a context information in the converter. In my case it is session/user specific cryptography configuration.
Of course in such a solution it is very likely that a custom #FacesValidator is also needed, but thanks to CODI one have the possibility for using CDI here also (analog to converter).

Simple ejb client app (servlet) using JNDI architecture.

I'm tryng to build a simple "Hello World" app using EJB's and Servlet. I'm strongly confused about how it should look like. I have two projects in eclipse. One contains EJB Stateless session bean and its interface (both of them in this same package) the other one contains bean, interface and servlet which should be able to call both beans methods. I've added the remote project (from the other project) to "Projects" in build path. Now i've imported the remote interfaces to the servlet. Now the servlet looks like this:
import pl.test.IHelloRemote;
import pl.other.IDateRemote;
#WebServlet("/TestServlet")
public class Control extends HttpServlet {
private static final long serialVersionUID = 1L;
private InitialContext ic;
private IHelloRemote iHelloRemote;
private IDateRemote iDateRemote;
public Control() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
ic = new InitialContext();
iHelloRemote = ic.lookup ("java:global/test-project/HelloBean!pl.test.iHelloRemote");
iDateRemote = ic.lookup ("java:global/other-project/DateBean!pl.other.iDateRemote");
} catch (NamingException ex) {
ex.printStackTrace();
}
iHelloRemote.sayHello();
iDateRemote.getDate();
}
}
As I mention the "other-project" is included in the build path. Here's how the single bean that I want to use looks like:
package pl.other;
import javax.ejb.Stateless;
#Stateless
public class DateBean implements IDateRemote {
public DateBean() {}
#Override
public void getDate {
//some code here
}
}
Now my questions.
Why (and how to fix that) during deployment i get error:
Class [ Lpl/other/IDateRemote; ] not found. Error while loading [ class TestServlet]
Can EJB beans be located in default package? Why Elipse does'nt allows that?
Where shall bean interfaces be located? In the same package as bean? In the Servlet package? What is the "best practice" setup of the files in this kind of project?
As for now the setup is like follows:
test-project
|- (default package)
|- TestServlet
|- pl.test
|- IHelloRemote
|- HelloBean
other-project
|- pl.other
|- IDateRemote
|- DateBean
Thank You for replies and patience.
Smok.

null pointer exception error when revoking EJB method

I am new in creating Java modules, currently I'm trying to call method which inserts data into database. I have an 1) interface TestSEI. 2) The webservice called TestWS, implements TestSEI. 3) Class TestBean which implements methods which I call from TestWS. In this class I added new method DocPay, which have to call method from another EJB Module (named PayTestmodule) from class TestDB. I added PayTestmodule in dependency in my project.
In Netbeans I am succesfully build and deploy EAR file on Jobss, but when I revoke method DocPay in class TestBean I get error - Null pointer exception. I spend all day to fing out possible solution, but no success. Here some snippets:
1.TestSEI
#WebService(name="TestWS")
#SOAPBinding(style=SOAPBinding.Style Document)
public interface TestSEI {
......
2.TestWS
#WebService
public class TestWS implements TestSEI {
#Inject
private TestBean domain
....
domain.DocPay(Object RQU)
...
3.TestBean
#stateless
#Localbean
public class TestBean {
....
Public DocPay(Object request)
PayDB dbapi=new PayDB();
String id=dbapi.insertdata(Stringparams)
....
4.PayDB class(defined in another EJB module)
#Stateless
public class PayDB implements PayDBLocal
......
Publis String insertdata(Stringparams) throws ....
5.PayDBLocal
#Local
public interface PayDBLocal
.......
Please, help me to understand what I am doing wrong?
Did you mean "when I revoke method DocPay in class TestBean" or its in TestWS??
Use this in TestWS
#EJB
private TestBean domain