Moving Your Cygwin Installation

I was running out of space on one drive so I decided to move my Cygwin installation to another drive.

It turned out not to be too difficult thanks to this article Hints for Setting up Cygwin.  This is how I did it though there might be a simpler way.

  1. In a bash shell save the mount points as a batch script.
    $ mount -m > /c/cygwin-mount.bat
  2. Close the bash shell.
  3. Move the cygwin folder from one drive to the other.  In my case that was from C:\cygwin to D:\cygwin.
  4. Update all the short cuts for Cygwin in the Start Menu to use the new drive.
  5. Update  cygwin.bat, which is in the top level of your Cygwin installation, to use the new drive.
  6. Update your Windows environment variable to use the new Cygwin bin path, e.g. D:\cygwin\bin.
  7. Open a DOS cmd prompt.  Run umount to unmount the old Cygwin mounts.
    > umount
  8. Next run the mount points script.
    > C:\cygwin-mount.bat
  9. Open a Cygwin bash shell to make sure everything is working correctly.

Do Not Return From a Try Block

Practical Java(TM) Programming Language Guide (Addison-Wesley Professional Computing Series)In Peter Hagar’s book, Practical Java, he recommends that you do not return from a try block.  This is because the finally block may change the return value.

Traditionally, programmers think that when they execute a return statement they immediately leave the method they are executing.  In Java, this is no longer true with the usage of finally

To avoid this pitfall, be sure you do not issue a return, break or continue statement inside of a try block. If you cannot avoid this, be sure the existence of a finally does not change the return value of the method. This particular problem can arise during maintenance of your code, even with careful design and implementation. Good comments and careful code reviews ward it off.

Practical Java Programming Language … – Google Book Search

URL.equals and hashCode make blocking Internet connections

Who knew that something as innocent as java.net.URL.equals and hashCode would make blocking Internet connections?

The javadoc of URL.equals says: “Since hosts comparison requires name resolution, this operation is a blocking operation.”, but who reads the documentation of equals?  There is a general contract around equals.  Joshua Bloch writes in Effective Java: “Don’t write an equals that relies on unreliable resources” (Chapter 3, page 34). Hey Sun, as far as I know, the Internet is not reliable 😉

Eclipse and Java Blog by Michael Scharf: java.net.URL.equals and hashCode make (blocking) Internet connections….

WordPress Impressive Permalink Functionality

A long time ago I enabled the WordPress permalink functionality so that the links for my blog articles were somewhat human readable.  I used my own custom format, /%category%/%year%/%monthnum%/%day%/%postname%/.

Awhile ago I began regretting that format because if I changed the category of the article then the URL would change and any bookmarks to that article would be broken.

Today I started reorganizing a lot of categories and decided it was time to bite the bullter.  I changed the permalink format to the standard /%year%/%monthnum%/%day%/%postname%/.   What was so impressive is that the old URL’s still worked, they redirected to the new URL.  WordPress rocks.

Mac Hibernate

I tried to make the Mac hibernate based on this article, Make my Macbook Pro Hibernate. Unfortunately it did not work and I am wondering if it is because this article is older and maybe it does not work for Mac OS X Leopard.

In the end I have gotten used to not having hibernate on the Mac since start up and shutdown are so fast.

ServletException root cause

Java’s Throwable class defines the getCause() method for accessing the cause of the exception.  This method returns a Throwable object which itself could have a cause.  By traversing down this chain you can find the root cause of an exception.

However for some unknown reason in ServletException the getRootCause() method was added.  Therefore when trying to determine the root cause of an exception in a J2EE environment one has to check what type of exception you have.  I do this in the following code.

  /**
   * Logs all the nested exceptions for the specified exception.
   *
   * @param ex the exception
   */
  protected void logNestedExceptions(Throwable ex) {
    int count = 1;
    Throwable cause = getCause(ex);
    while (cause != null) {
      logger.error("Nested Exception " + count, cause);
      cause = getCause(cause);
      count++;
    }
  }

  /**
   * Gets the cause of the exception.
   *
   * @param ex the exception
   * @return the cause
   */
  protected Throwable getCause(Throwable ex) {
    Throwable cause;
    if (ex instanceof ServletException) {
      ServletException sex = (ServletException) ex;
      cause = sex.getRootCause();
    }
    else {
      cause = ex.getCause();
    }
    return cause;
  }

Finally you need to configure web.xml to use your SiteMap servlet.

<servlet>
    <servlet-name>sitemap</servlet-name>
    <servlet-class>com.betweengo.servlet.SiteMap</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>sitemap</servlet-name>
    <url-pattern>/sitemap.xml</url-pattern>
</servlet-mapping>

Perforce Review

You can have Perforce alert you about changes to sections of the depot.

To do this using the Perforce GUI.

  1. Go to User -> Edit <your name>…
  2. Add the branches in the Reviews section that you want to monitor.  For example:
    //depot/main/.../pom.xml
    //depot/main/com/betweengo/foo/...
    //depot/proj/.../bar/...

Thanks to fellow developer Bill Crook for the tip.

Update: Michael Delaney, Perforce guru, pointed out that this works only if a P4Review daemon is running which it is not by default.

Extracting Text From PDF Files

From the Mac Tricks and Tips blog I saw this interesting article, Extracting Text From PDF Files.

  1. Open Automator, found in Applications > Utilities
  2. In Automator drag out “Get Selected Finder Items”, then “Extract PDF Text”. I recommend changing the output option to Rich Text.
  3. Save the file. Either as an application or a file.
  4. Test. Drag a PDF file onto the Automator file and let it do its work, after a short time you will have a text file with the extracted text.

Deconstructing software to find bugs

In the September/October 2008 Technology Review I read a short article on Seth Hallem, one of the TR35.  The subtitle of this article was “Deconstructing software to find bugs.”

As a graduate student at Stanford, Seth Hallem perfected an improved approach to finding bugs, called static analysis…  Hallem developed algoritems to … examine only the most important combinations, allowing millions of lines of code to be examined quickly and efficiently.  He cofounded Coverity in San Francisco.