Logged JavaEE Interceptor for JSF logging
import java.io.Serializable;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.interceptor.AroundInvoke;
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;
/**
*
* @author happyFace
*/
@Logged
@Interceptor
public class LoggedInterceptor implements Serializable {
public static final Logger LOGGER = Logger.getLogger(LoggedInterceptor.class.getName());
public LoggedInterceptor() {
}
@AroundInvoke
public Object logMethodEntry(InvocationContext invocationContext)
throws Exception {
LOGGER.log(Level.INFO, "Entering method: {0} in class {1} at Time {2}.",
new Object[]{invocationContext.getMethod().getName(),
invocationContext.getMethod().getDeclaringClass().getName(),
new Date()});
return invocationContext.proceed();
}
//}
}
---------------------------------------------------------------------------
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.interceptor.InterceptorBinding;
/**
*
* @author happyFace
*/
@Inherited
@InterceptorBinding
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface Logged {
}
--------------------------------------------------------------------------------------
import java.security.Principal;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import javax.interceptor.Interceptors;
/**
*
* @author happyFace
*/
@Named
@ViewScoped
@Interceptors({ LoggedInterceptor.class})
public class Index {
private String name = "Get A Lawyer";
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
public String logout(){
Principal userPrincipal = FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal();
System.out.println("Principal: "+userPrincipal);
return "login?faces-redirect=true";
}
}
No comments:
Post a Comment