예제 #1
0
  @Test
  public void testPutsNullObjectWithoutException() {
    log.debug(
        new JsonObject()
            .putObject("null", null) // this shouldn't cause a NullPointerException
            .encode());

    log.debug(
        new JsonObject().putObject("null", new JsonObject().putString("foo", "bar")).encode());
  }
예제 #2
0
  @Test
  public void testInsertJsonElementIntoJsonArrayWithouException() {
    JsonObject objElement = new JsonObject().putString("foo", "bar");
    JsonArray arrayElement = new JsonArray().addString("foo");

    /* Insert an Object */
    log.debug(new JsonArray().addElement(objElement).encode());

    /* Insert an Array */
    log.debug(new JsonArray().addElement(arrayElement).encode());
  }
예제 #3
0
  @Test
  public void testJsonElementConversionWithoutException() {
    JsonElement objElement = new JsonObject().putString("foo", "bar");
    JsonElement arrayElement = new JsonArray().addString("foo");

    JsonObject retrievedObject = objElement.asObject();
    JsonArray retrievedArray = arrayElement.asArray();

    log.debug(retrievedObject.encode());
    log.debug(retrievedArray.encode());
  }
예제 #4
0
  private void checkAndSend(
      boolean send,
      final String address,
      Object body,
      final SockJSSocket sock,
      final String replyAddress) {
    final Handler<Message> replyHandler;
    if (replyAddress != null) {

      replyHandler =
          new Handler<Message>() {
            public void handle(Message message) {
              // Note we don't check outbound matches for replies
              // Replies are always let through if the original message
              // was approved
              checkAddAccceptedReplyAddress(message.replyAddress);
              deliverMessage(sock, replyAddress, message);
            }
          };
    } else {
      replyHandler = null;
    }
    if (log.isDebugEnabled()) {
      log.debug("Forwarding message to address " + address + " on event bus");
    }
    if (send) {
      eb.send(address, body, replyHandler);
    } else {
      eb.publish(address, body);
    }
  }
예제 #5
0
 private void doSendOrPub(
     final boolean send, final SockJSSocket sock, final String address, final JsonObject message) {
   final Object body = getMandatoryValue(message, "body");
   final String replyAddress = message.getString("replyAddress");
   if (log.isDebugEnabled()) {
     log.debug("Received msg from client in bridge. address:" + address + " message:" + body);
   }
   Match curMatch = checkMatches(true, address, body);
   if (curMatch.doesMatch) {
     if (curMatch.requiresAuth) {
       final String sessionID = message.getString("sessionID");
       if (sessionID != null) {
         authorise(
             message,
             sessionID,
             new AsyncResultHandler<Boolean>() {
               public void handle(AsyncResult<Boolean> res) {
                 if (res.succeeded()) {
                   if (res.result) {
                     cacheAuthorisation(sessionID, sock);
                     checkAndSend(send, address, body, sock, replyAddress);
                   } else {
                     log.debug(
                         "Inbound message for address "
                             + address
                             + " rejected because sessionID is not authorised");
                   }
                 } else {
                   log.error("Error in performing authorisation", res.exception);
                 }
               }
             });
       } else {
         log.debug(
             "Inbound message for address "
                 + address
                 + " rejected because it requires auth and sessionID is missing");
       }
     } else {
       checkAndSend(send, address, body, sock, replyAddress);
     }
   } else {
     log.debug("Inbound message for address " + address + " rejected because there is no match");
   }
 }
예제 #6
0
        @Override
        public void handle(Message<JsonObject> message) {
          if (log.isDebugEnabled()) {
            log.debug(
                String.format(
                    "%s - Received message %s", DefaultGroupManager.this, message.body().encode()));
          }

          String action = message.body().getString("action");
          if (action != null) {
            switch (action) {
              case "ping":
                doPing(message);
                break;
              case "find":
                doFind(message);
                break;
              case "list":
                doList(message);
                break;
              case "select":
                doSelect(message);
                break;
              case "deploy":
                doDeploy(message);
                break;
              case "undeploy":
                doUndeploy(message);
                break;
              default:
                message.reply(
                    new JsonObject()
                        .putString("status", "error")
                        .putString("message", "Invalid action " + action));
                break;
            }
          } else {
            message.reply(
                new JsonObject()
                    .putString("status", "error")
                    .putString("message", "Must specify an action"));
          }
        }
예제 #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);
    }
  }