Pencils Down

This weblog is about my experiences in software development

I like Spring MVC.  It is a great next step in the Spring history of Java/Web development.

I don’t like that Spring has decided that if you redirect from a POST then it unwraps all request parameters and throws them onto all subsequent URLs that are generated.

We have what I think is a fairly common search/results page.  We POST the search criteria when the user changes some aspect and GET to perform manipulations within a search result (such as page, paging, or sorting).

I had been storing the criteria in the model passed to the POST request.  This appears to be the culprit.

If I remove the model setting of the criteria and just store the criteria in the true HTTP session and redirect to my GET handler then everything works correctly.  No ugly URLs with umpteen parameters off the side of the screen.

So, the order of steps is something like:

  1. User changes criteria and POSTS to handler
  2. POST handler stores criteria POJO in the HTTP request object and redirects to same page GET handler
  3. GET handler retrieves criteria from HTTP session and performs updated query

This error is the result of trying to mock a class instead of an interface.  In order to mock a class you have to use the class extensions instead.

So, change your code from:

import static org.easymock.EasyMock.*;

To:

import static org.easymock.classextension.EasyMock.*;

I think it’s just historically how EasyMock got developed:

  1. first interfaces like nice Java/Spring/JUnit theory says your supposed to develop,
  2. then normal classes for the working Java programmers of the world.

Per the display tag library documentation you should be able to specify any properties that you need in the displaytag.properties file OR within the display:table tags in you code.  I have usually opted for that latter as the properties are specific to the table I am programming rather than across the board functionality within your application.

At least one of the properties can not be used this way.  It must be specified in the properties file.  The export class to use, as in:

export.pdf.class=com.myweb.PdfView

It is the only way to get displaytag to invoke your code.  If you run across other properties that are not being picked up, try moving them to the properties file.  Bet it will work that way.

This is a wonderful error message that probably has nothing to do with right parenthesis.  You have probably seen this as a result of some Hibernate coding and wondering how on earth you can have Hibernate generate code with missing parens!

The error really means you have an ORDER BY clause in a subquery.  Oracle can not do this.  The best you can achieve is the use of a WHERE ROWNUM=n clause in the subquery, but any ordering makes Oracle lose it’s mind.

There are theoretical reasons for this which won’t help you get your job done.  Just have to live with the limitation in Oracle.

EasyMock, like other JUnit mock testers, allows you to set expectations for method calls on the mocked object.  However, the expect() method wants to see a return type from the method call.  So, what to do?

A built-in mechanism of EasyMocks is to just “call” the void method in question.  For example a commonly mocked object is the HttpSession:

    expect(request.getSession()).andReturn(session);
    expect(session.getAttribute(name)).andReturn(null);
    session.setAttribute(eq(name), isA(InSession.class));
    replay(session);
    //whatever you are really trying to test
    verify(session);

Could only be easier if the doc told you this clearly.

We have all used SimpleDateFormat for some time.  I think it’s the most prevalent built-in Java package for parsing and formatting dates and times.  Surprise – you may not be using it correctly!

Let’s take a simple example: “MM/dd/yyyy” – a standard US format date string.  You would expect that parsing a date string like 2/31/2011 to throw a ParseException error due to the invalid date.  Wrong!

A little known attribute of SimpleDateFormat is called lenient.  The lenient attribute is defaulted to true.  (I am sure there is some theoretical reason for default true)  Lenient true means the package will attempt to convert/parse whatever you throw at it and take a guess if the string/date doesn’t quite fit the format passed.  So, a date like 2/31 or 29/4 (transpose months and days) produces some date when parsed.

Setting lenient false does what you expect – both dates fail on parse.

Isn’t it fun thinking back about all that code you have written over the years that may not be doing the right thing?

A problem came up the other day that a mapping file had a Where clause that did not appear to be taking effect.  It should have only included records in a result set that had the specific settings in place.

Looking into the where clause there are caveats to it’s use: “specify an arbitrary SQL WHERE condition to be used when retrieving objects of this class”.  Like most of Hibernate documentation, what does that mean?  When does the Where clause come into effect?

There are several such questions floating around the internet.  One good response from the Hibernate team was “The where clause is not used at all during association navigation.”  Meaning if you are just doing normal object references the Where clause has no effect.  You see there is no additional logic to the generated SQL.  He did mention using a Filter later on.

A Filter is plunked in the middle of the mapping file.  It looks just like a Where clause.  It can be used per session.  It is defined in the mapping and can be used in classes or collections as needed.  It works the way you would expect the Where clause to work.

It seems like the Where clause is in there for some historical reason.  It should be deprecated.

Watson Wins Jeapardy

No comments

This week the popular tv game show Jeopardy featured a contest between two human Jeopardy champions and Watson, a mammoth computer from IBM.  Before the games there was some thinking that the humans might be able to compete against Watson.  Kind of like the talk about chess programs when they first started challenging humans.  The thinking was that the game is built upon a lot of nuances that a computer would have a hard time grasping.

Wrong!  Watson kicked butt from the get go.  Watson beat the nearest human rival by a factor of 3.  I’m sure some marketing types at IBM were sweating it out before the games.  Now, with the success, IBM is marketing Watson to solve other complex problems, notably some medical problems that require a vast amount of knowledge to interpret.  Sounds similar to the game show knowledge base.

Radio station disc jockeys all noted the win with humor and a tinge of fear in their voices.  I don’t think it would take a full Watson to replace some dj’s/talk show hosts.  Callers were making references to the Terminator series and other science fiction.  Such fiction portrays the self-awareness of machines and a bad outcome for humans when the computers realized they were better than humans.  One reference expected self-awareness to occur around 2045.

It’s interesting that IBM is marching straight ahead with the win to other big, complex knowledge bases.  I know it’s silly but it reminds me of the line in Jurassic Park, “You were so happy that you could do the science that you never thought about whether you should.”  I wonder if the IBM’ers have some thought in the systems like the Asimov robot rules built in.  At what point do the recommendations coming out of Watson have some filter that checks whether the recommendations might hurt someone?

The other idea floating around in my head is I thought that IBM/United States had lost the lead in supercomputers to the Japanese years ago.  What kind of system have they got floating around that might make Watson look like a child?

If you are using Spring MVC you may see this message at times when generating HTML from your JSPs.

The full text of the message is:

500 Internal Server Error
Servlet error: An exception occurred. The current application deployment descriptors do not allow for including it in this response. Please consult the application log for details.

Looking at the log you will not see any error or exception whatsoever.

The problem is the coding in the JSP is accessing a variable in the model incorrectly.  Either a getter is missing or a field was changed, added, removed – etc…  Unfortunately this is all occurring in the bowels of Spring so the exception never percolates up.

If it was working fine before some recent change, then you are in luck.  The recent change is the culprit.

If you are on a bigger team or have been out of the loop for a while then you are out of luck.  You may need to do a binary search on the coding (remove 1/2, see if it works, remove 1/4, see if it works, …)

Perfection

No comments

I got dragged along to see Black Swan.  It was better than I expected.  The main character is obsessed with having a perfect performance.   That got me thinking.  How often have you tried to produce a perfect solution?  Not a solution that works, or meets budget or time schedule, but a solution that is perfect?

I heard a speech by the chairman of Kyoto Ceramic where a student asked about being the best in their industry.  The person looked confused once the question was translated.  After some discussion the translator finally came back with “Kyoto is not interested in being the best, they want to be perfect”.  

Imagine having that as the goal for your company, product, service, software.   What would you do different?  Why aren’t you doing those things now?