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
}

}
typical keytool implementation



C:\jboss-4.0.3SP1\server\default\conf>keytool -genkey -dname "cn=actwskey, ou=Comira, o=CatsTest, c=US" -alias actwskey -keypass ###### -keystore d:\jboss-4.0.3SP1\server\default\conf\keys\actwskey.keystore -storepass ###### validity 180 -keysize 1024 -keyalg RSA



view the contents of a keystore



keytool -list -v -keystore keys\actwskey.keystore

Monday, November 26, 2007

Example calling axis endpoint and jwsdp endpoint




private void doAxisImpl(){
try {

Service service = new Service();
Call call = (Call) service.createCall();
String url = "http://cwcdev.corporate.act.org/schedule/ALMVTC.WSDL";
call.setTargetEndpointAddress(new java.net.URL(url));

org.apache.axis.message.SOAPEnvelope env = new org.apache.axis.message.SOAPEnvelope();
org.apache.axis.message.SOAPBodyElement sbe
//= new org.apache.axis.message.SOAPBodyElement(XMLUtils.StringToElement("http://cwcdev.corporate.act.org/schedule/ALMVTC.WSDL", "getVersion", ""));
= new org.apache.axis.message.SOAPBodyElement(XMLUtils.StringToElement("http://actcenters.com/ALMVTC/message/", "sayHello", ""));
SOAPFactory factory = SOAPFactoryImpl.newInstance();
Name name = factory.createName("name");
SOAPElement param = sbe.addChildElement(name);
param.addTextNode("James Zhang");

call.setProperty(Call.SOAPACTION_USE_PROPERTY, new Boolean(true));
call.setProperty(Call.SOAPACTION_URI_PROPERTY, "http://actcenters.com/ALMVTC/action/Schedule.sayHello");
call.addParameter("name", Constants.XSD_STRING, ParameterMode.IN);

env.addBodyElement(sbe);

//XMLUtils.PrettyElementToStream(env.getAsDOM(), System.out);
//env = new SignedSOAPEnvelope(env, "http://cwcdev.corporate.act.org");

System.out.println("\n============= Request ==============");
XMLUtils.PrettyElementToStream(env.getAsDOM(), System.out);

call.invoke(env);

MessageContext mc = call.getMessageContext();
System.out.println("\n============= Response ==============");
XMLUtils.PrettyElementToStream(mc.getResponseMessage().getSOAPEnvelope().getAsDOM(), System.out);
}
catch (Exception e) {
e.printStackTrace();
}
}

jwsdp ex




private void doSOAPImpl(){

/**/

SOAPConnection soapConn = null;
try {
MessageFactory msgFactory = MessageFactory.newInstance();
SOAPMessage msg = msgFactory.createMessage();
msg.setProperty(SOAPMessage.WRITE_XML_DECLARATION, "true");
msg.setProperty(SOAPMessage.CHARACTER_SET_ENCODING, "utf-8");
msg.getMimeHeaders().addHeader("SOAPAction", "http://actcenters.com/ALMVTC/action/Schedule.sayHello");
// SOAPPart soapPart = msg.getSOAPPart();
// SOAPEnvelope soapEnvelope = soapPart.getEnvelope();

// for the tutorial web service, we don't have any headers,
// so get the "free" one and detach it:
//SOAPHeader soapHeader = soapEnvelope.getHeader();
//soapHeader.detachNode();

// to the body, add the document we want to send to the
// ping request web service:
SOAPBody body = msg.getSOAPBody();
SOAPFactory soapFactory = SOAPFactory.newInstance();
Name bodyName = soapFactory.createName("sayHello", "wsdlns", "http://actcenters.com/ALMVTC/message/"
);



SOAPBodyElement bodyElement = body.addBodyElement(bodyName);
Name paramName = soapFactory.createName("name");
SOAPElement param = bodyElement.addChildElement(paramName);
param.addTextNode("James Zhang");

// echo the document first:
System.out.println("\nAbout to send the following SOAPMessage:\n");
msg.writeTo(System.out);
String url = "http://cwcdev.corporate.act.org/schedule/ALMVTC.WSDL";
System.out.println("\nto endpoint URL "+url+"\n");

// ...then send it, and echo the response:
SOAPConnectionFactory soapConnFactory = SOAPConnectionFactory.newInstance();
soapConn = soapConnFactory.createConnection();

SOAPMessage responseMsg = soapConn.call(msg, url);
System.out.println("\nGot the following SOAPMessage in response:\n");
responseMsg.writeTo(System.out);

} catch (UnsupportedOperationException e) {
logger.error(e.getMessage());
} catch (SOAPException e) {
logger.error(e.getMessage());
} catch (IOException e) {
logger.error(e.getMessage());
}finally{
try {
soapConn.close();
} catch (SOAPException e) {
e.printStackTrace();
}
}
}

Wednesday, November 21, 2007

WS-Security using JBoss-4.0.3SP1 and jwsdp-1.6

http://www.devx.com/Java/Article/28816/1954?pf=true
http://xfire.codehaus.org/WS-Security
A more detailed description of key generation can be found here:
http://www.churchillobjects.com/c/11201e.html
http://java.sun.com/j2se/1.4.2/docs/tooldocs/windows/keytool.html

How to create a production certificate can be found here:
http://support.globalsign.net/en/objectsign/java.cfm

Thursday, November 08, 2007

Using NetBeans java_cvs library:

NetBeans JavaCVS library