Saturday, 28 April 2012

Filtered JTree

The solutions (1, 2) out there for filtering a JTree's nodes weren't suitable for me, so I rolled by own FilteredTreeModel class which wraps a JTree's underlying TreeModel and applies a string filter to the node names.

The tree model recurses over the tree nodes, from the root to the leaf nodes, checking if the nodes toString() value contains the filter value:

FilteredTreeModel.java

package org.adrianwalker.filteredjtree;

import javax.swing.event.TreeModelListener;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;

public final class FilteredTreeModel implements TreeModel {

  private TreeModel treeModel;
  private String filter;

  public FilteredTreeModel(final TreeModel treeModel) {
    this.treeModel = treeModel;
    this.filter = "";
  }

  public TreeModel getTreeModel() {
    return treeModel;
  }

  public void setFilter(final String filter) {
    this.filter = filter;
  }

  private boolean recursiveMatch(final Object node, final String filter) {

    boolean matches = node.toString().contains(filter);

    int childCount = treeModel.getChildCount(node);
    for (int i = 0; i < childCount; i++) {
      Object child = treeModel.getChild(node, i);
      matches |= recursiveMatch(child, filter);
    }
    
    return matches;
  }

  @Override
  public Object getRoot() {
    return treeModel.getRoot();
  }

  @Override
  public Object getChild(final Object parent, final int index) {
    int count = 0;
    int childCount = treeModel.getChildCount(parent);
    for (int i = 0; i < childCount; i++) {
      Object child = treeModel.getChild(parent, i);
      if (recursiveMatch(child, filter)) {
        if (count == index) {
          return child;
        }
        count++;
      }
    }
    return null;
  }

  @Override
  public int getChildCount(final Object parent) {
    int count = 0;
    int childCount = treeModel.getChildCount(parent);
    for (int i = 0; i < childCount; i++) {
      Object child = treeModel.getChild(parent, i);
      if (recursiveMatch(child, filter)) {
        count++;
      }
    }
    return count;
  }

  @Override
  public boolean isLeaf(final Object node) {
    return treeModel.isLeaf(node);
  }

  @Override
  public void valueForPathChanged(final TreePath path, final Object newValue) {
    treeModel.valueForPathChanged(path, newValue);
  }

  @Override
  public int getIndexOfChild(final Object parent, final Object childToFind) {
    int childCount = treeModel.getChildCount(parent);
    for (int i = 0; i < childCount; i++) {
      Object child = treeModel.getChild(parent, i);
      if (recursiveMatch(child, filter)) {
        if (childToFind.equals(child)) {
          return i;
        }
      }
    }
    return -1;
  }

  @Override
  public void addTreeModelListener(final TreeModelListener l) {
    treeModel.addTreeModelListener(l);
  }

  @Override
  public void removeTreeModelListener(final TreeModelListener l) {
    treeModel.removeTreeModelListener(l);
  }
}

Example Application

JTree before and after filtering

Below is a simple example of how the FilteredTreeModel can be used. The tree is filtered using the value of the text field as characters are typed.

FilteredJTreeExample.java

package org.adrianwalker.filteredjtree;

import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;

public final class FilteredJTreeExample {

  public static void main(final String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {

      @Override
      public void run() {
        createAndShowGUI();
      }
    });
  }

  private static void createAndShowGUI() {
    JFrame frame = new JFrame("Filtered JTree Demo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    addComponentsToPane(frame.getContentPane());

    frame.pack();
    frame.setVisible(true);
  }

  private static void addComponentsToPane(final Container pane) {
    pane.setLayout(new GridBagLayout());

    JTree tree = createTree(pane);
    JTextField filter = createFilterField(pane);

    filter.getDocument().addDocumentListener(createDocumentListener(tree, filter));
  }

  private static JTree createTree(final Container pane) {
    DefaultMutableTreeNode root = new DefaultMutableTreeNode("JTree");
    FilteredTreeModel model = new FilteredTreeModel(new DefaultTreeModel(root));
    JTree tree = new JTree(model);
    JScrollPane scrollPane = new JScrollPane(tree);
    GridBagConstraints c = new GridBagConstraints();
    c.weightx = 1;
    c.weighty = 1;
    c.fill = GridBagConstraints.BOTH;
    c.gridx = 0;
    c.gridy = 1;
    pane.add(scrollPane, c);
    createTreeNodes(root);
    expandTree(tree);

    return tree;
  }

  private static JTextField createFilterField(final Container pane) {
    JTextField filter = new JTextField();
    GridBagConstraints c = new GridBagConstraints();
    c.weightx = 0;
    c.weighty = 0;
    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridx = 0;
    c.gridy = 0;
    pane.add(filter, c);

    return filter;
  }
  
  private static DocumentListener createDocumentListener(final JTree tree, final JTextField filter) {
    return new DocumentListener() {

      @Override
      public void insertUpdate(final DocumentEvent e) {
        applyFilter();
      }

      @Override
      public void removeUpdate(final DocumentEvent e) {
        applyFilter();
      }

      @Override
      public void changedUpdate(final DocumentEvent e) {
        applyFilter();
      }

      public void applyFilter() {
        FilteredTreeModel filteredModel = (FilteredTreeModel) tree.getModel();
        filteredModel.setFilter(filter.getText());
        
        DefaultTreeModel treeModel = (DefaultTreeModel) filteredModel.getTreeModel();
        treeModel.reload();
        
        expandTree(tree);
      }
    };
  }

  private static void expandTree(final JTree tree) {
    for (int i = 0; i < tree.getRowCount(); i++) {
      tree.expandRow(i);
    }
  }

  private static void createTreeNodes(final DefaultMutableTreeNode node) {
    DefaultMutableTreeNode ab = new DefaultMutableTreeNode("ab");
    DefaultMutableTreeNode cd = new DefaultMutableTreeNode("cd");
    DefaultMutableTreeNode ef = new DefaultMutableTreeNode("ef");
    DefaultMutableTreeNode gh = new DefaultMutableTreeNode("gh");
    DefaultMutableTreeNode ij = new DefaultMutableTreeNode("ij");
    DefaultMutableTreeNode kl = new DefaultMutableTreeNode("kl");
    DefaultMutableTreeNode mn = new DefaultMutableTreeNode("mn");
    DefaultMutableTreeNode op = new DefaultMutableTreeNode("op");
    DefaultMutableTreeNode qr = new DefaultMutableTreeNode("qr");
    DefaultMutableTreeNode st = new DefaultMutableTreeNode("st");
    DefaultMutableTreeNode uv = new DefaultMutableTreeNode("uv");
    DefaultMutableTreeNode wx = new DefaultMutableTreeNode("wx");
    DefaultMutableTreeNode yz = new DefaultMutableTreeNode("yz");

    node.add(ab);
    node.add(cd);
    ab.add(ef);
    ab.add(gh);
    cd.add(ij);
    cd.add(kl);
    ef.add(mn);
    ef.add(op);
    gh.add(qr);
    gh.add(st);
    ij.add(uv);
    ij.add(wx);

    node.add(yz);
  }
}

Source Code

Usage

Build and install the Filtered JTree project, using 'mvn clean install'.

Run the FilteredJTreeExample class, and enter characters in the filter text field.

Monday, 5 March 2012

Java 7 New Features Cookbook Review

Java 7 is the latest major release of the Java Language from Oracle. This release contains a number of language enhancements and new APIs, including better exception handling, new threading mechanisms and additions to the core libraries.

Packt Publishing requested that I review one of their titles about Java 7: Java 7 New Features Cookbook by Richard M. Reese and Jennifer L. Reese, available to buy from Packt's website.

Java 7 New Features Cookbook is an OK, but sometimes inconsistent book. I found parts of it very informative and other parts frustrating, with errors here and there in the example code – overall I'd give it an average score. 

The information in the book is very densely packed, but often repetitive and sometimes very slow reading. The information is presented in recipes - how-to examples focusing on a specific Java 7 feature, put together to form a cookbook.

Each recipe is presented in a formulaic way, with information under the sub-headings: 'Getting ready', 'How to do it...', 'How it works...' and 'There's more'. The code examples sufficiently cover the new feature they are trying to demonstrate, and are quite self contained, so you don't need to read the whole chapter to quickly get going with a new Java 7 feature. The 'There's more' sections expand on the basics, fleshing out the examples and include some good tips.

The book covers the full range of Java 7 new features, without going over old ground with respect to previous versions of Java. The book is definitely aimed at developers who are familiar with writing programs in Java 5 and beyond, and not for developers new to Java and OOP. A previous knowledge of Java file I/O, concurrency and a bit of SQL is required to get the most from this book.

This book was difficult to read from cover to cover, and I wouldn't suggest reading it that way. I would recommend consulting its relevant recipes before implementing new code.

A Java 7 JDK installation and an editor are requirements to reproduce the examples in the book. NetBeans 7 is stated as the IDE used to develop the code, but Eclipse or any other IDE or text editor could be used.

A note on the code examples in the book: the immediate thing you notice about the code examples is that they are badly formatted. This coupled with occasional errors in the code and new language syntax, such as try-with-resources, makes for frustrating debugging. The poor code layout is a problem throughout the book and spoilt my enjoyment of it slightly.

Chapter 1 covers new language features from Project Coin. These are small language enhancements designed to make code more readable, more useful and reduce verbosity. The useful new features to take away from this chapter are the new try-with-resources exception handling code, the use of the new AutoClosable interface, catching multiple exception types and using the diamond operator. 

This chapter is a good introduction to Java 7 and I think most developers will want to get to grips with all the recipes.

Chapter 2 introduces the new Paths and FileSystem objects, which are used extensively in subsequent chapters. The new classes provide utility methods to simplify the manipulation and comparison of file paths, helping to reducing some of the bespoke code you may have had to write in previous Java versions. The most important feature introduced in this chapter is the managing of symbolic links, but you will need to be running an operating system that supports them.

Chapter 3 deals with file and directory attributes. The first recipe demonstrates a useful new method for determining a files MIME type, something which has required 3rd party libraries in the past. The chapter goes on to give examples of managing file attributes, including support for DOS and POSIX file systems, very useful for managing Linux file permissions, which wasn't directly possible before.

Chapter 4 builds on the previous chapters and uses the new Files object to create, copy, move and delete files and directories with much more control, and much less code than was previously possible. Useful new features presented are temporary file and directory management and creating symbolic and hard links.

Chapter 5 has loads of useful information for interacting with file systems. Recipes include: accessing root file store information, processing directory contents with DirectoryStream, file filtering, globbing patterns, file event monitoring and more! Lastly in this chapter, the ZIP filesystem provider recipe demonstrates how to treat a ZIP or JAR file as though they were file systems.

Java is often criticised for the amount of code needed to perform simple file reading and writing operations. Chapter 6 shows how simple these operations can now be with Java 7, including new APIs for working with buffered I/O.  The new SeekableByteChannel class for random access I/O, and the new AsynchronousServerSocketChannel class for building client-server applications are recipes also worth visiting.

Thankfully, after half the book, this is the last chapter that deals with paths, files, files systems, file permissions and file I/O.

Chapter 7 showcases additions to swing which can improve the look of your GUI interfaces. Some of the cool looking new features include window opacity, varying gradient window translucency, and custom window shapes. Less exciting, but definitely useful, border type and print dialog box control recipes are also worth taking a look at in this chapter.

Chapter 9 contains a mix of new features, but has some really useful recipes. Firstly the RowSetFactory example shows how to really reduce the amount of JDBC boiler plate code needed to connect to a SQL database, execute queries and iterate over the returned data.

The next recipe demonstrates further database enhancements for reading database schema and pseudo-column information.

Another recipe worth a look include secure client-server communication using the ExtendedSSLSession interface, making use of the keytool, a serverside keystore and a client side trust store.

A new nested class, ProcessBuilder.Redirect, has been introduced to provide redirecting of input and output of external processes – useful for interacting with other executables and scripts.

I think chapter 9 is another chapter all developers will want to read.

Chapter 10's recipes deal with further concurrency enhancements to the Java language, which have been getting more and more powerful since Java 5. The new fork/join framework allows a task to be split into smaller parallelisable chunks, and then combining the results.

Other useful recipes in this chapter demonstrate using the new ThreadLocalRandom class for more efficient random number generation in a threaded environment. And the ConcurrentLinkedDeque example shows how concurrent threads can safely access the same collection data.

Chapter 11 contains bit and bobs which don't fit in other chapters. The Handling null references recipe introduces the new Objects class which provides utility methods for object comparison, null checking and hashing.

The JavaBean enhancements recipe shows how to use the new Expression class to programatically create and execute Java statements. Also the Introspector and BeanInfo class provide a new way of determining a classes' methods and fields without using the Reflection API.

Overall, I think this book is good in places, but repetitive and difficult to work though in others. About half the book is devoted to paths, files, filesystems and I/O, which I'm sure could be slimmed down a bit. I'd recommend using the book as a reference to consult rather than a book to pick up and read cover to cover.  Java 7 New Features Cookbook is definitely worth a read, and I will be using the things I've learned from it in future blog posts.

Downloads From Packt

Friday, 2 December 2011

Java Multiline String

Here is an implementation of multiline string literals in Java, using Javadoc comments, an annotation and an annotation processor.

This method works by annotating a String field with a @Multiline annotation, placing the fields initialisation value in a Javadoc comment, then using an annotation processor to set the fields value to the contents of the Javadoc comment at compile time.

First off, the @Multiline annotation.

Multiline.java

package org.adrianwalker.multilinestring;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.SOURCE)
public @interface Multiline {
}

Next, the annotation processor to insert the value of the Javadoc comment into the String field.

MultilineProcessor.java

package org.adrianwalker.multilinestring;

import com.sun.tools.javac.model.JavacElements;
import com.sun.tools.javac.processing.JavacProcessingEnvironment;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.TreeMaker;
import java.util.Set;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;

@SupportedAnnotationTypes({"org.adrianwalker.multilinestring.Multiline"})
public final class MultilineProcessor extends AbstractProcessor {

  private JavacElements elementUtils;
  private TreeMaker maker;

  @Override
  public void init(final ProcessingEnvironment procEnv) {
    
    super.init(procEnv);

    JavacProcessingEnvironment javacProcessingEnv = (JavacProcessingEnvironment) procEnv;
    this.elementUtils = javacProcessingEnv.getElementUtils();
    this.maker = TreeMaker.instance(javacProcessingEnv.getContext());
  }

  @Override
  public boolean process(final Set<? extends TypeElement> annotations, final RoundEnvironment roundEnv) {

    Set<? extends Element> fields = roundEnv.getElementsAnnotatedWith(Multiline.class);
    for (Element field : fields) {
      String docComment = elementUtils.getDocComment(field);
      if (null != docComment) {
        JCTree.JCVariableDecl fieldNode = (JCTree.JCVariableDecl) elementUtils.getTree(field);
        fieldNode.init = maker.Literal(docComment);
      }
    }

    return true;
  }
}

And finally, a class which demonstrates the usage of the annotation.

MultilineStringUsage.java

package org.adrianwalker.multilinestring;

public final class MultilineStringUsage {

  /**
  <html>
    <head/>
    <body>
      <p>
        Hello<br/>
        Multiline<br/>
        World<br/>
      </p>
    </body>
  </html>
  */
  @Multiline
  private static String html;
   
  public static void main(final String[] args) {
    System.out.println(html);
  }
}

Maven

Remember to include an <annotationProcessor> element when compiling classes which use @Multiline in the compiler plugin configuration when using Maven.

pom.xml

...
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin</artifactId>
  <version>2.3.2</version>
  <configuration>
    <source>1.6</source>
    <target>1.6</target>
    <annotationProcessors>
      <annotationProcessor>
        org.adrianwalker.multilinestring.MultilineProcessor
      </annotationProcessor>
    </annotationProcessors>
  </configuration>
</plugin>
...

Eclipse

To use in a non Maven Java project with Eclipse, try this: https://github.com/benelog/multiline/wiki/Non-Maven-Java-project-with-Eclipse

Source Code

Usage

Build and install the Multiline String project which contains the @Multiline annotation and annotation processor, using 'mvn clean install'.

Build and install the Multiline String Usage project which contains an example of how to use the @Multiline annotation, using 'mvn clean install'.

Run the MultilineStringUsage class to output the multiline string.

Monday, 11 July 2011

HTTP Proxy

Sometimes you just want a simple HTTP proxy without the hastle.

HttpProxy.java

package org.adrianwalker.httpproxy;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public final class HttpProxy {

  public static final int MAX_THREADS = 2;
  public static final int LOCAL_PORT = 9090;
  public static final String REMOTE_HOST = "localhost";
  public static final int REMOTE_PORT = 8080;

  public static void main(final String[] args) throws Exception {

    ServerSocket server = new ServerSocket(LOCAL_PORT);

    Socket remoteSocket = new Socket(REMOTE_HOST, REMOTE_PORT);
    while (true) {
      Socket localSocket = server.accept();

      ExecutorService executor = Executors.newFixedThreadPool(MAX_THREADS);
      executor.submit(new SocketStreamCopy(remoteSocket.getInputStream(), localSocket.getOutputStream()));
      executor.submit(new SocketStreamCopy(localSocket.getInputStream(), remoteSocket.getOutputStream()));
    }
  }

  public static final class SocketStreamCopy implements Callable<Void> {

    public static final int BUFFER_SIZE = 1024;
    private final BufferedInputStream in;
    private final BufferedOutputStream out;

    SocketStreamCopy(final InputStream in, final OutputStream out) {

      this.in = new BufferedInputStream(in);
      this.out = new BufferedOutputStream(out);
    }

    @Override
    public Void call() throws Exception {
      byte[] b = new byte[BUFFER_SIZE];
      int n;
      try {
        while ((n = in.read(b)) > 0) {
          out.write(b, 0, n);
          out.flush();

          System.out.write(b, 0, n);
          System.out.flush();
        }
      } finally {
        in.close();
        out.close();
      }

      return Void.TYPE.newInstance();
    }
  }
}

jboard - yet another imageboard

This time I've kept it super simple - no SQL databases, no EJB containers, no extra features, just a simple imageboard web app which writes to the file system.

Image resizing is handled by ImageMagick which needs to be installed and configured before you run jboard.

Source Code

Usage

Set the location of ImageMagick's convert binary in: src\main\resources\BoardConfiguration.properties

Run the project with 'mvn clean install tomcat:run-war' and point your brower at http://localhost:8080/jboard.

Monday, 4 July 2011

Java EE 6 Development with NetBeans 7 Review

NetBeans is Oracle’s (formerly Sun Microsystems’s) open source pure Java integrated design environment (IDE). Since starting to program in Java I’ve tried a few free IDEs, Eclipse, JEdit and IntelliJ IDEA Community, but I believe non are currently as intuitive, feature rich, reliable and well integrated with Java EE technologies as NetBeans.

In the past NetBeans has been considered to be slow and less feature rich when compared to Eclipse – these things are simply no longer true. Eclipse may still have a sight performance edge over NetBeans because of its native code components, but not enough to be bothered by, and more importantly everything you can do in Eclipse, you can do in NetBeans and loads more!

NetBeans is not only a great IDE for Java development, but also supports other languages. NetBeans is also my preferred IDE for C and Python development.

JavaEE 6 (JEE 6) is the latest Java Enterprise specification, consisting of EJB 3.1, JPA 2.0, JSF 2.0, Servlet 3.0 and other updates to the JavaEE stack. New additions include JAX-RS for RESTful web service development and CDI for dependency injection.

The JavaEE 6 standard is my technology of choice for new projects, vastly superior to the older EJB 2.0 technology, a productivity step up from JavaEE 5 / EJB 3.0, and in my opinion more reliable and simpler to maintain than Spring/Hibernate technologies.

Packt Publishing requested that I review one of their new titles about NetBeans and JavaEE: JavaEE 6 Development with NetBeans 7 by David R. Heffelfinger, available to buy from Packt’s website.

JavaEE 6 Development with NetBeans 7 is a very good book which complements a very good IDE. It has improved my usage of NetBeans and has brought me up to speed with the latest additions to the Java EE specification. I’m looking forward to the next project which will let me put into practice the new things I’ve learnt from this book. JavaEE 6 Development with NetBeans 7 covers the full JEE 6 stack, from first principals, with examples deployed on GlassFish application server.

The book focuses on the full JEE 6 technology range, including some elements which aren’t used much anymore in production deployments (JSP SQL tags), but are there for the sake of thoroughness. Each part of the JEE stack has a chapter which is a great introduction to that topic, and will get you up and running with a usable example in no time at all.

No part of the stack is covered in great depth, but you wouldn’t expect that from book only 360ish pages long. Each chapter provides a solid foundation for you get grips with the basics, and up and running with a working example, so you can feel confident in exploring further using other books or online sources if you wanted.

The book will appeal to a range of developers. If you are familiar with Java and wanted to learn JavaEE 6 development, this book would give a great start. If you are already a JEE developer and wanted to brush up on the new additions to the JEE standards than this book is a worthwhile read. Or if you’re a developer, convinced that Eclipse, Spring, Hibernate and JBoss are the pinnacle of Enterprise Java development, then please give this book a try, it could be an eye opener.

The first chapter starts out by introducing the reader to NetBeans and it’s JEE capabilities. You are shown where to download the IDE from, what bundle to choose from, and how to install it on your development platform; with information for Windows, Linux and Mac OS X. The chapter guides you through the installation procedure with excellent step by step screen shots (which are provided for every step in every example throughout the book), and how to start the IDE for the first time.

The book then goes through the steps to integrate NetBeans with other applications servers and databases in case you want to use JBoss or MySQL, but I decided to stick with GlassFish and Derby that come installed by default. To make sure everything is working, the books show you how to create a sample application, deploy to GlassFish and test it in your browser. Only a few pages into the book and you’re up and running with a JavaEE web app – good stuff.

The remainder of the chapter details the developer productivity features that NetBeans provides, such as code completion, code templates, keyboard shortcuts and the editors visual queues; helpful stuff for knocking out the code faster and with fewer errors.

Chapter two covers how to create and deploy a simple JSP and Servlet application. The reader is guided thought how to modify the default new project code and how to create new HTML by dragging and dropping from NetBeans HTML palette. The Model-View-Controller design pattern is introduced and implemented, and authentication and authorisation are added to the application using GlassFish security realms, with form based authentication using a file realm. Finally the code is made more maintainable by using JSP fragments to reduce code duplication.

At only 100 pages into the book you have rapidly created a maintainable, secure and well architected web application!

Building on the previous chapter, chapter three introduces using JSP tags for conditional logic. SQL tags are introduced for querying and modifying data from a database, and the reader is rightly advised that a more robust method should be used for accessing the database in production systems. Custom tags are then used to encapsulate HTML mark-up and JSP functionality.

Chapter four introduces JavaServer Faces and chapter five builds on it with PrimeFaces. I’ve not used JSF since version 1.2 and was not familiar with PrimeFaces, so I was very impressed with the visual results achieved by the end of the two chapters. PrimeFaces and its AJAX capabilities are definitely something I want to explore further.

Chapter six contains some great information; it begins with how to access the database using the Java Persistence API, covering Entity Beans and how to annotate them. The Data Access Object pattern in demonstrated with JPA Controllers for encapsulating data access functionality. Now this is where NetBeans really starts to shine with respect to its code generation capabilities. The last half of the chapter shows how to generate Entities and Controllers from the database without having to manually write a line of code. Finally a whole JSF application for viewing and modifying the database data is generated from the Entity Beans. By the end of this chapter you can create a complete end-to-end data driven web application without writing a line of code. If you’re a developer who is already familiar with the JPA, its worth giving this chapter a read for the new features introduced as part of JPA 2.0, such as the new JSR 303 Bean Validation annotations.

Chapter 7 covers how you will implement your business rules and domain logic in an EJB environment using session beans. The chapter guides us through creating remote stateless session beans and how their functionality can be access across the network from a client. Aspect Oriented Programming using Interceptors is introduced as well as the EJB Timer service for scheduling tasks. Again NetBeans code generation capabilities are used to automatically create session beans from our JPA entities, saving development time.

The new API introduced in Java 6 for dependency injection CDI (Contexts and Dependency Injection), is covered in chapter 8. This great new feature should simplify integrating different application layers, and improve the maintainability of your code. CDI scopes and the @Inject and @Named annotations are used in the examples along with Stereotypes to group together CDI code.

Messaging with JMS and message driven beans is covered in chapter nine. The chapter introduces Queues and Topics, message producers and consumers, and demonstrates how to implement a message Queue and receiver bean. NetBeans can generate the all boilerplate code required to use Message Driven Beans, taking the work out of creating loosely coupled architectures.

The final two chapters detail the usage of Web Services. Chapter ten covers how to create SOAP Web Services with JAX-WS, and chapter eleven RESTful Web Services with JAX-RS. The advantages of using web services as they are client platform agnostic are discussed before creating a web service by using the @WebService and @WebMethod annotations.

NetBeans graphical Web Service design interface is used to create a web service without having to manually write a WSDL file, and then tested with NetBeans web service testing features to view the XML messages sent backwards and forwards.

The rest of chapter ten shows us how to simple crate a SOAP client my dragging and dropping the Web Service methods on to a class, how to generate code to create a web service from an existing Session Bean and how to generate a web service from an existing WSDL.

The last chapter shows us how to generate the code to REST enable a database and create a client with just a few clicks.

Overall I think this is a great book. It contains the things that make me enthusiastic about Enterprise Java Development: EJB 3.1, JPA 2.0 and GlassFish. The book is well written and well structured; it flows from one chapter to the next, building on what you have learned before. The text is accurate and concise, and the screen shots throughout the book are so useful that you could follow most of the examples from them alone.

Most importantly, the code printed in the book is correct and easy to follow, I found no syntax errors in it.

The book should appeal to novice Enterprise Java developers as well as experienced programmers who want to brush up on the latest standards or need a firm foundation in an area of JEE they haven’t covered before.

I’m looking forward to using some of the new things I’ve learned from the book in new projects. I hope the author continues to update the book in subsequent editions as and when the JEE standards evolve.

Downloads From Packt

EJB 3.1 Cookbook Review

The Enterprise JavaBeans 3.1 specification is the latest standard from Oracle to further simplify EJB technology. It provides developers with the ability to construct simple and reliable architectures, building on the previous EJB 3.0 standard with more annotations, more POJOS, simplified packaging and less XML configuration.

EJB 3.1 is part of the wider JavaEE 6 enterprise development platform, and is my favoured technology for building new projects, vastly superior to the older EJB 2.0 technology, a productivity step up from JavaEE 5 / EJB 3.0.

Packt Publishing requested that I review one of their titles about EJB 3.1 and the JavaEE 6 platform: EJB 3.1 Cookbook by Richard M. Reese, available to buy from Packt's website.

EJB 3.1 Cookbook is at best an average book. The information in the book is very densely packed, but often repetitive and sometimes tangential to the current topic being addressed. The information in the book is presented in recipes - short how-to examples focusing on a specific JavaEE / EJB feature, put together to form a cookbook.

Each recipe is presented in a formulaic way, with information under the sub-headings: Getting ready, How to do it..., How it works... and There's more. Some of the code examples, especially at the start of the book, are trivial; and the explanations of how the code works are sometimes no more useful then reading the JavaEE 6 javadoc.

The book covers the full range of EJB beans and the Java Persistence API, with about equal time being spent on features new to EJB 3.1 and existing features from 3.0.

It's difficult to say who the books target audience is. It doesn't provide enough guidance to developers new to Java, but spends too much time covering some of the basics which more experienced EJB developers will already be familiar with. If you're already experienced with EJB 3.0 development and need a handy reference for existing annotations, and want to get a good understanding of the new annotations in 3.1 and how to use them, then this book might be useful.

This book was laborious to read from cover to cover, and I wouldn't suggest reading it that way. I would recommend consulting its relevant recipes before implementing new code.

NetBeans 6.9.1 and GlassFish v3.0.1 are stated as requirements for using this book. In practice, specific features of these products are barely used. There is no reason why you couldn't use Eclipse and JBoss AS, or any other code editors and application servers which support EJB 3.1.

Chapter 1 is an introduction to Enterprise JavaBeans, Dependency Injection and the Java Naming and Directory Interface. It covers creating a simple Stateless session bean and accessing its functionality from a variety of clients. Developers who are familiar with EJB development will find this chapter a waste of time. Developers new to EJB and JNDI will find it baffling and of no immediate practical use, I'd recommend skipping this chapter.

Chapter 2 covers session beans - Stateless, Stateful and the new Singleton bean. It provides examples of how to use the new @Startup and @DependsOn annotations to control order of singleton initialisation, concurrency and locking, controlling initialization and local and remote interfaces.

The last and most useful part of the chapter uses the @Asynchronous annotation to schedule background processes, and demonstrates the Future and AsyncResult classes.

Chapter 3 explains Message Driven Beans and introduces Queues and Topics. Good program design and possible performance enhancements of separating object production from consumption is illustrated. More interesting and useful recipes are presented towards the end of the chapter, but the first five which deal with different message types are almost identical.

Chapter 4 introduces Object-relational mapping and the Java Persistence API. Familiar JPA concepts and annotations are covered as well as new annotations for temporal validation @Past and @Future, regular expression validation using @Pattern and integer and boolean validation.

Chapter 5 builds on chapter 4 and introduces the Java Persistence Query Language and the new Criteria API. CRUD operations using JPQL are covered extensively, but the new Criteria API is only given one recipe. I would have liked as many pages covering the Criteria API as there were on JPQL, the increased performance and type safety of this new feature are things I would like to know more about.

Chapter 6 contains very useful information on transaction processing. The defaults of Container Managed Transactions have always been sufficient for me, so I was interested to learn more about transaction types and Bean Managed Transactions.

EJB Security is covered in chapter 7. The concepts of users, groups and realms are introduced and specific features of GlassFish are used for the first time to prepare a File Realm. Declarative role based security using annotations is covered, as well as controlling security programmatically when annotations are inadequate.

Chapter 8 introduces Aspect Oriented Programming and the @Interceptor annotation. I've never been impressed with AOP, it always seemed more trouble than it was worth, but this chapter shows how simple EJB 3.1 makes it. There is some good information in this chapter, it demonstrates well how AOP can simplify code by moving functionality not central to a business method outside of the method.

Chapter 9 - Timer Services demonstrates automatic/declarative timers, implemented using annotations and programmatic timers using the TimerService class.

Chapter 10 covers SOAP web services using JAX-WS, which will be familiar to EJB 3.0 developers, as well as creating RESTful services with JAX-RS.

The pros of using XML deployment descriptors in environments which require more dynamic configuration are highlighted in chapter 11. Also covered is the usage of the 'jar' command to examine JAR and WAR file contents, and a quick overview of the java classpath and class loading.

Chapter 12 ends the book with recipes which are applicable to Java programming in general, not just EJB development. The chapter contains general good practice tips like using a logging framework instead of printing to System.out, not leaving empty catch blocks, using BigDecimal to avoid rounding errors for currency, and avoiding string concatenation performance inefficiencies by using StringBuilder.

Overall, I think this book is OK. I'd recommend using it as a reference to consult rather than a book to pick up and work through end to end.

I think the book is suitable for developers looking to brush up on the newest EJB annotations, but because of this, much of the book will be old news to many readers. Even the information I felt was relevant and interesting to me can easily be found on the Oracle website in the form of The Java EE 6 Tutorial and javadocs.

Downloads From Packt