public boolean isLoggedIn() {
   return isConnected()
       && session != null
       && session.isConnected()
       && channel != null
       && channel.isConnected();
 }
 @Test
 public void testCreateClientFailsIfPwdReturnsRelativePath() throws Exception {
   final String remoteRoot = "some/directory/in/my/home/dir";
   hostConfig = createWithOverrideUsernameAndPassword(mockJSch);
   getHostConfig().setRemoteRootDir(remoteRoot);
   final BapSshCommonConfiguration commonConfiguration =
       new BapSshCommonConfiguration("Ignore me", null, null, false);
   getHostConfig().setCommonConfig(commonConfiguration);
   expect(
           mockJSch.getSession(
               getHostConfig().getUsername(),
               getHostConfig().getHostname(),
               getHostConfig().getPort()))
       .andReturn(mockSession);
   mockSession.setPassword(TEST_PASSPHRASE);
   mockSession.setConfig((Properties) anyObject());
   mockSession.connect(getHostConfig().getTimeout());
   expect(mockSession.openChannel("sftp")).andReturn(mockSftp);
   mockSftp.connect(getHostConfig().getTimeout());
   testHelper.expectDirectoryCheck(getHostConfig().getRemoteRootDir(), true);
   mockSftp.cd(getHostConfig().getRemoteRootDir());
   expect(mockSftp.pwd()).andReturn("home/bap/" + remoteRoot);
   expect(mockSftp.isConnected()).andReturn(false);
   expect(mockSession.isConnected()).andReturn(false);
   assertCreateClientThrowsException("home/bap/" + remoteRoot);
 }
Exemple #3
0
 public void disconnect() throws GenericFileOperationFailedException {
   if (session != null && session.isConnected()) {
     session.disconnect();
   }
   if (channel != null && channel.isConnected()) {
     channel.disconnect();
   }
 }
 public void start() throws Exception {
   if (!channel.isConnected()) {
     channel.connect();
   }
 }
Exemple #5
0
  public boolean connect(RemoteFileConfiguration configuration)
      throws GenericFileOperationFailedException {
    if (isConnected()) {
      // already connected
      return true;
    }

    boolean connected = false;
    int attempt = 0;

    while (!connected) {
      try {
        if (LOG.isTraceEnabled() && attempt > 0) {
          LOG.trace(
              "Reconnect attempt #{} connecting to + {}",
              attempt,
              configuration.remoteServerInformation());
        }

        if (channel == null || !channel.isConnected()) {
          if (session == null || !session.isConnected()) {
            LOG.trace("Session isn't connected, trying to recreate and connect.");
            session = createSession(configuration);
            if (endpoint.getConfiguration().getConnectTimeout() > 0) {
              LOG.trace(
                  "Connecting use connectTimeout: "
                      + endpoint.getConfiguration().getConnectTimeout()
                      + " ...");
              session.connect(endpoint.getConfiguration().getConnectTimeout());
            } else {
              LOG.trace("Connecting ...");
              session.connect();
            }
          }

          LOG.trace("Channel isn't connected, trying to recreate and connect.");
          channel = (ChannelSftp) session.openChannel("sftp");

          if (endpoint.getConfiguration().getConnectTimeout() > 0) {
            LOG.trace(
                "Connecting use connectTimeout: "
                    + endpoint.getConfiguration().getConnectTimeout()
                    + " ...");
            channel.connect(endpoint.getConfiguration().getConnectTimeout());
          } else {
            LOG.trace("Connecting ...");
            channel.connect();
          }
          LOG.info("Connected to " + configuration.remoteServerInformation());
        }

        // yes we could connect
        connected = true;
      } catch (Exception e) {
        // check if we are interrupted so we can break out
        if (Thread.currentThread().isInterrupted()) {
          throw new GenericFileOperationFailedException(
              "Interrupted during connecting",
              new InterruptedException("Interrupted during connecting"));
        }

        GenericFileOperationFailedException failed =
            new GenericFileOperationFailedException(
                "Cannot connect to " + configuration.remoteServerInformation(), e);
        LOG.trace("Cannot connect due: {}", failed.getMessage());
        attempt++;
        if (attempt > endpoint.getMaximumReconnectAttempts()) {
          throw failed;
        }
        if (endpoint.getReconnectDelay() > 0) {
          try {
            Thread.sleep(endpoint.getReconnectDelay());
          } catch (InterruptedException ie) {
            // we could potentially also be interrupted during sleep
            Thread.currentThread().interrupt();
            throw new GenericFileOperationFailedException("Interrupted during sleeping", ie);
          }
        }
      }
    }

    return true;
  }
Exemple #6
0
 public boolean isConnected() throws GenericFileOperationFailedException {
   return session != null && session.isConnected() && channel != null && channel.isConnected();
 }
 private void expectDisconnect() throws Exception {
   expect(mockSftp.isConnected()).andReturn(false);
   expect(mockSession.isConnected()).andReturn(false);
 }