/**
   * Create and setup the remoting connection
   *
   * @throws Exception
   */
  @BeforeClass
  public static void beforeTestClass() throws Exception {
    final Endpoint endpoint =
        Remoting.createEndpoint("ejb-remote-client-endpoint", OptionMap.EMPTY);
    endpoint.addConnectionProvider(
        "remote",
        new RemoteConnectionProviderFactory(),
        OptionMap.create(Options.SSL_ENABLED, Boolean.FALSE));

    // open a connection
    final int ejbRemotingPort =
        EJBRemoteManagementUtil.getEJBRemoteConnectorPort("localhost", 9999);
    final IoFuture<Connection> futureConnection =
        endpoint.connect(
            new URI("remote://localhost:" + ejbRemotingPort),
            OptionMap.create(Options.SASL_POLICY_NOANONYMOUS, Boolean.FALSE),
            new AnonymousCallbackHandler());
    connection = IoFutureHelper.get(futureConnection, 5, TimeUnit.SECONDS);
  }
  private synchronized void createConnection() {
    try {
      endpoint = Remoting.createEndpoint("endpoint", OptionMap.EMPTY);
      endpoint.addConnectionProvider(
          "remote",
          new RemoteConnectionProviderFactory(),
          OptionMap.create(Options.SSL_ENABLED, Boolean.FALSE));
      endpoint.addConnectionProvider(
          "http-remoting",
          new HttpUpgradeConnectionProviderFactory(),
          OptionMap.create(Options.SSL_ENABLED, Boolean.FALSE));
      endpoint.addConnectionProvider(
          "https-remoting",
          new HttpUpgradeConnectionProviderFactory(),
          OptionMap.create(Options.SSL_ENABLED, Boolean.TRUE));

      // open a connection
      final IoFuture<Connection> futureConnection =
          endpoint.connect(
              new URI(hostUrl),
              OptionMap.create(
                  Options.SASL_POLICY_NOANONYMOUS,
                  Boolean.FALSE,
                  Options.SASL_POLICY_NOPLAINTEXT,
                  Boolean.FALSE),
              callbackHandler);
      connection = IoFutureHelper.get(futureConnection, 30L, TimeUnit.SECONDS);

      final EJBClientContext ejbClientContext = EJBClientContext.create(classLoader);
      ejbClientContext.registerConnection(connection);

      this.clientContext = ejbClientContext;
    } catch (IOException e) {
      throw new RuntimeException(e);
    } catch (URISyntaxException e) {
      throw new RuntimeException(e);
    }
  }