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