Archive for the ‘Code Snippets’ Category

BlackBerry Application OTA Download Setup

While working on my latest project, BlackPing, I ran into another snag - I couldn’t get my application to download Over-The-Air (OTA).  The problem was due to my lack of understanding, hopefully my trouble is your gain.  Below you’ll find instructions that guide you through preparing your application for OTA downloads.

Setup the HTTP Servers MIME Types

If you do not have administrator access to your web server then you’ll need help from the administrator to perform the following.

Verify that there are MIME Types setup for COD and JAD files.  The correct MIME Types are respectively “application/vnd.rim.cod” and “text/vnd.sun.j2me.app-descriptor”

Upload the Application to the Server

You’ll need to upload the application’s COD, JAD and JAR files to the web server.

Create an HTML “Download” Page

This is the part that I spent a while figuring out.  During my first attempts at OTA installation I opened the BB Browser and went directly to the application’s JAD file.  Each time I did this the browser displayed the contents of the file instead of trying to download and install it.

To get around this issue, create a “download” page.  I recommend a simple page that way you don’t have to worry about the page renders on varios BlackBerry devices.  This page should reside in the same place as the application files, but you can place it whereever you want, you’ll just have to do more configuration.

My simple download page:

<html>
<head>
<Title>Download [APPLICATION_NAME]</Title>
</head>
<body>
<a href=”[APPLICATION_NAME].jad”>Download</a>
</body>
<html>


I hope this post saves you some trouble.  If it does, or if you have questions, please leave a comment.

Sunday, March 1st, 2009

BlackBerry Code Signing Help

I ran into a little trouble with BlackBerry’s code signing scheme.  The root problem was that Windows didn’t know what program to use when opening a .CSI file.  I feel this is a problem with the BlackBerry JDE Plug-In for Eclipse installation, but I still needed a resolution.  The tech support team at RIM referred me to a knowledge base article which pointed me in the right direction.  Since I had a compatible Sun JDK installed, and I didn’t want to re-install RIM’s tools again, I opted for the command line method.  Since you have to sign three different keys, I created a batch file to make things easier.

The batch file I created is below.  Please look at the simple code since you may have to change some of the paths to make them appropriate for your system.  Once you create the file, place it in the same directories as your .CSI files and then you’ll be able to simply drag-and-drop the .CSI file “into” the batch file.  You’ll then be prompted for all the information RIM needs to register your keys.

javaw -jar “C:\Program Files\Research In Motion\BlackBerry JDE 4.7.0\bin\signaturetool.jar” %1
pause

Now that your CSI files are signed, you need to register your application.  In Eclipse, you go to Eclipse >> Request Signatures.  Then you select each record in the table with a status of “Not Registered” and click the “Request” button.  This is where I, again, run into trouble.  Check back later, hopefully tomorrow, when I write up instructions on registering your application.

Monday, February 23rd, 2009

A Reflective toString Method for Java

Often times I find the debugger to onerous for viewing DAOs. They usually contain lots, and lots of attributes and viewing those attributes is tedious, at least in my opinion. I prefer looking at them as text output. With text I can quickly scan through them or copy them or do any number of other things. With the debugger, their values reside strictly in the debugger.

My first reaction has been to create a toString method. This works rather well but it too is tedious. So I’ve created a toString method that uses reflection to create the output. The code for this method is located at the bottom of this post.

Some things to note:

  • This code has been created for my own use. Therefore you won’t have some of the objects I reference.
  • This code is free for all uses. If you’d like to use it commercially or for your own pet projects feel free to do so. Although I wouldn’t mind a mention (or a link) in the Javadoc or on the application’s web page if you see fit to do so.
  • This code is by no means feature complete. For example, I imagine it could be recursive so that when an object is encountered, this method is called instead of the Object’s toString method.
  • I also notice that my escape characters are no longer escaped. This means that you’ll have to add your own backslashes.
  • This code uses StringBuffer when StringBuilder would be more appropriate.  I used StringBuffer because of constraints that were put on me when I originally developed it. (If you’re interested in the differences between these two check out: http://forum.java.sun.com/thread.jspa?threadID=652378)

The Code

    public static String toString(Object a_oToConvert)
    {

         if(a_oToConvert == null)
         {
              return "Object is null";
         }

         StringBuffer sb = new StringBuffer();

         sb.append("n");

         sb.append("t" + Util.DASHES + "n");
         sb.append("t" + a_oToConvert.getClass().getName() + "n");
         sb.append("t" + Util.DASHES + "n");

         Class cls = a_oToConvert.getClass();

         Field[] arrFields = cls.getDeclaredFields();

         for(int idxField = 0; idxField < arrFields.length; idxField++)
         {

              Field fldCurrent = arrFields[idxField];

              String sName = fldCurrent.getName();

              Object oVal = new String("---");

              char[] arrChars = sName.toCharArray();
              char[] arrMod = new char[arrChars.length];

              for(int x = 3; x < arrChars.length; x++)
              {
            	  arrMod[x - 3] = arrChars[x];
              }

              String sFirstChar = new String("" + arrMod[0]);
              arrMod[0] = sFirstChar.toUpperCase().toCharArray()[0];

              String sGetMethod = "get" + new String(arrMod).trim();

              try
              {
                   Method methGet = cls.getMethod(sGetMethod, null);

                   if(methGet != null)
                   {
                        oVal = methGet.invoke(a_oToConvert, null);
                   }

                   if(oVal == null)
                   {
                        oVal = new String("Value is null");
                   }
                   else
                   {
                        if(oVal instanceof List)
                        {
                             oVal = "List Size: " + ((List)oVal).size();
                        }
                   }

              }
              catch(NoSuchMethodException nme)
              {
                   oVal = "No such getter - " + sGetMethod + "()";
              }
              catch(IllegalAccessException iae)
              {
                   oVal = "Not public - " + sGetMethod + "()";
              }
              catch(InvocationTargetException ite)
              {
                   oVal = "ITE" + ite.getLocalizedMessage();
              }

              sb.append("t" + Util.beautify(sName, oVal.toString()) + "n");

         }

         sb.append("t" + Util.DASHES);

         return sb.toString();

    }

Monday, February 18th, 2008

A Struts Compatible OpenID Authenticator for OpenId4Java

While working on a side project of mine, I decided that I wanted to allow users to authenticate themselves using OpenId. Being a Java programmer, who is reluctant to re-invent the wheel, I immediately searched Google in hopes that someone had already created a Java library I could use. After a few moments I came across OpenId4Java. These guys created a wonderful product which made implementation a breeze. It’s also worth noting that the support is pretty amazing as well.

After playing around with the code a little bit, I noticed that the sample code didn’t play too well with my Struts application. So I wrapped their code into my own class OpenIdAuthenticator. This class simply wraps the logic from the sample code to make it compatible with Struts.

Feel free to use the class for any purpose - commercial, private, etc. If you so desire, please give me some credit somewhere in the Javadoc.

Sunday, January 27th, 2008