Using Qualifiers with Jersey 2.0 and CDI - java

I am reading through Beginning J2EE 7 - Antonio Gonclaves and going through the source code, I have encountered an exception "org.glassfish.hk2.api.UnsatisfiedDependencyException" which I'm finding hard to solve. Without any qualifiers the application works fine. I'm using Netbeans 7.3.1 and Glassfish 4.0. Below is my source code and exception log
These are my service classes which I'm using #Qualifiers to differentiate
#Stateless
#ISBNAlt
public class ISBNGenerator implements INumberGenerator{
#Override
public String generateNumberr() {
return "ISBN generated";
}
}
Second service class
#Stateless
#ISBNDef
public class ISSNGenerator implements INumberGenerator{
#Override
public String generateNumberr() {
return "Working version 2";
}
}
The resource class
#ManagedBean
#RequestScoped
#Path("/test")
public class TestClass {
#Inject #ISBNAlt
private INumberGenerator ng;
#Produces(MediaType.TEXT_PLAIN)
#GET
public String getValue()
{
return ng.generateNumberr();
}
}
and i use this class for adding resource classes
import java.util.Set;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
/**
*
* #author naphlin
*/
#ApplicationPath("webresource")
public class ApplicationConfig extends Application{
#Override
public Set<Class<?>> getClasses() {
Set<Class<?>> resources = new java.util.HashSet<Class<?>>();
// following code can be used to customize Jersey 2.0 JSON provider:
try {
Class jsonProvider = Class.forName("org.glassfish.jersey.jackson.JacksonFeature");
// Class jsonProvider = Class.forName("org.glassfish.jersey.moxy.json.MoxyJsonFeature");
// Class jsonProvider = Class.forName("org.glassfish.jersey.jettison.JettisonFeature");
resources.add(jsonProvider);
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
addRestResourceClasses(resources);
return resources;
}
/**
* Do not modify addRestResourceClasses() method.
* It is automatically re-generated by NetBeans REST support to populate
* given list with all resources defined in the project.
*/
private void addRestResourceClasses(Set<Class<?>> resources) {
resources.add(helpers.TestClass.class);
}
}
After deploying my application and invoking the REST endpoint, i get this exception
Info: Initiating Jersey application, version Jersey: 2.0 2013-05-03 14:50:15...
Warning: StandardWrapperValve[config.ApplicationConfig]: Servlet.service() for servlet config.ApplicationConfig threw exception
org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at Injectee(requiredType=INumberGenerator,parent=TestClass,qualifiers={#annotations.ISBNAlt()}),position=-1,optional=false,self=false,unqualified=null,1495563497)
at org.jvnet.hk2.internal.ThreeThirtyResolver.resolve(ThreeThirtyResolver.java:74)
at org.jvnet.hk2.internal.ClazzCreator.resolve(ClazzCreator.java:191)
at org.jvnet.hk2.internal.ClazzCreator.resolveAllDependencies(ClazzCreator.java:214)
at org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:311)
at org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:448)
at org.glassfish.jersey.process.internal.RequestScope.findOrCreate(RequestScope.java:157)
at org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2204)
at org.jvnet.hk2.internal.ServiceLocatorImpl.getService(ServiceLocatorImpl.java:579)
at org.jvnet.hk2.internal.ServiceLocatorImpl.getService(ServiceLocatorImpl.java:566)
at org.glassfish.jersey.internal.inject.Injections.getOrCreate(Injections.java:172)
at org.glassfish.jersey.server.model.MethodHandler$ClassBasedMethodHandler.getInstance(MethodHandler.java:185)
at org.glassfish.jersey.server.internal.routing.PushMethodHandlerRouter.apply(PushMethodHandlerRouter.java:105)
at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:118)
at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:121)
at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:121)
at org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:121)
at org.glassfish.jersey.server.internal.routing.RoutingStage.apply(RoutingStage.java:102)
at org.glassfish.jersey.server.internal.routing.RoutingStage.apply(RoutingStage.java:62)
at org.glassfish.jersey.process.internal.Stages.process(Stages.java:197)
at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:215)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:198)
at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:946)
at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:323)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:372)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:335)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:218)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
at java.lang.Thread.run(Thread.java:745)
Any

Works on my machine ;o) No, actually I reproduced your code quickly with JBoss Forge. You can run the following JBoss Forge script if you want :
$ project-new --named testrest
$ rest-setup
$ cdi-setup
$ cdi-new-qualifier --named ISBNAlt --targetPackage org.testrest.util
$ cdi-new-qualifier --named ISBNDef --targetPackage org.testrest.util
$ java-new-interface --named INumberGenerator2 --targetPackage org.testrest.service
$ ejb-new-bean --named ISBNGenerator
$ ejb-new-bean --named ISSNGenerator
$ faces-setup
I deployed it on WildFly, and it's working fine. BTW, as simplification, you don't need #ManagedBean and #RequestScoped on your REST endpoint. Try to get rid of it and re-deploy on GlassFish. You can also give it a try on WildFly first.

Related

I want to pass variables to another page using the session storage and it give me java.lang.IllegalStateException

I'm pretty new to programming and i encountered this problem i have search internet and couldn't find the answer i was looking for. Hope you can help me here. Thanks!
I'm trying to pass 2 variables which is the name and custid.
(i'm using mysql workbench as my database).
I'm guessing my error is something to do with.
String email=request.getParameter("email");
String password=request.getParameter("password");
here's my code:
#WebServlet("/login")
public class loginServlet extends HttpServlet{
#Resource(name="jdbc/eg3752")
private DataSource usercatalogue;
#Override
protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
{
String email=request.getParameter("email");
String password=request.getParameter("password");
Connection connection=null;
Statement statement=null;
ResultSet resultset=null;
String login="SELECT * FROM customer where email='"+email+"'";
try
{
connection=usercatalogue.getConnection();
statement=connection.createStatement();
resultset=statement.executeQuery(login);
while(resultset.next())
{
// check if password is same in the database
if(password.equals(resultset.getString("password")))
{
HttpSession session =request.getSession();
session.setAttribute("name", resultset.getString("fullname"));
session.setAttribute("custid", resultset.getString("customerId"));
response.sendRedirect(this.getServletContext().getContextPath()+"/search.html");
}
else
{
response.sendRedirect(this.getServletContext().getContextPath() + "/login.html");
}
}
}
catch(Exception e)
{
e.printStackTrace();
System.err.println(e.getMessage());
}
}
}
and this is the error on the GlassFish Server 4
Severe: java.lang.IllegalStateException
at org.apache.catalina.connector.ResponseFacade.sendRedirect(ResponseFacade.java:518)
at com.aleksi.loginServlet.doPost(loginServlet.java:58)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
at java.lang.Thread.run(Thread.java:745)
You did not include the "caused by" sections of the stack trace, which are the important part.
An IllegalStateException on a sendRedirect() has to do with the HTTP protocol and occurs when the response to the client has already been committed. This will usually happen when either the response buffer has reached max size or someone has called flush() explicitly or written something to the output stream and caused the response to be committed.
After calling sendRedirect() add return; and then try.

Injection exception on servlet

Successfully deploy to Glassfish. Go to run it and
com.sun.enterprise.container.common.spi.util.InjectionException: Error creating managed object for class: class com.wellpoint.mobility.aggregation.admin.HomeServlet
at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl.createManagedObject(InjectionManagerImpl.java:329)
at com.sun.enterprise.web.WebContainer.createServletInstance(WebContainer.java:983)
at com.sun.enterprise.web.WebModule.createServletInstance(WebModule.java:2130)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1404)
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:1211)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:237)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
at java.lang.Thread.run(Thread.java:745)
Caused by: com.sun.enterprise.container.common.spi.util.InjectionException: Exception attempting to inject Env-Prop: com.wellpoint.mobility.aggregation.admin.FireboltServlet/entityManager#Field-Injectable Resource. Class name = com.wellpoint.mobility.aggregation.admin.FireboltServlet Field name=entityManager#java.lang.String### into class com.wellpoint.mobility.aggregation.admin.FireboltServlet: Can not set javax.persistence.EntityManager field com.wellpoint.mobility.aggregation.admin.FireboltServlet.entityManager to com.wellpoint.mobility.aggregation.admin.HomeServlet
at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl._inject(InjectionManagerImpl.java:717)
at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl.inject(InjectionManagerImpl.java:484)
at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl.injectInstance(InjectionManagerImpl.java:141)
at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl.injectInstance(InjectionManagerImpl.java:127)
at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl.createManagedObject(InjectionManagerImpl.java:324)
... 30 more
Caused by: java.lang.IllegalArgumentException: Can not set javax.persistence.EntityManager field com.wellpoint.mobility.aggregation.admin.FireboltServlet.entityManager to com.wellpoint.mobility.aggregation.admin.HomeServlet
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:164)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:168)
at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:55)
at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:75)
at java.lang.reflect.Field.set(Field.java:741)
at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl._inject(InjectionManagerImpl.java:665)
... 34 more
FireboltServlet is our implementation of HttpServlet, all of our Servlets extend that class. It has a field entityManager
#PersistenceContext(unitName = "persistenceUnit", type = PersistenceContextType.TRANSACTION)
private EntityManager entityManager;
...
public EntityManager getEntityManager()
{
if (entityManager == null)
{
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("persistenceUnit");
entityManager = entityManagerFactory.createEntityManager();
}
return entityManager;
}
public void setEntityManager(EntityManager entityManager)
{
this.entityManager = entityManager;
}
But I can't find anything attempting to map HomeServlet to that field, nor can I imagine why it would. This exception seems to have popped up out of nowhere, I haven't made any recent changes that I can imagine causing this. Thanks for your help!
Sometimes the server freezes objects mainly the latter, as it does not recognize it, what I do is create a phantom method to lock and release that I need to compile and recognized.
That I do when I do not recognize some method.

Problems with serialization in java, calling a stateless javabean from a primefaces baking bean

First of all: sorry for my english, I'll try to do my best :)
I have a Java EE application, with javabeans providing the logic via remote interfaces and a primefaces interface with his baking beans. For example I have this in my ejb project:
#Stateless
#LocalBean
public class reporteService implements reporteServiceRemote {
[...]
public LinkedList<LineaBalanceteSaldosClientesTransport> balanceteSaldosClientes(...parameters...) {
LinkedList<LineaBalanceteSaldosClientesTransport> lineas = new LinkedList<>();
// execute a native query and return the results
// the query return about 6/700 rows
List results = this.balanceteSaldosClientes(...parameters...);
Iterator it = results.iterator();
while (it.hasNext()) {
Object[] result = (Object[])it.next();
String codigoCliente = (String) result[0];
Integer idCliente = (Integer) result[1];
Integer nombreCliente = (String) result[2];
// generate new line
linea = new LineaBalanceteSaldosClientesTransport();
linea.setIdCliente(idCliente);
linea.setCodigoCliente(codigoCliente);
linea.setNombreCliente(nombreCliente);
lineas.add(linea);
}
return lineas;
}
[...]
}
#Remote
public interface ReporteServiceRemote {
[...]
public LinkedList<LineaBalanceteSaldosClientesTransport> balanceteSaldosClientes(...parameters...);
[...]
}
This works fine, I've tried outputting the lines and the final linkedlist in the log and the method ends fine. The point is calling the method from the war, using his remote interface. This is the baking bean for a primefaces page that is supposed to show the results:
#ManagedBean
#ViewScoped
public class ReporteBean implements Serializable {
[...]
#EJB
private ReporteServicesRemote reporteServices;
[...]
public LinkedList<LineaBalanceteSaldosClientesTransport> obtenerBalanceteSaldosClientes() {
try {
LinkedList<LineaBalanceteSaldosClientesTransport> lbsct = reporteServices.balanceteSaldosClientes(
this.getFechaSaldo(),
this.getIdsMonedas(),
cliente != null ? cliente.idCliente : null
);
return lbsct;
} catch (Exception ex) {
LOG.log(Level.SEVERE, "ERROR", ex);
this.messagesBean.message(FacesMessage.SEVERITY_ERROR, "ERROR", "Error al intentar generar Balancete de Saldos: " + ex.getLocalizedMessage());
}
return null;
}
[...]
}
when it call the method reporteServices.balanceteSaldosClientes it ends fine, but (I think) the serialization of the resulting object fail, throwing this exception:
javax.ejb.EJBException: java.rmi.ServerError: Error occurred in server thread; nested exception is:
java.lang.NoClassDefFoundError: Could not initialize class org.glassfish.pfl.dynamic.copyobject.impl.Exceptions
at com.myproject.services.reportes._ReporteServicesRemote_Wrapper.balanceteSaldosClientes(com/myproject/services/reportes/_ReporteServicesRemote_Wrapper.java)
at com.tecytal.utils.servlets.testServlet.processRequest(testServlet.java:124)
at com.tecytal.utils.servlets.testServlet.doGet(testServlet.java:158)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
at java.lang.Thread.run(Unknown Source)
Caused by: java.rmi.ServerError: Error occurred in server thread; nested exception is:
java.lang.NoClassDefFoundError: Could not initialize class org.glassfish.pfl.dynamic.copyobject.impl.Exceptions
at com.sun.corba.ee.impl.javax.rmi.CORBA.Util.wrapException(Util.java:699)
at com.sun.corba.ee.impl.presentation.rmi.StubInvocationHandlerImpl.privateInvoke(StubInvocationHandlerImpl.java:257)
at com.sun.corba.ee.impl.presentation.rmi.StubInvocationHandlerImpl.invoke(StubInvocationHandlerImpl.java:150)
at com.sun.corba.ee.impl.presentation.rmi.codegen.CodegenStubBase.invoke(CodegenStubBase.java:226)
at com.myproject.services.reportes.__ReporteServicesRemote_Remote_DynamicStub.balanceteSaldosClientes(com/myproject/services/reportes/__ReporteServicesRemote_Remote_DynamicStub.java)
... 32 more
Caused by: java.lang.NoClassDefFoundError: Could not initialize class org.glassfish.pfl.dynamic.copyobject.impl.Exceptions
at org.glassfish.pfl.dynamic.copyobject.impl.ClassCopierBase.copy(ClassCopierBase.java:131)
at org.glassfish.pfl.dynamic.copyobject.impl.ClassCopierOrdinaryImpl$ClassFieldCopierUnsafeImpl$17.copy(ClassCopierOrdinaryImpl.java:810)
at org.glassfish.pfl.dynamic.copyobject.impl.ClassCopierOrdinaryImpl$ClassFieldCopierUnsafeImpl.copy(ClassCopierOrdinaryImpl.java:1008)
at org.glassfish.pfl.dynamic.copyobject.impl.ClassCopierOrdinaryImpl.doCopy(ClassCopierOrdinaryImpl.java:1128)
at org.glassfish.pfl.dynamic.copyobject.impl.ClassCopierBase.copy(ClassCopierBase.java:129)
at org.glassfish.pfl.dynamic.copyobject.impl.ClassCopierOrdinaryImpl$ClassFieldCopierUnsafeImpl$17.copy(ClassCopierOrdinaryImpl.java:810)
at org.glassfish.pfl.dynamic.copyobject.impl.ClassCopierOrdinaryImpl$ClassFieldCopierUnsafeImpl.copy(ClassCopierOrdinaryImpl.java:1008)
at org.glassfish.pfl.dynamic.copyobject.impl.ClassCopierOrdinaryImpl.doCopy(ClassCopierOrdinaryImpl.java:1128)
at org.glassfish.pfl.dynamic.copyobject.impl.ClassCopierBase.copy(ClassCopierBase.java:129)
[...]
(lot of similar lines repeated)
Someone pointed me to a memory problem, but in this case java was running with 8GB of dedicated memory (Xmx8192m, Xms8192m, MaxPermSize=4096m) and the linkedlist in this case contains only 600/700 objects. If I put i.e. a LIMIT 100 in the native query that returns the first list of results I get no exception and all works flawlessy.
I'm doing it wrong? Is there a more efficient way to pass datas between beans? Or there's another bug I cannot see?
Thank you for every suggestion.
This problem can be resolved using the Value List Handler pattern, more informations and working examples can be found here
Core J2EE Patterns - Value List Handler -
http://www.oracle.com/technetwork/java/valuelisthandler-142464.html
EJB 3.0 Stateful Session Bean Example -
http://www.go4expert.com/articles/ejb-30-stateful-session-bean-example-t4212/
Hope this can help someone.

I need help identifying where my null pointer exception is coming from

I am using PrimeFaces 4.0, NetBeans associated with two xhtml pages. The xhtml renders a select one radio menu from which the user may select an experiment about which they'd like more information. When they do, I'd like for the xhtml to pass the experiment back to java as a string. After the user hits submit, I'd like another xhtml page to open from which the user can further narrow their search criteria. As of right now, when the user clicks submit, there is a null pointer exception. Also, using System.out.println(), I can see that java is not getting the string back from the web page as my Glassfish server output prints (in part) : "Info: nullaaaaaaaaaaaaaaaaaaaaa". It doesn't print the other println() at all.
XHTML portion
<h:form>
<h:panelGrid>
<p:selectOneRadio id="Test" value="Please Selecte a Test">
<p:ajax update="Test" partialSubmit="#{targetBeantFranz.selectedNameOfExperiments}"/>
<f:selectItems value="#{targetBeantFranz.nameofexperiments}" />
</p:selectOneRadio>
<h:commandButton value="Select" action="open" />
</h:panelGrid>
</h:form>
and here are the affected Java methods (The entire JAVA file is much, much longer than this):
#ManagedBean(name = "targetBeantFranz", eager = true)
#SessionScoped
public class TargetBeanFranz implements Serializable {
public String selectedNameOfExperiments;
private final Map<String, String> nameofexperiments;
public TargetBeanFranz() throws SQLException {
nameofexperiments = new HashMap<String, String>();
XYexpdataServiceAdapter xydata = new XYexpdataServiceAdapterImpl();
List<String> dbnameofexperiments = xydata.getNameofexperiments();
for (String ta : dbnameofexperiments) {
nameofexperiments.put(ta, ta);
}
}
public String getSelectedNameOfExperiments() {
System.out.println(selectedNameOfExperiments + "aaaaaaaaaaaaaaaaaaaaa");
return selectedNameOfExperiments;
}
public void setSelectedNameOfExperiments(String selectedNameOfExperiments) {
System.out.println(selectedNameOfExperiments + "bbbbbbbbbbbbbbbbbbbbbbbbb");
this.selectedNameOfExperiments = selectedNameOfExperiments;
}
}
public Map<String, String> getNameofexperiments() {
return nameofexperiments;
}
}
ETA: My stacktrace looks like this:
java.lang.NullPointerException
at javax.faces.component.SelectItemsIterator$MapIterator.next(SelectItemsIterator.java:346)
at javax.faces.component.SelectItemsIterator$MapIterator.next(SelectItemsIterator.java:315)
at javax.faces.component.SelectItemsIterator.next(SelectItemsIterator.java:155)
at javax.faces.component.SelectItemsIterator.next(SelectItemsIterator.java:64)
at javax.faces.component.SelectUtils.matchValue(SelectUtils.java:86)
at javax.faces.component.UISelectOne.validateValue(UISelectOne.java:153)
at javax.faces.component.UIInput.validate(UIInput.java:983)
at javax.faces.component.UIInput.executeValidate(UIInput.java:1249)
at javax.faces.component.UIInput.processValidators(UIInput.java:712)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1261)
at javax.faces.component.UIForm.processValidators(UIForm.java:253)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1261)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1261)
at org.primefaces.component.layout.Layout.processValidators(Layout.java:233)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1261)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1261)
at javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:1195)
at com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:76)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
at java.lang.Thread.run(Thread.java:745)
Check this article for further info about JSF UI component <h:selectOneMenu> .
it might be the case that u need to initialize in the bean's constructor: selectedNameOfExperiments

Is it necessary to add an EJB module as a library in a Web module to inject session beans

I created a new Java EE 6 Enterprise-Application in Netbeans 7.4. with Glassfish 4.0 application server. So I have three Projects:
EAR
EJB
WAR
when I inject session bean from the EJB module into the managed bean of Web(WAR) module, it generates the following error
WARNING: /login.xhtml #16,131 value="#{loginBean.username}": Target Unreachable, identifier 'loginBean' resolved to null
javax.el.PropertyNotFoundException: /login.xhtml #16,131 value="#{loginBean.username}": Target Unreachable, identifier 'loginBean' resolved to null
at com.sun.faces.facelets.el.TagValueExpression.getType(TagValueExpression.java:100)
at org.primefaces.renderkit.InputRenderer.findImplicitConverter(InputRenderer.java:170)
at org.primefaces.renderkit.InputRenderer.findConverter(InputRenderer.java:190)
at org.primefaces.renderkit.InputRenderer.getConvertedValue(InputRenderer.java:196)
at javax.faces.component.UIInput.getConvertedValue(UIInput.java:1046)
at javax.faces.component.UIInput.validate(UIInput.java:976)
at javax.faces.component.UIInput.executeValidate(UIInput.java:1249)
at javax.faces.component.UIInput.processValidators(UIInput.java:712)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1261)
at javax.faces.component.UIForm.processValidators(UIForm.java:253)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1261)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1261)
at org.primefaces.component.layout.Layout.processValidators(Layout.java:246)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1261)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1261)
at javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:1195)
at com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:76)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:344)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
at filters.AuthFilter.doFilter(AuthFilter.java:44)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:316)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
at java.lang.Thread.run(Thread.java:744)
Caused by: javax.el.PropertyNotFoundException: Target Unreachable, identifier 'loginBean' resolved to null
at com.sun.el.parser.AstValue.getTarget(AstValue.java:174)
at com.sun.el.parser.AstValue.getType(AstValue.java:86)
at com.sun.el.ValueExpressionImpl.getType(ValueExpressionImpl.java:201)
at org.jboss.weld.el.WeldValueExpression.getType(WeldValueExpression.java:93)
at com.sun.faces.facelets.el.TagValueExpression.getType(TagValueExpression.java:98)
... 51 more
And during deployment the Glassfish server also generates the following error
SEVERE: Exception while loading the app
SEVERE: Undeployment failed for context /ZTE-WMS-war
SEVERE: Exception while loading the app : java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: java.lang.RuntimeException: java.lang.NoClassDefFoundError: Lcom/wms/sessions/UseraccountManager;
I wrote the managed bean class(LoginBean) as follows
package com.wms.managedbeans;
import com.wms.sessions.UseraccountManager;
import java.io.Serializable;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import javax.servlet.http.HttpSession;
//#Named("loginBean")
#ManagedBean(name="loginBean")
#RequestScoped
public class LoginBean implements Serializable {
private static final long serialVersionUID = 1L;
private String password;
private String message, username;
#EJB
private UseraccountManager uam;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String uname) {
this.username = uname;
}
public String getUser() {
System.out.println(Util.getUserId());
return Util.getUserId();
}
public String getUserType(){
HttpSession session = Util.getSession();
if ( session != null )
return (String) session.getAttribute("userType");
else
return null;
}
public String login() {
String userType = uam.validateUser(username, password);
HttpSession session = Util.getSession();
if (userType.equals("unknown")){
FacesContext.getCurrentInstance().addMessage(
null,
new FacesMessage(FacesMessage.SEVERITY_WARN,
"Invalid Login!",
"Please Try Again!"));
// invalidate session, and redirect to other pages
//message = "Invalid Login. Please Try Again!";
return "login";
}
else{
// get Http Session and store username
session.setAttribute("username", username);
session.setAttribute("userType",userType);
if (userType.equals("administrator"))
return "admin/adminHome?faces-redirect=true";
else if(userType.equals("warehouseman"))
return "WM/wmHome?faces-redirect=true";
else if(userType.equals("employee"))
return "emp/empHome?faces-redirect=true";
else
return "man/manHome?faces-redirect=true";
}
}
public String logout() {
HttpSession session = Util.getSession();
session.invalidate();
return "/login?faces-redirect=true";
}
}
When I added the "EJB jar" into the WEB(WAR) module as a library it works perfectly.
Now my question is, is it mandatory to add the EJB jar as a library in-order to inject session beans into managed beans. if there is any other way(s) please help me

Categories

Resources