Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Logging username Using MethodInterceptor with AOP

 
ujjwal soni
Ranch Hand
Posts: 403
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
MethodInterceptor? Can you give us some context? Which API are you using?
 
ujjwal soni
Ranch Hand
Posts: 403
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. I'll move this to a more appropriate forum.
 
Karthik Guru
Ranch Hand
Posts: 1209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 403
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you specify a small example for that ??
 
Karthik Guru
Ranch Hand
Posts: 1209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Providing a ThreadLocal example would be trivial. But I believe, you would want to read this
article first.
 
ujjwal soni
Ranch Hand
Posts: 403
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its Working Successfully now !!! I got the username in the log..Thanks
 
Hussein Baghdadi
clojure forum advocate
Bartender
Posts: 3479
Clojure Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 403
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3479
Clojure Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ujjwal,
May I ask you to post some of your code regarding how did you use ThreadLocal ?
 
ujjwal soni
Ranch Hand
Posts: 403
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;
}

}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic