/**
* Copyright (c) 2010 Perforce Software. All rights reserved.
*/
package com.perforce.maven.scm.provider.p4.command;
import java.io.File;
import java.io.IOException;
import java.util.List;
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.client.IClient;
import com.perforce.p4java.client.IClientViewMapping;
import com.perforce.p4java.exception.P4JavaException;
import com.perforce.p4java.impl.generic.client.ClientView;
import com.perforce.p4java.impl.generic.client.ClientView.ClientViewMapping;
import com.perforce.p4java.impl.mapbased.client.Client;
/**
* This class is for Perforce client specific command. It initializes an
* instance of the Perforce server and an instance of the Perforce client. </p>
*/
public class P4ClientExecutor extends P4ServerExecutor {
/**
* 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 P4ClientExecutor(P4ScmProviderRepository repository,
ScmFileSet fileSet, ScmLogger logger) throws ScmException {
super(repository, fileSet, logger);
}
/**
* Initialize an instance of the Perforce client from the server with a
* specified client name. Set the current client on the server.
*
* @throws ScmException
* the scm exception
*/
protected void initClient() throws ScmException {
File workDir = new File(fileSet.getBasedir().getAbsolutePath());
String rootDir = workDir.getAbsolutePath();
try {
rootDir = workDir.getCanonicalPath();
} catch (IOException ex) {
}
rootDir = encodeWildcards(rootDir);
try {
// Get an instance of the Perforce client.
client = server.getClient(p4Client);
if (client == null) {
if (logger.isInfoEnabled()) {
logger.info("The specified Perforce client name '"
+ p4Client + "' doesn't exists.");
}
p4Client = generateClientName();
if (logger.isInfoEnabled()) {
logger.info("Generated new Perforce client name '"
+ p4Client + "'.");
}
// Get an instance of the Perforce client.
client = server.getClient(p4Client);
if (client == null) {
// Create a new client
ClientViewMapping clientViewMapping = createClientViewMapping(
repository, fileSet.getBasedir());
ClientView clientView = new ClientView();
clientView.addEntry(clientViewMapping);
IClient newClient = new Client(p4Client, null, null,
"Created by P4Maven", null, p4User, rootDir, null,
null, null, null, server, clientView);
String message = server.createClient(newClient);
if (logger.isInfoEnabled()) {
logger.info(message);
}
// Get an instance of the Perforce client.
client = server.getClient(p4Client);
if (client != null) {
p4ClientCreated = true;
if (logger.isInfoEnabled()) {
logger.info("Created new client: "
+ client.getName());
}
}
}
}
if (client != null) {
// Set it to the sever as the current client.
server.setCurrentClient(client);
boolean exists = false;
ClientViewMapping clientViewMapping = createClientViewMapping(
repository, fileSet.getBasedir());
ClientView clientView = client.getClientView();
if (clientView == null) {
clientView = new ClientView();
}
List<IClientViewMapping> list = clientView.getEntryList();
if (list != null) {
for (IClientViewMapping map : list) {
if (map.getDepotSpec().equals(
clientViewMapping.getDepotSpec())
&& map.getClient().equals(
clientViewMapping.getClient())) {
exists = true;
break;
}
}
}
if (!exists) {
clientView.addEntry(clientViewMapping);
client.setClientView(clientView);
if (client.canUpdate()) {
client.update();
}
if (client.canRefresh()) {
client.refresh();
}
}
}
} catch (P4JavaException e) {
if (getLogger().isErrorEnabled()) {
getLogger().error(e);
}
throw new ScmException(e.getLocalizedMessage(), e);
}
}
/**
* Cleanup the Perforce client instance.
*/
protected void cleanClient() {
// Set the client to null.
p4Client = null;
}
/**
* @see com.perforce.maven.scm.provider.p4.command.P4Executor#initP4()
*/
@Override
protected void initP4() throws ScmException {
// Initialize the Perforce server.
initServer();
// Initialize the Perforce client.
initClient();
}
/**
* @see com.perforce.maven.scm.provider.p4.command.P4Executor#cleanP4()
*/
@Override
protected void cleanP4() throws ScmException {
// Cleanup the Perforce client.
cleanClient();
// Cleanup the Perforce server.
cleanServer();
}
}
# |
Change |
User |
Description |
Committed |
|
#2
|
10055 |
Matt Attaway |
Pull over numerous changes from @dantran's branch. |
|
|
#1
|
8496 |
Matt Attaway |
Move P4Ant and P4Maven into their proper homes |
|
|
//public/perforce/p4maven/main/com.perforce.maven/src/main/java/com/perforce/maven/scm/provider/p4/command/P4ClientExecutor.java |
#1
|
8270 |
Matt Attaway |
Initial add of P4Maven source code |
|
|