예제 #1
0
 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;
 }
예제 #2
0
 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;
   }
 }
예제 #3
0
  private boolean loadIncludedModules(
      File currentModuleDir, ModuleReference mr, String includesString) {
    checkWorkerContext();
    for (String moduleName : parseIncludeString(includesString)) {
      ModuleIdentifier modID = new ModuleIdentifier(moduleName);
      ModuleReference includedMr = moduleRefs.get(moduleName);
      if (includedMr == null) {
        File modDir = locateModule(currentModuleDir, modID);
        if (modDir == null) {
          if (!doInstallMod(modID)) {
            return false;
          }
        }
        modDir = locateModule(currentModuleDir, modID);
        List<URL> urls = getModuleClasspath(modDir);
        JsonObject conf = loadModuleConfig(modID, modDir);
        ModuleFields fields = new ModuleFields(conf);

        boolean res = fields.isResident();
        includedMr =
            new ModuleReference(
                this,
                moduleName,
                new ModuleClassLoader(platformClassLoader, urls.toArray(new URL[urls.size()])),
                res);
        ModuleReference prev = moduleRefs.putIfAbsent(moduleName, includedMr);
        if (prev != null) {
          includedMr = prev;
        }
        String includes = fields.getIncludes();
        if (includes != null) {
          loadIncludedModules(modDir, includedMr, includes);
        }
      }
      includedMr.incRef();
      mr.mcl.addParent(includedMr);
    }
    return true;
  }
예제 #4
0
 private boolean doInstallMod(final ModuleIdentifier modID) {
   checkWorkerContext();
   if (repos.isEmpty()) {
     log.warn("No repositories configured!");
     return false;
   }
   if (locateModule(null, modID) != null) {
     log.error("Module is already installed");
     return false;
   }
   ModuleZipInfo info = getModule(modID);
   if (info != null) {
     return unzipModule(modID, info, true);
   }
   return false;
 }
예제 #5
0
 private void loadRepos() {
   try (InputStream is = getClass().getClassLoader().getResourceAsStream(REPOS_FILE_NAME)) {
     if (is != null) {
       BufferedReader rdr = new BufferedReader(new InputStreamReader(is));
       String line;
       while ((line = rdr.readLine()) != null) {
         line = line.trim();
         if (line.isEmpty() || line.startsWith("#")) {
           // blank line or comment
           continue;
         }
         int colonPos = line.indexOf(':');
         if (colonPos == -1 || colonPos == line.length() - 1) {
           throw new IllegalArgumentException("Invalid repo: " + line);
         }
         String type = line.substring(0, colonPos);
         String repoID = line.substring(colonPos + 1);
         RepoResolver resolver;
         switch (type) {
           case "mavenLocal":
             resolver = new MavenLocalRepoResolver(repoID);
             type = "maven";
             break;
           case "maven":
             resolver = new MavenRepoResolver(vertx, repoID);
             break;
           case "bintray":
             resolver = new BintrayRepoResolver(vertx, repoID);
             break;
           case "old":
             resolver = new OldRepoResolver(vertx, repoID);
             break;
           default:
             throw new IllegalArgumentException("Unknown repo type: " + type);
         }
         repos.add(resolver);
       }
     }
   } catch (IOException e) {
     log.error("Failed to load " + LANG_PROPS_FILE_NAME + " " + e.getMessage());
   }
 }
예제 #6
0
  private void doDeployMod(
      final boolean redeploy,
      final String depName,
      final ModuleIdentifier modID,
      final JsonObject config,
      final int instances,
      final File currentModDir,
      final Handler<String> doneHandler) {
    checkWorkerContext();
    File modDir = locateModule(currentModDir, modID);
    if (modDir != null) {
      JsonObject conf = loadModuleConfig(modID, modDir);
      ModuleFields fields = new ModuleFields(conf);
      String main = fields.getMain();
      if (main == null) {
        log.error("Runnable module " + modID + " mod.json must contain a \"main\" field");
        callDoneHandler(doneHandler, null);
        return;
      }
      boolean worker = fields.isWorker();
      boolean multiThreaded = fields.isMultiThreaded();
      if (multiThreaded && !worker) {
        throw new IllegalArgumentException("Multi-threaded modules must be workers");
      }
      boolean preserveCwd = fields.isPreserveCurrentWorkingDirectory();

      // If preserveCwd then use the current module directory instead, or the cwd if not in a module
      File modDirToUse = preserveCwd ? currentModDir : modDir;

      List<URL> urls = getModuleClasspath(modDir);
      if (urls == null) {
        callDoneHandler(doneHandler, null);
        return;
      }

      ModuleReference mr = moduleRefs.get(modID.toString());
      if (mr == null) {
        boolean res = fields.isResident();
        mr =
            new ModuleReference(
                this,
                modID.toString(),
                new ModuleClassLoader(platformClassLoader, urls.toArray(new URL[urls.size()])),
                res);
        ModuleReference prev = moduleRefs.putIfAbsent(modID.toString(), mr);
        if (prev != null) {
          mr = prev;
        }
      }
      ModuleIdentifier enclosingModID = getEnclosingModID();
      if (enclosingModID != null) {
        // If enclosed in another module then the enclosing module classloader becomes a parent of
        // this one
        ModuleReference parentRef = moduleRefs.get(enclosingModID.toString());
        mr.mcl.addParent(parentRef);
        parentRef.incRef();
      }

      // Now load any included moduleRefs
      String includes = fields.getIncludes();
      if (includes != null) {
        if (!loadIncludedModules(modDir, mr, includes)) {
          callDoneHandler(doneHandler, null);
          return;
        }
      }

      final boolean autoRedeploy = fields.isAutoRedeploy();

      doDeploy(
          depName,
          autoRedeploy,
          worker,
          multiThreaded,
          main,
          modID,
          config,
          urls.toArray(new URL[urls.size()]),
          instances,
          modDirToUse,
          mr,
          new Handler<String>() {
            @Override
            public void handle(String deploymentID) {
              if (deploymentID != null && !redeploy && autoRedeploy) {
                redeployer.moduleDeployed(deployments.get(deploymentID));
              }
              callDoneHandler(doneHandler, deploymentID);
            }
          });
    } else {
      if (doInstallMod(modID)) {
        doDeployMod(redeploy, depName, modID, config, instances, currentModDir, doneHandler);
      } else {
        callDoneHandler(doneHandler, null);
      }
    }
  }