/**
* Copyright (c) 2010 Perforce Software. All rights reserved.
*/
package com.perforce.maven.scm.provider.p4.command;
import java.net.URISyntaxException;
import org.apache.maven.scm.ScmException;
import org.apache.maven.scm.ScmFileSet;
import org.apache.maven.scm.log.ScmLogger;
import com.perforce.maven.scm.provider.p4.repository.P4ScmProviderRepository;
import com.perforce.p4java.exception.MessageSeverityCode;
import com.perforce.p4java.exception.P4JavaException;
import com.perforce.p4java.impl.mapbased.rpc.sys.helper.RpcSystemFileCommandsHelper;
import com.perforce.p4java.server.ServerFactory;
import com.perforce.p4java.server.callback.ICommandCallback;
/**
* Base class for Perforce server specific command. It initializes an instance
* of the Perforce server. </p>
*/
public class P4ServerExecutor extends P4Executor {
/**
* Instantiates a new p4 server executor.
*
* @param repository
* the repository
* @param fileSet
* the file set
* @param logger
* the logger
* @throws ScmException
* the scm exception
*/
public P4ServerExecutor(P4ScmProviderRepository repository,
ScmFileSet fileSet, ScmLogger logger) throws ScmException {
super(repository, fileSet, logger);
}
/**
* Initialize an instance of the Perforce server from the factory using the
* specified protocol, server port, protocol specific properties and usage
* options. Register callback on the server. Connect to server; set the user
* (if present) to server and login to the server with the user's password
* (if present).
*
* @throws ScmException
* the scm exception
*/
protected void initServer() throws ScmException {
try {
// Set default system file helper
ServerFactory
.setRpcFileSystemHelper(new RpcSystemFileCommandsHelper());
// Get an instance of the P4J server.
server = ServerFactory.getOptionsServer(
"p4java://" + p4Port.toString(), p4ServerProperties,
p4ServerOptions);
// Register server callback.
server.registerCallback(new ICommandCallback() {
public void receivedServerMessage(int key, int genericCode,
int severityCode, String message) {
// Log warning messages from server, since it's not included
// in the other callback methods.
if (severityCode == MessageSeverityCode.E_WARN) {
if (logger.isWarnEnabled()) {
logger.warn(message);
}
}
}
public void receivedServerInfoLine(int key, String infoLine) {
if (logger.isInfoEnabled()) {
logger.info(infoLine);
}
}
public void receivedServerErrorLine(int key, String errorLine) {
if (logger.isErrorEnabled()) {
logger.error(errorLine);
}
}
public void issuingServerCommand(int key, String command) {
if (logger.isInfoEnabled()) {
logger.info(command);
}
}
public void completedServerCommand(int key, long millisecsTaken) {
if (logger.isInfoEnabled()) {
logger.info("Command completed in " + millisecsTaken
+ "ms");
}
}
});
// Connect to the server.
server.connect();
// Set the Perforce charset.
if (!isEmpty(p4Charset)) {
if (server.isConnected()) {
if (server.supportsUnicode()) {
server.setCharsetName(p4Charset);
}
}
}
// Set server user.
if (!isEmpty(p4User)) {
server.setUserName(p4User);
// Login to the server with a password.
// Password can be null if it is not needed (i.e. SSO logins).
server.login(p4Passwd, p4LoginOptions);
}
} catch (URISyntaxException e) {
if (getLogger().isErrorEnabled()) {
getLogger().error(e);
}
throw new ScmException(e.getLocalizedMessage(), e);
} catch (P4JavaException e) {
if (getLogger().isErrorEnabled()) {
getLogger().error(e);
}
throw new ScmException(e.getLocalizedMessage(), e);
}
}
/**
* Cleanup the Perforce server instance. Disconnect from the Perforce
* server. Also, set the server to null.
*
* Note: It does not logout, because that will delete the user's ticket.
*
* @throws ScmException
* the scm exception
*/
protected void cleanServer() throws ScmException {
try {
server.disconnect();
server = null;
} catch (P4JavaException e) {
if (getLogger().isErrorEnabled()) {
getLogger().error(e);
}
throw new ScmException(e.getLocalizedMessage(), e);
}
}
/**
* @see com.perforce.maven.scm.provider.p4.command.P4Executor#initP4()
*/
@Override
protected void initP4() throws ScmException {
// Initialize the Perforce server.
initServer();
}
/**
* @see com.perforce.maven.scm.provider.p4.command.P4Executor#cleanP4()
*/
@Override
protected void cleanP4() throws ScmException {
// Cleanup the Perforce server.
cleanServer();
}
}