All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.y3tu.tool.ssh.JschSessionPool Maven / Gradle / Ivy

There is a newer version: 1.5
Show newest version
package com.y3tu.tool.ssh;

import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.jcraft.jsch.Session;

public enum JschSessionPool {
	INSTANCE;

	/** SSH会话池,key:host,value:Session对象 */
	private Map sessionPool = new ConcurrentHashMap();

	/**
	 * 获取Session,不存在返回null
	 * 
	 * @param key 键
	 * @return Session
	 */
	public Session get(String key) {
		return sessionPool.get(key);
	}

	/**
	 * 加入Session
	 * 
	 * @param key 键
	 * @param session Session
	 */
	public void put(String key, Session session) {
		this.sessionPool.put(key, session);
	}

	/**
	 * 关闭SSH连接会话
	 * 
	 * @param key 主机,格式为user@host:port
	 */
	public void close(String key) {
		Session session = sessionPool.get(key);
		if (session != null && session.isConnected()) {
			session.disconnect();
		}
		sessionPool.remove(key);
	}

	/**
	 * 关闭所有SSH连接会话
	 */
	public void closeAll() {
		Collection sessions = sessionPool.values();
		for (Session session : sessions) {
			if (session.isConnected()) {
				session.disconnect();
			}
		}
		sessionPool.clear();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy