Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No EjbContext available

I'm using JBoss AS 7.1 to develop simple EJB application with external database (Postgres).

I want to find out currently logged user's username inside a Bean. I'm getting this exception:

javax.servlet.ServletException: JBAS014558: No EjbContext available as no EJB invocation is active
javax.faces.webapp.FacesServlet.service(FacesServlet.java:606)
org.jboss.weld.servlet.ConversationPropagationFilter.doFilter(ConversationPropagationFilter.java:62)

root cause:

javax.naming.NamingException: JBAS014558: No EjbContext available as no EJB invocation is active [Root exception is java.lang.IllegalStateException: JBAS014558: No EjbContext available as no EJB invocation is active]
org.jboss.as.naming.ServiceBasedNamingStore.lookup(ServiceBasedNamingStore.java:135)
org.jboss.as.naming.ServiceBasedNamingStore.lookup(ServiceBasedNamingStore.java:74)
org.jboss.as.naming.NamingContext.lookup(NamingContext.java:178)
org.jboss.as.naming.InitialContext.lookup(InitialContext.java:123)
org.jboss.as.naming.NamingContext.lookup(NamingContext.java:214)
javax.naming.InitialContext.lookup(InitialContext.java:411)
com.FantasyGame.DataProviderBean.getForests(DataProviderBean.java:130)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:606)
javax.el.BeanELResolver.invokeMethod(BeanELResolver.java:735)
javax.el.BeanELResolver.invoke(BeanELResolver.java:467)
javax.el.CompositeELResolver.invoke(CompositeELResolver.java:246)
org.apache.el.parser.AstValue.getValue(AstValue.java:159)
org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:189)
org.jboss.weld.el.WeldValueExpression.getValue(WeldValueExpression.java:50)
com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109)
javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194)
javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182)
javax.faces.component.UIData.getValue(UIData.java:731)
javax.faces.component.UIData.getDataModel(UIData.java:1798)
javax.faces.component.UIData.setRowIndexWithoutRowStatePreserved(UIData.java:484)
javax.faces.component.UIData.setRowIndex(UIData.java:473)
com.sun.faces.renderkit.html_basic.TableRenderer.encodeBegin(TableRenderer.java:81)
javax.faces.component.UIComponentBase.encodeBegin(UIComponentBase.java:820)
javax.faces.component.UIData.encodeBegin(UIData.java:1118)
javax.faces.component.UIComponent.encodeAll(UIComponent.java:1777)
javax.faces.component.UIComponent.encodeAll(UIComponent.java:1782)
javax.faces.component.UIComponent.encodeAll(UIComponent.java:1782)
com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:402)
com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:125)
javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:288)
com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121)
com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:594)
org.jboss.weld.servlet.ConversationPropagationFilter.doFilter(ConversationPropagationFilter.java:62)

when trying to access context:

@Singleton
@ManagedBean(name = "dataProviderBean")public class DataProviderBean {
//...
    InitialContext initialContext = new InitialContext();
    EJBContext sessionContext = (SessionContext)initialContext.lookup("java:comp/EJBContext");

I'm accessing dataProviderBean from .xhtml page:

<h:dataTable value="#{dataProviderBean.getForests()}" var="category">

I thought adding jndi.properties file with configuration would help, but it still didn't work. I'm beginner and don't know what I'm missing. Do you have an idea what is causing the problem? I'd be glad to provide any more information.

Thanks in advance.

like image 389
thedazed Avatar asked Oct 20 '22 03:10

thedazed


1 Answers

Ok, I made this work. Probably, just like @Szarpul mentioned, EJBContext cannot be obtained from ManagedBean. Thus I've created another class to provide context:

@Stateless
public class ContextProviderBean {

    @Resource
    private SessionContext sctx;

    public void setSctx(SessionContext sctx) {
        this.sctx = sctx;
    }

    public SessionContext getSctx() {
        return sctx;
    }

}

and added it to my ManagedBean:

@ManagedBean(name = "dataProviderBean")
public class DataProviderBean {
    @EJB
    private ContextProviderBean ctxProvider;

    public DataProviderBean() {
        super();
        ctxProvider = new ContextProviderBean();
    }
//...
}

This way I can access context anywhere in DataProviderBean:

EJBContext sessionContext = ctxProvider.getSctx();
String currentUser = sessionContext.getCallerPrincipal().getName();
like image 125
thedazed Avatar answered Oct 21 '22 17:10

thedazed