Пример #1
0
 private VerticleHolder getVerticleHolder() {
   Context context = vertx.getContext();
   if (context != null) {
     VerticleHolder holder = (VerticleHolder) context.getDeploymentHandle();
     return holder;
   } else {
     return null;
   }
 }
Пример #2
0
 private void doRead() {
   final VertxInternal vertx = VertxInternal.instance;
   vertx.executeInBackground(
       new Runnable() {
         public void run() {
           try {
             byte[] buff = new byte[BUFFER_SIZE];
             int bytesRead = in.read(buff);
             if (bytesRead != -1) {
               if (bytesRead < BUFFER_SIZE) {
                 byte[] buff2 = new byte[bytesRead];
                 System.arraycopy(buff, 0, buff2, 0, bytesRead);
                 buff = buff2;
               }
               final Buffer ret = Buffer.create(buff);
               vertx.executeOnContext(
                   contextID,
                   new Runnable() {
                     public void run() {
                       vertx.setContextID(contextID);
                       if (!paused && dataHandler != null) {
                         dataHandler.handle(ret);
                         if (!paused && dataHandler != null) {
                           doRead();
                         }
                       }
                     }
                   });
             } else {
               // Will this ever happen?
               if (endHandler != null) {
                 endHandler.handle(null);
               }
             }
           } catch (IOException e) {
             if (exceptionHandler != null) {
               exceptionHandler.handle(e);
             }
           }
         }
       });
 }
Пример #3
0
 // Must be synchronized since called directly from different thread
 private void addVerticle(Deployment deployment, Verticle verticle, VerticleFactory factory) {
   String loggerName =
       "org.vertx.deployments." + deployment.name + "-" + deployment.verticles.size();
   Logger logger = LoggerFactory.getLogger(loggerName);
   Context context = vertx.getContext();
   VerticleHolder holder =
       new VerticleHolder(
           deployment, context, verticle, loggerName, logger, deployment.config, factory);
   deployment.verticles.add(holder);
   context.setDeploymentHandle(holder);
 }
Пример #4
0
 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());
     }
   }
 }
Пример #5
0
  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;
    }
  }
Пример #6
0
 private Handler<String> wrapDoneHandler(final Handler<String> doneHandler) {
   if (doneHandler == null) {
     return null;
   }
   final Context context = vertx.getContext();
   return new Handler<String>() {
     @Override
     public void handle(final String deploymentID) {
       if (context == null) {
         doneHandler.handle(deploymentID);
       } else {
         context.execute(
             new Runnable() {
               public void run() {
                 doneHandler.handle(deploymentID);
               }
             });
       }
     }
   };
 }
Пример #7
0
  private void doDeploy(
      final String depName,
      boolean autoRedeploy,
      boolean worker,
      boolean multiThreaded,
      String theMain,
      final ModuleIdentifier modID,
      final JsonObject config,
      final URL[] urls,
      int instances,
      final File modDir,
      final ModuleReference mr,
      final Handler<String> doneHandler) {
    checkWorkerContext();

    final String deploymentName = depName != null ? depName : genDepName();

    log.debug(
        "Deploying name : " + deploymentName + " main: " + theMain + " instances: " + instances);

    // How we determine which language implementation to use:
    // 1. Look for a prefix on the main, e.g. 'groovy:org.foo.myproject.MyGroovyMain' would force
    // the groovy
    //    language impl to be used
    // 2. If there is no prefix, then look at the extension, if any. If there is an extension
    // mapping for that
    //    extension, use that.
    // 3. No prefix and no extension mapping - use the default runtime

    LanguageImplInfo langImplInfo = null;

    final String main;
    // Look for a prefix
    int prefixMarker = theMain.indexOf(COLON);
    if (prefixMarker != -1) {
      String prefix = theMain.substring(0, prefixMarker);
      langImplInfo = languageImpls.get(prefix);
      if (langImplInfo == null) {
        throw new IllegalStateException("No language implementation known for prefix " + prefix);
      }
      main = theMain.substring(prefixMarker + 1);
    } else {
      main = theMain;
    }
    if (langImplInfo == null) {
      // No prefix - now look at the extension
      int extensionMarker = main.lastIndexOf('.');
      if (extensionMarker != -1) {
        String extension = main.substring(extensionMarker + 1);
        String langImplName = extensionMappings.get(extension);
        if (langImplName != null) {
          langImplInfo = languageImpls.get(langImplName);
          if (langImplInfo == null) {
            throw new IllegalStateException(
                "Extension mapping for "
                    + extension
                    + " specified as "
                    + langImplName
                    + ", but no language implementation known for that name");
          }
        }
      }
    }
    if (langImplInfo == null) {
      // Use the default
      langImplInfo = languageImpls.get(defaultLanguageImplName);
      if (langImplInfo == null) {
        throw new IllegalStateException(
            "Default language implementation is "
                + defaultLanguageImplName
                + " but no language implementation known for that name");
      }
    }

    // Include the language impl module as a parent of the classloader
    if (langImplInfo.moduleName != null) {
      if (!loadIncludedModules(modDir, mr, langImplInfo.moduleName)) {
        log.error("Failed to load module: " + langImplInfo.moduleName);
        doneHandler.handle(null);
        return;
      }
    }

    final VerticleFactory verticleFactory;

    final Container container = new DefaultContainer(this);

    try {
      // TODO not one verticle factory per module ref, but one per language per module ref
      verticleFactory = mr.getVerticleFactory(langImplInfo.factoryName, vertx, container);
    } catch (Exception e) {
      log.error("Failed to instantiate verticle factory", e);
      doneHandler.handle(null);
      return;
    }

    final int instCount = instances;

    class AggHandler {
      AtomicInteger count = new AtomicInteger(0);
      boolean failed;

      void done(boolean res) {
        if (!res) {
          failed = true;
        }
        if (count.incrementAndGet() == instCount) {
          String deploymentID = failed ? null : deploymentName;
          callDoneHandler(doneHandler, deploymentID);
        }
      }
    }

    final AggHandler aggHandler = new AggHandler();

    String parentDeploymentName = getDeploymentName();
    final Deployment deployment =
        new Deployment(
            deploymentName,
            main,
            modID,
            instances,
            config == null ? new JsonObject() : config.copy(),
            urls,
            modDir,
            parentDeploymentName,
            mr,
            autoRedeploy);
    mr.incRef();
    deployments.put(deploymentName, deployment);

    if (parentDeploymentName != null) {
      Deployment parentDeployment = deployments.get(parentDeploymentName);
      parentDeployment.childDeployments.add(deploymentName);
    }

    ClassLoader oldTCCL = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(mr.mcl);
    try {

      for (int i = 0; i < instances; i++) {

        // Launch the verticle instance

        Runnable runner =
            new Runnable() {
              public void run() {

                Verticle verticle = null;
                boolean error = true;

                try {
                  verticle = verticleFactory.createVerticle(main);
                  error = false;
                } catch (ClassNotFoundException e) {
                  log.error(
                      "Cannot find verticle "
                          + main
                          + " in "
                          + verticleFactory.getClass().getName(),
                      e);
                } catch (Throwable t) {
                  log.error(
                      "Failed to create verticle "
                          + main
                          + " in "
                          + verticleFactory.getClass().getName(),
                      t);
                }

                if (error) {
                  doUndeploy(
                      deploymentName,
                      new SimpleHandler() {
                        public void handle() {
                          aggHandler.done(false);
                        }
                      });
                  return;
                }
                verticle.setContainer(container);
                verticle.setVertx(vertx);

                try {
                  addVerticle(deployment, verticle, verticleFactory);
                  if (modDir != null) {
                    setPathAdjustment(modDir);
                  }
                  VoidResult vr = new VoidResult();
                  verticle.start(vr);
                  vr.setHandler(
                      new AsyncResultHandler<Void>() {
                        @Override
                        public void handle(AsyncResult<Void> ar) {
                          if (ar.succeeded()) {
                            aggHandler.done(true);
                          } else {
                            log.error(
                                "Failed to deploy verticle "
                                    + main
                                    + " in "
                                    + verticleFactory.getClass().getName(),
                                ar.exception);
                            aggHandler.done(false);
                          }
                        }
                      });
                } catch (Throwable t) {
                  t.printStackTrace();
                  vertx.reportException(t);
                  doUndeploy(
                      deploymentName,
                      new SimpleHandler() {
                        public void handle() {
                          aggHandler.done(false);
                        }
                      });
                }
              }
            };

        if (worker) {
          vertx.startInBackground(runner, multiThreaded);
        } else {
          vertx.startOnEventLoop(runner);
        }
      }
    } finally {
      Thread.currentThread().setContextClassLoader(oldTCCL);
    }
  }
Пример #8
0
 // 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);
 }