Tuesday, November 27, 2007

java cvs functions
Checkout from CVS



public void checkoutFromCVS(String userName, String password, String hostName, String repository) throws ExecutionException{
PServerConnection c = null;
try {
logger.info("entering checkOutImages");

String encryptedPassword = StandardScrambler.getInstance().scramble(password);

CVSRoot root = CVSRoot.parse(":pserver:"+userName+"@"+hostName+":"+SC_PORT+repository);
c = (PServerConnection)ConnectionFactory.getConnection(root);

c.setEncodedPassword(encryptedPassword);

logger.info("open connection....");
c.open();
logger.info("after open connection");

Client client = new Client(c, new StandardAdminHandler());
GlobalOptions options = new GlobalOptions();
options.setCVSRoot(":pserver:"+userName+"@"+hostName+":"+SC_PORT+repository);

client.ensureConnection();

CheckoutCommand checkout = new CheckoutCommand(true,SC_MODULE);
checkout.setPruneDirectories(true);
File f = new File(SC_CVS_HOME);
if(!f.exists())
f.mkdirs();

client.setLocalPath(SC_CVS_HOME);
client.getEventManager().addCVSListener(new LogCVSListener());

boolean success = client.executeCommand(checkout,options);
logger.info("cmd checkout execute success:"+success);
//client.executeCommand(uc, options);
} catch (CommandAbortedException e) {
logger.error("command aborted ex:"+e.getMessage());
throw new ExecutionException(e.getMessage());
} catch (AuthenticationException e) {
logger.error("authenticate ex:"+e.getMessage());
throw new ExecutionException(e.getMessage());
}catch (CommandException e) {
logger.error("command ex:"+e.getCause());
throw new ExecutionException(e.getMessage());
} finally {
if(null != c) {
try {
c.close();
} catch (IOException e) {
logger.error(e.getMessage());
}
}
}
}



Add to CVS


public void addToCVS(List filesToAdd, File fileToAdd, String userName, String password, String hostName, String repository)throws ExecutionException{
logger.info("entering addToCVS");
PServerConnection c = null;
try {
String[] children;
// It is also possible to filter the list of returned files.
// This example does not return any files that end with `.txt'.
FilenameFilter filter = new FilenameFilter() {
public boolean accept(File dir, String name) {
if(dir.getName().equals("CVS")){
return false;
}

return true;
}
};
children = fileToAdd.list(filter);
File f = null;

for (int i=0; i < children.length; i++) {

f = new File(fileToAdd.getAbsoluteFile()+"/"+children[i]);

try {
logger.info("file:"+f.getCanonicalFile());
logger.info("is file:"+f.isFile()+" is dir:"+f.isDirectory());
if(f.isDirectory() ) {
if(!f.getName().equals("CVS")){
logger.info("recursive...");
filesToAdd.add(f);
addToCVS(filesToAdd, f, userName, password, hostName, repository);
}
}else {
filesToAdd.add(f);
}
}catch(IOException e) {
throw new ExecutionException(e.getMessage());
}
}

if(filesToAdd.size() == 0) {
logger.info("nothing to add....");
return;
}

String encryptedPassword = StandardScrambler.getInstance().scramble(password);

CVSRoot root = CVSRoot.parse(":pserver:"+userName+"@"+hostName+":"+SC_PORT+repository);
c = (PServerConnection)ConnectionFactory.getConnection(root);

c.setEncodedPassword(encryptedPassword);

logger.info("open connection....");
c.open();
logger.info("after open connection");

Client client = new Client(c, new StandardAdminHandler());

GlobalOptions options = new GlobalOptions();
options.setCVSRoot(":pserver:"+userName+"@"+hostName+":"+SC_PORT+repository);


AddCommand add = new AddCommand();
add.setMessage("adding images for site portal...");
add.setKeywordSubst(KeywordSubstitutionOptions.BINARY);
logger.info("file path:"+fileToAdd.getPath());
logger.info("file parent:"+fileToAdd.getParent());
client.setLocalPath(SC_DIR_TO_SAVE);
client.ensureConnection();

add.setFiles((File[])filesToAdd.toArray(new File[filesToAdd.size()]));
client.getEventManager().addCVSListener(new LogCVSListener());

logger.info("executing command...");
boolean success = client.executeCommand(add,options);
logger.info("cmd add execute success:"+success);

} catch (CommandAbortedException e) {
logger.error("command aborted ex:"+e.getMessage());
throw new ExecutionException(e.getMessage());
} catch (AuthenticationException e) {
logger.error("authenticate ex:"+e.getMessage());
throw new ExecutionException(e.getMessage());
}catch (CommandException e) {
logger.error("command ex:"+e.getMessage());
throw new ExecutionException(e.getMessage());

}finally {

if(null != c) {
try {
c.close();
} catch (IOException e) {
logger.error(e.getMessage());
}
}
}
logger.info("leaving addToCVS");
}



Commit to CVS


public void commitToCVS(File fileToCommit, String userName, String password, String hostName, String repository) throws ExecutionException{
PServerConnection c = null;
try {
logger.info("entering commitToCVS");

String encryptedPassword = StandardScrambler.getInstance().scramble(password);

CVSRoot root = CVSRoot.parse(":pserver:"+userName+"@"+hostName+":"+SC_PORT+repository);
c = (PServerConnection)ConnectionFactory.getConnection(root);

c.setEncodedPassword(encryptedPassword);

logger.info("open connection....");
c.open();
logger.info("after open connection");

Client client = new Client(c, new StandardAdminHandler());
GlobalOptions options = new GlobalOptions();
options.setCVSRoot(":pserver:"+userName+"@"+hostName+":"+SC_PORT+repository);


client.ensureConnection();

client.getEventManager().addCVSListener(new BasicListener());

//File file = new File(SC_CVS_HOME+"\\"+SC_MODULE+"\\resize_needed\\"+SC_TESTER);
File afile[] = new File[] { fileToCommit };

CommitCommand commit = new CommitCommand();
commit.setMessage("auto commit adding to cvs...");
commit.setFiles(afile);


commit.setForceCommit(true);

client.getEventManager().addCVSListener(new LogCVSListener());
client.setLocalPath(SC_DIR_TO_SAVE);

logger.info("executing command...");
boolean success = client.executeCommand(commit,options);
logger.info("cmd commit execute success:"+success);

//client.executeCommand(uc, options);
}catch (CommandAbortedException e) {
logger.error("command aborted ex:"+e.getMessage());
throw new ExecutionException(e.getMessage());
}catch (AuthenticationException e) {
logger.error("authenticate ex:"+e.getMessage());
throw new ExecutionException(e.getMessage());
}catch (CommandException e) {
logger.error("command ex:"+e.getCause());
throw new ExecutionException(e.getMessage());
} finally {
if(null != c) {
try {
c.close();
} catch (IOException e) {
logger.error(e.getMessage());
}
}
}
logger.info("leaving commitToCVS");
}


InnerClass LogCVSListener


static public class LogCVSListener implements CVSListener {
protected CVSCommandResult result = new CVSCommandResult();
protected Logger logger = Logger.getLogger(LogCVSListener.class.getName());
public LogCVSListener() {
}

/***
* @return Returns the result.
*/
public CVSCommandResult getResult() {
return this.result;
}

/*
* (non-Javadoc)
*
* @see org.netbeans.lib.cvsclient.event.CVSListener#messageSent(org.netbeans.lib.cvsclient.event.MessageEvent)
*/
public void messageSent(MessageEvent e) {
if (e.isError()) {
logger.error(e.getMessage());
// permet de stocker les traces
result.getTrace().append(e.getMessage() + "\n");
} else {
logger.info(e.getMessage());
}
}

/*
* (non-Javadoc)
*
* @see org.netbeans.lib.cvsclient.event.CVSListener#messageSent(org.netbeans.lib.cvsclient.event.BinaryMessageEvent)
*/
public void messageSent(BinaryMessageEvent e) {
logger.info(e.getMessage());
}

/*
* (non-Javadoc)
*
* @see org.netbeans.lib.cvsclient.event.CVSListener#fileAdded(org.netbeans.lib.cvsclient.event.FileAddedEvent)
*/
public void fileAdded(FileAddedEvent e) {
logger.debug("File has been added.");
result.getFileAdded().add(e.getFilePath());
}

/* (non-Javadoc)
* @see org.netbeans.lib.cvsclient.event.CVSListener#fileToRemove(org.netbeans.lib.cvsclient.event.FileToRemoveEvent)
*/
public void fileToRemove(FileToRemoveEvent e) {
logger.debug("File to remove: " + e.getFilePath());
//result.getFileToRemove().add(e.getFilePath());

}

/*
* (non-Javadoc)
*
* @see org.netbeans.lib.cvsclient.event.CVSListener#fileRemoved(org.netbeans.lib.cvsclient.event.FileRemovedEvent)
*/
public void fileRemoved(FileRemovedEvent e) {
logger.debug("File is removed: " + e.getFilePath());
}

/*
* (non-Javadoc)
*
* @see org.netbeans.lib.cvsclient.event.CVSListener#fileUpdated(org.netbeans.lib.cvsclient.event.FileUpdatedEvent)
*/
public void fileUpdated(FileUpdatedEvent e) {
logger.debug("File has been updated");
result.getFileUpdated().add(e.getFilePath());
}

/*
* (non-Javadoc)
*
* @see org.netbeans.lib.cvsclient.event.CVSListener#fileInfoGenerated(org.netbeans.lib.cvsclient.event.FileInfoEvent)
*/
public void fileInfoGenerated(FileInfoEvent fileInfoEvent) {
logger.debug("File status information has been received");
// Avec le diffInfo, possibilité de gestion plus fine des différences.
// DiffInformation diffInfo =
// ((SimpleDiffBuilder)fileInfoEvent.getSource()).createDiffInformation();
result.getFileInfo().add(fileInfoEvent.getInfoContainer());
}

/*
* (non-Javadoc)
*
* @see org.netbeans.lib.cvsclient.event.CVSListener#commandTerminated(org.netbeans.lib.cvsclient.event.TerminationEvent)
*/
public void commandTerminated(TerminationEvent e) {
if (e.isError()) {
logger.error("Server responses has error");
result.setError(true);
} else {
logger.debug("Server responses has OK");
}
}

/*
* (non-Javadoc)
*
* @see org.netbeans.lib.cvsclient.event.CVSListener#moduleExpanded(org.netbeans.lib.cvsclient.event.ModuleExpansionEvent)
*/
public void moduleExpanded(ModuleExpansionEvent e) {
// ne fait rien
}

}

No comments: