/*******************************************************************************
* Copyright (c) 2013, Perforce Software
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
******************************************************************************/
package com.perforce.search.server.impl;
import java.util.List;
import java.util.Properties;
import org.apache.log4j.Logger;
import com.perforce.p4java.extension.exception.ServerAccessException;
import com.perforce.p4java.extension.exception.ServerConnectionException;
import com.perforce.p4java.extension.exception.ServerErrorException;
import com.perforce.p4java.extension.server.ConnectionPool;
import com.perforce.p4java.extension.server.PerforcePrincipalCredential;
import com.perforce.p4java.extension.server.PrincipalCredential;
import com.perforce.p4java.extension.server.Server;
import com.perforce.p4java.extension.server.ServerLocation;
import com.perforce.p4java.extension.server.internal.ServerConnectionPool;
import com.perforce.p4java.extension.server.internal.ServerLocationImpl;
import com.perforce.search.configuration.Configuration;
import com.perforce.search.server.ServerConnectionPoolFacade;
/**
* Creates and maintains a ConnectionPool<Server> using server host and port
* extracted from a supplied Configuration.
*/
public class ServerConnectionPoolFacadeImpl implements
ServerConnectionPoolFacade {
private static Logger log = Logger.getLogger(ServerConnectionPool.class);
public static final String DEFAULT_PROGRAM_NAME = "P4SearchIndexer";
public static final String DEFAULT_PROGRAM_VERSION = "13.1"; // TODO: make
// this better
private final ConnectionPool<Server> pool;
private ServerLocation serverLocation;
private PrincipalCredential indexUser;
private Configuration configuration;
public ServerConnectionPoolFacadeImpl(final Configuration config) {
this.configuration = config;
serverLocation = new ServerLocationImpl(
ServerLocation.P4DProtocol.fromString(configuration
.getProtocol()), configuration.getHost(),
configuration.getPort(), configuration.getCharset());
Properties properties = new Properties();
properties.setProperty("useAuthMemoryStore", "1");
pool = create(config.getCapacity(), serverLocation,
DEFAULT_PROGRAM_NAME, DEFAULT_PROGRAM_VERSION,
PerforcePrincipalCredential.createUser(config.getIndexerUser(),
config.getIndexerPassword()));
// yeti bug: make sure to get over trust issues here by acquiring a
// connection. yeti will do the "trust" but it skips the rest of the
// credential setting code
Server temp = null;
try {
temp = this.acquireIndexer();
} finally {
this.release(temp);
}
}
public PrincipalCredential getServerCredential() {
return this.indexUser;
}
@Override
public Server acquireIndexer() {
try {
return pool.acquire(indexUser);
} catch (Exception e) {
log.error("Failed to acquire connection to Perforce server", e);
}
return null;
}
/**
* we do (may) not have the password, but instead the ticket
*
* @param user
* @return
* @throws ServerErrorException
* @throws ServerConnectionException
* @throws ServerAccessException
*/
@Override
public Server acquireTicketed(final PrincipalCredential user)
throws ServerAccessException, ServerConnectionException,
ServerErrorException {
Server server = pool.acquire(user);
// does the password look like a ticket?
// if so use server.loginAs(), else use the normal "login" method
if (user.getCredential().length() == 32
&& user.getCredential().matches("[0-9A-Fa-f]+")) {
server.loginAs(user); // this will set the ticket
} else {
server.login(user);
}
return server;
}
/**
* @see com.perforce.p4java.extension.server.ConnectionPool#acquire(com.perforce.p4java.extension.server.PrincipalCredential)
*/
public Server acquire(final PrincipalCredential user)
throws ServerAccessException, ServerConnectionException,
ServerErrorException {
return pool.acquire(user);
}
/**
* @see com.perforce.p4java.extension.server.ConnectionPool#release(com.perforce.p4java.extension.server.Server)
*/
public void release(final Server server) {
pool.release(server);
}
public void releaseAllConnections() {
if (pool instanceof ServerConnectionPool) {
((ServerConnectionPool) pool).releaseAllConnections();
}
}
@Override
public List<Server> getConnected() {
return pool.getConnected();
}
@Override
public List<Server> getAquired() {
return pool.getAquired();
}
@Override
public Server acquire() {
return this.acquireIndexer();
}
private ConnectionPool<Server> create(final int capacity,
final ServerLocation serverLocation, final String programName,
final String programVersion, final PrincipalCredential indexUser) {
this.serverLocation = serverLocation;
this.indexUser = indexUser;
Properties properties = new Properties();
// since we are a multi-user environment with constant login/logouts,
// use the in-memory auth store instead of a tickets file
properties.setProperty("useAuthMemoryStore", "1");
return new ServerConnectionPool(capacity, serverLocation, programName,
programVersion, properties);
}
}