Posts Tagged Java

Apache Ivy and Spring EBR

Here is how I set up the Apache Ivy dependency manager so that it can fetch springframework JARs from the SpringSource Enterprise Bundle Repository.

Listing: ivysettings-custom.xml

<ivysettings>
    <resolvers>
        <url name="com.springsource.repository.bundles.release">
            <ivy pattern="http://repository.springsource.com/ivy/bundles/release/[organisation]/[module]/[revision]/[artifact]-[revision].[ext]" />
            <artifact pattern="http://repository.springsource.com/ivy/bundles/release/[organisation]/[module]/[revision]/[artifact]-[revision].[ext]" />
        </url>
        <url name="com.springsource.repository.bundles.external">
            <ivy pattern="http://repository.springsource.com/ivy/bundles/external/[organisation]/[module]/[revision]/[artifact]-[revision].[ext]" />
            <artifact pattern="http://repository.springsource.com/ivy/bundles/external/[organisation]/[module]/[revision]/[artifact]-[revision].[ext]" />
        </url>
        <chain name="spring">
            <resolver ref="com.springsource.repository.bundles.release"/>
            <resolver ref="com.springsource.repository.bundles.external"/>
        </chain>
        <ibiblio name="jboss" root="http://repository.jboss.org/maven2/" m2compatible="true"/>
        <chain name="main" dual="true">
            <resolver ref="shared" />
            <resolver ref="public" />
            <resolver ref="spring" />
            <resolver ref="jboss" />
        </chain>
        <chain name="default" returnFirst="true">
            <resolver ref="local" />
            <resolver ref="main" />
        </chain>
    </resolvers>
</ivysettings>

Listing: ivysettings.xml

<ivysettings>
    <settings defaultResolver="default" />
    <include url="${ivy.default.settings.dir}/ivysettings-public.xml" />
    <include url="${ivy.default.settings.dir}/ivysettings-shared.xml" />
    <include url="${ivy.default.settings.dir}/ivysettings-local.xml" />
    <include file="ivysettings-custom.xml" />
</ivysettings>

This means that dependencies such as:

<dependency org="org.springframework" name="org.springframework.web.servlet" rev="3.0.2.RELEASE" />

should now be resolved correctly from the EBR.

Tags: , , , ,

Jsoup – BeautifulSoup for Java

HTML is notoriously difficult to parse and it has usually been a pain to do this in Java. Yes I know that there are parsers (like jtidy and nekohtml) that try to create a proper DOM but I’ve been waiting for something more lightweight.

Enter Jsoup. It feels like a mix of JQuery and Beautiful Soup (for Python).

String html = response.getContentAsString();
Document document = Jsoup.parse(html);

Elements elements = document.select("#errorRef");
assertThat(elements.size(), equalTo(1));

assertThat(elements.first().text(), equalTo(errorRef));

Mmm, full flavour, none of the fat.

Tags: , , , ,

StringTemplate views for Spring

StringTemplate is a great templating engine. It’s powerful, simple and quite opinionated. I’ve come really appreciate its simple purpose: render data. No assignment, no arbitrary method invocation. It is not Turing-complete and it would make a lousy rules engine.

SiteMesh is a web-page layout and decoration framework that is my current “golden hammer” when I need to provide a consistent layout across a java web-application. It seems to fit with the way that I think about web pages a whole lot more than something like Tiles – I really prefer decoration over composition as a means of layout control.

I’ve used StringTemplate it in the past on java projects and for a while have wanted to create a way to integrate it with Spring Framework’s MVC so I can stop using Freemarker and Velocity in Spring-heavy projects. Not that Freemarker is that bad, it’s just that I really don’t need or want all of the bells and whistles that come with it and allow it to be so frequently misused. All I really want is a templating engine that renders the model and does not get in my way. It just so happens that SiteMesh also makes an appearance on these projects and I’ve been wanting to use StringTemplate to provide layout decorators as well as Spring views.

Today I’ve released version 1.0 of my spring-stringtemplate integration library as a GitHub project. It provides an implementation of a Spring MVC View and ViewResolver for StringTemplate, and a decorator servlet for SiteMesh.

Feedback is always welcome.

Tags: , , , ,