Pages

Thursday, April 25, 2013

After I did the JAX-RS Tip of the Day today, I wondered about reading the authorization header from JSF. The technique is the same as the JAX-RS version, but the methods are different depending on what is available to the JSF application. The JAX-RS Base64 class is not part of the web profile in Java EE 6. It will be part of Java EE 7 so you could use it. I chose to add the comments in the code below, but decided that I would use the com.sun.misc.Base64Decoder which is currently available in Java SE 6 and 7.UPDATE: I got a suggestion on Google+ from +Thomas Darimont who mentioned using DatatypeConverter.parseBase64Binary() from the Java API for XML Binding (JAXB). I confirmed that it is available in Java EE 5 and 6. It is also in the Web Profile in Java EE 6.

I was looking for a simple method to read the HttpHeaders to determine the username and password for a request. I didn't find a quick answer so I wrote a simple utility method that will return the Base 64 decoded values from the authorization header.

Thursday, April 11, 2013

Introduction

I was asked to look at various charting solutions for JSF. Specifically, I was asked for some choices for use with RichFaces 4.3.1. I knew that PrimeFaces has really good chart components already so I knew it might be kind of fun to integrate them if possible. I was also aware of JFreeChart which will work, but produces some... rather ugly charts. Finally, someone had suggested that I look at jsflot. This was a very promising solution. This project is a proof of concept which demonstrates integration between projects, and technologies.

Wednesday, April 10, 2013

Introduction

This is hopefully one simple example of how to make a "back" button when an exception occurs, and sends you to an exception page. The question becomes "How do I get back to the page where the exception occurred?" The navigation back to the offending page is possible, but you want to make sure that you handle the exception, or you may get into a cycle.

A solution was suggested to me by my friend Markus Eisele using an ExceptionHandler in JSF. I had used exception handlers in the past, and thought that it was a simple and elegant idea. The code I am providing below DOES NOT handle the exception. This is specific to your implementation. This is just a handler that sits on top of your exception hierarchy, and provides a convenient mechanism to navigate back.

Tuesday, April 09, 2013

Background

I have been asked to interview a number of engineers over the last couple of years. We were interested in hiring an engineer with a background in JSF specifically PrimeFaces, Java EE 6, and NetBeans.

Basically I wanted someone...like me.

I came into work one day, and I was told that I needed to interview someone in about an hour about a position. The person claimed experience in all of the aforementioned technologies. I needed to come up with a coding exam idea for them. Tick tock tick tock.

I decided to create a NetBeans project using the sample Java DB database, JPA, EJB facades, and a JSF front end using PrimeFaces with a pie chart. The whole process took about 7 minutes to create. When the engineer arrived, I showed him the result and said "I want to see the same results", and explained that I used the sample database from NetBeans.

The engineer was successful and created something similar in about 30 minutes, and additionally showed me up by adding percentage tags to the chart. We hired him after the interview. I am glad to say he is an integral part of our team today.

Coding Exam

What do I want?

Please create a JSF application using PrimeFaces that displays a pie chart using data provided. The application must be developed using NetBeans and Java EE 6 technologies. The example data is provided using the sample Java DB database in NetBeans We would like to have a pie chart that shows sales totals by customer as a percentage of all sales. You have 1 hour to complete the code, and will be required to explain your design decisions, and results.

Note: I just showed them the pie chart that was generated in the browser and not the actual code. I don't want to sway the candidates decision making, or give them hints on how to solve the problem.

Saturday, April 06, 2013

I got an email from a developer a couple of days ago that was complaining that the filters he implemented were not executing on the url patterns he had set. The email was not explicit enough for me to determine if the ordering was an issue too. I created a simple project that shows how to use @WebFilter (Servlet Filters). I was going to dispose of the code, but I thought it might have some redeeming qualities for those who may need a working example.

I have commented out the @WebFilter annotations so that the web.xml controls the ordering. If you uncomment the @WebFilter annotations, you will need to comment the lines in the web.xml. Doing so will produce the same issues reported to me about execution and ordering.

A friend of mine asked me if there was a list of reserved words in EL and JSF. He had previously looked for it, and after some Google searching I didn't find a comprehensive list anywhere. I decided to create one for him and anyone else who may need it. If you are aware of any other keywords, please post a comment and I will add them to the listings.

Expression Language (EL) Reserved Keywords

Value

Description

and

Logical operator alternative to (&&)

false

Boolean literal

le

Relation operator less than or equal to alternative to (<=)

not

Logical operator reverse alternative to (!)

div

Arithmetic operator division alternative to (/)

ge

Relational operator greater or equal to alternative to (>=)

lt

Relational operator less than alternative to (<)

null

Null literal

empty

The empty operator is a prefix operation that can be used to determine whether a value is null or empty.

gt

Relational operator greater than alternative to (>)

mod

Arithmetic operator modulo alternative to (%)

or

Logical operator alternative to (||)

eq

Logical operator alternative to (==)

instanceof

Java Keyword to do a Class comparison between Objects

ne

Relational operator not equal alternative to (!=)

true

Boolean literal

JSF 2.x Expression Language (EL) Implicit Objects

Value

Description

application

This provides user access to the ApplicationContext implementation of ServletContext that represents a web application's execution environment.Note: This is not related to the JSF Application object.

applicationScope

Maps application-scoped variable names to their values.

cc

Implicit EL object that provides access to the JSF Composite Component. cc refers to the top level composite component processed at the time of evaluation

Provides user access to the EL implicit javax.faces.context.Flash object. It may additional obtained via #{facesContext.externalContext.flash}. The implementation must ensure that the flash is usable from both JSP and from Facelets for JSF 2.

Tuesday, April 02, 2013

I have some code that I originally wrote for a proof of concept for doing some RichFaces<rich:tree /> examples. The modified code in these examples demonstrate how to use custom TreeNode, and TreeDataModel implementations to make really nice and functional trees. The code for my generic TypedTreeNode and TreeNodesSequenceKeyModel are fully functional and can be used out of the box as the basis of your own tree implementations.

The examples include the model provided by RichFaces, my custom model (TreeNodesSequenceKeyModel), and a custom implementation of a node. The custom node is generic so you can pass in any object you like. I chose to use text, but you could use a more complex object.

The custom model, custom node, and tree are shown in the image on the right. This also has events being shown when a node is selected, or toggled.

Monday, April 01, 2013

I saw a question posted a couple of months ago on stackoverflow, or a forum. I can't remember where I saw it actually. The question was how do I determine what classes are currently loaded by the ClassLoader when an application is loaded and running. A number of folks posted various solutions like using java -verbose which are of limited help. Another solution was to get using something like ClassLoader.getSystemClassLoader();. The latter looks very promising, but is wrong. I knew that there are a number of classes that are loaded that this would not display.

Ssssh... I will show you how I know.

The problem and solution is surprisingly non-trivial. I thought I would come up with a solution like the one above in 5 minutes. I did come up with one above in about that much time. It turns out to be incorrect.

The solution is to use a Java agent to instrument the JVM and see what it is loading. I am sure a number of you have seen the -javaagent:[=] flag for the VM and wondered what is that for. I am going to show you.

The first value all indicates all of the classes loaded by the JVM. That is a lot of classes. This is via an Instrumentation agent.

The second value system indicates all of the classes loaded by the System ClassLoader. This is significantly less than loaded by the JVM. This is via an Instrumentation agent.

The third value is the appLoader which is the application classloader. It matches the System, but this may not always be the case. This is via an Instrumentation agent.

Finally, the last value classes is what you get from the ClassLoader without instrumentation. It is a paltry amount of the total classes loaded.

So which one is right? Good question... Here is an answer only a parent, or teacher can give. "It depends."

If I am looking at everything being loaded to check for something forensically I would use the 815 and look at what these classes are and where they came from. If I am checking which classes are loaded to help with reflection, I would look at the 61.