I very nearly fell for this one!
A couple of AS7 migration tweaks
Enabling SSL for your web app, in standalone mode:
<connector name="https" protocol="HTTP/1.1" socket-binding="https" scheme="https" secure="true"> <ssl name="https" password="password" certificate-key-file="standalone/configuration/server.keystore"/> </connector>
Also worth noting that checking of security roles in your web.xml is more strict under AS7, so you may find you need to define them explicitly:
<security-role> <role-name>admin</role-name> </security-role> <security-role> <role-name>normal</role-name> </security-role>
DatabaseServerLoginModule in JBoss AS7
The arrival of the next version of JBoss is imminent and so I’ve begun to port an application I’m developing to it. Naturally there are slight differences along the way as the entire stack seems to have been rebuilt from the ground up in a modular fashion, loading only what’s needed. This has resulted in a serious improvement in boot time – an order of magnitude even.
Anyway, the purpose of this post is simply to outline the custom security domain I’ve added to the standalone.xml configuration for my server to allow me to use the DatabaseServerLoginModule:
<security-domain name="my-domain" cache-type="default"> <authentication> <login-module code="Database" flag="required"> <module-option name="dsJndiName" value="MyAppDS"/> <module-option name="principalsQuery" value="SELECT password FROM users WHERE username=?"/> <module-option name="rolesQuery" value="SELECT role, 'Roles' FROM role WHERE username=?"/> </login-module> </authentication> </security-domain>
Syntactic sugar without waiting for Java 7 or 8
Project Lombok is a small project which makes use of annotations to save you from some boiler plate code which in most normal scenarios is largely redundant.
I originally found Lombok when I was tired of adding getters and setters to my POJOs that in the most common case were a very verbose way of just saying I wanted to be able to get and set a list of variables. Lombok adds Getter and Setter annotations which you can apply to a field and it will add a getter and setter method to the class file for you. They’ve taken this a step further though and added a class level @Data annotation so instead of annotating each method twice you can annotate your class once to say that getters and setters should be created:
@Data public class Main { private Long id; }
This not only removes a lot of otherwise redundant code, but it means that when you do choose to define a getter which does something a little out of the ordinary then these differences are now clear to see, instead of being lost in a sea of standard getters and setters.
Take a look at the @Log annotation too as a cleaner way of setting up a logging library. Not all the annotations are worth singing the praises of though, so do be careful and there is one other caveat to this library. Because it makes compile time changes to the code IDEs will have to be made aware that at runtime these additional methods and fields will exist. Download the Lombok jar, double click it and tell it where your IDE installation directory is and it will go and install itself into it. Your Eclipse will now list the getters and setters which you haven’t written!
Auditing of Entities
Hardly a project goes by these days without some sort of requirement for auditing records for legal purposes or storing revisions of entities for purely historical reasons. I only recently discovered the Hibernate Envers project.
It’s an addon module for Hibernate that allows you to annotate your Entity beans as @Audited and add a few listeners to your persistence.xml and it will then create audit tables storing previous revisions of annotated Entities. It’s beautifully simple and from Hibernate4 you’ll no longer even have to add the listeners.
In the meantime, if you do want to use Envers, you’ll need to add some or all of these.
Upgrading to Ubuntu 11.04 over SSH
These days I spent most of my time connected via a Macbook Air, one I haven’t yet installed Linux on. I decided to update the Mac Mini which sits under my TV running Boxee though SSH and my normal:
$ update-manager -drefused to do it over SSH for Health & Safety reasons! These days though there’s a nice text based alternative:
$ do-release-upgrade -dTwo new APIs from JBoss
JBoss Application Server 7 has now moved into beta. It’s brought with it two new APIs which are approaching their first 1.0 releases; ShrinkWrap and Arquillian.
The ShrinkWrap API is designed to allow you to build Java archives (JARs, WARs, etc.) using Java itself. And it’s painlessly simple:
JavaArchive archive = ShrinkWrap.create( JavaArchive.class,"archive.jar") .addClasses(MyClass.class, MyOtherClass.class) .addResource("mystuff.properties");
Arquillian builds upon ShrinkWrap to give you an API which abstracts away all the container lifecycle and deployment configuration from integration tests, simplifying the testing of applications which require a JEE container. Again, another simple, clean API:
import static org.junit.Assert.assertEquals; import javax.ejb.EJB; import org.jboss.arquillian.api.Deployment; import org.jboss.arquillian.junit.Arquillian; import org.jboss.shrinkwrap.api.ShrinkWrap; import org.jboss.shrinkwrap.api.spec.JavaArchive; import org.junit.Test; import org.junit.runner.RunWith; @RunWith(Arquillian.class) public class GreetingManagerTest { @Deployment public static JavaArchive createDeployment() { return ShrinkWrap.create(JavaArchive.class, "test.jar") .addClasses(GreetingManager.class, GreetingManagerBean.class); } @EJB private GreetingManager greetingManager; @Test public void shouldGreetUser() throws Exception { String name = "Earthlings"; assertEquals("Hello, " + name, greetingManager.greet(name)); } }
Add the following to your ~/.bashrc to have your bash prompt display your working git branch and whether or not you have made any modifications, additions or deletions. Note the dependency on the git-completion bash script witihin your git install.
# Funky git PS1 status. GIT_COMPLETION_PATH="/usr/local/git/contrib/completion/git-completion.bash" if [ -f "$GIT_COMPLETION_PATH" ]; then GIT_PS1_SHOWDIRTYSTATE=true . "$GIT_COMPLETION_PATH" ADD_PS1='$(__git_ps1)' fi PS1="\[\033[01;32m\]\u@\h\[\033[01;34m\] \w\[\033[33m\]$ADD_PS1\[\033[34m\] \$\[\033[00m\] "
20% time for software engineers
There are a collection of difficulties which affect many modern software-focused companies; how to innovate new products and ideas, how to improve and automate processes, how to schedule time for working on such improvements which cannot be directly sold to an existing customer.
There are companies out there though which have found a solution that works and for the likes of Google, Atlassian and others this is what they call 20% time. 20% time is where software engineers spend 20% of their time at work on projects of their own choosing, regardless of how or if it relates to their core projects.
Implementing this has a few difficulties, but the biggest hurdle has to be simply persuading a company to make that leap of faith and just try it.
http://blogs.atlassian.com/developer/2009/03/20_percent_continuing.html
http://www.youtube.com/watch?v=u6XAPnuFjJc
Looping 1 to 1000 in Java without using conditionals
A colleague suggested a Java puzzler today; can you write a program to print the numbers 1 to 1000 without using conditionals? No loops, no checking for equality. I would never want to use this code, but this was my suggestion:
public class Test { public static void main(String[] args) { int i = 0; int[] array = new int[1000]; incrementAndPrint(i, array); } private static void incrementAndPrint(int i, int[] array) { System.out.println(++i); try { int x = array[i]; } catch (ArrayIndexOutOfBoundsException e) { System.exit(0); } incrementAndPrint(i, array); } }
I wonder if there is a way to do it without exceptions. I suppose there must be…