title: Keeping a counter for execution of java application
body: <p>Here is the scenario:</p>
  
  <p>I am running a Java Application which makes a call to REST interface, and gets back some states of variables as integers (Basically LAST Successful Build # form Teamcity)</p>
  
  <p>Now, since i am running the utility from Jenkins, i want to compare the LastSuccessfulBuild Number between two REST Calls. What is the Best way to do this ? Here is what i have in terms of code.</p>
  
  <pre><code>import java.io.File;
  import java.io.FileInputStream;
  import java.text.DateFormat;
  import java.text.SimpleDateFormat;
  import java.util.Date;
  import java.util.Properties;
  import java.util.TimeZone;
  
  import com.sun.jersey.api.client.Client;
  import com.sun.jersey.api.client.ClientResponse;
  import com.sun.jersey.api.client.WebResource;
  import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;
  import org.apache.commons.configuration.PropertiesConfiguration;
  import org.apache.commons.io.FileUtils;
  
  public class LastSuccessBuildNum {
  
      private  final static String getDateTime()
      {
          DateFormat df = new SimpleDateFormat("yyyy-MM-dd_hh:mm:ss");
          df.setTimeZone(TimeZone.getTimeZone("EST"));
          return df.format(new Date());
      }
  
      public static void main(String[] args) {
  
          try {
  
              Client client = Client.create();
              Properties properties = new Properties();
              File f = File.createTempFile("default", ".properties");
  
              properties.load(new FileInputStream("config.properties"));
  
              String currentDir = System.getProperty("user.dir");
  
  
              String username = properties.getProperty("username");
  
              String pass = properties.getProperty("password");
              //System.out.println(pass);
              String buildNumber = properties.getProperty("buildNumber");
  
  
              client.addFilter(new HTTPBasicAuthFilter("username", "password"));
  
              WebResource webResource = client
                      .resource("http://a.b.c.d/teamcity/app/rest/buildTypes/id:bt26/builds/status:SUCCESS/number");
              ClientResponse response = webResource.accept("text/plain").get(
                      ClientResponse.class);
  
              if (response.getStatus() != 200) {
                  throw new RuntimeException("Failed : HTTP error code : "
                          + response.getStatus());
              }
  
              /**
               * Prepend timestamp to latestBuildNum
               */
  
              System.out.println(getDateTime());
              String latestBuildNum_1 = response.getEntity(String.class);
  
              //System.out.println("Output from Server .... \n");
  
              /** 
               * Save latest build number to a local file
               */
              //System.out.println(latestBuildNum);
  
              String datelbn = (getDateTime() + "-" + latestBuildNum_1);
              //System.out.println(datelbn);
              //If latestBuildNumberLocal &lt; latestBuildNum, then save to file latestBuildNum
              // Else do nothing
              FileUtils.writeStringToFile(new File("PreMDNSlatestSuccBuildNum.txt"), latestBuildNum_1);
              /**
               * Get Content from the file, which would be the LatestBuildNumber at LAST run.
               * SO the NEXT run, will save the number in a different file.
               * Then we compare the contents of these two files
               * using StringToInt() and compare
               */
  
          } catch (Exception e) {
  
              e.printStackTrace();
  
          }
  
      }
  
  }
  </code></pre>