Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7952105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:45:45+00:00 2026-06-04T02:45:45+00:00

I have an simple (webprofile) EJB 3.1 Application and try do determine the current

  • 0

I have an simple (webprofile) EJB 3.1 Application and try do determine the current user within an @ApplicationScoped CDI Bean, so I use:

Principal callerPrincipal = this.sessionContext.getCallerPrincipal()

that works fine (so I can determine the name of the current user).

But after any exception in any (other) EJB this invocation does not work any more (I need to restart the Server)! Instead of returning the caller principal the method throws this exception.

Caused by: java.lang.NullPointerException
at com.sun.ejb.containers.EJBContextImpl.getCallerPrincipal(EJBContextImpl.java:421)
at de.mytest.service.CurrentUserService.getCurrentUserId(CurrentUserService.java:102)

Does anybody can give me a hint what I am doing wrong?


Implementation Details:

Server Glassfish 3.1.2

CurrentUserService:

@ApplicationScoped
public class CurrentUserService {

    @Resource
    private SessionContext sessionContext;

 public long getCurrentUserId() {

        if (this.sessionContext == null) {
            throw new RuntimeException("initialization error, sessionContext must not be null!");
        }

 /*line 102 */     Principal callerPrincipal = this.sessionContext.getCallerPrincipal(); 
        if (callerPrincipal == null) {
            throw new RuntimeException("callerPrincipal must not be null, but it is");
        }

        String name = callerPrincipal.getName();
        if (name == null) {
            throw new RuntimeException("could not determine the current user id, because no prinicial in session context");
        }

        return this.getUserIdForLogin(name);        
    }

The EJB Facad which resist between the Faces Controller and the CDI Service

@Stateless
@RolesAllowed("myUser")
public class TeilnehmerServiceEjb {

    @Inject
    private CurrentUserService currentUserService;

    public long currentUserId() {
        return  = currentUserService.getCurrentUserId();
    }
}

web.xml

<security-constraint>
    <web-resource-collection>
        <web-resource-name>All Pages</web-resource-name>
        <url-pattern>/*</url-pattern>
    </web-resource-collection>
    <auth-constraint>
        <role-name>myUser</role-name>
    </auth-constraint>
</security-constraint>

<login-config>
    <auth-method>BASIC</auth-method>
    <realm-name>mySecurityRealm</realm-name>
</login-config>

glassfish-web.xml

<security-role-mapping>
    <role-name>myUser</role-name>
    <group-name>APP.MY.USER</group-name>
</security-role-mapping>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T02:45:46+00:00Added an answer on June 4, 2026 at 2:45 am

    The reason it doesn’t work is because your SessionContext object is declared as a global variable and since you are using @ApplicationScope, the ititialization of that resource via IoC will be done only once when the app is built.

    If you want to keep the bean as @ApplicationScope, i would recommend you to try accessing the SessionContext each time you need it manually from within the method that performs the action but instead of IoC use the JNDI API manually.
    See the example how to see hot to perform a JNDI lookup to access a resource manually using:

    public long getCurrentUserId() {
          //..
         try {
              InitialContext ic = new InitialContext();
              SessionContext sessionContext=(SessionContext)   ic.lookup("java:comp/env/sessionContext");
    
              System.out.println("look up injected sctx: " + sessionContext);
    
         //Now do what you want with the Session context:
             Principal callerPrincipal = sessionContext.getCallerPrincipal();
        //..
          } catch (NamingException ex) {
              throw new IllegalStateException(ex);
          }
    //..
    
    }
    

    If you are interested in knowing about more ways of accessing the SessionContext, have a look at this link where i found that code snipet:

    http://javahowto.blogspot.com/2006/06/4-ways-to-get-ejbcontext-in-ejb-3.html

    I hope this helps

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have simple WinForms application where modifying Windows Registry. The problem is that in
I have simple user model: var user = Backbone.Model.extend({ initialize: function(){ this.bind(change:auth, function (){
Have simple Spring Security webapp with password encoding: <security:authentication-manager alias=authenticationManager> <security:authentication-provider user-service-ref=personService> <security:password-encoder hash=md5
I have simple user registration form. In which i am puting city as a
I have simple application with single AppDomain which is periodicaly launched on a server.
I have simple registration form. Once all information entered, user click submit button and
I have simple report (not interactive). When I try to remove column from its
I have simple JSF application. Everything works fine, but not navigation. It behaves like
I have simple WCF Service Application (based on this tutorial : Getting Started ).
I have simple web application based on JSP. Root of application looks like this:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.