kenn
Random musings and opinions of a software architect.

Proxy Servlet for GWT in devmode

One of the problems with using GWT is that when you run in devmode, GWT manages its own instance of a Jetty server (I think it’s a Jetty server anyway). Unless you want to use GWT’s own server implementation, and perhaps even run in on Google AppEngine, you’re going to run into the same origin policy. In order to get around this problem, you have to set up a proxy server of some sort. I found a bit of servlet code written by Stou Sandalski which allows you to do this, made to solve the exact same problem in fact. However, I wanted a proxy which would not only support GET and POST requests, but basically allow me to do whatever I wanted. I also wanted it to send all request headers across to the target server. The only dependency is Apache HttpCore and HttpClient.
read more »

Installing dependencies with Apache Ant

I’ve recently been messing around with GWT and Mercurial, and came across the age old dilemma: “add libraries to version control or not …”. On one hand it’s easier to deploy and get new people to use it, on the other hand it’s pretty redundant and down right ugly. So, the only other solutions are A: tell people to install it manually, which is bound to end in tears, anger, and frustration; or B: make an auto installation script. I’ve written a few of these before, but it’s a horribly dull task. So with the GWT’s SDK pretty much forcing me to use Ant anyway, I thought it might be up for the job, and here is the build.xml script I came up with:

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <project name="test" default="build" basedir=".">
  3. <property name="dir.lib" value="lib" />
  4.  
  5. <!-- GWT version, source, and destination -->
  6. <property name="gwt.version" value="2.0.3" /> <!-- Version number for easy reference -->
  7. <property name="gwt.depfolder" value="gwt-${gwt.version}" /> <!-- The folder that the archive will produce -->
  8. <property name="gwt.deparchive" value="${gwt.depfolder}.zip" /> <!-- Filename of the archive, both local and on server -->
  9. <property name="gwt.depsource" value="http://google-web-toolkit.googlecode.com/files/${gwt.deparchive}" /> <!-- The URL to fetch the archive from -->
  10. <property name="gwt.sdk" location="${dir.lib}/gwt" /> <!-- Final location -->
  11.  
  12. <target name="installdeps" description="Install dependencies">
  13. <delete dir="${gwt.sdk}" />
  14. <get src="${gwt.depsource}" dest="${dir.lib}/${gwt.deparchive}" usetimestamp="true" />
  15. <unzip src="${dir.lib}/${gwt.deparchive}" dest="${dir.lib}" />
  16. <move file="${dir.lib}/${gwt.depfolder}" tofile="${gwt.sdk}" />
  17. <!-- <symlink link="${gwt.sdk}" resource="${dir.lib}/gwt-${gwt.version}" /> -->
  18. </target>
  19. </project>
plain

The script is actually quite simple:

  • Line 6-10: Figure out where to get it from and where to put it. This might look a bit complicated, but it should allow you to control pretty much anything from those five lines, leaving the actual target to do its work.
  • Line 12: The actual Ant target. This will be run when you execute ant installdeps.
  • Line 13: Delete the old SDK directory.
  • Line 14: Fetch the zip file from the source specified in line 9, unless the file already exists or is older than the one on the server.
  • Line 15: Extract into lib folder.
  • Line 16: Move it to a generic folder from where it can be easily accessed. I personally like symlinks (line 17), but I know they can be a pain to deal with on Windows, so I opted for just moving it.

Type-checking in Javascript

A while back I wrote a few functions to reliably determine types in javascript. The reason I made these functions is because javascript’s typeof operator doesn’t always return what you want. The main idiosyncrasy is typeof [] and typeof null both returning "object".

At the time of coding, I also wanted a function to tell me whether something was an integer, something javascript doesn’t support natively on its own. Of course, it doesn’t actually tell you if it’s an integer, merely whether it has a fraction part.

  1. function isInteger(val) {
  2. return isNumber(val) && Math.floor(val) == val;
  3. }
  4.  
  5. function isNumber(val) {
  6. return typeof val == "number";
  7. }
  8.  
  9. function isString(val) {
  10. return typeof val == "string";
  11. }
  12.  
  13. function isBoolean(val) {
  14. return typeof val == "boolean";
  15. }
  16.  
  17. function isObject(val) {
  18. return typeof val == "object" && val !== null;
  19. }
  20.  
  21. function isArray(val) {
  22. return val instanceof Array;
  23. }
  24.  
  25. function isFunction(val) {
  26. return typeof val == "function";
  27. }
plain