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.io.File; import java.util.Map; import net.kamhon.ieagle.application.Application; import net.kamhon.ieagle.application.ServerConfiguration; import net.sf.jasperreports.engine.JasperCompileManager; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.Action; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.ActionProxy; import com.opensymphony.xwork2.config.entities.ResultConfig; import com.opensymphony.xwork2.interceptor.Interceptor; public class JasperReportInterceptor implements Interceptor { private static final long serialVersionUID = 1L; private static final Log log = LogFactory.getLog(JasperReportInterceptor.class); private static final boolean IS_PRODUCTION_MODE; static { IS_PRODUCTION_MODE = ((ServerConfiguration) Application.lookupBean(ServerConfiguration.BEAN_NAME)) .isProductionMode(); } public void destroy() { log.debug("call JasperReportInterceptor.destroy()"); } public void init() { log.debug("call JasperReportInterceptor.init()"); } public String intercept(ActionInvocation actionInvocation) throws Exception { File reportFile; String jasperPath = ""; String path = ""; // HttpServletRequest req = ServletActionContext.getRequest(); // path = req.getRealPath("/"); path = ServletActionContext.getServletContext().getRealPath("/"); if (actionInvocation.getAction() instanceof Action) { Action action = (Action) actionInvocation.getAction(); log.debug("action = " + action); } ActionProxy actionProxy = actionInvocation.getProxy(); log.debug("actionName() = " + actionProxy.getActionName()); if (!IS_PRODUCTION_MODE) { // Get Jasper Report Location if (actionProxy != null) { Map<String, ResultConfig> results = actionProxy.getConfig().getResults(); for (ResultConfig config : results.values()) { if (config != null) { Map<String, String> param = config.getParams(); if (StringUtils.isNotBlank(param.get("location"))) { if (StringUtils.contains(param.get("location"), "jasper")) { jasperPath = param.get("location"); log.debug("Jasper Path = " + jasperPath); break; } } } } } // Compile Jasper Report if (StringUtils.isNotBlank(jasperPath)) { jasperPath = path + jasperPath; jasperPath = StringUtils.replace(jasperPath, "/", File.separator); reportFile = new File(jasperPath); if (reportFile.exists()) { reportFile.delete(); } log.debug("Compile Jasper Report"); try { jasperPath = StringUtils.replace(jasperPath, ".jasper", ".jrxml"); JasperCompileManager.compileReportToFile(jasperPath); } catch (Exception e) { e.printStackTrace(); } } } return actionInvocation.invoke(); } }