Saturday, May 1, 2010

JDeveloper 11g PS2 - getUserPrincipalName() Returns Anonymous in prepareSession() Method

I should say, migration from JDeveloper 11g PS1 to PS2 went very smoothly for us - we didn't noticed any significant problems. However, there is one issue with getUserPrincipalName() method, when you want to get current user name from ADF BC. Thanks to Steve Muench, there is already bug logged on Oracle Metalink - bug# 9672139. There are also good news about security, we don't have anymore these annoying security warnings in the log, for every protected resource. Today I will tell you, how you can get logged in user name in prepareSession() method, until mentioned bug will be fixed.

Download sample application - This sample shows two approaches how to get logged in user name in ADF BC. One of them works and another no. Hopefully in next release, both of them will work. Because we were using getUserPrincipalName() method, and it stopped working, we moved to second approach - to get logged in user name through ADF Context.

We need to get logged in user name in Application Module Implementation class:

I override framework method - prepareSession(), will get user name in this method. Its especially useful, if you need to inform database, about current web user name. First approach to get logged in user name is based on this.getUserPrincipalName() (currently not working) and second accessing ADF Context:

There is one user declared in the jazn - scott/welcome1:

Let's test it a bit. If we run ADF BC tester - both approaches work well and print currently logged in user name:

Now we run Web part and log in into application - you can see user scott is logged in:

But if we check the log - only ADF Context returns correct user name in prepareSession() method. When we access this.getUserPrincipalName directly, anonymous is returned - it is incorrect:

So, for now get logged in user name on ADF BC level from ADF Context object.


Don Kleppinger said...

This bug affects the writing of user name for entities defined with history columns. The history column gets set to anonymous. I fixed this by defining a custom base class for all entities and overriding the prepareSession method to set it from the adf context.
protected void prepareSession(Session session) {

if ("anonymous".equals(session.getEnvironment().get(PropertyMetadata.USER_PRINCIPAL.getName()))){



This worked everywhere except in our log-in bean, immediately after logging in when it wrote the login history. I had to specifically get the app module for that one and set the logged in user using the same technique.

Andrej Baranovskij said...

Hi Don,

Thanks for update, thats very important to know.


- ASK - said...

Hi all,

thanks for your suggestions.

Do you have tested
FIX from metalink bug id 9546149?
I have not found the patch on ;-(

Andrej Baranovskij said...


I didnt had chance to look into that particular patch.

Probably you should raise request on Metalink.


Kristaps said...

Hi! How can I define a custom base class for all entities and overriding the prepareSession method? Pleas explain step by step what to do?

Andrej Baranovskij said...

This is described in ADF Developer Guide, but I can describe in my next blog with step by step.


Kristaps said...

Thanks for fast reply, when You will write Your new blog? When I can read it?

Best regards, Kristaps