Java

You are currently browsing articles tagged Java.

I came across this TeamCity Twitter plugin via StackOverflow the idea is to Tweet the build status as it changes. Pretty neat idea, however it will only run under Java 1.6. I tried to install on our build server at work but the TeamCity version we are running doesn’t run under Java 1.6 There was only one thing to do, write my own that will work under Java 1.5. It has to do authenticated POST request to the Twitter REST API. The build Twitter account can be found here, not that your that interested in our build Twitter feed :)

//
import java.io.*;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.net.URLEncoder;
import java.net.URLConnection;
//
import jetbrains.buildServer.serverSide.BuildServerAdapter;
import jetbrains.buildServer.serverSide.SBuildServer;
import jetbrains.buildServer.serverSide.SRunningBuild;
import jetbrains.buildServer.messages.Status;
//
import com.intellij.openapi.diagnostic.Logger;

// Tweets build start / finish to the build account
public class TwitterNotifier extends BuildServerAdapter
{
   private final static Logger LOG = Logger.getInstance(TwitterNotifier.class.getName());
   
   
   // Constructor, register the listener ( this )
   public TwitterNotifier(SBuildServer aBuildServer)
   {
       LOG.info(“*** Notifier adding listener=” + this );

       // Register with TC
       aBuildServer.addListener(this);
   }

   // Build started, set the version number
   public void buildStarted(SRunningBuild build)
   {
       String sTcBuildNumber = build.getBuildNumber();
       sendTweet( “Build “ + sTcBuildNumber + ” started on “ + build.getAgentName() );
   }

   public void buildChangedStatus(SRunningBuild build, Status oldStatus, Status newStatus)
   {
           String sTcBuildNumber = build.getBuildNumber();
       sendTweet( “Build “ + sTcBuildNumber + ” status, “ + newStatus.getText() );
   }
     
   public void buildInterrupted(SRunningBuild build)
   {
           String sTcBuildNumber = build.getBuildNumber();
       sendTweet( “Build “ + sTcBuildNumber + ” interrupted status, “ + build.getBuildStatus().getText() );
   }
   
   public void buildFinished(SRunningBuild build)
   {
           String sTcBuildNumber = build.getBuildNumber();
       sendTweet( “Build “ + sTcBuildNumber + ” finished, “ + build.getBuildStatus().getText() );          
   }
   
   // Updates the twitter account
   public static void sendTweet( String psText )
   {
           try
           {
               LOG.info( “*** TwitterNotifier plugin, “ + psText );
              
               //  Using paramaters known to work
               String urlString = “http://twitter.com/statuses/update.xml”;
               String data = “status=” + URLEncoder.encode( psText, “UTF-8″);
               String username = “twitteruser”;
               String password = “twitterpassword”;
              
               // 
               Authenticator.setDefault(new MyAuthenticator(username, password));
               // Send data
                URL url = new URL(  urlString );
                URLConnection conn = url.openConnection();
                conn.setDoOutput(true);
                OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
                out.write(data);
                out.flush();
                
                // Get the response
                StringBuffer sb = new StringBuffer();
                BufferedReader readIn = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                String line;
                while ((line = readIn.readLine()) != null)
                        sb.append(line + \n );

                //
                //System.out.println( "*** Response=" + sb.toString() );
                
                // Close       
                out.close();
                readIn.close();
           }
           catch( Exception ex )
           {
               LOG.info( “***ERROR=” + exception2string( ex ) );
           }
   }

   public static String exception2string(Exception e)
   {
      try
      {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        e.printStackTrace(pw);
        return “——\r\n + sw.toString() + “——\r\n;
      }
      catch(Exception ex)
      {
        return “error converting exception to string”;
      }
   }

   
   static class MyAuthenticator extends Authenticator
   {
       private String username, password;

       public MyAuthenticator(String user, String pass)
       {
         username = user;
         password = pass;
       }

       protected PasswordAuthentication getPasswordAuthentication()
       {
         /*
         System.out.println("Requesting Host  : " + getRequestingHost());
         System.out.println("Requesting Port  : " + getRequestingPort());
         System.out.println("Requesting Prompt : " + getRequestingPrompt());
         System.out.println("Requesting Protocol: "
             + getRequestingProtocol());
         System.out.println("Requesting Scheme : " + getRequestingScheme());
         System.out.println("Requesting Site  : " + getRequestingSite());
         */

         return new PasswordAuthentication(username, password.toCharArray());
       }
   }

}  // EOC
 

I went yesterday to see my friend Eric Wendelin speak about javaFX at the BoulderJUG meeting.   Eric did a great job with a fresh and informative presentation.   Also on tap was Tim Berglund giving a talk about Liquibase and Database Refactoring in general, good job.

Since we are on the topic of java, why not check out the new jQuery talk by young Dmitri.

PDC, MS and other stuff

With PDC under way, if your a MS developer you might want to try - Microsoft Pre-release Software Visual Studio 2010 and .NET Framework 4.0 Community Technology Preview (CTP).  This CTP release is available in English only as a Virtual PC image, only 75GB of space needed !

Then there is the Windows Azure Tools for Microsoft Visual Studio October 2008 CTP.

And if you want to .Net MVC here it is.  It turns out the MVC idea isn’t that new at least not from Trygve Reenskaugs standpoint, what does he have to say about it ?  It certainly has been brewing on the Java front for some time.  For example Struts or Ruby etc

And if that wasn’t enough, LiveId is going to support OpenId, that’s a good thing.

In other news, I did not know that Tim Berners-Lee invented the World wide web.

I’m having problem at my hosting place with a java servlet.  The servlet hits another website with authenticated http REST request.  This works fine on my local development box.  However when setting the authentication (Authenticator.setDefault) running at the hosted website it blows up.  Like so

java.security.AccessControlException: access denied (java.net.NetPermission setDefaultAuthenticator) at java.security.AccessControlContext.checkPermission(AccessControlContext.java:269) at java.security.AccessController.checkPermission(AccessController.java:401) at java.lang.SecurityManager.checkPermission(SecurityManager.java:524) at java.net.Authenticator.setDefault(Authenticator.java:84) at
TestServlet.main(
TestServlet.java:48)

It seems to be permission problem on the server.  The hosting place is using Resin.  I’m going to see what they have to say about it.  They might have to adjust the security policy file for me.

I took the old java project from previous post loaded under Eclipse and compiled.   Only got one error, that was broken code anyway.  With that one line commented out I got 429 warnings but no errors, nice.

Started setting up a test configuration at a voice network.   VoiceGenie has been bought by Genesys.  They didn’t have a clear link to the dev section or maybe I just didn’t see it later found it here.  For some reason Tellme does not allow google email address or any free service address, can’t use that.  Voxeo had a clear dev section and got me signed up in a matter of minutes.

I mounted a test application “HelloWorld” of course what else ?   It worked fine out of the box.  Then to try something more daring.   Dusted off an old application “Blackjack”, that ran fine out of the box too.   I’m really surprised pleased to see that an VoiceXml application written in 2001.   Using our java Api ( qIVR ) also from 2001 runs on the current voice network.  That is without changing a single line of code.

If your so inclined you can call the Blackjack application by dialing (720) 897-8933.  If you prefer skype you can use +99000936 9991261498   Maybe you can win a few bucks :)  I have to say being able to use skype to call / test the voice apps is a big plus.

How does it work ? The voice network, Voxeo in this instance takes the phone call.   Then it will go to the URL that your application is located at.   It fetches the voiceXml, then It interacts with the user on the phone as instructed by the voiceXml code you give it.  A simple HelloWorld looks something like this.

<vxml version="2.0" xmlns="http://www.w3.org/2001/vxml">
  <form>
    <block>
      <prompt>
        Hello world!
      </prompt>
    </block>
  </form>
</vxml>

A lot of times apps are served up as static pages.  If your application is more than a handful of pages it becomes hard to handle and maintain.   And that’s exactly why we developed the qIVR “quiver” voiceXml java Api.   qIVR allows you to handle the phone conversation in java code by using java objects.  Without even having to learn voiceXml.   Here is HelloWorld again, qIVR style.

pCall.speak( pCall.factory().qivrPrompt(” Hello world “) );
pCall.hangup();