Simple authentication with java bean

J2EE offer different ways to check user authentication and authorization (JAAS, Apache SHIRO, Servlet Filters), often for demo software (or small ones) what you really need is a simple Session Bean as EventListener.

This ipotetic Session Bean manages user redirections between JSF views; consider this example with two views:

If a Guest user goes to http://localhost:8080/demo/business.xhtml he is gets redirected to loginform.xhtml, the Listener method save the originating URI (business.xhtml) and, if the auhentication is successful, brings the user back to the business view.

Setting the Event in view permit us to deny the access calling SessionBean.verifyUserLogin():

<f:event type="preRenderView" listener="#{SessionBean.verifyUserLogin()}"></f:event>

Continue reading “Simple authentication with java bean” »


Java code example: simple singleton lock using with Atomic variables

I needed a very simple lock system for a software demo for operation on HashMap, instead of using ReentrantLock from java.util.concurrent.locks i chose to use Atomic classes from java.util.concurrent.atomic.* .

My demo has a very LONG blocking thread that writes on a shared HashMap, the following class permits me to tune the sleep time for the lock without using a syncronized queue.
Continue reading “Java code example: simple singleton lock using with Atomic variables” »