package com.landray.kmss.plugin.sign; import com.intellij.openapi.project.Project; import com.landray.kmss.plugin.sign.SignTool; import com.landray.kmss.plugin.sign.SignToolFactory; import com.landray.kmss.plugin.util.StringUtil; import java.awt.Color; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; import javax.swing.JLabel; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; public class SignJob { private static final Logger logger = LogManager.getLogger(SignJob.class); private static final String[] CFGFILES = new String[]{"design.xml", "plugin.xml", "spring.xml", "struts.xml", "hibernate.xml"}; private String[] signFileExtNames = new String[]{".xml", ".class", ".jar"}; private String[] signFileDirs = new String[]{"/WEB-INF/classes/", "/WEB-INF/KmssConfig/", "/WEB-INF/lib/", "/WEB-INF/patch/"}; private Map params; private List resources; private String webContentPath; private SignTool tool; private int signCount = 0; private List inputs = new ArrayList(); private String outputPath; private ZipOutputStream zipOutput; private long startTime = System.currentTimeMillis(); private Project project; private JLabel statusJLabel; public long getStartTime() { return this.startTime; } public void setStartTime(long startTime) { this.startTime = startTime; } public JLabel getStatusJLabel() { return this.statusJLabel; } public void setStatusJLabel(JLabel statusJLabel) { this.statusJLabel = statusJLabel; } public SignJob(Project project, Map params, List resources, String webContentPath) { this.params = params; this.resources = resources; this.webContentPath = webContentPath; this.project = project; } public SignJob(String path) { } public List run() { List time; try { System.out.println("SignJob.run"); logger.debug("SignJob.run"); this.statusJLabel.setText("签名准备..."); this.tool = SignToolFactory.build(); long var32 = this.getStartTime(); String e; if((e = this.buildInput()) != null) { List var33 = this.showMessage(e, true); return var33; } int k = 0; this.statusJLabel.setText("正在计算工作数量..."); int workCount = 0; Iterator var6; String e1; for(var6 = this.inputs.iterator(); var6.hasNext(); workCount += this.caculateWorkCount(new File(this.webContentPath + e1))) { e1 = (String)var6.next(); ++k; } System.out.println("inputs.size()=" + this.inputs.size()); this.statusJLabel.setText("正在获取客户信息..." + (workCount + 10 + 1)); List var34; if((e = this.tool.signInit(this.params)) != null) { var34 = this.showMessage(e, true); return var34; } this.statusJLabel.setText("正在创建输出目录"); if((e = this.buildOutput()) != null) { var34 = this.showMessage(e, true); return var34; } this.statusJLabel.setText("正在输出文件(共" + workCount + "个)"); var6 = this.inputs.iterator(); while(var6.hasNext()) { e1 = (String)var6.next(); ++k; this.statusJLabel.setText("正在输出文件(共" + workCount + "个)-->" + k); if((e = this.scan(e1)) != null) { List var8 = this.showMessage(e, true); return var8; } } var32 = (System.currentTimeMillis() - var32) / 1000L; e = "发版签名操作已完成!
共处理了" + workCount + "个原文件+" + this.signCount + "个签名文件,签名耗时:" + var32 / 60L + "分" + var32 % 60L + "秒
"; var34 = this.showMessage(e, false); return var34; } catch (Throwable var30) { this.statusJLabel.setText(""); logger.error("在执行“发版签名”期间发生内部错误", var30); time = this.showMessage("在执行“发版签名”期间发生内部错误", true); } finally { try { if(this.tool != null) { this.tool.close(); } } catch (Throwable var29) { logger.error(var29); } try { if(this.zipOutput != null) { this.zipOutput.close(); } } catch (Throwable var28) { logger.error(var28); } } return time; } private String buildInput() { String scope = (String)this.params.get("scope"); String message; if("all".equals(scope)) { logger.info("签名整个项目"); this.inputs.add("/"); } else { Iterator var3 = this.resources.iterator(); while(var3.hasNext()) { String resource = (String)var3.next(); if("module".equals(scope)) { logger.info("签名指定模块"); if((message = this.buildInputModule(resource)) != null) { return message; } } else { logger.info("签名选定文件/文件夹"); if((message = this.buildInputSelected(resource)) != null) { return message; } } } } if((message = this.optimizeInput()) != null) { return message; } else { this.writeModulesData(); return null; } } private String buildInputModule(String resource) { String basePath = this.project.getBasePath().replace("\\", "/"); String path = resource.replace("\\", "/"); if(path.indexOf(basePath) != -1) { path = path.substring(basePath.length()); } if(path.startsWith("/")) { path = path.substring(1); } String module = null; if(path.startsWith("src/com/landray/kmss/")) { module = path.substring("src/com/landray/kmss/".length()); } else if(!path.startsWith("WebContent/WEB-INF/classes/com/landray/kmss/")) { if(path.startsWith("WebContent/WEB-INF/KmssConfig/")) { module = path.substring("WebContent/WEB-INF/KmssConfig/".length()); } else if(path.startsWith("WebContent/")) { module = path.substring("WebContent/".length()); } } if(module != null && module.length() != 0) { String[] tmp = module.split("/"); if(tmp.length < 2) { module = null; return null; } else { if(tmp.length > 2) { module = tmp[0] + "/" + tmp[1]; } module = module + "/"; System.out.println("module=" + module); if((new File(this.webContentPath + "/WEB-INF/classes/com/landray/kmss/" + module)).exists()) { this.inputs.add("/WEB-INF/classes/com/landray/kmss/" + module); this.statusJLabel.setText("/WEB-INF/classes/com/landray/kmss/" + module); } if((new File(this.webContentPath + "/" + module)).exists()) { this.inputs.add("/" + module); this.statusJLabel.setText("/" + module); } if((new File(this.webContentPath + "/WEB-INF/KmssConfig/" + module)).exists()) { this.inputs.add("/WEB-INF/KmssConfig/" + module); this.statusJLabel.setText("/WEB-INF/KmssConfig/" + module); } File[] jarFiles = (new File(this.webContentPath + "/WEB-INF/lib")).listFiles(); String jarPrefix = "kmss_" + module.substring(0, module.length() - 1).replace('/', '_'); File[] var8 = jarFiles; int var9 = jarFiles.length; for(int var10 = 0; var10 < var9; ++var10) { File jarFile = var8[var10]; if(jarFile.isFile()) { String fileName = jarFile.getName(); if(fileName.equals(jarPrefix + ".jar") || fileName.endsWith(".jar") && fileName.startsWith(jarPrefix + "_")) { this.inputs.add("/WEB-INF/lib/" + fileName); this.statusJLabel.setText("/WEB-INF/lib/" + fileName); } } } return null; } } else { return null; } } private String buildInputSelected(String resource) { String basePath = this.project.getBasePath().replace("\\", "/"); String path = resource.replace("!/", ""); path = path.replace("\\", "/"); if(path.indexOf(basePath) != -1) { path = path.substring(basePath.length()); } if(!"WebContent".equals(path) && !"".equals(path) && !"/".equals(path)) { if(path.contains("WebContent/WEB-INF/classes/com/landray/kmss/")) { return null; } else { File resourceFile = new File(this.project.getBasePath() + File.separator + path.replace("/", File.separator)); if(!path.startsWith("/")) { path = "/" + path; } if("/src".equals(path)) { this.inputs.add("/WEB-INF/classes/"); } else if(path.startsWith("/src/")) { if(resourceFile.isFile() && resourceFile.getName().endsWith(".java")) { String input = "/WEB-INF/classes/" + path.substring("/src/".length(), path.length() - 5) + ".class"; File file = new File(this.webContentPath + input); if(!file.exists()) { return "签名文件不存在(2):" + input; } this.inputs.add(input); String filePrefix = file.getName(); filePrefix = filePrefix.substring(0, filePrefix.length() - 6) + "$"; File folder = file.getParentFile(); String folderPath = input.substring(0, input.lastIndexOf(47)); File[] members = folder.listFiles(); File[] var11 = members; int var12 = members.length; for(int var13 = 0; var13 < var12; ++var13) { File member = var11[var13]; if(member.getName().startsWith(filePrefix) && member.getName().endsWith(".class")) { this.inputs.add(folderPath + "/" + member.getName()); } } } else if(resourceFile.isFile()) { this.inputs.add("/WEB-INF/classes/" + path.substring("/src/".length())); } else { this.inputs.add("/WEB-INF/classes/" + path.substring("/src/".length()) + "/"); } } else { if(!path.startsWith("/WebContent/")) { return "签名操作只能处理WebContent或src目录下的文件:" + resourceFile.getPath(); } if(resourceFile.isFile()) { this.inputs.add(path.substring("/WebContent".length())); } else { this.inputs.add(path.substring("/WebContent".length()) + "/"); } } return null; } } else { this.inputs.clear(); this.inputs.add("/"); return null; } } private String optimizeInput() { if(this.inputs.isEmpty()) { return "您选择的范围不正确,请重新选择"; } else { Iterator i = this.inputs.iterator(); String path1; while(i.hasNext()) { path1 = (String)i.next(); File j = new File(this.webContentPath + path1); if(!j.exists()) { return "签名文件不存在(1):" + this.webContentPath + path1; } } int var5 = 0; while(var5 < this.inputs.size()) { path1 = (String)this.inputs.get(var5); int var6 = var5 + 1; while(true) { if(var6 < this.inputs.size()) { label59: { String path2 = (String)this.inputs.get(var6); if(path1.equals(path2)) { this.inputs.remove(var6); --var6; } else { if(path2.endsWith("/") && path1.startsWith(path2)) { this.inputs.remove(var5); --var5; break label59; } if(path1.endsWith("/") && path2.startsWith(path1)) { this.inputs.remove(var6); --var6; } } ++var6; continue; } } ++var5; break; } } return null; } } private void writeModulesData() { File dir = new File(this.webContentPath + "/WEB-INF/KmssConfig"); StringBuffer modules = new StringBuffer(); this.findModules(modules, dir, ""); if(modules.length() > 0) { this.params.put("modules", modules.substring(1)); } } private void findModules(StringBuffer modules, File dir, String path) { int var6; int var7; if(path.length() > 0) { String files = dir.getAbsolutePath(); String[] var5 = CFGFILES; var6 = var5.length; for(var7 = 0; var7 < var6; ++var7) { String file = var5[var7]; File fileName = new File(files + "/" + file); if(fileName.exists()) { if(!this.isInputPath(path + "/") && !this.isInputPath("/WEB-INF/KmssConfig" + path + "/") && !this.isInputPath("/WEB-INF/classes/com/landray/kmss" + path + "/") && !this.isInputPath("/WEB-INF/lib/kmss" + path.replace('/', '_') + ".jar")) { return; } modules.append(";").append(path.substring(1)); return; } } } File[] var10 = dir.listFiles(); if(var10 != null) { File[] var11 = var10; var6 = var10.length; for(var7 = 0; var7 < var6; ++var7) { File var12 = var11[var7]; String var13 = var12.getName(); if(!var13.startsWith(".") && var12.isDirectory()) { this.findModules(modules, var12, path + "/" + var13); } } } } private boolean isInputPath(String path) { Iterator var2 = this.inputs.iterator(); String input; do { if(!var2.hasNext()) { return false; } input = (String)var2.next(); if(path.equals(input)) { return true; } if(input.endsWith("/") && path.startsWith(input)) { return true; } } while(!path.endsWith("/") || !input.startsWith(path)); return true; } private int caculateWorkCount(File file) { if(file.isFile()) { this.statusJLabel.setText("正在计算文件数量..." + file.getName()); return 1; } else { File[] members = file.listFiles(); if(members == null) { return 0; } else { int count = 0; File[] var4 = members; int var5 = members.length; for(int var6 = 0; var6 < var5; ++var6) { File member = var4[var6]; if(!member.getName().startsWith(".")) { count += this.caculateWorkCount(member); } } return count; } } } private String buildOutput() { this.outputPath = ((String)this.params.get("output")).replace('\'', '/'); this.outputPath = this.outputPath.replace("\\", "/"); if(this.outputPath.endsWith("/")) { this.outputPath = this.outputPath.substring(0, this.outputPath.length() - 1); } File out = new File(this.outputPath); if(!out.exists()) { out.mkdirs(); } String zipOpt = (String)this.params.get("zip"); if(!"none".equals(zipOpt)) { try { File e = this.createNewFile(this.outputPath + "/result." + zipOpt); this.zipOutput = new ZipOutputStream(new FileOutputStream(e)); return null; } catch (Exception var4) { var4.printStackTrace(); return "无法创建输出文件"; } } else { return null; } } private String scan(String input) { File file = new File(this.webContentPath + input); try { if(file.isFile()) { this.outFile(file, input); return null; } else { if(!"/".equals(input)) { if(this.zipOutput == null) { (new File(this.outputPath + input)).mkdirs(); } else { this.zipOutput.putNextEntry(new ZipEntry(input.substring(1))); } } File[] e = file.listFiles(); if(e == null) { return null; } else { File[] var4 = e; int var5 = e.length; for(int var6 = 0; var6 < var5; ++var6) { File member = var4[var6]; if(!member.getName().startsWith(".")) { String message; if(member.isFile()) { message = this.scan(input + member.getName()); } else { message = this.scan(input + member.getName() + "/"); } if(message != null) { return message; } } } return null; } } } catch (IOException var9) { logger.error("处理文件失败:" + file.getAbsolutePath(), var9); return "处理文件失败:" + file.getAbsolutePath(); } } private void outFile(File file, String path) throws IOException { this.statusJLabel.setText("正在处理..." + file.getName()); if(this.zipOutput == null) { File out = this.createNewFile(this.outputPath + path); this.copy(new FileInputStream(file), new FileOutputStream(out), true); if(this.needSign(file)) { out = this.createNewFile(this.outputPath + path + ".sign"); this.copy(this.tool.sign(file), new FileOutputStream(out), true); ++this.signCount; } } else { this.zipOutput.putNextEntry(new ZipEntry(path.substring(1))); this.copy(new FileInputStream(file), this.zipOutput, false); if(this.needSign(file)) { this.zipOutput.putNextEntry(new ZipEntry(path.substring(1) + ".sign")); this.copy(this.tool.sign(file), this.zipOutput, false); ++this.signCount; } } } private boolean needSign(File file) { String path = file.getAbsolutePath().replace('\\', '/'); String[] var3 = this.signFileDirs; int var4 = var3.length; for(int var5 = 0; var5 < var4; ++var5) { String dir = var3[var5]; if(path.startsWith(this.webContentPath + dir)) { String[] var7 = this.signFileExtNames; int var8 = var7.length; for(int var9 = 0; var9 < var8; ++var9) { String extName = var7[var9]; if(path.toLowerCase().endsWith(extName)) { return true; } } return false; } } return false; } private void copy(InputStream in, OutputStream out, boolean closeOut) throws IOException { try { byte[] e = new byte[8192]; int len; while((len = in.read(e)) != -1) { out.write(e, 0, len); } } finally { if(closeOut) { try { out.close(); } catch (Exception var14) { logger.error(var14); } } try { in.close(); } catch (Exception var13) { logger.error(var13); } } } private File createNewFile(String path) throws IOException { File file = new File(path); if(!file.exists()) { file.getParentFile().mkdirs(); file.createNewFile(); } return file; } private List showMessage(String message, boolean error) { ArrayList rtn = new ArrayList(); rtn.add(Boolean.valueOf(error)); rtn.add(message); return rtn; } void updateStatusJLabel(String msg, Color color) { try { if(StringUtil.isNotNull(msg)) { this.statusJLabel.setText(msg); } else { this.statusJLabel.setText(""); } if(color == null) { this.statusJLabel.setForeground(Color.CYAN); } else { this.statusJLabel.setForeground(color); } } catch (Exception var4) { ; } } void updateStatusJLabel(String msg) { this.updateStatusJLabel(msg, (Color)null); } }