Java EE Custom Annotation Lookup

Thinking about plugins in a webapp, I wanted a way to have an interface that my code would call when certain things happened.

As a POC, I created an annotation and interface to run after an action completes. First we create the interface:

We will call the afterComplete() method when the action is complete. For simplicity, it just takes a String.

Now, we need an annotation so we can look it up in the container’s context:

This is a simple annotation that allows us to locate each bean that is annotated with it, and get the implementing class. If we create the EJB we want to discover like this, we can easily find it. This is a simple class that prints a log message:

The following code will find all the EJBs that have our @AfterComplete annotation and call their afterComplete(…) method with a simple String:

We can use this method in a Servlet, REST endpoint, or whatever else we want to create callbacks for different methods. There is an example Servlet and 2 beans that have a discover-able annotation at that is a Maven project that can be used for a demonstration.

Check it out and run mvn package to generate a war file, then deploy it and go to:

http://localhost:8080/ejb-custom-annotations-1.0/ to see the output. The output should be:

class net.mikeski.ejb_annotations.AfterCompletePrinterclass net.mikeski.ejb_annotations.AfterCompleteErrPrinter

and your console should have 2 log messages like this:

18:45:51,937 INFO [stdout] (default task-43) After complete printer: After complete called on Proxy for view class: net.mikeski.ejb_annotations.IAfterComplete of EJB: AfterCompletePrinter

18:45:51,939 ERROR [stderr] (default task-43) After complete called on Proxy for view class: net.mikeski.ejb_annotations.IAfterComplete of EJB: AfterCompleteErrPrinter

Apache Shiro – HTTP auth and Form auth in same project

I wanted a way to do regular form auth with an application. The idea is that regular users would use form authentication, and API users could use Simple HTTP authorization.

I had a REST service that I exposed at 2 different URLs by creating 2 Application classes extending and using;

to expose 2 different URLs. The first is at /rest and the second is at /api. Now, in the shiro.ini I setup a different filter for each path, and to access /rest you have to be logged in via form auth, and to access /api you must be logged in with HTTP Simple authentication.

I implemented a custom AuthorizationRealm and added the api role to the users that can access the API.

That works great, and simplifies any API connections by allowing Simple auth. Simple auth should also force SSH because it is not secure over clear HTTP, but that’s well documented by the Shiro project. There will be a post coming soon about how to implement a custom AuthorizingRealm to use your own DAOs to lookup users, but it is only 3 methods and is pretty straightforward.

Spring Date converter using @InitBinder annotation

Here’s code for a controller to allow Spring to process dates properly.

Java Convert List to JSON

This requires the jackson databind lib, here’s the maven dependency:

Postgres on Openshift Java Application

Using an openshift JBoss/Postgres setup trying to create a webapp with a postgres database.

Turns out that the openshift ${OPENSHIFT_POSTGRESQL_DB_URL} variable does not work with the postgres 9.2 JDBC driver.

It’s broken.

The URL should be: jdbc:postgres://hostname:port/database. The URL provided by openshift is not that format.

Note also that SSL is not supported by the database server.

Here is the properties placeholder I’m using with Spring.


I know that the system creates a JNDI connection, but my maven build copies a properties file if the Openshift profile is run, and copies a different properties file if it’s building locally. This allows the proper database connection to be made depending on where it is built.

Java MD5 with no external dependencies

How to generate an MD5 sum with Java – no external dependencies.

To run the file, do:

javac && java T

java T