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;
}
}