Ruby Programmer/Developer

George Armhold's Blog

Data-driven webapps built with Ruby, Java & Wicket

How to Get JNDI Working With Wicket 1.5 and Jetty 7.5

The Wicket 1.5 archetype sets up a project to use Jetty 7.5. Quite a lot has changed in Jetty since version 6, and this broke my JNDI config. Here’s how I put things right again.

First of all, the imports have all been moved in 7.x.  Here’s where they landed:

import org.eclipse.jetty.plus.webapp.EnvConfiguration;
import org.eclipse.jetty.webapp.WebInfConfiguration;
import org.eclipse.jetty.webapp.Configuration;
import org.eclipse.jetty.webapp.WebXmlConfiguration;

Next, you’ll need a jetty-env.xml.

<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">

<Configure id="wac" class="org.eclipse.jetty.webapp.WebAppContext">
    <New class="org.eclipse.jetty.plus.jndi.EnvEntry">
    <Arg>jdbc/mydatasource</Arg>
    <Arg>
        <New class="com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource">
            <Set name="Url">jdbc:mysql://localhost/mydatabase?characterEncoding=utf8</Set>
            <Set name="User">username</Set>
            <Set name="Password">password</Set>
        </New>
    </Arg>
    </New>
</Configure>

Normally this goes into src/main/webapp/WEB-INF, but you probably don’t want to deploy that with your app in your production war file. So instead I put mine in src/test/jetty/jetty-env.xml. You’ll need to modify your Start.java to tell Jetty to find the relocated config file.

EnvConfiguration envConfiguration = new EnvConfiguration();
URL url = new File("src/test/jetty/jetty-env.xml").toURI().toURL();
envConfiguration.setJettyEnvXml(url);
bb.setConfigurations(new Configuration[]{
    new WebInfConfiguration(),
    envConfiguration,
    new WebXmlConfiguration()
});

I found that I also had to set a couple of environment properties:

System.setProperty("java.naming.factory.url.pkgs",
                   "org.eclipse.jetty.jndi");
System.setProperty("java.naming.factory.initial",
                   "org.eclipse.jetty.jndi.InitialContextFactory");

With this, I can finally access my JNDI datasource happily from Wicket/Jetty.

Update: I’ve created a gist with the full source code.

Automatically Generate Maven Dependency Coordinates for Random Jar Files

Have you just inherited an Ant project that you’re trying to convert to Maven? Maybe it came with a “lib” directory full or random jar files. And worse, some thoughtless developer neglected to include version strings in the filenames?

Fear not! The Sonatype checksum search REST service can give you the Maven coordinates based on the jar’s SHA1 hash.

Still too much work? Not to worry, I just wrote a quick program to make it even easier for you. Provenance will take a directory full of jar files and write out the XML dependency information for every jar it finds. You can then copy/paste this right into the <dependencies> section of your pom.xml.

Enjoy.

Adding Git SHAs to Wicket Pages Automatically

If you have a non-trivial project, it’s handy to be able to tell what code was used to build a particular release once it’s been deployed. Especially if you’ve recently discovered the joys of branching and merging with Git.

Here’s a handy way to add a Git SHA to all your app’s pages via Wicket and Maven.

Maven

First, we’ll use the exec-maven-plugin to create a git.properties file for us. Add this to the section in your pom.xml:

<plugin>
   <groupId>org.codehaus.mojo</groupId>
   <artifactId>exec-maven-plugin</artifactId>
   <version>1.1</version>
   <executions>
       <execution>
          <phase>compile</phase>
          <goals>
             <goal>exec</goal>
          </goals>
       </execution>
   </executions>
   <configuration>
       <executable>git</executable>
       <arguments>
            <argument>log</argument>
            <argument>--pretty=format:gitsha=%H %ci</argument>
            <argument>-n1</argument>
       </arguments>
       <outputFile>target/classes/git.properties</outputFile>
   </configuration>
</plugin>

This will create a git.properties file containing the Git SHA, along with the commit timestamp whenever your code is compiled. You can learn how to further customize this here.

Wicket Application Subclass

Now we’ll need to read in the git.properties file when our application starts up.

public class Application extends WebApplication
{
    private String gitSHA;

    public AppgravityApplication()
    {
        java.util.Properties props = new java.util.Properties();
        try
        {
            props.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("git.properties"));
            gitSHA = props.getProperty("gitsha");
            log.info("gitsha: " + gitSHA);
        }
        catch (IOException e)
        {
            log.severe(e.getMessage());
            gitSHA = "unknown";
        }
    }

    public String getGitSHA()
    {
        return gitSHA;
    }

Wicket WebPage Subclass

Now we’ll create a WebPage subclass that renders the Git SHA into a tag when the page is rendered.

public abstract class MyPage extends WebPage
{
    @Override
    protected void onBeforeRender()
    {
        Label metaGitSHA = new Label("metaGitSHA", "");
        metaGitSHA.add(new AttributeModifier("content", Model.of(((Application) getApplication()).getGitSHA())));
        addOrReplace(metaGitSHA);
        super.onBeforeRender();
    }
}

You’ll want to extend MyPage for each of your pages. You’ll need to add the placeholder meta tag to each of your HTML pages like this:

<head>
    <meta wicket:id="metaGitSHA" id="metaGitSHA" name="metaGitSHA" content=""/>
</head>

And you’re done!