Dashboard > Pulse v2.1 > ... > Remote API Examples > Java Example - GetLatestBuildsForProject
  Pulse v2.1 Log In | Sign Up   View a printable version of the current page.  
  Java Example - GetLatestBuildsForProject
Added by Jason Sankey, last edited by Jason Sankey on Mar 19, 2009
Labels: 
(None)

Pulse Manual Index

Overview

This example illustrates reporting over build results. The results are returned as XML-RPC structs (user-defined types), which are converted into HashMaps by the Apache library.

Code

import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
import org.apache.xmlrpc.client.XmlRpcClient;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import java.util.HashMap;

/**
 * An example of reporting over builds.  Retrieves up to the latest five builds
 * for a project and outputs some of their key details.
 */
public class GetLatestBuildsForProject
{
    private static final String PULSE_URL = "http://pulse/xmlrpc";
    private static final String USERNAME = "user";
    private static final String PASSWORD = "secret";
    private static final String PROJECT = "my project";

    public static void main(String[] argv) throws MalformedURLException, XmlRpcException
    {
        // Create a client to talk to the Pulse XML-RPC service.
        XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
        config.setServerURL(new URL(PULSE_URL));
        XmlRpcClient client = new XmlRpcClient();
        client.setConfig(config);

        // Login to obtain an authentication token.
        String token = (String) client.execute("RemoteApi.login", Arrays.asList(USERNAME, PASSWORD));

        // Use the token as credentials when retrieving the latest builds.
        // Note that XML-RPC arrays are returned as Java arrays.
        Object[] builds = (Object[]) client.execute("RemoteApi.getLatestBuildsForProject", Arrays.asList(token, PROJECT, true, 5));

        // Logout now that we are done with the credentials.
        client.execute("RemoteApi.logout", Arrays.asList(token));

        // The builds are custom structs, which Apache XML-RPC returns as
        // hashmaps.  These map from field name to field value.
        for (Object build: builds)
        {
            showBuild((HashMap) build);
        }
    }

    private static void showBuild(HashMap build)
    {
        System.out.println("Build number " + build.get("id") + ":");
        System.out.println("  Status: " + build.get("status"));
        System.out.println("  Stages:");

        Object[] stages = (Object[]) build.get("stages");
        for (Object stage: stages)
        {
            showStage((HashMap) stage);
        }
    }

    private static void showStage(HashMap stage)
    {
        System.out.println("    Stage " + stage.get("name") + ": ");
        System.out.println("      Status: " + stage.get("status"));
        System.out.println("      Agent : " + stage.get("agent"));
    }
}

Zutubi wiki is Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.2.10 Build:#528 Nov 29, 2006) - Bug/feature request - Contact Administrators