private File unzipIntoTmpDir(ModuleZipInfo zipInfo, boolean deleteZip) { String tdir = generateTmpFileName(); File tdest = new File(tdir); tdest.mkdir(); if (!unzipModuleData(tdest, zipInfo, deleteZip)) { return null; } else { return tdest; } }
public synchronized void uninstallModule(String moduleName) { log.info("Uninstalling module " + moduleName + " from directory " + modRoot); File modDir = new File(modRoot, moduleName); if (!modDir.exists()) { log.error("Cannot find module to uninstall"); } else { try { vertx.fileSystem().deleteSync(modDir.getAbsolutePath(), true); log.info("Module " + moduleName + " successfully uninstalled"); } catch (Exception e) { log.error("Failed to delete directory: " + e.getMessage()); } } }
private boolean checkModDirs() { if (!modRoot.exists()) { if (!modRoot.mkdir()) { log.error("Failed to create mods dir " + modRoot); return false; } } if (!systemModRoot.exists()) { if (!systemModRoot.mkdir()) { log.error("Failed to create sys mods dir " + modRoot); return false; } } return true; }
private boolean unzipModule( final ModuleIdentifier modID, final ModuleZipInfo zipInfo, boolean deleteZip) { // We synchronize to prevent a race whereby it tries to unzip the same module at the // same time (e.g. deployModule for the same module name has been called in parallel) String modName = modID.toString(); synchronized (modName.intern()) { if (!checkModDirs()) { return false; } File fdest = new File(modRoot, modName); File sdest = new File(systemModRoot, modName); if (fdest.exists() || sdest.exists()) { // This can happen if the same module is requested to be installed // at around the same time // It's ok if this happens log.warn("Module " + modID + " is already installed"); return true; } // Unzip into temp dir first File tdest = unzipIntoTmpDir(zipInfo, deleteZip); if (tdest == null) { return false; } // Check if it's a system module JsonObject conf = loadModuleConfig(modID, tdest); ModuleFields fields = new ModuleFields(conf); boolean system = fields.isSystem(); // Now copy it to the proper directory String moveFrom = tdest.getAbsolutePath(); try { vertx .fileSystem() .moveSync(moveFrom, system ? sdest.getAbsolutePath() : fdest.getAbsolutePath()); } catch (Exception e) { log.error("Failed to move module", e); return false; } log.info("Module " + modID + " successfully installed"); return true; } }
private boolean doPullInDependencies(File modRoot, ModuleIdentifier modID) { File modDir = new File(modRoot, modID.toString()); if (!modDir.exists()) { log.error("Cannot find module to uninstall"); } JsonObject conf = loadModuleConfig(modID, modDir); if (conf == null) { log.error("Module " + modID + " does not contain a mod.json"); } ModuleFields fields = new ModuleFields(conf); List<String> mods = new ArrayList<>(); String includes = fields.getIncludes(); if (includes != null) { mods.addAll(Arrays.asList(parseIncludeString(includes))); } String deploys = fields.getDeploys(); if (deploys != null) { mods.addAll(Arrays.asList(parseIncludeString(deploys))); } if (!mods.isEmpty()) { File internalModsDir = new File(modDir, "mods"); if (!internalModsDir.exists()) { internalModsDir.mkdir(); } for (String modName : mods) { File internalModDir = new File(internalModsDir, modName); if (!internalModDir.exists()) { ModuleIdentifier theModID = new ModuleIdentifier(modName); ModuleZipInfo zipInfo = getModule(theModID); if (zipInfo.filename != null) { internalModDir.mkdir(); if (!unzipModuleData(internalModDir, zipInfo, true)) { return false; } else { log.info("Module " + modName + " successfully installed in mods dir of " + modName); // Now recurse so we bring in all of the deps doPullInDependencies(internalModsDir, theModID); } } } } } return true; }
private boolean unzipModuleData( final File directory, final ModuleZipInfo zipinfo, boolean deleteZip) { try (InputStream is = new BufferedInputStream(new FileInputStream(zipinfo.filename)); ZipInputStream zis = new ZipInputStream(new BufferedInputStream(is))) { ZipEntry entry; while ((entry = zis.getNextEntry()) != null) { String entryName = zipinfo.oldStyle ? removeTopDir(entry.getName()) : entry.getName(); if (!entryName.isEmpty()) { if (entry.isDirectory()) { new File(directory, entryName).mkdir(); } else { int count; byte[] buff = new byte[BUFFER_SIZE]; BufferedOutputStream dest = null; try { OutputStream fos = new FileOutputStream(new File(directory, entryName)); dest = new BufferedOutputStream(fos, BUFFER_SIZE); while ((count = zis.read(buff, 0, BUFFER_SIZE)) != -1) { dest.write(buff, 0, count); } dest.flush(); } finally { if (dest != null) { dest.close(); } } } } } } catch (Exception e) { log.error("Failed to unzip module", e); return false; } finally { directory.delete(); if (deleteZip) { new File(zipinfo.filename).delete(); } } return true; }
private File locateModule(File currentModDir, ModuleIdentifier modID) { if (currentModDir != null) { // Nested moduleRefs - look inside current module dir File modDir = new File(new File(currentModDir, LOCAL_MODS_DIR), modID.toString()); if (modDir.exists()) { return modDir; } } File modDir = new File(modRoot, modID.toString()); if (modDir.exists()) { return modDir; } else if (!systemModRoot.equals(modRoot)) { modDir = new File(systemModRoot, modID.toString()); if (modDir.exists()) { return modDir; } } return null; }
private List<URL> getModuleClasspath(File modDir) { List<URL> urls = new ArrayList<>(); // Add the urls for this module try { urls.add(modDir.toURI().toURL()); File libDir = new File(modDir, "lib"); if (libDir.exists()) { File[] jars = libDir.listFiles(); for (File jar : jars) { URL jarURL = jar.toURI().toURL(); urls.add(jarURL); } } return urls; } catch (MalformedURLException e) { // Won't happen log.error("malformed url", e); return null; } }
// We calculate a path adjustment that can be used by the fileSystem object // so that the *effective* working directory can be the module directory // this allows moduleRefs to read and write the file system as if they were // in the module dir, even though the actual working directory will be // wherever vertx run or vertx start was called from private void setPathAdjustment(File modDir) { Path cwd = Paths.get(".").toAbsolutePath().getParent(); Path pmodDir = Paths.get(modDir.getAbsolutePath()); Path relative = cwd.relativize(pmodDir); vertx.getContext().setPathAdjustment(relative); }