Wednesday, May 27, 2009

Import DVM using Java-Ant

While writing an Ant build script to deploy the BPEL/ESB processes, it became absolutely important to deploy/import the DVMs automatically using Ant build scripts. While surfing thru the web, I came across a piece of Java code that will accomplish this task. I have made few enhancements to the code which will dynamically get the DVM file details from a property file for import.


1. You will need to import the commons-codec-1.3.jar available in the SOA suite ($SOA_Home\Integration\esb\lib)
2. Download the commons-httpclient-3.1.jar from the internet (Any older version will throw errors!)
3. Import commons-logging.jar from $SOA_Home\Integration\esb\lib folder

Functional Aspects:

a.) The following piece of code will use a property file called which will contain the dynamic parameters such as hostname, port number, username, password and more importantly the dvm file(s) location

b.) Multiple DVM files can be imported at a time by providing a comma (,) separated list of DVM file locations for the "dvm" parameter in the properties file

Java Source Code:

import java.util.Properties;
import java.util.StringTokenizer;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.methods.multipart.StringPart;

public class ImportDVM {
public static void main(String[] args) {
Properties props = new Properties();
try {
InputStream in = Class.forName("ImportDVM").getResourceAsStream(""); // Properties file to define your dynamic parameters
String esbHost = props.getProperty("hostname");
int esbPort = Integer.parseInt(props.getProperty("port"));
String username = props.getProperty("username");
String password = props.getProperty("password");
String fileLocation = props.getProperty("dvm"); //dvm file location
StringTokenizer st = new StringTokenizer(fileLocation, ",");
while (st.hasMoreTokens()) {
String dvm = st.nextToken();
importDVM(esbHost, esbPort, username, password, dvm);
} catch (Exception ex) {
public static void importDVM(String esbHost, int esbPort, String username, String password, String fileLocation) {
try {
InputStream is = null;
HttpClient client = new HttpClient();
int responseCode = 0;

// Authenticate
String authUrl = "http://"+esbHost+":"+esbPort+"/esb/j_security_check?j_username="+username+"&j_password="+password;
GetMethod auth = new GetMethod(authUrl);
responseCode = client.executeMethod(auth);

// ImportDVM
File targetFile = new File(fileLocation);
PostMethod post = new PostMethod("http://"+esbHost+":"+esbPort+"/esb/esbConfiguration/executeCommand?action=ImportDVM");
post.setRequestHeader("User-Agent", "ESB Client/1.0");
//StringPart s1 = new StringPart("importType","map");
//StringPart s2 = new StringPart("importMode","overwrite");
//FilePart f1 = new FilePart(targetFile.getName(), targetFile);
Part[] parts = {new StringPart("importType","map"), new StringPart("importMode","overwrite"), new FilePart(targetFile.getName(), targetFile)};
post.setRequestEntity(new MultipartRequestEntity(parts, post.getParams()));
responseCode = client.executeMethod(post);
System.out.println("Response code: " + responseCode);
is = post.getResponseBodyAsStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int c;
while ((c = != -1) {
byte[] responseBody = baos.toByteArray();
String response = new String(responseBody);
} catch (Exception e) {

Executing from Ant:

So far so good. Now it turns out to be very simple. Just compile the class and call this using the "java" ANT task.
You got a class not found exception huh? Yup, you have forgotten to keep the Java class location in your classpath.

We are now good to go!


  1. Dear Satym,
    You rocks. This code is nice and clean. I was looking for this kind of code since long. Thanks for your posting.

    Good Luck.


  2. Hi,

    I ran this program, no error while executing... However I am not able to see the DVM changes inside the ESB console.


  3. Hi Khaleel, Have you ensured valid hostname, port, oc4jadmin username & password parameters while executing the Java class?

  4. Hi Sathya,

    I executed your code in Jdeveloper and Eclipse. Is works great !!
    I am facing issue, when i execute in command prompt.

    I have included the jar files in class path. But it throws following error

    Exception in thread "main" java.lang.NoSuchMethodError: org.apache.commons.httpclient.methods.multipart.Part.getLengthOfParts([Lorg/apache/commons/httpclient/methods/multipart/Part;[B)J
    at org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity.getContentLength(
    at org.apache.commons.httpclient.methods.EntityEnclosingMethod.getRequestContentLength(
    at org.apache.commons.httpclient.methods.EntityEnclosingMethod.addContentLengthRequestHeader(
    at org.apache.commons.httpclient.methods.EntityEnclosingMethod.addRequestHeaders(
    at org.apache.commons.httpclient.HttpMethodBase.writeRequestHeaders(
    at org.apache.commons.httpclient.HttpMethodBase.writeRequest(
    at org.apache.commons.httpclient.HttpMethodBase.execute(
    at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(
    at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(
    at org.apache.commons.httpclient.HttpClient.executeMethod(
    at org.apache.commons.httpclient.HttpClient.executeMethod(
    at DVMInstall.main(

    Please do help in sorting the issue.

    Thank you

    Naveen M