Installing Ubuntu 10 on Thinkpad X201 via USB

Today was a good day. Have received my little new black racing machine. It’s a Lenovo Thinkpad X201 12″ ultra portable notebook. It is loaded with Intel’s latest 620M i7 processor with 4MB cache and an Intel X25-M SSD. It’s quite a racer, for example, have you ever seen a notebook cold boot to the login prompt on a stock Ubuntu 9 / 10 Linux Gnome session in 15 seconds? N i c e. (Not that I expect to be booting all day, but you get the picture).

However, being lightweight and all, it does not have a DVD/CD drive. That’s ok, because here are some notes on how I installed Ubuntu 10 very easily on it today using a 16gigs Voyager mini.

  • Use UNetbootin (Universal Netboot Installer) to prepare an USB stick that runs the Ubuntu Live CD from USB (which you can install Ubuntu of from)

The Intel Wireless driver: “iwlwifi”  has been merged into the mainline kernel since 2.6.24. However, my Intel Advanced-N+ WiMAX 6250 wireless card, which does both WiMax and AGN did not work out of the box.

  • Download the required microcode image from intellinuxwireless.org and follow the instructions in the included readme file.
Intel Advanced-N+ WiMAX 6250
Posted in Linux, Opensource, OS | Tagged | 5 Comments

Ubuntu 9.10 (karmic koala) notes to self

Having tried Ubuntu for a few days three years ago (Feisty Fawn7.04), it just didn’t cut it at the time. I had to jump through one too many hoop in order to get all my devices working.

Now the time has come to give it another go (Karmic Koala 9.10). This time, out of the box everything just pretty much works as a charm. Some notes to self follow that make me feel more at home in Ubuntu.

Setting up your Java development environment

Java Development Kit

Currently my main language is Java, therefore I need to setup and be able to switch JDK versions whenever I like. Although some people just install the sun jdk through aptitude, I’d rather be in control myself. Here’s an easy way to achieve just that:

  • Get Java SE binaries directly from Sun (eg. download “jdk-6u20-linux-i586.bin”).  Make the bin executable and run it.
  • Own the extracted directory: “sudo chown -hR rob jdk1.6.0_20″
  • Move it to a sensible directory, eg: “$HOME/development/jdks/java-6-sun-1.6.0.20″
  • Create a  symlink in the jdks directory that points to the version to use: “ln -s jdk1.6.0_20 current-jdk”
  • Do the same for maven

Environment variables

Google where to put your environment variables and be surprised at the sheer amount of suggested ways to do this. Doing a little research in the Ubuntu documentation and environment scripts myself, I concluded this works best for me:

Initialize environment variables via “$HOME/.profile”, since it gets executed automatically by the DisplayManager during the startup process desktop session as well as by the login shell when one logs-in from the textual console. This ensures environment variables to be available in the terminal as well as to the desktop session. The latter ensures launcher shortcuts depending on these variables will work without further ado.

Note that the Ubuntu documentation mentions that “~/.profile” is only executed when “~/.bash_profile” and “~/.bash_login” do not exist; Luckily, by default they don’t ;-) .

Note on copy/pasting double quotes

When you copy paste script stuff found below, make sure you use proper quotes, as copy pasting it from this blog will give you the wrong double quotes. :-S

Initalize via .profile

Now, to have the environment script executed each time your session starts, append the following to your “$HOME/.profile”:

# custom initialization
if [ -f "$HOME/development/scripts/environment.sh" ]; then
. “$HOME/development/scripts/environment.sh”
fi

Environment.sh

Create a file “environment.sh” in a folder of your liking, I chose “$HOME/development/scripts/environment.sh”. We’ll use this script to initialize environment variables, by having it called via “$HOME/.profile”. Here’s the contents of environment.sh:

# session environment variables
export DEV_HOME=”$HOME/development”
export JAVA_HOME=”$DEV_HOME/jdk/current-jdk”
export JDK_HOME=”$JAVA_HOME”

export M2_HOME=”$DEV_HOME/tools/apache-maven-3.0-alpha-7″
export MAVEN_HOME=”$M2_HOME”
export MAVEN_OPTS=”-Xmx256m -Xms128m -XX:MaxPermSize=128m”

export INTELLIJ_HOME=”$DEV_HOME/tools/idea”
export IDEA_JDK=”$JDK_HOME”

PATH=”$PATH:$JAVA_HOME/bin:$M2_HOME/bin:$INTELLIJ_HOME/bin”

IntelliJ IDEA

Additionally, IntelliJ scans for the “MAVEN_HOME” environment variable. When found, it will use that path by default.

IntelliJ maven settings

IntelliJ maven settings

Further system tweaks

Getting Ubuntu to use 4 GB of memory (instead of the limited 3 GB)

See this article about the ram limitation and what do do about it.

Terminal

I like to have my terminal positioned somewhat in the center of my screen.

  • Align center screen with preferred size; Set terminal launcher properties (edit menu): “gnome-terminal –geometry=105×30+275+250″
  • Nice alternative font: “DejaVu Sans Mono Book 9″
  • Have nautilus show a contextual menu: Open in Terminal: “sudo aptitude install nautilus-open-terminal”

Tweak your font rendering

In order to make your fonts render like Safari does on Mac, create “.fonts.conf” in your home folder and add this to it:

<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<fontconfig>
  <match target="font">
    <edit name="autohint" mode="assign">
      <bool>true</bool>
    </edit>
  </match>
</fontconfig>

Limit swap usage

For more details on swap usage, see the Ubuntu Swap FAQ. By default, Ubuntu has a swapiness of 60/100; Where 0 is use swap only as a last resort, and 100 is agressive swapping. Ubuntu Swap FAQ recommends 10, so here’s how to change it:

Edit the configuration file with your favorite editor:

gksudo gedit /etc/sysctl.conf

Search for vm.swappiness and change its value as desired. If vm.swappiness does not exist, add it to the end of the file like so:

vm.swappiness=10

Famous last words

Ubuntu seems to have grown to a serious alternative to Windows. Will update this post when more noteworthy notes to self come to mind.

Posted in Linux, OS | Leave a comment

How to easily syndicate Magnolia CMS content as RSS

Introduction

Recently I have worked with Magnolia CMS. This is a CMS written in Java that really shines when it comes to providing a rich user experience that even non-technical people can understand (demo). Furthermore, it:

  • RSS feed iconsis tested opensource, based on open standards (eg. JCR)
  • has an active community
  • provides optional professional support through an enterprise license
  • has a plethora of extension modules (plugins) readily available (eg. MVC integration, public user registration)

With the shameless plug out of the way, this blog post focuses on how to use a recent feature of the RSS aggregator module:

Easy RSS syndication of Magnolia content

The RSS aggregator module is one of the modules that I have used when working with Magnolia. It allows to show an aggregate list of recent blog posts, gathered from different blogs. After having used the module a little, I encountered the use case to syndicate content from the CMS as RSS.

The goal of this post is to show you what it takes to be able to syndicate any Magnolia content as RSS, using the RSS aggregator  module.

Behind the scenes

Before we dive into the code, let’s consider for a moment what actually happens when someone requests our RSS syndication url. How does the RSS aggregator module get from an incoming request to an output RSS feed?

The RSS aggregator module registers a FeedSyndicationServlet that is mapped to handle requests on”/rss/*”.Given this mapping, a request for a RSS feed syndicated through the RSS aggregator module could look like this:

“http://server/rss/?generatorName=newsRssGenerator”

The FeedSyndicationServlet will attempt to resolve an appropriate FeedGenerator through its FeedGeneratorResolver dependency, based on given request parameters.

The FeedGeneratorResolver determines what FeedGenerator implementation to use for generating the output feed, by using the value of the “generatorName” parameter as a logical name that maps to an implementation class. This mapping is configured in the Content Repository under “config:/modules/rssaggregator/config/feedGenerators”, as is shown in the image on the right.

In this case, for the FeedGeneratorResolver will return an instance of  “custom.generator.NewsRssGenerator“. The servlet now uses this FeedGenerator to generate a Feed, and writes the content of the Feed to the response.

Now you know what core concepts are involved to syndicate a RSS feed, let’s implement our own FeedGenerator!

Setting the stage

We’ll need the following prerequisites in order to be able to leverage the new RSS syndication infrastructure in the RSS aggregator module, being:

We should use *-bundle.zip (or .tag.gz) of the RSS aggregator module link above, as it contains the dependencies required by the RSS aggregator module. Note that version 1.0-m2 mistakenly bundles “xercesImpl-2.4.0.jar”, which should be removed. Update: This unintended packaging has been fixed in the latest revision of the sourcecode in version control (see Resources).

Besides a running CMS with the above installed, we will need to use some of the infrastructure provided by the RSS aggregator module. Assuming you are using maven, add a dependency to the RSS module with the required repository, so you’ll have the libraries required in order to build our own FeedGenerator. With this in place, we can start rolling our own RSS syndication of our content.

    <repositories>
        <repository>
            <id>magnolia</id>
            <name>Magnolia Repository</name>
            <url>http://svn.magnolia-cms.com/maven/m2</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>info.magnolia</groupId>
            <artifactId>magnolia-core</artifactId>
            <version>${magnolia.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>info.magnolia</groupId>
            <artifactId>magnolia-module-rssaggregator</artifactId>
            <version>1.0-m2</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

Let’s write some code

Let’s assume that we run a Magnolia CMS site that has a newspage which contains newsitems. These newsitems we would like to syndicate as an RSS channel. How do we go about this? Open your favorite IDE;-)

The first thing we need to do, is implement a FeedGenerator that retrieves the news from the CMS and generate a Feed from it. The FeedGenerator is a simple interface that looks like this:

public interface FeedGenerator {
    Feed generate() throws FeedGenerationException;
}

The Feed returned by the “generate()” call looks like this:

public class Feed {

    private final String xml;
    private final String characterEncoding;

    public Feed(String xml, String characterEncoding) {
        this.xml = xml;
        this.characterEncoding = characterEncoding;
    }

    // getters follow...
}

Our FeedGenerator just needs to generate a simple encoded xml string and wrap it in a Feed in order to work. However, we still need to retrieve the news nodes from the CMS and convert this to xml; Luckily there are some convenience classes that makes that trivial.

By extending the abstract base class AbstractSyndFeedGenerator, we leverage the open source ROME library for generating an RSS for us. All we need to do is implement two template methods.

public class NewsSyndFeedGenerator extends AbstractSyndFeedGenerator {

    @Override
    public void setFeedInfo(SyndFeed feed) {
        feed.setTitle("Latest news");
        feed.setDescription("Only the latest news!");
        feed.setLink("http://mywebsite.com/rss/?generatorName=NewsRssGenerator");
    }

    @Override
    public List<SyndEntry> loadFeedEntries() {
        // retrieve news items from Content Repository
        // convert news items to SyndEntry list and return these
    }
}

The “setFeedInfo(SyndFeed)” method takes a SyndFeed as its argument. A SyndFeed is ROME’s representation of a syndication feed. A new instance of such a SyndFeed is provided as a parameter by the AbstractSyndFeedGenerator, implementors only need to set the properties above.

The “loadFeedEntries()” method is less trivial. It requires retrieving the news items from the Content Repository, and map these to SyndEntry instances. In order to do this in a convenient way, MagnoliaTemplate was introduced. Honouring the Spring JDBC template pattern, this class allows to query the Content Repository and map the resulting Content nodes using a ContentMapper in a one liner.

First we’ll need to know how to query for the news items, in this example the news items reside under “/home/newsitems” in the “website” repository and are of type CONTENTNODE. A good way to try out and tune your query to retrieve the proper content, is to use the JCR Queries tool, provided in Magnolia CMS, see the image below.

Magnolia JCR Query tool

In addition to being able to query the news items, we also need the properties of the news items, so we can map them appropriately. Keep in mind that any rich text content in the news items might contain links to uploaded images or other media. A useful utility class to resolve these kind of links is LinkUtil, provided by the Magnolia core.

You can have a look at the image below to get an impression of the news item content type we are about to map to RSS in this example.

News items content type layout

Given the above layout of news items in our Content Repository, the implementation of the “loadFeedEntries()” then looks like this:

    @Override
    public List<SyndEntry> loadFeedEntries() {
        String query = "/jcr:root/home/newsitems/*";
        return magnoliaTemplate.xpathQueryForList("website", query, ItemType.CONTENTNODE, new NewsMapper());
    }

The “xpathQueryForList()” method on the MagnoliaTemplate, takes four arguments:
the repository to query, the query to execute, the type of node to query for, and a ContentMapper.

The ContentMapper is a simple interface that enables mapping each of the result nodes of the query execution, to an object instance:

public interface ContentMapper<T> {

    /**
     * Map a content node that defines a type T to an instance of type T.
     *
     * @param content the content node to map
     * @return the result object of type T
     * @throws RepositoryException when problems occur accessing the underlying Repository
     */
    T map(Content content) throws RepositoryException;
}

At this point you must be curious how the implementation of the our NewsMapper looks like. So without further ado, here it is:

class NewsMapper implements ContentMapper<SyndEntry> {

    @Override
    public SyndEntry map(Content content) throws RepositoryException {
        SyndEntry entry = new SyndEntryImpl();
        entry.setTitle(content.getNodeData("title").getString());
        if (content.getNodeData("date").getDate() != null) {
            entry.setPublishedDate(content.getNodeData("date").getDate().getTime());
        }
        SyndContent description = new SyndContentImpl();
        description.setType("text/html");
        String text = content.getNodeData("text").getString();
        // Replace internal links that use the special magnolia link format (looks like ${link: {uuid: ... etc)
        // (see info.magnolia.cms.link.UUIDLink for an example of the special format that this next line handles)
        String textWithResolvedLinks = new LinkResolverImpl().convertToExternalLinks(text);
        description.setValue(textWithResolvedLinks);
        entry.setDescription(description);
        return entry;
    }
}

Aside from the tests you have naturally written while implementing the code above, you should now be able to verify your FeedGenerator implementation works by finishing off the exercise by following the three steps:

  • Make sure your FeedGenerator implementation is deployed to Magnolia
  • Configure this FeedGenerator implementation in the Repository so that it maps to a “generatorName“, remember?
  • Request the feed from your Magnolia instance

Summary

In this post you have seen that the Magnolia CMS RSS aggregator module can now syndicate content from your CMS as RSS with minimal effort: Implement a FeedGenerator, wire it with a generatorName in the Content Repository, have your feed requested and read too!

Lastly, know that you can always find the complete sample code for this post in the resources section below.

Resources

Posted in CMS, Java, Opensource | Tagged , , , , | 5 Comments

Integration testing email sent (preventing spam in the process)

Introduction

Anyone that has an email address can confirm that any (web)app these days is expected to -and indeed does- send email.

When developing and testing software however, such a mail-sending requirement oftentimes results in a bunch of test-emails showing up in the developers mailbox. These mails are in turn inspected by the human developer eye for correctness and then deleted.

Any seriously lazy software engineer however, shouldn’t find it to hard to see the problems this introduces, namely being:

  • One is dependant upon an externally running SMTP service
  • Errorprone manual inspection of the receival- and properties of email sent
  • A mailbox full of test emails

In the rest of this post I will introduce a solution to these problems. Sourcecode provided with this article can be found here.

A solution approach

For engineers using Java, know that you are fortunate for there exists Subetha SMTP:

SubEthaSMTP is an easy to understand Java library which provides a receptive SMTP server component. By plugging this component into your Java application, you can easily receive SMTP mail using a simple abstract Java interface.

One of it’s suggested uses includes:

An email test harness (Implemented in the Subetha SMTP project. It’s called Wiser.)

So basically this is a library that allows you to very easily run a local SMTP server which you can use to target any sent emails to. Bingo!

As a big proponent of test driven development and fan of the Spring Framework, I will provide an approach to using Wiser (as a local SMTP server) in your integration test using Spring.

What we need:

  1. A component that eventually triggers email(s) being sent
  2. ‘Application’ configured to target local SMTP
  3. An actual local SMTP server
  4. And all this under the control of a testing harness…

The ingredients

Inspired by the more than excellent Spring Reference Documentation, I introduce the SimpleOrderManager:

import nl.jteam.samples.domain.Order;
import org.springframework.mail.MailException;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import static java.lang.String.*;

public class SimpleOrderManager implements OrderManager {

    private MailSender mailSender;
    private SimpleMailMessage templateMessage;

    public void placeOrder(Order order) {
        // Do whatever is needed to process the order...
        sendCustomerNotificationFor(order);
    }

    private void sendCustomerNotificationFor(Order order) {
        SimpleMailMessage msg = new SimpleMailMessage(this.templateMessage);
        msg.setTo(order.getCustomer().getEmailAddress());
        msg.setText(format("Thanks %s for order %s. It'll be sent ASAP.", order.getCustomer().getName(), order.getNumber()));
        try {
            this.mailSender.send(msg);
        } catch (MailException ex) {
            System.err.println(ex.getMessage());
        }
    }

    public void setMailSender(MailSender mailSender) {
        this.mailSender = mailSender;
    }

    public void setTemplateMessage(SimpleMailMessage templateMessage) {
        this.templateMessage = templateMessage;
    }
}

And obviously this requires the necessary bean definitions:

    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host" value="mail.ourdomain.com"/>
    </bean>

    <bean id="templateMessage" class="org.springframework.mail.SimpleMailMessage">
        <property name="from" value="noreply@jteam.nl"/>
        <property name="subject" value="Your order"/>
    </bean>

    <bean id="orderManager" class="nl.jteam.samples.support.SimpleOrderManager">
        <property name="mailSender" ref="mailSender"/>
        <property name="templateMessage" ref="templateMessage"/>
    </bean>

The SimpleOrderManager is capable of sending emails, but all will go through the configured “mail.ourdomain.com” and end up in the customer mailbox. When invoked via an integration test, typically being a developers mailbox.

This is the point where we would want to be able to catch emails sent and verify them in automated fashion. To achieve this we need run a local SMTP server and reconfigure the JavaMailSender to target the local SMTP server. One way to make the latter happen, is to use placeholders for the SMTP server addres and make sure the placeholder during test time.

Here a BeanPostProcessor in order to reconfigure JavaMailSenders in the application context, do whatever blows up your skirt.

Add the Subetha dependency to your project. If you’re using Maven, you can add the following to your pom.xml:

        <dependency>
            <groupId>org.subethamail</groupId>
            <artifactId>subethasmtp-smtp</artifactId>
            <version>1.2</version>
        </dependency>

        <dependency>
            <groupId>org.subethamail</groupId>
            <artifactId>subethasmtp-wiser</artifactId>
            <version>1.2</version>
        </dependency>

Now we need some bean definitions on top of the ones introduced earlier. For example introduce a “test-localMailServer.xml”:

    <bean abstract="true" name="mailConfiguration">
        <property name="hostname" value="localhost"/>
        <property name="port" value="2525"/>
    </bean>

    <bean id="localMailServer"
          class="org.subethamail.wiser.Wiser" init-method="start" destroy-method="stop"
          parent="mailConfiguration"/>

    <bean class="nl.jteam.samples.test.JavaMailSenderReconfigurer"
          parent="mailConfiguration"/>

The “mailConfiguration” bean is abstract and only acts as configuration common to the beans defined below it. The “localMailServer” bean is a Subetha Wiser local SMTP server instance, having its startup and shutdown methods managed by Spring. The last anonymous bean definition is the JavaMailSenderReconfigurer, that will ensure that any JavaMailSenders will be configured to target the local SMTP server.

This in combination with the SimpleOrderManager context can in turn be used in an integration test:

import static junit.framework.Assert.*;

import nl.jteam.samples.domain.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.subethamail.wiser.Wiser;
import org.subethamail.wiser.WiserMessage;

import javax.annotation.Resource;
import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
        "classpath:/context/applicationContext.xml",
        "classpath:/context/test-localMailServer.xml"
})
public class SimpleOrderManagerIntegrationTest {

    @Resource
    protected SimpleOrderManager simpleOrderManager;

    @Resource
    protected Wiser localMailServer;

    @Test
    public void testPlaceOrder() throws Exception {
        Order order = new Order(1L, new Customer("Jane", "jane@volkswagen.com"));
        simpleOrderManager.placeOrder(order);

        List messages = localMailServer.getMessages();
        WiserMessage message = messages.get(0);
        assertEquals(1, messages.size());
        assertEquals("noreply@jteam.nl", message.getEnvelopeSender());
        assertEquals("jane@volkswagen.com", message.getEnvelopeReceiver());
        assertEquals("Your order", message.getMimeMessage().getSubject());
        // etc...
    }
}

The above integration test illustrates how we can use the Spring test infrastructure to inject the component and its collaborators under test. Having access to the local SMTP server, we can easily do assertions on the mails sent as well.

Summary

Running integration tests that send email can be perceived problematic. When using Java these problems can be addressed by running an embedded local SMTP server: Subetha Wiser. Leveraging the infrastructure provided by the Spring Framework this indeed does become a breeze.

Resources

Posted in Java, Testing | Tagged , , , , | 11 Comments