Monday, 28 June 2010

Getting IP Addresses of Local Machine in Groovy

Actually gets the IP Addresses of the network adaptors in the machine rather than just the loopback address (127.0.0.1)


NetworkInterface.getNetworkInterfaces().each { iface ->
iface.inetAddresses.each { addr ->
println addr.hostAddress
}
}

Friday, 25 June 2010

Finding the request contextpath using Freemarker

I spent a good while Googling how to reference the request context path in Freemarker, and finally tracked down a solution on the Spring Source forum.

If a controller is intercepting requests at /app/blah/something, and your view loads CSS from static/css/, then your view will try and resolve the CSS at /app/blah/static/css/ -- which doesn't exist.

The solution is simple: in your views.xml, define the "requestContextAttribute" property, and assign it a value.

<bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="cache" value="true" />
<property name="prefix" value="" />
<property name="suffix" value=".ftl" />
<property name="exposeSpringMacroHelpers" value="true"/>
<property name="requestContextAttribute" value="rc"></property>
</bean>


Now in your templates you can use ${rc.contextPath} and your CSS will try and resolve to /app/static/css/ - perfect!

Wednesday, 16 June 2010

AppEngine first steps

I have just got a prototype of a small application for a client. This takes input data, processes it and displays the result.

I needed to get it up on a web site so the client could have a look at it. Seemed like the perfect excuse to have a go at Google AppEngine.

I downloaded the SDK and Eclipse Plugin. As I had already created the web application, I could not use the support in the plugin to run the application out of the box. The fix was quite simple though, I created a AppEngine project, then copied the launch configuration. Playing with the path to the war folder and the classpath, soon got the app running in the local AppEngine server.

The first issue I ran into was that Freemarker does not work on AppEngine. A quick google search, turned up a patched Freemarker jar for AppEngine - perfect. I put this into our ivy repository, changed the dependency in the project, rebuild and the application runs perfectly.

Now to get it live. I created an application in AppEngine and used the SDK to upload the application - this is really easy - ./appcfg.sh update /path/to/war/folder.

After a few minutes all seems ready, however when I go to the application, an error page is displayed. What is wrong? After I found out how to get to the admin console (the URL is different if you have a Google Apps for Domains Account), I found a log viewer. This showed some more Freemarker errors.

java.lang.IllegalAccessError: Class com.google.apphosting.runtime.security.shared.intercept.java.lang.reflect.Method_$1 can not access a member of class freemarker.ext.jsp.FreeMarkerPageContext21 with modifiers "static"

Turned out to be a silly mistake. Another ivy dependency was bringing in the old Freemarker jar file. I did get to find out about how AppEngine keeps old and versions of your application as well and lets you access them via alternate URLs of the form http://[verion].latest.[your-app].appspot.com. Very handy for final testing before "going live".