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 ()
  {}
  private static String _debugSession (@Nonnull final Session aSession)
  {
    return "[Session@" +
           Integer.toHexString (System.identityHashCode (aSession)) +
           " - " +
           (aSession.isConnected () ? "connected" : "free") +
           "]";
  }
  public static Session createSession (@Nonnull final IServerConnectionSettingsPassword aSettingsthrows JSchException
  {
    if (aSettings == null)
      throw new NullPointerException ("settings");
    final Session aSession = new JSch ().getSession (aSettings.getUserName (),
                                                     aSettings.getServerAddress (),
                                                     aSettings.getServerPort ());
    aSession.setPassword (aSettings.getPassword ());
    //
    // 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);
    if (.isDebugEnabled ())
      .debug ("Created new session " + _debugSession (aSession));
    aSession.connect ();
    return aSession;
  }
  public static EChange destroySession (@Nonnull final Session aSession)
  {
    if (aSession == null)
      throw new NullPointerException ("session");
    if (!aSession.isConnected ())
      return .;
    aSession.disconnect ();
    if (.isDebugEnabled ())
      .debug ("Closed session " + _debugSession (aSession));
    return .;
  }
New to GrepCode? Check out our FAQ X