Monday, December 5, 2011

Reading Spring Controller and RequestMapping Definitions

My current, major task at work is to bring two existing web applications into one utilizing a single-signon mechanism and overhaul the UI based on my recommendations previously made.  Part of the background architecture work involved in allowing the creation of a menu structure that would ultimately be utilized to create a similar structure in the UI.

I wanted to use JAXB to unmarshall my menu XML into Java objects but I suspect the complex nature of the XML schema and some limitations of the JAXB framework combined with my lack of a complete understanding of JAXB hindered this process to such a degree that I had to stop due to the time I was spending on it.  So, instead of getting the created objects "for free" I had to resort to writing my own SAX Parser event handler to build up the menu structure in Java land.  I had tried a DOM-based approach first, but the recursive nature of the elements made me cringe a bit while writing the code and if you ever have the thought of "there's got to be an easier way to do this" while writing code, your best bet is to just flat out stop what you are doing and take some time to thoroughly think things through before spending any more time on the task.

The event handler also built up the menu item URI paths using attributes on the hierarchical menu structure in the menu XML.  This gave the menu item actions (arbitrary) name-spacing.  I say arbitrary because when using Spring MVC with only one DispatcherServlet, the URI paths are essentially meaningless.

One other thing I wanted to ensure was that we would never have a dangling menu item - that is a menu item defined (and in the UI) but not actually tied back to a Spring Controller method.  So, I created a unit test that loads my menu XML, pulls a list of all the RequestMapping annotated methods, and confirms that every menu item ties back to a server-side method.

Here's the code:


  Map<String, Object> controllers =
    this.ctx.getBeansWithAnnotation(Controller.class);
  for(Map.Entry<String, Object> entry : controllers.entrySet()) {

    Class controllerClass = entry.getValue().getClass();
    for(Method method : controllerClass.getDeclaredMethods()) {

      RequestMapping rm =
        method.getAnnotation(RequestMapping.class);
      if(rm == null) {
        continue;
      }
      else {

        String[] vals = rm.value();
        if (vals.length == 1) {
          this.mappingSet.add(vals[0]);
        }
        else {
          String msg =
            "this test assumes only one request mapping per method.  " +
            "check class: " + controllerClass + " and method: " + method;
          Assert.fail(msg);
        }
      }
    }
  }


Two notes:

  1. The reference "this.ctx" refers to a Spring ApplicationContext, in my case here an instance of the XmlWebApplicationContext class that loads my Spring configuration.
  2. The reference "this.mappingSet" is simply a class-local field (HashSet<String>) in which I simply store the RequestMapping values.
Finally, after this is complete I confirm that every menu item action URI is contained in the set of extracted RequestMapping values.

Friday, December 2, 2011

Five Weeks Post-Op

Today I'm five weeks out from knee surgery and it's doing awesome.  I started swimming again and after a hectic November travel schedule with a fifth anniversary vacation to Jamaica followed five days later by a trip to the in-laws for Thanksgiving I hope to establish more of a training routine because quite frankly I've lost my fitness and put on a little extra weight.  I'm not so much worried about the weight because it was gained from doing nothing for the first three weeks after the surgery and I gained nothing over Thanksgiving (a rare feat).  But I am disappointed about the loss of fitness because it's so damn hard to get back at first, i.e., the first week or two suck.

I remember when I took four months off after IM CDA in 2009 to let my foot fully heal that the first week or two I struggled to even run a mile.  Now that all the travelling is out of my schedule for the foreseeable future, I can start a training schedule again:  Master's swimming 2x a week and some trainer time.  I've been allowed to ride the trainer, but haven't gotten around to even setting it up yet.  This weekend for sure....along with all the other crap I have on my list.

Tuesday, September 20, 2011

Busy Summer: Injury and Fatherhood

There won't be a part II.  It's been too long and I really don't want to relive that day again.  Psychologists would say the more you talk about a thing, the less that thing holds power over you.  Yea, I can live with that for now.

My first race of the season was also my last race.  About a week prior to my first race, the Summer Open Triathlon in mid-May, I started having right knee issues.  I figured I could fix it on my own but it just never got better.  I saw Matt at Altitude for a ton of sessions and got to the point where I was able to run again pain free but it didn't last.  I've now not run since early August and now the pain manifests itself randomly after bike rides.

I've officially hung up the tri and road bikes opting for some quality mountain biking.  It was clear from the first few rides that being fearless on a road bike is easy but being fearless on a mountain bike requires cojones I don't know that I will ever possess.  That said, my bike handling skills are improving and I have been able to navigate some rough terrain with increasing ease.  I suspect this will change when I have my first wipeout, but so far, my clothes have remained clean.

This past Saturday I did an epic, 2.5 hour trail ride covering some 30 miles.  I don't have the climbing data, but I'm sure it was in the multiple thousands of feet.  That afternoon, I could barely walk because of my knee.  Sunday was a bit rough as well and I would have to randomly do some stretches to get it to stop hurting.

Recalling something Uli mentioned at Master's swim about how, possibly, bike position might aggravate the injury I made an appointment to see Mark Plaatjes to get a different take on the injury and fixing it.

Fatherhood has been fantastic.  It's really cool to see the development of a child - to see when they first start learning even the most basic of things, like smiling.  It's also a lot of work because they require nearly constant attention when awake which can be very mentally draining.  Elin is sitting up now and rolls around quite a bit.  She's starting to figure out how to pull her knees up under her and I suspect she will be crawling within a few months.