Tuesday, September 17, 2013

JSF/CDI Tip of the Day: How to create a @Producer for JSF

I was using this code as a utility class to get the current HttpServletRequest object. I added the @Named annotation to expose it via Expression Language (EL). However, it should be noted that the #{request} is an implicit object already. You can see from the output below that they do produce the same output.

ServletRequestProducer.java


JSF 2.x Tip of the Day: Resetting the Current UIViewRoot

I needed to clear the UIViewRoot for something I was working on. In the process, I created a simple method that replaces the current view with a new view using the same view ID. I figured I would publish this little gem before I deleted the proof of concept I was working on.

I also thought I should include an example where I am using it to navigate to another page.

Wednesday, September 11, 2013

JAX-RS 1.x Tip of the Day: Using POST with JAX-RS

http://commons.wikimedia.org
I was looking over some more unpublished code, and found a simple gem that demonstrates how to use a HTTP POST with JAX-RS and a @Singleton.

The code was originally developed with JAX-RS 1.0, but I went back and retrofitted it to work on JAX-RS 1.1. The code has a @Singleton that manages the "Widgets" that are created using the HTTP POST. It is a simple and fun example of what you can do with JAX-RS.

It is a simple application that creates "Widget" objects and returns JSON with the widget data.

The code for the project can be found here: jersey-post-example

Note: This example application needs to be run on GlassFish 3.1.2+ because of an issue with Guava GlassFish 4.

JSON Response


WidgetSingleton.java



ExampleResource.java



Widget.java


Monday, September 09, 2013

JSF 2.1 Tip of the Day: Creating a Custom Switch List

I found some code where I was tweaking on an example that Jim Driscoll wrote as a series of articles on Java.net.

I decided that I should publish it along with the references to the other articles.

JSF has a number of component frameworks available from groups like PrimeFaces and RichFaces. These are better choices than this code to accomplish the same thing.

However, you don't need all the additional code to do the same thing with plain old vanilla JSF. You will just need to write all the code.

The code for this project can be found here: switchlist-example

index.xhtml



ListHolder.java



References

ExecutorService and CountDownLatch Example

Image from Jeff Blogs
One of the most difficult concepts in teaching programming is threads. If you don't do a lot of thread programming, that knowledge has a quick way of being replaced in your mind.

Today, modern frameworks especially Java EE make thread programming even less apparent to the developer.

I had a very highly asynchronous application that was generating data based on examining data returned from a measurement sensor in a grain silo. The sensor was generating a lot of data especially as the silo was being filled, or emptied. I had a Java application that was generating real-time data that would start/stop various hardware based on how full, or empty the silo was. This application was also generating data charts. We have one big giant potential mess if the application does not work right. The Java Concurrency API came to the rescue. The charting data could be kicked off using CountDownLatch to ensure that we didn't start generating a new chart until the last chart was generated from the data. The sensor was generating more data than our application could chart in real-time.

In the last year, I have taught a couple of programming courses to our JUG, and invariably I need to cover Thread programming.

This is an example application that shows what happens if threads are left to their own devices. This example "works" sometimes, but usually the counter does not match up correctly with the other counters. You may need to run it a couple of times to "see" it break.

If you have a simple example that demonstrates this concept, please let me know. I am always looking for good ideas.

Here is the project code: ExecutorLatchExample

Executor.java


Runner.java


StaticCounter.java


Woodstock File Upload Example


Here is another example from my code proof of concept archives.

As many of you may know, I was a big proponent of using Project Woodstock (Visual JSF). I still believe that Visual JSF and NetBeans were a great combination. In this project, you will see a couple of example applications for doing file uploads. The project was built using NetBeans 6.7.1 with the Visual JSF libraries and runtime installed.

Using the older NetBeans IDE will allow you to see Visual JSF and why it was cool in addition to getting the correct libraries installed.
Visual JSF was the "killer" framework that got me interested in JSF. It is a JSF 1.2 based framework. If you have need for nice looking components and a really easy framework + IDE combination, it is a good choice.

The code for the project can be found here: WoodstockFileUploadExample

Here is the download page visually in the IDE.


Page1.jsp


Page2.jsp

The code is really simple to create and use via NetBeans enjoy. This code will work on Java EE5, EE6, and EE7.

Sunday, September 08, 2013

Apache Commons IO File Monitoring Example

Here is another example from the archives of the mythical archana on File monitoring using Apache Commons I/O for file monitoring. I remember working on this to see if we could come up with a solution since we could not upgrade the application to Java SE 7. So this proof of concept was to try out a couple of different file monitoring options.

This example uses a FileAlterationListener to monitor file changes. It is simple to implement.

The code for the project can be found here: file-monitor

Friday, September 06, 2013

Apache Commons File Upload Servlet Example

I was doing some code cleanup, and found this servlet that does file upload using Apache Commons Fileupload.

I have a couple of other articles and code that are better, but I thought I would publish this just as another example. I did a quick code search and it appears I was using some code example snippets from the Apache Commons fileupload site, some forum posts, and an article OnJava on the O'Reilly & Associates site: Using the Jakarta Commons, Part 1.

Simply put, the provenance of this code is questionable. If you contributed to it, please send me a link to your code and I will add your name to the authors list. I don't want to claim the code as my own since I am sure I got it from multiple F/OSS sources.

Here are some of the other articles which I am sure I know about:
Here is the servlet code: apache-commons-file-upload

ApacheCommonsFileUploadServlet.java

Java EE Tip of the Day: @WebFilter Testing Using Arquillian, Warp, and Drone

Introduction

I was looking for a simple solution to test @WebFilter servlet filters. The tests that I had used a lot of mocks, but really just left me unsatisfied. It was not a real world production solution. How would they "really" behave in the actual server environment. I had been using Arquillian to do some testing of our CDI layer, and decided to try it on filters.

I looked at the API, and examples and found really nothing. I found one really "bogus" test example from core api tests that left me really wondering if I should bother. As many of you know though, I love a challenge. I tried a number of ideas including making the filter a JSR-316 @ManagedBean. I am not sure what glue I sniffed for that one (it didn't work...), and using @Inject (that didn't work either... more glue).

What I settled on was using Arquillian Warp and Drone to check the HttpServletRequest and HttpServletResponse after the filter processed it. There are a couple of annotations @BeforeServlet and @AfterServlet that helped me with checking my filters. There are no before and after filter annotations though. That would have made life easier.

Note: The thing to keep in mind with these annotations is that the filtering will have already occurred. You are looking at the post-filter results.

I also wanted to be able to demonstrate another really cool feature called ShrinkWrap, and its Java EE descriptor extensions. This allows you to programmatically create your web.xml file to provide additional flexibility like overriding annotations.

Problem Description

We want to be able to test a @WebFilter that modifies HttpServletRequest attributes, and HttpServletResponse headers. The filter should be tested in isolation, and in a real container to examine the real-world response. The tests should be able to be run on a variety of containers with no modification to the test code.

A Solution

Using MavenArquillianWarpDroneShrinkWrap, and GlassFish 3.1.2.2 embedded we can accomplish all aspects of the testing requirements. I used GlassFish since it is the Reference Implementation for Java EE 5, EE 6 and EE7, and it is a great application server. The container can be swapped out easily in maven with another profile. In the example code below, I used NetBeans 7.4 Beta to do the development.

The code for the project can be found on BitBucket here: maven-arqullian-filter-test

There are two types of examples included. One set of examples demonstrate simple integration tests that use annotations, and annotation override using web.xml. The second set of examples demonstrate dynamic web.xml generation, and overriding. Here are some code snippets from the project.

Popular Posts