Java tutorial
/* * This file is part of Cherry. * * Cherry is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Cherry is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with Cherry. If not, see <http://www.gnu.org/licenses/>. * */ package net.sheehantech.cherry.pool; import org.apache.commons.pool2.BasePooledObjectFactory; import org.apache.commons.pool2.PooledObject; import org.apache.commons.pool2.impl.DefaultPooledObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.net.ssl.SSLSocketFactory; import java.net.Socket; public class PooledPushSocketFactory extends BasePooledObjectFactory<PooledPushSocket> { private Logger logger = LoggerFactory.getLogger(PooledPushSocketFactory.class); private SSLSocketFactory socketFactory; private String gateway; private Integer port; public PooledPushSocketFactory(SSLSocketFactory socketFactory, String gateway, Integer port) { super(); this.socketFactory = socketFactory; this.gateway = gateway; this.port = port; } @Override public PooledPushSocket create() throws Exception { Socket socket = socketFactory.createSocket(gateway, port); socket.setKeepAlive(true); PooledPushSocket pushSocket = new PooledPushSocket(socket); pushSocket.prepare(); logger.info("Initialised new socket {}", pushSocket); return pushSocket; } @Override public PooledObject wrap(PooledPushSocket pushSocket) { logger.debug("Wrapping push socket {}", pushSocket); return (new DefaultPooledObject<PooledPushSocket>(pushSocket)); } @Override public void destroyObject(PooledObject<PooledPushSocket> pooledObject) { PooledPushSocket pushSocket = pooledObject.getObject(); pushSocket.close(); logger.debug("Closed push socket {}", pushSocket); } @Override public boolean validateObject(PooledObject<PooledPushSocket> pooledObject) { logger.debug("Validating socket {}", pooledObject.getObject()); return (pooledObject.getObject().test()); } // @Override // public void passivateObject(PooledObject<PooledPushSocket> pooledObject) { // logger.debug("Passivating push socket {}", pooledObject.getObject()); // } // // @Override // public void activateObject(PooledObject<PooledPushSocket> pooledObject) { // logger.debug("Activating push socket {}", pooledObject.getObject()); // } }