org.ow2.petals.microkernel.transport.platform.nio.client.NioClientAgentPoolObjectFactory Maven / Gradle / Ivy
/**
* Copyright (c) 2009-2012 Capgemini, 2009-2012 EBM WebSourcing, 2012-2016 Linagora
*
* This program/library 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 2.1 of the License, or (at your
* option) any later version.
*
* This program/library 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 Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program/library; If not, see http://www.gnu.org/licenses/
* for the GNU Lesser General Public License version 2.1.
*/
package org.ow2.petals.microkernel.transport.platform.nio.client;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.pool.BaseKeyedPoolableObjectFactory;
import org.ow2.petals.microkernel.api.configuration.ConfigurationService;
import org.ow2.petals.microkernel.api.configuration.ContainerConfiguration;
import org.ow2.petals.microkernel.transport.TransportException;
import org.ow2.petals.microkernel.transport.platform.nio.monitoring.TcpTransporterMonitoring;
import org.ow2.petals.microkernel.transport.platform.nio.selector.NioSelector;
import com.ebmwebsourcing.easycommons.log.LoggingUtil;
/**
* NIO Client factory.
* @author Christophe DENEUX - Capgemini Sud
*/
public class NioClientAgentPoolObjectFactory extends BaseKeyedPoolableObjectFactory {
private final ConfigurationService configurationService;
private final NioSelector selectorAgent;
private final Logger logger;
private final LoggingUtil log;
private final long connectionTimeout;
private final TcpTransporterMonitoring monitoring;
public NioClientAgentPoolObjectFactory(final ConfigurationService configurationService,
final NioSelector selectorAgent,
final long connectionTimeout, final TcpTransporterMonitoring monitoring, final Logger logger) {
this.configurationService = configurationService;
this.selectorAgent = selectorAgent;
this.logger = logger;
this.log = new LoggingUtil(logger);
this.connectionTimeout = connectionTimeout;
this.monitoring = monitoring;
}
@Override
public NioClientAgent makeObject(final String containerName) throws Exception {
if (this.logger.isLoggable(Level.FINE)) {
this.log.debug("Creating new connection to container '" + containerName + "'");
}
final ContainerConfiguration containerConfiguration = this.configurationService
.getContainerConfiguration(containerName);
if (containerConfiguration == null) {
throw new TransportException("Cannot get the information for the container "
+ containerName);
}
final String host = containerConfiguration.getHost();
final int port = containerConfiguration.getTCPPort();
final NioClientAgent clientConnection = new NioClientAgent(this.logger, this.selectorAgent);
if (this.logger.isLoggable(Level.FINE)) {
this.log.debug("Connecting to " + host + ":" + port + " (Connection timeout: "
+ this.connectionTimeout + ")");
}
clientConnection.connect(host, port, this.connectionTimeout);
this.monitoring.newOutgoingConnection(containerName);
return clientConnection;
}
@Override
public void destroyObject(final String key, final NioClientAgent nioClientAgent) throws Exception {
if (this.logger.isLoggable(Level.FINE)) {
this.log.debug("The NIO client agent connected to the container '" + key
+ "' is disconnected and removed from the pool.");
}
nioClientAgent.disconnect();
}
@Override
public boolean validateObject(final String key, final NioClientAgent nioClientAgent) {
return nioClientAgent.isUsable();
}
}