private static void handleExportAttributesFromJAR(Element e, File config, File toDir) { for (Element c : ((List<Element>) e.getChildren()).toArray(new Element[0])) { Attribute a = c.getAttribute("EXPORT"); if (a != null && a.getValue().equals("copy")) { /* Copy file from JAR */ File file = GUI.restoreConfigRelativePath(config, new File(c.getText())); InputStream inputStream = GUI.class.getResourceAsStream("/" + file.getName()); if (inputStream == null) { throw new RuntimeException("Could not unpack file: " + file); } byte[] fileData = ArrayUtils.readFromStream(inputStream); if (fileData == null) { logger.info("Failed unpacking file"); throw new RuntimeException("Could not unpack file: " + file); } if (OVERWRITE || !file.exists()) { boolean ok = ArrayUtils.writeToFile(file, fileData); if (!ok) { throw new RuntimeException("Failed unpacking file: " + file); } logger.info("Unpacked file from JAR: " + file.getName()); } else if (OVERWRITE) { logger.info("Skip: unpack file from JAR: " + file.getName()); } } /* Recursive search */ handleExportAttributesFromJAR(c, config, toDir); } }
private static void handleExportAttributesToJAR(Element e, GUI gui, File toDir) { /* Checks configuration for EXPORT attributes: * copy: file copy file to exported JAR, update file path. * discard: remove config element */ for (Element c : ((List<Element>) e.getChildren()).toArray(new Element[0])) { Attribute a = c.getAttribute("EXPORT"); if (a != null && a.getValue().equals("copy")) { /* Copy file to JAR */ File file = gui.restorePortablePath(new File(c.getText())); if (!file.exists()) { throw new RuntimeException("File not found: " + file); } byte[] data = ArrayUtils.readFromFile(file); if (data == null) { throw new RuntimeException("Could not copy file: " + file); } String newFilename = file.getName(); while (new File(toDir, newFilename).exists()) { newFilename += "-1"; } boolean ok = ArrayUtils.writeToFile(new File(toDir, newFilename), data); if (!ok) { throw new RuntimeException("Error when copying file: " + file); } logger.info( "Simconfig: Copied file: " + file.getAbsolutePath() + " -> " + ("[CONFIG_DIR]/" + newFilename)); ((Element) c).setText("[CONFIG_DIR]/" + newFilename); } else if (a != null && a.getValue().equals("discard")) { /* Remove config element */ e.removeChild(c.getName()); logger.info("Simconfig: Discarded element '" + c.getName() + "': " + c.getText()); continue; } else if (a != null) { throw new RuntimeException("Unknown EXPORT attribute value: " + a.getValue()); } /* Recursive search */ handleExportAttributesToJAR(c, gui, toDir); } }
private static void execute() { String executeDir = null; try { executeDir = new File(ExecuteJAR.class.getProtectionDomain().getCodeSource().getLocation().toURI()) .getName(); if (!executeDir.endsWith(".jar")) { logger.fatal("Not a proper JAR archive: " + executeDir); System.exit(1); } executeDir = executeDir.substring(0, executeDir.length() - ".jar".length()); new File(executeDir).mkdir(); } catch (URISyntaxException e1) { logger.fatal("Can't access JAR file name: " + e1.getMessage()); System.exit(1); } /* Unpack JAR dependencies - only when they do not already exist! */ try { InputStream inputStream; File diskFile = new File(executeDir, SIMCONFIG_FILENAME); if (OVERWRITE || !diskFile.exists()) { logger.info( "Unpacking simulation config: " + SIMCONFIG_FILENAME + " -> " + diskFile.getName()); inputStream = GUI.class.getResourceAsStream("/" + SIMCONFIG_FILENAME); byte[] fileData = ArrayUtils.readFromStream(inputStream); if (fileData == null) { logger.info("Failed extracting file (read fail)"); System.exit(1); } boolean ok = ArrayUtils.writeToFile(diskFile, fileData); if (!ok) { logger.info("Failed extracting file (write fail)"); System.exit(1); } } else { logger.info("Skip: simulation config already exists: " + diskFile); } diskFile = new File(executeDir, EXTERNALTOOLS_FILENAME); if (OVERWRITE || !diskFile.exists()) { logger.info( "Unpacking external tools config: " + EXTERNALTOOLS_FILENAME + " -> " + diskFile.getName()); inputStream = GUI.class.getResourceAsStream("/" + EXTERNALTOOLS_FILENAME); byte[] fileData = ArrayUtils.readFromStream(inputStream); if (fileData == null) { logger.info("Failed extracting file (read fail)"); System.exit(1); } boolean ok = ArrayUtils.writeToFile(diskFile, fileData); if (!ok) { logger.info("Failed extracting file (write fail)"); System.exit(1); } } else { logger.info("Skip: external tools config already exists: " + diskFile); } GUI.externalToolsUserSettingsFile = diskFile; /* Unpack files from JAR (with attribute EXPORT=copy) */ SAXBuilder builder = new SAXBuilder(); Document doc = builder.build(new File(executeDir, SIMCONFIG_FILENAME)); handleExportAttributesFromJAR( doc.getRootElement(), new File(executeDir, SIMCONFIG_FILENAME), new File(executeDir)); } catch (Exception e) { logger.fatal("Error when unpacking executable JAR: " + e.getMessage()); return; } logger.info("Starting simulation"); GUI.setLookAndFeel(); GUI.quickStartSimulationConfig(new File(executeDir, SIMCONFIG_FILENAME), false, null); }