Java tutorial
/* * Copyright 2012 Eng Kam Hon (kamhon@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.kamhon.ieagle.struts2.interceptor; import java.util.Map; import net.kamhon.ieagle.application.Application; import net.kamhon.ieagle.exception.DataException; import net.kamhon.ieagle.exception.ExceptionLoggingBundle; import net.kamhon.ieagle.util.CollectionUtil; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import com.opensymphony.xwork2.Action; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.ActionProxy; import com.opensymphony.xwork2.interceptor.Interceptor; @SuppressWarnings("serial") public class LoggerInterceptor implements Interceptor { private static final Log log = LogFactory.getLog(LoggerInterceptor.class); private static ExceptionLoggingBundle exceptionLoggingBundle; public void destroy() { log.debug("call LoggerInterceptor.destroy()"); } public void init() { log.debug("call LoggerInterceptor.init()"); } public String intercept(ActionInvocation actionInvocation) throws Exception { Map<String, Object> params = actionInvocation.getInvocationContext().getParameters(); if (actionInvocation.getAction() instanceof Action) { Action action = (Action) actionInvocation.getAction(); log.debug("action = " + action.getClass().getName()); } ActionProxy actionProxy = actionInvocation.getProxy(); String actionType = params.get("actionType") != null ? ((String[]) params.get("actionType"))[0] : null; log.debug("action detail(name, method, type) = [" + actionProxy.getActionName() + ", " + actionProxy.getMethod() + ", " + actionType + "]"); log.debug("params = " + CollectionUtil.toLog(params)); try { return actionInvocation.invoke(); } catch (Exception ex) { if (exceptionLoggingBundle == null) { exceptionLoggingBundle = (ExceptionLoggingBundle) Application .lookupBean(ExceptionLoggingBundle.BEAN_NAME); } if (exceptionLoggingBundle.isNeedToLog(ex)) { log.error(ex, ex.fillInStackTrace()); } if (ex instanceof NullPointerException) { throw new DataException(ex.toString()); } else { throw ex; } } } }