Review of „Getting started with Google Guava“

I recently read the book Getting Started with Google Guava by Bill Bejeck, published by PACKT Publishing. I have been working with Google Guava a long time and I was surprised how many new things I learned from the book, because I thought I knew this library pretty well.

The book itself is very practical and there are tons of code examples. I especially liked the fact that most code examples are written as JUnit tests, so you immediately get a feel of how to use the utility class under discussion and in which context they can be used. I want to discuss each chapter in detail now:

Basic Guava Utilities

This chapter discusses utils from Guava which you will find in many projects. The author explains the advantages of the Guava classes for String manipulations like Splitter, Joiner, Strings and Charmatcher. This chapter is pretty straightforward and if you don’t know these utilities yet, you’ll immediately see how these utils can simplify your life. Guavas Preconditions class and different builders for toString(), hashCode() and equals() methods are also discussed in this chapter.

Functional Programming

One of the most amazing features of the Guava library are the utilities to enable functional-style programming in Java. The author discusses in-depth when to use this functional paradigm and when it’s better to stick with the imperative classic Java approach. You’ll find many examples for the use of the Function and Predicate classes of Guava. Although Suppliers are introduced in this chapter, too, I personally think the Supplier classes were not discussed extensive enough because in my opinion Guavas Suppliers are way too underrated and I use them very frequently.

Collections

The Guava originated from the google-collections library and so it’s no surprise that a big part of Guava are utils for working with Collections or completely new collection classes. The book demonstrates the use of all relevant collection utils and covers additional collection classes like Immutable Collections, Table, BiMap, etc. This chapter also introduces the Ordering class as a complement to classic Comparators.

Concurrency

Although very short, this is one of the chapters I enjoyed most in the book. It covers the most important classes in Guava for dealing with concurrency and introduces Monitor, ListenableFuture, FutureCallback, AsyncFunction and RateLimiter. I learned a lot here and especially Monitor and ListenableFuture are classes which I intend to use more. Especially ListenableFuture is interesting as it can completely replace classic Java Futures and are more powerful.

Cache

Guavas Cache implementation is very useful and is a lightweight alternative to libraries like EHCache. It’s widely adopted and so it’s no surprise that the Cache gets an extra chapter. For people who are looking for a lightweight, configurable and powerful local cache implementation, this chapter covers everything you will ever need to know about Guavas Cache.

Eventbus

I was excited to see that Guavas Eventbus is covered in a whole chapter because I use the EventBus regularly and it’s an easy and lightweight way to decouple your application. The chapter discusses how to use the event bus and and what advantage you get in your application using it. The code examples are great and show how easy it is to decouple your application components.
Finally it’s discussed how to integrate the Eventbus with your Dependency Injection Framework for convenient usage. The code examples are for spring which is a bit irritating, I personally think Google Guice would be a better fit. The concepts for integrating are the same, though.

Files

The eight chapter discusses the shortcomings of classic Java File handling and how Guava fills this hole. It introduces many utility classes for manipulating files, file contents and folders. I personally get bored quickly when reading about file handling, but this is nothing I can blame the author for.

Other utilities

The last chapter covers some other utilities form Guava which are worth mentioning. It introduces Hashing functionality, Bloom Filters, and the super handy Optional class. I am using the later heavily and I think this class should have deserved more than just a very short introduction.

Conclusion

I was very excited from the book, it covers all relevant utils from Guava thoroughly and I learned a lot. The author decided to let code speak and included many code snippets for every topic discussed in the book. I can absolutely recommend this practical book for anyone who is not familiar with Guava (yet) and even long time users of the library will learn some new and exciting stuff.

Large File Downloads with JBoss Seam 2 and Servlets

Today I worked an an issue which presented users of our application a 403 error page when downloading large files which were generated on the fly. The files were delivered by a servlet and the files were directly copied to the ServletOutputStream, so the files were streamed instead of holding them in memory.

After some debugging it turned out, that the files were not streamed since the Seam Ajax4JSF Filter doesn’t allow to stream files and this resulted in OutOfMemoryExceptions. There are a few ways how to disable or extend the Ajax4JSF Filter (see this Seam Forum Thread).

Since all our download resources are mapped to servlets which resides in the „/media/*“ path, it was pretty easy to just enable the Ajax4JSF Filter for all paths except the „/media/*“ paths. To achieve this, just add to your components.xml:

<web:ajax4jsf-filter regex-url-pattern="^(?:[^/]|/(?!media/))*$"/>

Set Context Root for a JavaEE 6 Application with JBoss 7.1

Per default, a Web Application which is packaged as a WAR file is mapped to „http://jbossurl:port/war_file_name“ when deployed to JBoss AS 7.1. We wanted the application to be mapped to the root context instead of the file name. It turns out, that this is pretty easy to achieve:

  1. Edit standalone.xml in the JBoss configuration directory. Edit the relevant part of the file like this:
    <subsystem xmlns="urn:jboss:domain:web:1.0" default-virtual-server="default-host">
        <connector name="http" protocol="HTTP/1.1" socket-binding="http" scheme="http"/>
        <virtual-server name="default-host" enable-welcome-root="false">

    The important part is the enable-welcome-root=“false“.

  2. Create a file „jboss-web.xml“ in the WEB-INF folder of your application.
  3. Add the following to the file:

    <?xml version="1.0" encoding="UTF-8"?>
    <jboss-web>
        <context-root>/</context-root>
    </jboss-web>

Now your Application will be mapped to the context-root of the Application Server and should be accessible with „http://jbossurl:port“.

Programatically Login User with Seam 2 Security Framework

This is a quick one:

When you want to login a user programmatically without checking if the credentials are right or the user is in the IdentityStore with Seam 2, that is pretty easy.

 
final Principal principal = new SimplePrincipal("username");
identity.acceptExternallyAuthenticatedPrincipal(principal);
identity.authenticate();

This will override the Seam 2 Security Framework authentication mechanism and a user with the given username will be logged in, even if the user is not in the IdentityStore of the Application.

I use this regularly when I have some sort of administration interface where I cannot use the default IdentityStore of the application. In this case I provide an own implementation of the required authentication mechanism and just log in the the user with the approach described above. When doing this I add a role to the logged in user (via identity.addRole("myRole")), so I can use all the Seam goodies like the Authorization Annotations (@Restrict) or checks like Identity.loggedIn.

SLF4J Logger Injection with Guice

I experimented with Google Guice Custom Injections because I wanted to inject a Logger to my guicified application. There is a great part in the Guice documentation about Custom Logger Injection, but in my case I wanted a SLF4J Logger with Logback as implementation. The example in the Guice Wiki is with Log4j Injection.

Here is how I wanted to inject loggers:

1
2
3
4
5
6
@Log
Logger log;
 
public void doSomething{
  log.info("Logmessage");
}

Weiterlesen

Different ways to redirect with JSF and Seam

This is just a quick reminder for myself how to implement programmatic redirects with JBoss Seam and JSF.

I will describe three different techniques:

  • Redirecting to a 404 Error Page from a JSF Backing Bean
  • Redirecting to an external Website
  • Redirecting to a JSF view in a Seam Application

Weiterlesen

Time consuming processes with Seam, Richfaces a4j:poll and Quartz

Today I had a hard time implementing time-consuming processes with AJAX status updates. I stripped down everything to a minimal working example. The technologies used in this example are

  • Seam 2.2.2
  • Richfaces 3.3
  • Quartz 1.6

So here is what I wanted to achieve: The user of my application should click a button and in the background, a long running process should do some database stuff. Since this could take a while, I wanted to show some progress to the user. I did not want a progress bar, but I wanted to display some text to the user, depending on what the long-running tasks current status was.

I stripped it all down to an minimal example, which starts a long running process (huge for loop which generates random UUIDs) when a button is clicked and displays the current UUID to the user. With this basic example I hope you can get the ideas behind the a4j:poll component and how to do asynchronous long-running tasks with Seam. I must admit, that I was heavily(!) inspired by Andrey Chorniys blog post Show dynamic progress of time-consuming process in Seam/RichFaces where I shamelessly stole code and adapted it to my needs.

Here are the neccessary steps to get started

Weiterlesen

Brand new useful Netbeans Code Templates

I am using Netbeans for a few weeks now and I have to admit, that I really enjoy this IDE. But my happiness with the IDE is not what I want to blog about today :)

Programmers are (or should be) lazy. We do not want to write the same code again and again. Fortunately every sophisticated IDE has some kind of Code Template mechanism, so does Netbeans. Here are some of my created Templates which I think other people could find it as useful as I do:
Weiterlesen

Upgrade Seam Project froom 2.2.1.CR1 to 2.2.1CR2

We had a Problem with JBoss Seam 2.2.1.CR1 Logger. We were not able to log with parameters. (This is a known bug: https://jira.jboss.org/browse/JBSEAM-4606 )

I was looking for a tutorial which covers upgrading Seam, but I did not find any. So this is my solution how to upgrade from Seam 2.2.1.CR1 to Seam 2.2.1.CR2:

It is kind of a trivial job, because in normal case you just have to replace libraries. Here is a step to step tutorial:

Weiterlesen

[m2eclipse] Start Eclipse with JDK instead of JRE

If you install the m2eclipse Plugin for the  Maven Integration in Eclipse, this Error could possibly be shown in the Eclipse console:

Eclipse is running in a JRE, but a JDK is required
Some Maven plugins may not work when importing projects or updating source folders.

If you have this issue, you should edit your eclipse.ini and add the following lines (on a Windows System) :

-vm
C:\path\to\JDK\bin\javaw.exe

Now your Eclipse should use the JDK to run with and the m2eclipse plugin should be happy.