Java tutorial
/* * Copyright 2012 AMG.lab, a Bull Group Company * * 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 org.xlcloud.ssh; import javax.annotation.PostConstruct; import javax.enterprise.context.ApplicationScoped; import javax.inject.Inject; import org.apache.commons.pool.ObjectPool; import org.apache.commons.pool.PoolableObjectFactory; import org.apache.commons.pool.impl.GenericObjectPool; import org.apache.log4j.Logger; import com.jcraft.jsch.Session; /** * Responsible for handling ssh sessions pool - create, destroy and validate * sessions * * @author Tomek Adamczewski, AMG.net */ @ApplicationScoped public class SshPoolableSessionFactory implements SshSessionFactory { private static final Logger LOG = Logger.getLogger(SshPoolableSessionFactory.class); @Inject private Builder<Session> sessionBuilder; private ObjectPool sessionPool; private int maxActive = 10; @PostConstruct public void init() { GenericObjectPool genericPool = new GenericObjectPool(new SshSessionPoolableObjectFactory()); genericPool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_GROW); genericPool.setTestOnBorrow(true); genericPool.setMaxActive(maxActive); this.sessionPool = genericPool; } /** {@inheritDoc} */ public Session getSession() { return borrowSession(sessionPool); } /** {@inheritDoc} */ public void returnSession(Session session) { returnSession(session, sessionPool); } private Session borrowSession(ObjectPool pool) { try { return (Session) pool.borrowObject(); } catch (Exception e) { LOG.error("Could not borrow SSH session from pool", e); } return null; } private void returnSession(Session session, ObjectPool pool) { try { pool.returnObject(session); } catch (Exception e) { String host = "unknown"; if (session != null) { host = session.getHost(); } LOG.error("Could not return SSH session from pool for cluster: " + host, e); } } /** * @author Tomek Adamczewski, AMG.net */ private class SshSessionPoolableObjectFactory implements PoolableObjectFactory { @Override public Object makeObject() throws Exception { Session session = sessionBuilder.build(); session.connect(); return session; } @Override public void destroyObject(Object obj) throws Exception { Session session = (Session) obj; if (session.isConnected()) { session.disconnect(); } } @Override public boolean validateObject(Object obj) { Session session = (Session) obj; if (!session.isConnected()) { return false; } return true; } @Override public void activateObject(Object obj) { } @Override public void passivateObject(Object obj) { } } }