Blind Squirrel

Once in a while, even a blind squirrel finds a nut.

Embedding HTML Document in an IFRAME With GWT

While working on a recent project in GWT, I needed to embed a full HTML document inside an IFRAME. And I didn’t want to specify a remote URL for the IFRAME - I actually wanted to shove the HTML content directly into the IFRAME.

Subversion: File or Directory Is Out of Date; Try Updating

I recently started encountering a strange problem with Subversion on one of my machines. In larger commits, I would suddenly get an error:

1
2
3
4
5
$ svn ci -m "My commit message."
Sending        src/Foo.java
svn: Commit failed (details follow):
svn: File or directory 'src/Foo.java' is out of date; try updating
svn: resource out of date; try updating

Although I was positive the file(s) were up to date, I ran an svn up just to be safe:

1
2
$ svn up
At revision 10803.

Nothing changed. If I tried to commit again, I would get the same out of date error. Eventually I figured out that if I delete the folder containing the problematic file, ran svn up to restore the working copy and re-edited (or restored from a backup) the modified file, the problem went away. This suggested an issue with the Subversion metadata, but this blog post from Pageworthy describes a much easier work around:

Delete the all-wcprops file from the .svn metadata folder for the offending file.

In other words, if Subversion complains about src/com/mycompany/Foo.java, then delete src/com/mycompany/.svn/all-wcprops.

  • OS: Mac 10.6
  • IDE: Eclipse 20090920-1017
  • Subversive Team Provider: 0.7.8
  • Subversive Connectors: 2.2.1
  • SVNKit 1.3.0: 2.2.1
  • Subversion: 1.6.6

Kubuntu 9.10 Dual Monitors

A few months back I got a new 12.1” System76 Darter laptop and I finally decided to setup dual monitors on Kubuntu 9.10 (Karmic Koala).What I wanted was to have my laptop LCD on the left at 1280x800 and my ViewSonic LCD panel on the right running at 1680x1050. Apparently I’m old-skool, because I’m used to hacking away at my xorg.conf just to get dual monitor support to work under Linux. Somehow I never knew about xrandr, KRandR, etc.

Spring PropertyPlaceholderConfigurer Beans

So I found a scenario today where we had two different PropertyPlaceHolderConfigurer beans declared in two separate XML files. Unfortunately, the second property file wasn’t getting loaded, so we would get errors about Spring being unable to resolve particular properties.

I found this great post summarizing the solution:

http://dotal.wordpress.com/2007/09/14/mulitple-propertyplaceholderconfigurer-configurations/

Basically, you have to update the definition for the first PropertyPlaceHolderConfigurer bean that gets loaded and add the ignoreUnresolvablePlaceholders property:

1
2
3
4
<bean id="mailProps" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
         <property name="location" value="classpath:mail.properties"/>
         <property name="ignoreUnresolvablePlaceholders" value="true"/>
</bean> 

DRY Associations in Rails

I found a nice little article from a few months ago on The Rails Way. Written by koz, Assocation Proxies talks about some Best Practices for using assocations within Rails. I really felt the suggestions hit the proverbial nail on the head in terms of following the DRY principles. The first suggestion was on the best way to restrict access to user specific information. It’s incredibly useful to take advantage your RESTful patterns and make a single call like:

1
@todo_list = current_user.todo_lists.find(params[:id])

Of course, I also loved the final suggestion in Case Three: using a has_many with a condition on it:

1
has_many :completed_lists, :class_name => "TodoList", :conditions => { :completed => true }

Excellent!

Struts Redirect w/Parameters Part 2

In an earlier post, I described a class to handle redirects in Struts and passing parameters along. That technique is not necessary; as of Struts 1.2.7, you can use the ActionRedirect class instead.

Here’s a basic example from inside the execute method in an Action.

1
2
3
4
5
ActionRedirect redirect = new ActionRedirect(mapping.findForward("success"));
redirect.addParameter("myparam","myvalue");
redirect.addParameter("something","fornothing");
redirect.addParameter("answer","42");
return redirect;

The ActionRedirect class is basically the same as my ForwardParameter class and is probably preferable.

Exception Handling Framework for J2EE Applications

OnJava has an interesting article on generic approach to handling exceptions within a J2EE framework. The basic idea involves creating a generic base class for exceptions and a basic handler for all requests that includes dispatcher to pass the request along to the appropriate method.The fundamental idea is fairly sound and the idea of including a context to allow reuse of generic exceptions seems like a strong idea.

My concern is that in a large project, the use of a “context” may make it difficult to figure out which specific message code is used in a given place. The generic exception handling is especially beneficial for the “this should never happen” cases where there isn’t much the app can do about the exception, but should still handle it in a robust manner.

Struts Redirect w/Parameters

If you have a Struts Action servlet and you want to redirect to another page, the standard Struts technique is to return an ActionForward and setup an appropriate forward entry in struts-config.xml:

1
return mapping.findForward("success");

Unfortunately, this doesn’t provide a mechanism for passing request parameters to the target. So what can you do if you want to redirect to another page and pass some parameters along? You use an additional class:

ForwardParameters.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
public class ForwardParameters
{
  private Map params = null;

  public ForwardParameters()
  {
    params = new HashMap();
  }

  /**
   * Add a single parameter and value.
   *
   * @param paramName     Parameter name
   * @param paramValue    Parameter value
   *
   * @return A reference to this object.
   */
  public ForwardParameters add(String paramName, String paramValue)
  {
    params.put(paramName,paramValue);
    return this;
  }

  /**
   * Add a set of parameters and values.
   *
   * @param paramMap  Map containing parameter names and values.
   *
   * @return A reference to this object.
   */
  public ForwardParameters add(Map paramMap)
  {
    Iterator itr = paramMap.keySet().iterator();
    while (itr.hasNext())
    {
      String paramName = (String) itr.next();
      params.put(paramName, paramMap.get(paramName));
    }

    return this;
  }

  /**
   * Add parameters to a provided ActionForward.
   *
   * @param forward    The ActionForward object to add parameters to.
   *
   * @return ActionForward with parameters added to the URL.
   */
  public ActionForward forward(ActionForward forward)
  {
    StringBuffer path = new StringBuffer(forward.getPath());

    Iterator itr = params.entrySet().iterator();
    if (itr.hasNext())
    {
      //add first parameter, if avaliable
      Map.Entry entry = (Map.Entry) itr.next();
      path.append("?" + entry.getKey() + "=" + entry.getValue());

      //add other parameters
      while (itr.hasNext())
      {
        entry = (Map.Entry) itr.next();
        path.append("&amp;" + entry.getKey() + "=" + entry.getValue());
      }
    }

    return new ActionForward(path.toString());
  }
}

Here are some examples of using ForwardParameters:

Long form:

1
2
3
4
5
ActionForward forward = mapping.findForward("success");
ForwardParameters fwdParams = new ForwardParameters();
fwdParams.add("myparam", "myvalue");
fwdParams.add("something", "fornothing");
return fwdParams.forward(forward);

Concise form:

1
2
ActionForward forward = mapping.findForward("success");
return new ForwardParameters().add("myparam", "myvalue").add("something", "fornothing").forward(forward);

Ultra concise form:

1
return new ForwardParameters().add("myparam", "myvalue").add("something", "fornothing").forward(mapping.findForward("success"));

This is based on a suggestion I found on Experts-Exchange by dualsoul.


Edit June 5th, 2006:

As someone just pointed out to me this morning, you can use the ActionRedirect class instead of my ForwardParameters class. According to the JavaDocs, ActionRedirect was added in Struts 1.2.7 and is:

… a subclass of ActionForward which is designed for use in redirecting requests, with support for adding parameters at runtime.

JavaDocs

Prefactoring

Prefactoring is the process of using your experience to write better code. Specifically, writing code that can be refactored more easily, that is easier to read and easier to maintain.

The article is available on oreillynet.com:

What Is Prefactoring? by Ken Pugh, author of Prefactoring.