Application.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package com.uas.report;
  2. import com.uas.report.util.ContextUtils;
  3. import com.uas.report.util.FileUtils;
  4. import com.uas.report.util.ReportUtils;
  5. import net.sf.jasperreports.engine.DefaultJasperReportsContext;
  6. import net.sf.jasperreports.engine.JasperCompileManager;
  7. import net.sf.jasperreports.engine.design.JRCompiler;
  8. import org.slf4j.Logger;
  9. import org.slf4j.LoggerFactory;
  10. import org.springframework.beans.BeansException;
  11. import org.springframework.boot.SpringApplication;
  12. import org.springframework.boot.autoconfigure.SpringBootApplication;
  13. import org.springframework.boot.builder.SpringApplicationBuilder;
  14. import org.springframework.boot.web.support.SpringBootServletInitializer;
  15. import org.springframework.context.ApplicationContext;
  16. import org.springframework.context.ApplicationContextAware;
  17. import org.springframework.util.ResourceUtils;
  18. import org.springframework.web.servlet.config.annotation.EnableWebMvc;
  19. import java.io.*;
  20. import java.net.URL;
  21. import java.util.Enumeration;
  22. import java.util.zip.ZipEntry;
  23. import java.util.zip.ZipFile;
  24. /**
  25. * 不使用spring boot内嵌的容器,部署在自定义Tomcat下时的程序入口
  26. *
  27. * @author sunyj
  28. * @since 2017年1月11日 下午5:18:41
  29. */
  30. @SpringBootApplication(scanBasePackages = "com.uas.report")
  31. @EnableWebMvc
  32. public class Application extends SpringBootServletInitializer implements ApplicationContextAware {
  33. private static Logger logger = LoggerFactory.getLogger(Application.class);
  34. @Override
  35. protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
  36. try {
  37. File logFile = new File("logs/log.log");
  38. if (!logFile.getParentFile().exists()) {
  39. logFile.getParentFile().mkdir();
  40. }
  41. System.setErr(new PrintStream(new FileOutputStream(logFile, true)));
  42. } catch (FileNotFoundException e) {
  43. logger.error("", e);
  44. }
  45. // 程序入口
  46. return builder.sources(Application.class);
  47. }
  48. @Override
  49. public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
  50. // 注册spring上下文对象
  51. ContextUtils.setApplicationContext(applicationContext);
  52. logger.info("ApplicationContext registed");
  53. // 开启定时任务
  54. ReportUtils.startTask();
  55. }
  56. public static void main(String[] args) throws IOException {
  57. new SpringApplication(Application.class).run(args);
  58. setCompilerClasspath();
  59. }
  60. /**
  61. * 设置 japser 的 {@link JRCompiler#COMPILER_CLASSPATH}
  62. * <p/>
  63. * 以 spring boot jar 形式运行时,才需要设置
  64. * <p/>
  65. * 因为导出为 word 时,需要动态编译 java 文件 {@link JasperCompileManager#compile(net.sf.jasperreports.engine.design.JasperDesign)}
  66. * <p/>
  67. * 而以 spring boot jar 形式运行时, classpath 为 war 包名,javac -classpath 编译失败
  68. *
  69. * @throws IOException
  70. */
  71. private static void setCompilerClasspath() throws IOException {
  72. URL location = Application.class.getProtectionDomain().getCodeSource().getLocation();
  73. // 是否以 spring boot jar 形式运行
  74. if (ResourceUtils.isJarURL(location)) {
  75. // 提取 war 中的 jar 包到指定路径
  76. File warFile = ResourceUtils.getFile(ResourceUtils.extractJarFileURL(location));
  77. String libDir = ReportUtils.getLibDir();
  78. extractLib(warFile, new File(libDir));
  79. // 将提取的 jar 路径添加到 COMPILER_CLASSPATH
  80. String compilerClasspath = System.getProperty("java.class.path") + File.pathSeparator + libDir + File.separator + "*";
  81. DefaultJasperReportsContext.getInstance().setProperty(JRCompiler.COMPILER_CLASSPATH, compilerClasspath);
  82. }
  83. }
  84. /**
  85. * 提取 war 中的 jar
  86. *
  87. * @param warFile war 文件
  88. * @param libDir 提取的 jar 所存放的路径
  89. * @throws IOException
  90. */
  91. private static void extractLib(File warFile, File libDir) throws IOException {
  92. if (libDir.isFile()) {
  93. throw new IOException("路径并非文件夹");
  94. }
  95. // 删除旧的文件
  96. FileUtils.delete(libDir);
  97. if (!libDir.exists()) {
  98. libDir.mkdirs();
  99. }
  100. ZipFile zipFile = new ZipFile(warFile);
  101. Enumeration<? extends ZipEntry> entries = zipFile.entries();
  102. while (entries.hasMoreElements()) {
  103. ZipEntry zipEntry = entries.nextElement();
  104. // 只提取 jasperreports-$version.jar
  105. if (zipEntry.getName().matches("[\\s\\S]*?jasperreports-[\\d.]+?.jar")) {
  106. logger.info("extract... " + zipEntry.getName());
  107. try (InputStream inputStream = zipFile.getInputStream(zipEntry)) {
  108. String name = zipEntry.getName();
  109. if (name.lastIndexOf("/") != -1) {
  110. name = name.substring(name.lastIndexOf("/") + 1);
  111. }
  112. try (OutputStream outputStream = new FileOutputStream(new File(libDir, name))) {
  113. byte[] buffer = new byte[32 * 1024];
  114. int bytesRead;
  115. while ((bytesRead = inputStream.read(buffer)) != -1) {
  116. outputStream.write(buffer, 0, bytesRead);
  117. }
  118. outputStream.flush();
  119. }
  120. }
  121. }
  122. }
  123. }
  124. }