Tag Archives: Java

More Raspberry Pi Awesomeness: GPIO & Java

I’ve been playing with the GPIO capabilities of the Raspberry Pi before using Python, which comes with easy GPIO access for the Raspberry Pi.

Java 8 for ARM

As I’m yet not very familiar with Python I got excited when Oracle accounced Java 8 for the ARM platform, so I downloaded the JDK 8 (with JavaFX) for ARM Early Access from the Oracle website, copied it to the RPi, compiled an example app on my PC and copied the JAR to the RPi – and it runs!

Obviously it takes some time to start the application but it works as excepted; of course it was a very simple example and there are some limitations, especially when it comes to graphics output, but it’s a good start.

There is even JavaFX support in the JDK 8 which will output to RPi’s HDMI/Video output, but that’s a topic for another posting ;)

Java & GPIO

There is no native support for the RPi’s GPIO pins in Java, but there are two Open Source projects to get it running anyway:

  • WiringPi – an easy-to-use C library to control the GPIO pins
  • Pi4j – a Java class library providing a bridge between native code and Java to access the GPIO pins

As described on the WiringPi website you can get WiringPi via git, and then build it directly on the RPi.

The Pi4j website has lots of examples on how to work with the code, and from a Java developer’s perspective it is pretty straight forward to use Pi4j.

I’ve used some code directly from the Pi4j’s example pages, put an LED on a breadboard, connected two pins and when running the program the LED blinks! Great!

So, if you are planing to access GPIO via Java you should definitely take a look at WiringPi and Pi4j.

New open source project: jspinfo

Just some shameless self-promotion:

I recently uploaded my 3rd open-source project to SourceForge.net. It’s called jspinfo() and – for all of you who are familiar with PHP – it’s like phpinfo() but as a JSP file.

So, if you are a developer and you need some information about some common server settings, then give jspinfo() a try: just upload the file to your Java-based application server (preferrable Apache Tomcat) and open it in your browser.

For more information and the download please visit http://jspinfo.sourceforge.net/

If you have any comments, ideas or other feedback on jspinfo() please visit the jspinfo() Ticket system or the discussion forum.

Numeric converter using different bases

Inspired by a short conversation with jsilence I was looking for a NewBase60 converter written in Java and C#.

NewBase60 is a format suitable for building parts of URLs, e.g. for URL shorteners. Instead of just using numbers for the short links it uses letters as well, while internally still using numeric values. (There are different strategies to increase a numeric value: just increase the number, increase it using hexadecmial notation, or using an extended set of characters like NewBase60 does it.)

Sadly, the NewBase60 posting didn’t list a C# implementation, and the link to the Java implementation was broken.

So I decided to write my own version. And while NewBase60 with its given 60 characters is just a special case for such numeric conversions, my Base-N implementation not only includes NewBase60 but also a binary, octal and a hexadecimal converter, a flexible base implementation for user-defined bases/alphabets and a “max. base 36” implementation.

The Base-N code is up on my github page, so feel free to download, use and/or fork it.

If you encounter any bugs or just want to provide any other (positive?) feedback, feel free to leave a comment below.

A sneak peek into Apache James

I’ve been playing around with Apache James, a complete email server written in Java, in the last few months and I’d like to share some of my thoughts about it. (I’ll focus on version 2.3.2 as the 3.0 version is still in beta.)

In short
Apache James is a flexible, highly configurable and extendable email server (SMTP, POP3, IMAP4 [since James 3.0]). It can store emails and users either in the file system or in a database (e.g. MySQL, PostgreSQL, and others) and handles emails for multiple domains via mappings.

Some insights
There is a comprehensive documentation at the James website including a wiki, so I will not go too much into detail here. Some of the server’s main concepts are storage, processor, matcher and mailet.

Storage
Both emails and users (and other configuration data) is persisted in the storage. James comes with a filesystem-based and a JDBC-based storage implementation, but other types of storage are possible too (e.g. LDAP, JPA and others, some of them will be provided with version 3.0).

Processor
When an incoming email is processed by the server it is injected into a so-called processor, which is a kind of queue consisting of actions to be performed on the email. Those actions are performed by the mailets.

Mailets
A mailet is similar to a servlet in a web container (like Tomcat), but it handles an incoming email instead of an HTTP request. James comes with many ready-to-go mailets, but it is very easy to implement own mailets to perform special tasks.

Matcher
A matcher decides if a a mailet will be executed for an incoming email. It checks if a condition is met and tells the email engine to execute the associated mailet.

Making it work all together
Sounds all a little bit confusing? Actually it isn’t. This is how everything works together:

  • The email engine receives an incoming email, wrapping it internally with an container (which provides some helpful functions)
  • The email engine forwards the the wrapped email to the processor named “root”
  • The “root” processor consists of a sequence of mailets, each having a matcher associated.
  • The mailet’s matcher is executed, returning whether or not the mailet should be executed.
  • The matcher is then executed (or maybe not). It will “do something” with the email, e.g. a virus check, some validation, changing data, forwarding or rejecting or discarding the email, putting it into another processor, or doing some other action.
  • If the email was not discarded, then the next mailet is processed, then the next, and so on.

Actually, there is a lot more going on internally, and in many points I oversimplified things here, but I just to give you a first impression about how James works.

Why is this so great (or useful)?
For me the most interesting point is that you can completely configure and extend the way how an incoming  email is processed. You can directly interact with an incoming email: you can convert its content or forward it to a custom script or handle it any other way that you can imagine.

One example? Sure!
The following mailet adds support for an address mapping to receive emails with multiple address suffixes into one account: an email address like jcoder+projectone@example.com
will become jcoder@example.com (and will be delivered to the account “jcoder” then.)

package me.jcoder.blog.examples.james.mailet;

import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import javax.mail.MessagingException;
import org.apache.mailet.GenericMailet;
import org.apache.mailet.Mail;
import org.apache.mailet.MailAddress;

/**
 * Mailet to support the "plus sign alias" syntax in addresses, so that
 * name+something@domain becomes name@domain
 * @author jCoder
 */
public class PlusAliasSupport extends GenericMailet {
    private static final String MAILET_NAME = "PlusAliasSupport";

    @Override
    public void service(Mail mail) throws MessagingException {
        if (mail == null) {
            return;
        }
        try {
            Collection recipients = mail.getRecipients();
            Collection recipientsToRemove = new LinkedList();
            Collection recipientsToAdd = new LinkedList();
            for (Iterator itr = recipients.iterator(); itr.hasNext(); ) {
                MailAddress address = (MailAddress)itr.next();
                if (address == null) {
                    continue;
                }
                String localPartStr = address.getUser();
                int plusIndex = -1;
                if ((localPartStr != null) && ((plusIndex = localPartStr.indexOf('+')) > -1)) {
                    String newLocalPart = localPartStr.substring(0, plusIndex);
                    MailAddress replaceAddress = new MailAddress(newLocalPart, address.getHost());
                    recipientsToRemove.add(address);
                    recipientsToAdd.add(replaceAddress);
                }
            }
            recipients.removeAll(recipientsToRemove);
            recipients.addAll(recipientsToAdd);
        } catch (Exception ex) {
        }
    }

    @Override
    public String getMailetName() {
        return MAILET_NAME;
    }
}

It’s pretty helpful to get the source code of James to take a lokk at how the provided mailets and matchers are implemented.

Some notes and tipps
When writing matchers and mailets or running James as your (test) email server, there might be some pitfalls, so I’ll share some of the points that I came across.

  • When writing matchers and mailets you might want to download the Mailet SDK from the James website. This is generally a good idea, but make sure that you get the right version! James 2.3.2 normally uses the Mailet 2.3 API and the website provides the 2.4 API, so things will not work. If in doubt, add the mailet API-related JARs from a James installation.
  • When placing the JAR into the ${JAMES_DIR}/apps/james/SAR_INF/lib folder, don’t forget to add your mailet/matcher package name to the mailetpackagesmatcherpackages section of the config.xml file so it can be found by the server.
  • If you are going to use the JDBC-based storage, you need to put the JDBC driver for your database in a directory where the server can find it. It might not be best-practise, but I suggest to put the JDBC driver JAR into ${JAMES_DIR}/lib where it is accessable by the main server.
  • The same goes for using TLS/SSL. If you want to provide secure POP3 access, then you not only need a certificate (which is a whole other story to maybe being covered in an other posting), but you might run into some SSL-socket-related errors. One solution is to copy the sunjce_provider.jar from your Java runtime into ${JAMES_DIR}/lib as well.
I hope that this posting gives you a first impression about the flexibility of the email server James. If you have any questions or ideas, feel free to leave a comment.