Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) 2014 Philip Helger (www.helger.com) philip[at]helger[dot]com Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package com.helger.webbasics.ssh.sftp;
 
 
 
 
 
 public final class SftpSessionFactory
 {
   private static final Logger s_aLogger = LoggerFactory.getLogger (SftpSessionFactory.class);
   private static final IStatisticsHandlerCounter s_aStatsCounterCreated = StatisticsManager.getCounterHandler (SftpSessionFactory.class +
                                                                                                                "$created");
   private static final IStatisticsHandlerCounter s_aStatsCounterDestroyed = StatisticsManager.getCounterHandler (SftpSessionFactory.class +
                                                                                                                  "$created");
 
   private SftpSessionFactory ()
   {}
 
   @Nonnull
   private static String _debugSession (@Nonnull final Session aSession)
   {
     return "[Session@" +
            Integer.toHexString (System.identityHashCode (aSession)) +
            " - " +
            (aSession.isConnected () ? "connected" : "free") +
            "]";
   }
 
   @Nonnull
   private static Session _createSession (@Nonnull final JSch aJSch,
                                          @Nonnull final IBaseServerConnectionSettings aSettingsthrows JSchException
   {
     final Session aSession = aJSch.getSession (aSettings.getUserName (),
                                                aSettings.getServerAddress (),
                                                aSettings.getServerPort ());
     // Set timeout in session
     if (aSettings.getConnectionTimeoutMillis () >= 0)
       aSession.setTimeout (aSettings.getConnectionTimeoutMillis ());
 
     // Setup Strict HostKeyChecking to no so we don't get the
     // unknown host key exception
     final Properties aConfig = new Properties ();
     aConfig.put ("StrictHostKeyChecking""no");
     aSession.setConfig (aConfig);
 
     return aSession;
   }
 
   @Nonnull
   public static Session createSession (@Nonnull final IServerConnectionSettingsPassword aSettingsthrows JSchException
   {
     ValueEnforcer.notNull (aSettings"Settings");
 
     final JSch aJSch = new JSch ();
 
     // Create session
     final Session aSession = _createSession (aJSchaSettings);
     aSession.setPassword (aSettings.getPassword ());
 
     if (.isDebugEnabled ())
       .debug ("Created new session " + _debugSession (aSession));
 
     // Connect with or without timeout
     aSession.connect ();
     return aSession;
   }
  public static Session createSession (@Nonnull final IServerConnectionSettingsKeyPair aSettingsthrows JSchException
  {
    ValueEnforcer.notNull (aSettings"Settings");
    final JSch aJSch = new JSch ();
    // key pair
    final String sPassphrase = aSettings.getKeyPairPassphrase ();
    final byte [] aPassphrase = StringHelper.hasNoText (sPassphrase) ? new byte [0] : sPassphrase.getBytes ();
    aJSch.addIdentity (aSettings.getUserName (), aSettings.getPrivateKey (), aSettings.getPublicKey (), aPassphrase);
    // Create session
    final Session aSession = _createSession (aJSchaSettings);
    if (.isDebugEnabled ())
      .debug ("Created new session " + _debugSession (aSession));
    // Connect with or without timeout
    aSession.connect ();
    return aSession;
  }
  public static EChange destroySession (@Nonnull final Session aSession)
  {
    ValueEnforcer.notNull (aSession"Session");
    if (!aSession.isConnected ())
      return .;
    aSession.disconnect ();
    if (.isDebugEnabled ())
      .debug ("Closed session " + _debugSession (aSession));
    return .;
  }
New to GrepCode? Check out our FAQ X