This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Other Application Frameworks and the fly likes Logging username Using MethodInterceptor with AOP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Frameworks » Other Application Frameworks
Bookmark "Logging username Using MethodInterceptor with AOP" Watch "Logging username Using MethodInterceptor with AOP" New topic
Author

Logging username Using MethodInterceptor with AOP

ujjwal soni
Ranch Hand

Joined: Mar 28, 2007
Posts: 403
Hi,

I am Ujjwal Soni & I have a problem in logging for my website. I have used Method Interceptor & i am unable to get from the Request or the Session object from which i can fetch the username from it. I want to know how to fetch username from the MethodInterceptor. Can you tell me how it can be, by any example.

Regards,

Ujjwal B Soni
(+919998971048)


Cheers!!!
Ujjwal B Soni <baroda, gujarat, india> <+919909981973>
"Helping hands are better than praying lips......"
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

MethodInterceptor? Can you give us some context? Which API are you using?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
ujjwal soni
Ranch Hand

Joined: Mar 28, 2007
Posts: 403
I am using spring framework 2.0 for building my website & in logging part of my website i have used method interceptor for intercepting every call of every method but i am able to intercept the call & i am able to log methods successfully but in that username is what i am not able to get. In short i want "WHO DOES WHAT" in my log. I am using API of spring framework 2.0 .
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Ok. I'll move this to a more appropriate forum.
Karthik Guru
Ranch Hand

Joined: Mar 06, 2001
Posts: 1209
You need to attach the logged-in User object to a ThreadLocal in the web tier. You can then access the User object in the interceptor on the Spring Business tier layer.
ujjwal soni
Ranch Hand

Joined: Mar 28, 2007
Posts: 403
Can you specify a small example for that ??
Karthik Guru
Ranch Hand

Joined: Mar 06, 2001
Posts: 1209
Providing a ThreadLocal example would be trivial. But I believe, you would want to read this
article first.
ujjwal soni
Ranch Hand

Joined: Mar 28, 2007
Posts: 403
Its Working Successfully now !!! I got the username in the log..Thanks
Hussein Baghdadi
clojure forum advocate
Bartender

Joined: Nov 08, 2003
Posts: 3476

Why ujjwal was unable to get the username ?
The signature of an AOP method interceptor provides Method object and the parameters which allow an access to the method and the parameters...
Could you shed some lights here, please ?
[ March 30, 2007: Message edited by: John Todd ]
ujjwal soni
Ranch Hand

Joined: Mar 28, 2007
Posts: 403
Hi John,

I was not able to get the username because no session or http-request object were available from where i can fetch the username from, i got null pointer exception while doing this. Another thing is from the method interceptor, i did not find any method from where i can get the username. I had no option than using the ThreadLocal class. If you have any other way than using the ThreadLocal Class then please tell me............

Regards,

Ujjwal B Soni
(+91989895428)
Hussein Baghdadi
clojure forum advocate
Bartender

Joined: Nov 08, 2003
Posts: 3476

ujjwal,
May I ask you to post some of your code regarding how did you use ThreadLocal ?
ujjwal soni
Ranch Hand

Joined: Mar 28, 2007
Posts: 403
Hi John,

In my application, when the user logs in then the username that i get from the bean is where i have used the ThreadLocal variable to set the username in that. The code for LoginBean is as under



import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class LoginCommand{

private String loginName;
private String password;
private String newPassword;
private String confirmNewPassword;
private String currentPassword;
private static ThreadLocal name = new ThreadLocal();
Thread t=new Thread();

public void setLoginName(String loginName)
{
LoginCommand.name.set(loginName);
t.setName(loginName);
t.start();
this.loginName=loginName;


}

public Object initialValue() {
return null;
}



public void setPassword(String password)
{
this.password=password;
}

public String getLoginName()
{
return (String)name.get();

}

public String getPassword()
{
return password;
}

public String getNewPassword()
{
return newPassword;
}

public void setNewPassword(String newpassword)
{
this.newPassword=newpassword;

}

public void setCurrentPassword(String currentPassword)
{
this.currentPassword=currentPassword;
}

public String getCurrentPassword()
{
return currentPassword;
}
public void setConfirmNewPassword(String confirmNewPassword)
{
this.confirmNewPassword=confirmNewPassword;
}

public String getConfirmNewPassword()
{
return confirmNewPassword;
}


}

Here's the MethodInterceptor code from where i put the username that i get from the LoginBean


import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class LoggingMethodInteceptor implements MethodInterceptor {

private final Log logger = LogFactory.getLog(getClass());
HttpServletRequest request;
LoginCommand cmd=new LoginCommand();
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
logger.error("Beginning method: " + methodInvocation.getMethod().getDeclaringClass() + "::" + methodInvocation.getMethod().getName());
long startTime = System.currentTimeMillis();
Object retVal=null;
try
{
//Here's the code from where i get the username & put it in log
String username =cmd.getLoginName();

logger.error("User name"+username);
retVal= methodInvocation.proceed();
}

finally
{

logger.error("method:"+methodInvocation.getMethod().getDeclaringClass() + "::" + methodInvocation.getMethod().getName());

}
return retVal;
}

}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Logging username Using MethodInterceptor with AOP
 
Similar Threads
Problem in Logging For My Website
Logging username Using MethodInterceptor with AOP
Logging username Using MethodInterceptor with AOP
Retrieve Result Set data using core library
Problem connecting with MySQL using JSP