Vaadin and EJB 3 stateless bean access

Hi there!

Im having trouble obtaining some data via persistence layer in my J2EE application which uses vaadin.
Here’s what I’ve got:

application class:


@SessionScoped
public class LibraryApplication extends Application {


	Window main;
	
	@EJB
	DataSessionBeanLocal dataSessionBeanLocal;
	
	@Override
	public void init() {
		
		main = new Window("main window");
	
		
		System.out.println(dataSessionBeanLocal.getDvdList().get(0));
[...]

servlet class:


@WebServlet(urlPatterns="/*")
public class LibraryApplocationServlet extends AbstractApplicationServlet {

	
	@Inject
	LibraryApplication application;
	
	@Override
	protected Application getNewApplication(HttpServletRequest request)
			throws ServletException {
	return application;
	}

	@Override
	protected Class<? extends Application> getApplicationClass()
			throws ClassNotFoundException {
		
		
		return LibraryApplication.class;
	}

   
	
}

empty beans.xml file located in WEB-INF dir.:


<?xml version="1.0" encoding="UTF-8"?>
<beans>
<!-- Namespace declarations omitted -->
</beans>

java ee dependency included in each persistence & vaadin module:


<dependency>
    	<groupId>javax</groupId>
    	<artifactId>javaee-api</artifactId>
    	<version>6.0</version>
    	<type>jar</type>
    	<scope>compile</scope>
    </dependency>

java servlet registration:


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
    <display-name>Vaadin Web Application</display-name>
    <context-param>
        <description>Vaadin production mode</description>
        <param-name>productionMode</param-name>
        <param-value>false</param-value>
    </context-param>
    <servlet>
        <servlet-name>Vaadin Application Servlet</servlet-name>
        <servlet-class>com.vaadin.terminal.gwt.server.ApplicationServlet</servlet-class>
        <init-param>
            <description>Vaadin application class to start</description>
            <param-name>application</param-name>
            <param-value>org.library.vaadin.web.LibraryApplication</param-value>
        </init-param>
        <!-- 
        <init-param>
            <param-name>widgetset</param-name>
            <param-value>org.library.vaadin.web.gwt.ColorPickerWidgetSet</param-value>
        </init-param>
         -->
    </servlet>
    <servlet-mapping>
        <servlet-name>Vaadin Application Servlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>

bean and its interface are registerd with jndi :


Binding the following Entries in Global JNDI:

	library/DataSessionBeanImpl/local - EJB3.x Default Local Business Interface
	library/DataSessionBeanImpl/local-ejb.intf.DataSessionBeanLocal - EJB3.x Local Business Interface

but when I attempt to access application, jBoss says:


22:09:36,115 ERROR [[Vaadin Application Servlet]
] Servlet.service() for servlet Vaadin Application Servlet threw exception
java.lang.NullPointerException
	at org.library.vaadin.web.LibraryApplication.init(LibraryApplication.java:37)
	at com.vaadin.Application.start(Application.java:554)
	at com.vaadin.terminal.gwt.server.AbstractApplicationServlet.startApplication(AbstractApplicationServlet.java:1213)
	at com.vaadin.terminal.gwt.server.AbstractApplicationServlet.service(AbstractApplicationServlet.java:484)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
	at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:235)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
	at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:190)
	at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:92)
	at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process(SecurityContextEstablishmentValve.java:126)
	at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:70)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
	at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:330)
	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:829)
	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:601)
	at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
	at java.lang.Thread.run(Thread.java:679)

Im new at vaadin, so… Please can you also tell me what for do I need to have class inherited from AbstractApplicationServlet and also registred servlets in web.xml?

Thanks for advices!

Hi!

Very similar discussion


Here

I see that the problem there was with beans.xml. I have corrected it like was said.
But error is still the same.
I have downloaded an example and I see that there is no web.xml! How is it possible?
I did some research and I found somewhere that its needed to register beans in web.xml so I did:


[...]

<ejb-local-ref>
	<ejb-ref-name>dataSessionBeanLocal</ejb-ref-name>
	<ejb-ref-type>Session</ejb-ref-type>
	<local>ejb.intf.DataSessionBeanLocal</local>
  </ejb-local-ref>
[...]

but, still im getting java.lang.NullPointerException.
Can you tell me why its necessary to have a class that extends AbstractApplicationServlet??

thanks!

I found few examples like on vaadin wiki page and there and anywhere else nobody is implementing interface to EJBs and they call EJB methods directly… why??

nobody?
should I post whole app?

You should define ejb s in servlet to be injected.
Move ejb from application to servlet and pass it to application when you create application in servlet.

did you mean something like this?:


@WebServlet(urlPatterns = "/*")
public class LibraryApplicationServlet extends AbstractApplicationServlet{

	
	@Inject
	LibraryApplication libraryApplication;
	
	@EJB
	DataSessionBeanLocal dataSessionBeanLocal;

	
	
	@Override
	protected Application getNewApplication(HttpServletRequest request)
			throws ServletException {
		
		libraryApplication.setDataSessionBeanLocal(dataSessionBeanLocal);
		return libraryApplication;
	}

	@Override
	protected Class<? extends Application> getApplicationClass()
			throws ClassNotFoundException {
		// TODO Auto-generated method stub
		return LibraryApplication.class;
	}

	
	
	
	
}

Funny is that instead of showing some unformated table which is created in init() metgos of applicaion, I see just “Hello World” after deploy and jBoss gives me this warn:


WARN  [ClassLoaderManager]
 Unexpected error during load of:javax.el.ExpressionFactory
java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/el/ExpressionFactory

something’s very wrong here

Yes, that code is right.
But I could not see your other codes.
I think this exception is sourced from another place.
If there is more stack trace, please send them.

application class:


@SessionScoped
public class LibraryApplication extends Application {


	Window main;
	
	@EJB
	DataSessionBeanLocal dataSessionBeanLocal;
	
	public DataSessionBeanLocal getDataSessionBeanLocal() {
		return dataSessionBeanLocal;
	}

	public void setDataSessionBeanLocal(DataSessionBeanLocal dataSessionBeanLocal) {
		this.dataSessionBeanLocal = dataSessionBeanLocal;
	}

	
	@Override
	public void init() {
		
		main = new Window("main window");
	
		
		System.out.println(dataSessionBeanLocal.getDvdList().get(0));
		
		
		setMainWindow(main);
	
		Table table = new Table("tadaa");
		
		table.addStyleName("table");
		
		main.addComponent(table);
		
	}

	

	

	
}

abstrast servlet class:


@WebServlet(urlPatterns = "/*")
public class LibraryApplicationServlet extends AbstractApplicationServlet{

	
	@Inject
	LibraryApplication libraryApplication;
	
	@EJB
	DataSessionBeanLocal dataSessionBeanLocal;

	
	
	@Override
	protected Application getNewApplication(HttpServletRequest request)
			throws ServletException {
		
		libraryApplication.setDataSessionBeanLocal(dataSessionBeanLocal);
		return libraryApplication;
	}

	@Override
	protected Class<? extends Application> getApplicationClass()
			throws ClassNotFoundException {
		// TODO Auto-generated method stub
		return LibraryApplication.class;
	}

	
	
	
	
}

web.xml :


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<display-name>Vaadin Web Application</display-name>
	<context-param>
		<description>Vaadin production mode</description>
		<param-name>productionMode</param-name>
		<param-value>false</param-value>
	</context-param>
</web-app>

beans.xml:


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
</beans>

beans.xml and web.xml are located in WEB-INF dir.

stateless session bean:


@Stateless
@Local(DataSessionBeanLocal.class)
@TransactionManagement(TransactionManagementType.CONTAINER)
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class DataSessionBeanImpl extends AbstractSessionBean implements DataSessionBeanLocal{

	@SuppressWarnings("unchecked")
	public List<Dvd> getDvdList() {
		List<Dvd> result = getEntityManager().createQuery("SELECT d FROM dvd d").getResultList();
		return result;
	}

	public List<Dvd> getDvdListByContainingString(String contains) {
		return null;
	}

	public void setDvd(Dvd dvd) {
		// TODO Auto-generated method stub
		
	}

}

whole stacktrace:


17:56:32,264 WARN  [ClassLoaderManager]
 Unexpected error during load of:javax.el.ExpressionFactory
java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/el/ExpressionFactory
	at java.lang.ClassLoader.defineClass1(Native Method)
	at java.lang.ClassLoader.defineClass(ClassLoader.java:634)
	at org.jboss.classloader.spi.base.BaseClassLoader.access$200(BaseClassLoader.java:63)
	at org.jboss.classloader.spi.base.BaseClassLoader$2.run(BaseClassLoader.java:546)
	at org.jboss.classloader.spi.base.BaseClassLoader$2.run(BaseClassLoader.java:506)
	at java.security.AccessController.doPrivileged(Native Method)
	at org.jboss.classloader.spi.base.BaseClassLoader.loadClassLocally(BaseClassLoader.java:504)
	at org.jboss.classloader.spi.base.BaseClassLoader.loadClassLocally(BaseClassLoader.java:481)
	at org.jboss.classloader.spi.base.BaseDelegateLoader.loadClass(BaseDelegateLoader.java:134)
	at org.jboss.classloader.spi.filter.FilteredDelegateLoader.loadClass(FilteredDelegateLoader.java:131)
	at org.jboss.classloader.spi.base.ClassLoadingTask$ThreadTask.run(ClassLoadingTask.java:452)
	at org.jboss.classloader.spi.base.ClassLoaderManager.nextTask(ClassLoaderManager.java:258)
	at org.jboss.classloader.spi.base.ClassLoaderManager.process(ClassLoaderManager.java:152)
	at org.jboss.classloader.spi.base.BaseClassLoaderDomain.loadClass(BaseClassLoaderDomain.java:259)
	at org.jboss.classloader.spi.base.BaseClassLoaderDomain.loadClass(BaseClassLoaderDomain.java:1102)
	at org.jboss.classloader.spi.base.BaseClassLoader.loadClassFromDomain(BaseClassLoader.java:772)
	at org.jboss.classloader.spi.base.BaseClassLoader.loadClass(BaseClassLoader.java:415)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:314)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
	at org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:129)
	at org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:67)
	at java.lang.Class.getDeclaredFields0(Native Method)
	at java.lang.Class.privateGetDeclaredFields(Class.java:2308)
	at java.lang.Class.getDeclaredFields(Class.java:1760)
	at org.jboss.injection.InjectionUtil.processFieldAnnotations(InjectionUtil.java:154)
	at org.jboss.web.tomcat.service.injection.TomcatInjectionUtils.processDynamicBeanAnnotations(TomcatInjectionUtils.java:64)
	at org.jboss.web.tomcat.service.TomcatInjectionContainer.processAnnotations(TomcatInjectionContainer.java:421)
	at org.jboss.web.tomcat.service.TomcatInjectionContainer.processDynamicBeanAnnotations(TomcatInjectionContainer.java:396)
	at org.jboss.web.tomcat.service.TomcatInjectionContainer.newInstance(TomcatInjectionContainer.java:277)
	at org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:145)
	at org.apache.jasper.servlet.JspServletWrapper.getDependants(JspServletWrapper.java:258)
	at org.apache.jasper.compiler.Compiler.isOutDated(Compiler.java:448)
	at org.apache.jasper.compiler.Compiler.isOutDated(Compiler.java:369)
	at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:581)
	at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:312)
	at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:322)
	at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:249)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
	at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:235)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
	at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:190)
	at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:92)
	at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process(SecurityContextEstablishmentValve.java:126)
	at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:70)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
	at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:330)
	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:829)
	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:601)
	at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
	at java.lang.Thread.run(Thread.java:679)

and all Im hetting is “Hello World” string in browser :smiley:

Where “Hello World” come from???
Remove dataSessionBeanLocal from application.


@TransactionAttribute
@Local
public interface DataSessionBeanLocal
{
....
}


@Stateless
public DataSessionBeanImpl ...
{
...
}

I dont know where it comes from… I think maybe application class isn’t registered so vaadin only draws Hello World…
Is my class inherited from AbstractApplicationServlet replacement for registering servlet in web.xml?
How will I access EJBs services if I remove its interface declaration from application?

Any success with this?
How you solved a problem with injecting ejb in vaadin app?

Look at the
CDI-utils
addon for one example.
Very easy to use…