public synchronized boolean waitForConnection(long timeout, TimeUnit units)
     throws InterruptedException {
   long left = timeout > 0 ? units.toMillis(timeout) : Long.MAX_VALUE;
   while (!connectionMade.isSet() && left > 0) {
     long start = System.currentTimeMillis();
     this.wait(units.toMillis(timeout));
     left -= (System.currentTimeMillis() - start);
   }
   return connectionMade.isSet();
 }
  public void jmxIsReady() {
    synchronized (jmxReadyLock) {
      localJmxServerReady.set();
    }

    sendJmxReadyMessageIfNecessary();
  }
 private void shutdown(boolean fromShutdownHook, boolean forceImmediate) {
   if (clientStopped.attemptSet()) {
     DSO_LOGGER.info(
         "shuting down Terracotta Client hook=" + fromShutdownHook + " force=" + forceImmediate);
     shutdownClient(fromShutdownHook, forceImmediate);
   } else {
     DSO_LOGGER.info("Client already shutdown.");
   }
   synchronized (this) {
     //  notify in case the connection establisher is waiting for something
     notifyAll();
   }
 }
 private synchronized void openChannel(String serverHost, int serverPort)
     throws InterruptedException {
   while (!clientStopped.isSet()) {
     try {
       DSO_LOGGER.debug("Trying to open channel....");
       final char[] pw;
       if (config.getSecurityInfo().hasCredentials()) {
         Assert.assertNotNull(securityManager);
         pw =
             securityManager.getPasswordForTC(
                 config.getSecurityInfo().getUsername(), serverHost, serverPort);
       } else {
         pw = null;
       }
       this.channel.open(pw);
       DSO_LOGGER.debug("Channel open");
       break;
     } catch (final TCTimeoutException tcte) {
       CONSOLE_LOGGER.warn("Timeout connecting to server: " + tcte.getMessage());
       this.wait(5000);
     } catch (final ConnectException e) {
       CONSOLE_LOGGER.warn("Connection refused from server: " + e);
       this.wait(5000);
     } catch (final MaxConnectionsExceededException e) {
       DSO_LOGGER.fatal(e.getMessage());
       CONSOLE_LOGGER.fatal(e.getMessage());
       throw new IllegalStateException(e.getMessage(), e);
     } catch (final CommStackMismatchException e) {
       DSO_LOGGER.fatal(e.getMessage());
       CONSOLE_LOGGER.fatal(e.getMessage());
       throw new IllegalStateException(e.getMessage(), e);
     } catch (final IOException ioe) {
       CONSOLE_LOGGER.warn(
           "IOException connecting to server: "
               + serverHost
               + ":"
               + serverPort
               + ". "
               + ioe.getMessage());
       this.wait(5000);
     }
   }
 }
 public void requestStop() {
   if (stopRequested.attemptSet()) {
     readerComm.requestStop();
     writerComm.requestStop();
   }
 }
  public synchronized void start(final boolean createDedicatedMBeanServer) {
    started.set();

    Thread registrationThread =
        new Thread(
            new Runnable() {

              private static final int MAX_ATTEMPTS = 60 * 5;

              @Override
              public void run() {
                try {
                  boolean registered = false;
                  int attemptCounter = 0;
                  while (!registered && attemptCounter++ < MAX_ATTEMPTS) {
                    try {
                      if (logger.isDebugEnabled()) {
                        logger.debug(
                            "Attempt #"
                                + (attemptCounter + 1)
                                + " to find the MBeanServer and register the L1 MBeans");
                      }
                      attemptToRegister(createDedicatedMBeanServer);
                      registered = true;
                      if (logger.isDebugEnabled()) {
                        logger.debug(
                            "L1 MBeans registered with the MBeanServer successfully after "
                                + (attemptCounter + 1)
                                + " attempts");
                      }
                    } catch (InstanceAlreadyExistsException e) {
                      logger.error(
                          "Exception while registering the L1 MBeans, they already seem to exist in the MBeanServer.",
                          e);
                      return;
                    } catch (Exception e) {
                      // Ignore and try again after 1 second, give the VM a chance to get started
                      if (logger.isDebugEnabled()) {
                        logger.debug("Caught exception while trying to register L1 MBeans", e);
                      }
                      try {
                        Thread.sleep(1000);
                      } catch (InterruptedException ie) {
                        new Exception(
                                "JMX registration thread interrupted, management beans will not be available",
                                ie)
                            .printStackTrace();
                      }
                    }
                  }
                  if (registered) {
                    tunnelingHandler.jmxIsReady();
                  } else {
                    logger.error(
                        "Aborted attempts to register management"
                            + " beans after "
                            + (MAX_ATTEMPTS / 60)
                            + " min of trying.");
                  }
                } finally {
                  if (stopped) {
                    try {
                      L1Management.this.stop();
                    } catch (IOException e) {
                      logger.error("Error stopping L1 management from registration thread");
                    }
                  }
                }
              }
            },
            "L1Management JMX registration");
    registrationThread.setDaemon(true);
    registrationThread.start();
  }
 private synchronized void connectionMade() {
   connectionMade.attemptSet();
   notifyAll();
 }
 public boolean isTunnelingReady() {
   synchronized (jmxReadyLock) {
     return localJmxServerReady.isSet() && transportConnected;
   }
 }