Эх сурвалжийг харах

fix NotSerializableException when printing with REPORT_VIRTUALIZER

sunyj 8 жил өмнө
parent
commit
be9db05038

+ 20 - 10
src/main/java/com/uas/report/service/impl/PrintServiceImpl.java

@@ -65,7 +65,7 @@ public class PrintServiceImpl implements PrintService {
 			String otherParameters, ExportType exportType)
 			throws JRException, IOException, DocumentException, SQLException {
 		Map<String, Object> result = print(userName, profile, reportName, whereCondition, otherParameters, exportType,
-				null);
+				null, true);
 		if (!CollectionUtils.isEmpty(result)) {
 			return (byte[]) result.get("data");
 		}
@@ -76,7 +76,7 @@ public class PrintServiceImpl implements PrintService {
 	public Map<String, Object> preview(String userName, String profile, String reportName, String whereCondition,
 			String otherParameters, Integer pageIndex)
 			throws JRException, IOException, DocumentException, SQLException {
-		return print(userName, profile, reportName, whereCondition, otherParameters, null, pageIndex);
+		return print(userName, profile, reportName, whereCondition, otherParameters, null, pageIndex, true);
 	}
 
 	/**
@@ -89,6 +89,7 @@ public class PrintServiceImpl implements PrintService {
 	 * @param otherParameters
 	 * @param exportType
 	 * @param pageIndex
+     * @param useFileVirtualizer 是否使用文件虚拟化技术
 	 * @return 页码pageSize:int,数据data:byte[]
 	 * @throws JRException
 	 * @throws IOException
@@ -96,7 +97,7 @@ public class PrintServiceImpl implements PrintService {
 	 * @throws SQLException
 	 */
 	private Map<String, Object> print(String userName, String profile, String reportName, String whereCondition,
-			String otherParameters, ExportType exportType, Integer pageIndex)
+			String otherParameters, ExportType exportType, Integer pageIndex, boolean useFileVirtualizer)
 			throws JRException, IOException, DocumentException, SQLException {
 		// TODO 重新实现jasperserver接口
 		// try {
@@ -115,12 +116,14 @@ public class PrintServiceImpl implements PrintService {
 
 		// 向报表模板传递参数:报表路径、where条件、其他参数
 		Map<String, Object> parameters = new HashMap<>();
-		File virtualizerDir = new File(fileService.getMasterPath("tmp") + "/virtualizer");
-		if (!virtualizerDir.exists()) {
-			virtualizerDir.mkdirs();
-		}
-		parameters.put(JRParameter.REPORT_VIRTUALIZER, new JRFileVirtualizer(2, virtualizerDir.getAbsolutePath()));
-		parameters.put(ReportConstants.PARAMETER_REPORT_DIR, fileService.getMasterPath(masterOfJrxml));
+        if (useFileVirtualizer) {
+            File virtualizerDir = new File(fileService.getMasterPath("tmp") + "/virtualizer");
+            if (!virtualizerDir.exists()) {
+                virtualizerDir.mkdirs();
+            }
+            parameters.put(JRParameter.REPORT_VIRTUALIZER, new JRFileVirtualizer(2, virtualizerDir.getAbsolutePath()));
+        }
+        parameters.put(ReportConstants.PARAMETER_REPORT_DIR, fileService.getMasterPath(masterOfJrxml));
 		if (!StringUtils.isEmpty(whereCondition)) {
 			parameters.put(ReportConstants.PARAMETER_WHERE_CONDITION, whereCondition);
 		}
@@ -199,7 +202,14 @@ public class PrintServiceImpl implements PrintService {
 			// TODO 先写入文件,用时再读取,防止数据量太大,内存不足
 			result.put("data", data);
 			return result;
-		} finally {
+        } catch (JRRuntimeException e) {
+            // 如果因为某个类未实现 Serializable 而无法反序列化,则不使用 REPORT_VIRTUALIZER
+            if (e.getCause() instanceof NotSerializableException) {
+                logger.error(e.getMessage() + " 取消 REPORT_VIRTUALIZER ,重新打印");
+                return print(userName, profile, reportName, whereCondition, otherParameters, exportType, pageIndex, false);
+            }
+            throw e;
+        } finally {
 			if (connection != null) {
 				connection.close();
 			}

+ 90 - 0
src/main/java/net/sourceforge/barbecue/Module.java

@@ -0,0 +1,90 @@
+package net.sourceforge.barbecue;
+
+
+import net.sourceforge.barbecue.linear.code128.CharBuffer;
+import net.sourceforge.barbecue.output.Output;
+import net.sourceforge.barbecue.output.OutputException;
+
+import java.io.Serializable;
+import java.util.Arrays;
+
+/**
+ * 作用请见 {@link CharBuffer}
+ *
+ * @author sunyj
+ * @since 2017/11/13 11:42
+ */
+public class Module implements Serializable {
+    protected final int[] bars;
+    private String symbol;
+
+    public Module(int[] var1) {
+        this.bars = var1;
+    }
+
+    public String getSymbol() {
+        return this.symbol;
+    }
+
+    public void setSymbol(String var1) {
+        this.symbol = var1;
+    }
+
+    public int widthInBars() {
+        int var1 = 0;
+
+        for (int var2 = 0; var2 < this.bars.length; ++var2) {
+            var1 += this.bars[var2];
+        }
+
+        return var1;
+    }
+
+    protected int draw(Output var1, int var2, int var3, int var4, int var5) throws OutputException {
+        int var6 = 0;
+
+        for (int var7 = 0; var7 < this.bars.length; ++var7) {
+            int var8 = this.bars[var7];
+            int var9 = var8 * var4;
+            var6 += var1.drawBar(var2, var3, var9, var5, var7 % 2 == 0);
+            var2 += var9;
+        }
+
+        return var6;
+    }
+
+    public boolean equals(Object var1) {
+        if (this == var1) {
+            return true;
+        } else if (!(var1 instanceof Module)) {
+            return false;
+        } else {
+            Module var2 = (Module) var1;
+            return Arrays.equals(this.bars, var2.bars);
+        }
+    }
+
+    public int hashCode() {
+        int var1 = 0;
+
+        for (int var2 = 0; var2 < this.bars.length; ++var2) {
+            var1 += (var2 + 1) * this.bars[var2];
+        }
+
+        return var1;
+    }
+
+    public String toString() {
+        StringBuffer var1 = new StringBuffer();
+
+        for (int var2 = 0; var2 < this.bars.length; ++var2) {
+            if (var2 > 0) {
+                var1.append(", ");
+            }
+
+            var1.append(this.bars[var2]);
+        }
+
+        return var1.toString();
+    }
+}

+ 29 - 0
src/main/java/net/sourceforge/barbecue/linear/code128/Accumulator.java

@@ -0,0 +1,29 @@
+package net.sourceforge.barbecue.linear.code128;
+
+import java.io.Serializable;
+
+/**
+ * 作用请见 {@link CharBuffer}
+ *
+ * @author sunyj
+ * @since 2017/11/13 11:45
+ */
+public final class Accumulator implements Serializable {
+    private int value;
+
+    public Accumulator(int var1) {
+        this.value = var1;
+    }
+
+    public int getValue() {
+        return this.value;
+    }
+
+    public void add(int var1) {
+        this.value += var1;
+    }
+
+    public void increment() {
+        ++this.value;
+    }
+}

+ 49 - 0
src/main/java/net/sourceforge/barbecue/linear/code128/CharBuffer.java

@@ -0,0 +1,49 @@
+package net.sourceforge.barbecue.linear.code128;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * 覆盖原类,并实现 Serializable,以避免打印期间使用 REPORT_VIRTUALIZER 报错
+ *
+ * @author sunyj
+ * @since 2017/11/13 11:36
+ */
+public final class CharBuffer implements Serializable {
+    private final int size;
+    private List chars;
+
+    public CharBuffer(int var1) {
+        this.size = var1;
+        this.chars = new ArrayList();
+    }
+
+    public int size() {
+        return this.chars.size();
+    }
+
+    public void addChar(char var1) {
+        this.chars.add(new Character(var1));
+    }
+
+    public boolean isFull() {
+        return this.chars.size() == this.size;
+    }
+
+    public String toString() {
+        char[] var1 = new char[this.size];
+
+        for (int var2 = 0; var2 < this.size; ++var2) {
+            Character var3 = (Character) this.chars.get(var2);
+            var1[var2] = var3.charValue();
+        }
+
+        return new String(var1);
+    }
+
+    public void clear() {
+        this.chars.clear();
+    }
+}
+