Edward Thomson

Introducing the TFS SDK for Java

June 6, 2011  •  10:47 PM

We recently introduced the TFS SDK for Java, to allow Java developers to target applications to Team Foundation Server. One of the great features of an SDK for Java is that it allows cross-platform access to TFS.

As you might know, I'm the resident Mac guy on the TFS team, so I wanted to whip up a little demo for how you might use this on Mac OS. This little sample shows how to pop up a Growl notification every time a new work item is created that falls within a specific query. Obviously it's fairly simplistic, but hopefully it's a good starting point for thinking about what you might be able to do with the new SDK.

 1import java.util.HashSet;
 2import java.util.Set;
 3
 4import com.microsoft.tfs.core.TFSTeamProjectCollection;
 5import com.microsoft.tfs.core.clients.workitem.WorkItem;
 6import com.microsoft.tfs.core.clients.workitem.WorkItemClient;
 7import com.microsoft.tfs.core.clients.workitem.query.WorkItemCollection;
 8import com.microsoft.tfs.core.profiles.Profile;
 9import com.microsoft.tfs.core.profiles.ProfileProperties;
10
11public class WitToast
12{
13    /*
14      * This is a simple WIQL query to execute - work items included
15      * in this query will be notified for.
16      */
17    public static final String WIQL = "select [System.Id], [Microsoft.VSTS.Common.StackRank], [Microsoft.VSTS.Common.Priority], [System.WorkItemType], [System.State], [System.Title] from WorkItems where [System.AssignedTo] = @me and [System.State] <> 'Closed' and [System.WorkItemType] <> 'Shared Steps' order by [Microsoft.VSTS.Common.StackRank], [Microsoft.VSTS.Common.Priority], [System.WorkItemType], [System.Id]";
18
19    public static void main(String[] args) throws Exception {
20        Growl growl = new Growl("Team Foundation Server");
21        growl.setImageFile("/tmp/tee.icns");
22
23        Set<WorkItem> workItems = new HashSet<WorkItem>();
24
25        /* Build a connection profile */
26        Profile profile = new Profile();
27        profile.addValue(ProfileProperties.SERVER_URL,
28            "http://tfs.contoso.com:8080/tfs/DefaultCollection");
29        profile.addValue(ProfileProperties.USER_NAME, "username");
30        profile.addValue(ProfileProperties.USER_DOMAIN, "DOMAIN");
31        profile.addValue(ProfileProperties.PASSWORD, "Password");
32
33        /* Get the work item SOAP service */
34        WorkItemClient wit = new TFSTeamProjectCollection(profile)
35            .getWorkItemClient();
36
37        /*
38         * Set up the initial set of work items - anything created
39         * after this initial query will be notified for.
40         */
41        WorkItemCollection results = wit.query(WIQL);
42
43        for (int i = 0; i < results.size(); i++) {
44            workItems.add(results.getWorkItem(i));
45        }
46
47        while (true) {
48            results = wit.query(WIQL);
49
50            for (int i = 0; i < results.size(); i++) {
51                WorkItem workItem = results.getWorkItem(i);
52
53                /*
54                 * This work item hasn't been seen - pop a notification.
55                 */
56                if (!workItems.contains(workItem)) {
57                    growl.notify("The work item " + workItem.getTitle()
58                        + " was created.");
59
60                    workItems.add(workItem);
61                }
62            }
63
64            Thread.sleep(10000);
65        }
66    }
67
68    private static class Growl
69    {
70        private final String applicationName;
71
72        public Growl(final String applicationName) {
73            this.applicationName = applicationName;
74        }
75
76        public void notify(final String message) {
77            final String[] growlArguments = new String[5];
78            growlArguments[0] = "/usr/local/bin/growlnotify";
79            growlArguments[1] = "-n";
80            growlArguments[2] = applicationName;
81            growlArguments[3] = "-m";
82            growlArguments[4] = message;
83
84            try
85            {
86                Runtime.getRuntime().exec(growlArguments);
87            }
88            catch (Exception e)
89            {
90                e.printStackTrace();
91            }
92        }
93    }
94}