May 19 2008

Quick Tip: JDBC ParameterizedSingleColumnRowMapper in Spring 2.5.2+

Tag: java, jdbc, springpmularien @ 4:51 am

This simple change in Spring 2.5.2 and above lets you remove boilerplate code that you have probably written for simple JDBC queries performed with the generics-aware SimpleJdbcTemplate (read my earlier 5 Minute Guide to… if you don’t know what this is). Change this:

new ParameterizedRowMapper<String>() {
			@Override
			public String mapRow(ResultSet rs, int rowNum) throws SQLException {
				return rs.getString("State");
			}
		}

Into this:

new ParameterizedSingleColumnRowMapper<String>()

The fully qualified class name is org.springframework.jdbc.core.simple.ParameterizedSingleColumnRowMapper. Neat! If you’re interested in where this came from, you can have a look at the JIRA issue SPR-4320.


May 06 2008

How to Diagnose the Awful Websphere Portal EJPPG0003E Error

Tag: development, java, jsf, portlet, webspherepmularien @ 9:09 pm

If you have done Websphere Portal 5.1 development, you have probably seen this error (among many others) at some point in your development lifecycle:

EJPPG0003E: ServletContext lookup for /.MyPortalApp returned the portal context. It has to be a different one.

Let me tell you that this error can be caused by about a hundred different things. If the issue is actually a context issue, you can follow Jamie Mcllroy’s great instructions to try to resolve context root-type problems.

But let’s say you do that, and you’re still getting the error - what then? (I’m assuming, by the way, that you are running inside RAD with the Websphere Portal UTE.)

As near as I can figure out, this error is thrown when you try to access a portlet and the backing application (usually a WAR within an EAR) doesn’t initialize itself properly. If you read into this, it basically means this error can be caused by a wide variety of issues with the underlying application.

First, make sure that you have console logging enabled in your server configuration. To do this, open your server configuration, click on the “Portal” tab (far right), and ensure the “Enable console logging” box is checked.

Next, when you attempt to access your portlet, you should (hopefully!) see an error appear in the console log. If you do, have a look at it, and hopefully it will be obvious what’s wrong.

If it’s not obvious, or if you get the infamous PortletExeption, you may have to enlist the help of a debugger. I would suggest the following steps:

  1. Restart the server in Debug mode
  2. When the server starts up, set a Java Exception Breakpoint for Throwable
  3. Attempt to hit the offending servlet or portlet

If the servlet is throwing an error that ends up being reported as EJPPG0003E, you’ll see the debugger stop at the exception in question. I personally have seen this error caused by various JSF configuration errors (with portlets using JSF), and classloader issues (missing MANIFEST.MF entries, classloader configuration issues, PARENT_LAST vs PARENT_FIRST, etc).

Hope this helps someone! If you have any other suggestions for diagnosing this error, please share!


Apr 24 2008

How to Reference and Use JSTL in your Web Application

Tag: development, glassfish, java, jboss, jsp, jstl, spring, tomcatpmularien @ 6:06 am

As a frequent contributor to the Spring Framework user forums, I have noticed a common trend among people new to Spring MVC - they really don’t understand how to use JSTL and EL in their Spring-driven JSPs.

Although Spring MVC supports flexibility in choosing a view technology, in my [back of the napkin] estimate, at least 80% of the time it is paired with JSP and JSTL. Unfortunately, since JSP was pushed out about 4-5 years ago, a lot of the information that you find on the web is extremely dated, often going back to JSTL 1.0 syntax (or, gasp, using scriptlets!). In this article I’ll clear up the confusion around how to use JSTL with various app servers and webapp versions.
Continue reading “How to Reference and Use JSTL in your Web Application”


Apr 10 2008

5 Minute Guide to the Java Amazon Associates Web Service API

Tag: amazon, java, tutorial, webservicespmularien @ 6:17 am

On a side project recently, I decided to try out the Amazon Associated Web Service API, a Java library provided by Amazon which wraps the Amazon Associates Web Service calls with a friendly Java API.

For those who are considering integration with Amazon Associates Web Services (used for things as item search, item detail retrieval, and cart manipulation), this API provides a very deep and rich integration with Amazon, with a relatively shallow learning curve. I’ll provide a simple example here of doing an item lookup by ASIN (Amazon’s unique product IDs), and point you around some of the data structures you’ll encounter when dealing with ASINs.

Continue reading “5 Minute Guide to the Java Amazon Associates Web Service API”


Mar 27 2008

Automate NTLM Authenticated Web Service Testing with WebInject

Tag: java, ntlm, perl, soap, testing, webservicespmularien @ 8:09 pm

This is a bit of a different subject matter than I usually cover, so I apologize in advance. I was recently working on a project involving many, many remote web services. We were running into issues with some services being sporadically unavailable, and wanted to gather data on their uptime. One interesting twist was that all the services were protected by NTLM authentication, which severely limited the number of choices I could find easily.

I came across a Perl-based tool called WebInject. With some slight tweaking, it does support NTLM authentication, and it also supports POST body content, which I needed to be able to POST SOAP requests.

Here’s how to set it up on a Windows platform and implement NTLM support.

  • First, download the WebInject distribution. Unzip to a folder (say, c:\webinject).
  • Next, download and install ActivePerl 5.8 (latest) from here.
  • Once you install ActivePerl, you’ll need to install some Perl packages:

The following packages are required:

ppm install Error
ppm install Tk::ProgressBar::Mac
ppm install Authen::NTLM

WebInject comes with an executable which wraps up a Perl interpreter and all the packages you need. However, it doesn’t include the package with NTLM support. So we are setting things up so that our external Perl interpreter (ActivePerl) has all the dependencies it needs in order to run WebInject as a Perl script.

Once you’ve installed the packages listed above, you should be able to run WebInject as a Perl script:

perl webinjectgui.pl

You will need to make a minor change to the webinject.pl script to enable HTTP keepalives (these are required for NTLM authentication). Look for the LWP::UserAgent->new line and modify as such:

    $useragent = LWP::UserAgent->new(keep_alive=>1);

This will allow WebInject to communicate with an NTLM web service. Set up authentication as documented in the WebInject documentation. Of course, after I went through this, another colleague suggested trying out SoapUI, which also supports some types of NTLM authentication. I’ll try to write up that tool later on - first impressions look really good (certainly much more sophisticated than WebInject).

Related Reading:
http://www.goldb.org/goldblog/2007/05/16/WebInjectOpenSourceWebServiceTestingToolGetsHighMarks.aspx
http://www.infoworld.com/article/07/05/11/19TCwebservicetest_5.html
http://www.webinject.org/cgi-bin/forums/YaBB.cgi?board=Development;action=display;num=1185818423


Mar 10 2008

Auto-Expanding Collections as JDBC Parameters with Spring SimpleJdbcTemplate

Tag: development, hibernate, java, jdbc, springpmularien @ 7:54 am

One of the most irritating limitations of plain JDBC is that queries with a variable number of parameters are notoriously painful to deal with. The most common case of this is with the IN clause, which by definition is intended to accept a variable length argument list. JDBC, for those who don’t know, does not allow variable-length bound parameters.

Having worked with Spring’s Hibernate abstraction (HibernateTemplate) for some time, I have gotten used to Spring’s value-added feature of expanding Collections bound to HQL parameters (it’s a shame that Hibernate doesn’t natively support this, AFAIK). I was pleasantly surprised to find out that Spring offers JDBC support for this feature as well. Here’s a simple set of examples…

Continue reading “Auto-Expanding Collections as JDBC Parameters with Spring SimpleJdbcTemplate”


Feb 26 2008

5 Minute Guide to Spring and Simple[r!] JDBC

Tag: java, jdbc, springpmularien @ 1:05 am

I have noticed a trend recently among some folks in the Java world, where it is simply taken for granted that an ORM provider (usually Hibernate) will be automatically inserted in the technology stack of any new project. Quite often, this happens with little to no technical justification or analysis. If you’re reading this and nodding your head, this should be frightening to you. The reason is that if the use of an ORM isn’t required, it can cause projects (especially small ones) to have unneeded complexity. Additionally, abstraction without understanding what’s underneath can set a dangerous level of ignorance on the part of developers, who will simply collapse when the abstraction breaks and critical thinking through the abstraction is required to solve a problem.

In that spirit, I recently worked on a personal project to learn how one can write dead-simple plain old JDBC applications using only Spring Framework 2.5 without an ORM layer. Spring 2.5 has many features that provide some of the convenience of ORM libraries (simple mapping from ResultSets to Objects), some convenience above and beyond ORM libraries (mapping from ResultSets to primitives!*), and removes some of the complexity (caching, cascading, etc.). For applications with fewer tables than you have fingers on your hand, this can greatly ease development.

I’ll assume you already know how to work with Spring, and at least know (or can dredge up) the basics of JDBC. We’ll work through a simple example, mapping the ubiquitous “Person” table to a Person object, and back again through an insert operation. Hopefully this will open your mind to non-ORM solutions. I promise that in 5 minutes (or less!) you will be amazed at the things you can do with simple JDBC ;)

Continue reading “5 Minute Guide to Spring and Simple[r!] JDBC”


Feb 25 2008

Removing Windows Update restart nag

Tag: random, windowspmularien @ 9:16 pm

Hate this dialog?
Restart now?

Seen here and here. Posting so I don’t forget how to remove this darned thing in the future. Since I rarely shut my machines down, I find the dummy-proof-update feature of Windows incredibly annoying.


Feb 19 2008

Tutorial: How to set up Tomcat 6 to work with JSTL 1.2

Tag: development, java, jsp, jstl, spring, tomcatpmularien @ 11:37 pm

Tomcat 6 does not ship with an implementation of JSTL. I decided to write up this quick start guide, since it’s really, really hard for new folks to know how to get started with Spring MVC (which is very often combined with JSTL) on Tomcat 6.

Sadly, Sun’s JSTL site does not even point you at the actual reference implementation of JSTL 1.2 (at least there’s no very obvious link that I have been able to find - but what good would a Sun web site be if it was easy to find what you were looking for? ;) ).

The Apache Jakarta Taglibs project is the source of JSTL 1.0 and 1.1 reference implementations, but it is no longer maintained and will never implement JSTL 1.2.

The JSTL 1.2 reference implementation has been folded into the Glassfish application server. It seems that Sun in its infinite wisdom has decided to make the reference implementation almost impossible to find. The link to “Reference Implementation” on the JSR-052 page points you to the Sun Java EE download page (argh!)

So, how do you get this installed on Tomcat?

  • Download the latest version of Glassfish V2 application server here.
  • Unzip/install to a directory
  • From the “lib” directory of the install, copy the appserver-jstl.jar and javaee.jar files to one of two locations:
    • In the WEB-INF/lib directory of your web application. This is preferable because you can ensure that the correct version of JSTL lives with your application. The downside is that you may run into classloader issues when running on application servers other than Tomcat which supply their own (conflicting) JSTL implementations.
    • In the {tomcat-install}/lib folder. This will make the JSTL 1.2 libraries available to all web applications. This may cause classloader issues if web applications have their own versions of these classes, and as such is not recommended (but it will work).

Be aware that there’s a nasty bug with the JSTL/JSF 1.2_07 RI that I’ve blogged here. For you Spring MVC’ers trying to get started with the latest versions of Tomcat and JSTL, hopefully this helps you!


Feb 05 2008

Tomcat 6.0.14 + JSTL 1.2_07 RI = *Boom* NullPointerException

Tag: development, eclipse, java, jsf, jstl, tomcatpmularien @ 8:39 pm

I guess I’ve had a busy / buggy weekend noodling around with stuff. I ran into a [known] bug in the latest JSTL/JSF 1.2 Reference Implementation on Tomcat 6.0.14.

I was playing around with Spring MVC 2.5.1, and even the simplest JSP page gave me this:

java.lang.NullPointerException
	com.sun.faces.application.WebappLifecycleListener.syncSessionScopedBeans(WebappLifecycleListener.java:312)
	com.sun.faces.application.WebappLifecycleListener.requestDestroyed(WebappLifecycleListener.java:87)
	com.sun.faces.config.ConfigureListener.requestDestroyed(ConfigureListener.java:240)
[snip]

This is interesting, because I wasn’t even using JSF!

It turns out that there’s a bug that is lightly documented in the JSTL/JSF 1.2_07 RI release notes:

There is a chance for an NPE in com.sun.faces.application.WebappLifecycleListener with some configurations (see issue 670). Take for example, installing JSF in a container such that JSF will be available to all web applications. The NPE will occur for an application that doesn’t have the FacesServlet defined within it’s web.xml.

The workaround for this issue is, within the global web.xml for the container (JBoss and Tomcat both have one) add either a FacesServlet definition (no mapping) or add the context init parameter, com.sun.faces.forceLoadConfiguration, with a value of true. NOTE: GlassFish is not affected by this issue.

This is also covered in the Mojarra bug database as bug 670, and the JBoss bug database as bug JBAS-5063.

There’s some discussion in this JBoss forum thread indicating how to solve for JBoss AS, which is a good start.

For Tomcat 6, you can add the following to context.xml:

	<Parameter name="com.sun.faces.forceLoadConfiguration" value="true"/>

One caveat here is if you’re running an Eclipse-based Tomcat instance. Eclipse-configured Tomcat doesn’t have an easily accessible context.xml. Instead, you’ll have to configure this in the global default webapp. Look in your Eclipse workspace for the server configuration (by default, Eclipse will put this in a top-level project called “Servers”).

In the web.xml you’ll find in there, add the following:

 <context-param>
  <param-name>com.sun.faces.forceLoadConfiguration</param-name>
  <param-value>true</param-value>
 </context-param>

This should not be put in your web application’s web.xml!

Once you do this, you should not receive the NullPointerException upon first access to a JSP. It looks like this bug will be fixed in the next RI release of JSF/JSTL.


Next Page »