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

Monday, 20 June 2011

Java Sleep Sort - For The Lulz

Strictly for the lulz, here is an implementation of Sleep Sort, first seen on 4chan.

SleepSort.java

package sleepsort;

import java.util.Arrays;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public final class SleepSort {

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

    int[] numbers = {90, 80, 70, 60, 50, 40, 30, 20, 10};
    int[] sortedNumbers = sort(numbers);
    System.out.println(Arrays.toString(sortedNumbers));
  }

  private static int[] sort(final int[] numbers) throws InterruptedException, ExecutionException {

    ExecutorService executor = Executors.newFixedThreadPool(numbers.length);
    ExecutorCompletionService<Integer> ecs = new ExecutorCompletionService<Integer>(executor);
    for (int number : numbers) {
      ecs.submit(new SleepSortCallable((number)));
    }

    int[] sortedNumbers = new int[numbers.length];
    for (int i = 0; i < sortedNumbers.length; i++) {
      sortedNumbers[i] = ecs.take().get();
    }

    executor.shutdown();

    return sortedNumbers;
  }

  private static class SleepSortCallable implements Callable<Integer> {

    private final int number;

    public SleepSortCallable(final int number) {
      this.number = number;
    }

    @Override
    public Integer call() throws Exception {
      Thread.sleep(number);
      return number;
    }
  }
}

Tuesday, 8 March 2011

GWT File Upload With Event-Based Progress Bar

File upload with progress bar, and paged file downloads.

Adapting the event-based code from Chapter 9 of the excellent book Google Web Toolkit Applications, I coded up a file upload application with a progress bar, which is updated using server pseudo-push events.

The application also provides a download servlet, and layout using GWT UiBinder.

Full source for the webapp is available at the bottom of the page. Not all of the code is listed below, but some of the most important classes are explained here.

The UploadProgressService interface defines the server side functions available, the most important being getEvents(). This function will be called recursively on the client side to simulate a main loop.

UploadProgressService.java

package org.adrianwalker.gwt.uploadprogress.client;

import java.util.List;

import com.google.gwt.user.client.rpc.RemoteService;
import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;
import org.adrianwalker.gwt.uploadprogress.common.dto.FileDto;
import org.adrianwalker.gwt.uploadprogress.common.event.Event;

@RemoteServiceRelativePath("uploadprogress")
public interface UploadProgressService extends RemoteService {

  void initialise();

  int countFiles();

  List<FileDto> readFiles(int page, int pageSize);

  List<Event> getEvents();
}

The only event in the application is UploadProgressChangeEvent. It is concerned with reporting changes in file upload percentage.

UploadProgressChangeEvent.java

package org.adrianwalker.gwt.uploadprogress.common.event;

import java.io.Serializable;

public final class UploadProgressChangeEvent implements Event, Serializable {

  private String filename;
  private Integer percentage;

  public UploadProgressChangeEvent() {
  }

  public String getFilename() {
    return filename;
  }

  public void setFilename(final String filename) {
    this.filename = filename;
  }

  public Integer getPercentage() {
    return percentage;
  }

  public void setPercentage(final Integer percentage) {
    this.percentage = percentage;
  }

  @Override
  public String toString() {
    return filename + " - " + percentage;
  }
}

The UploadProgressServlet implements the methods defined by UploadProgressService. The getEvents() method returns the events stored in the servlets session scope. If the event list is empty, the thread waits for 30 seconds. If the list contained events, they are retuned to the client, and then the event list is cleared.

UploadProgressServlet.java

package org.adrianwalker.gwt.uploadprogress.server;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import org.adrianwalker.gwt.uploadprogress.client.UploadProgressService;
import org.adrianwalker.gwt.uploadprogress.common.dto.FileDto;
import org.adrianwalker.gwt.uploadprogress.common.event.Event;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public final class UploadProgressServlet extends RemoteServiceServlet implements UploadProgressService {

  private static final int EVENT_WAIT = 30 * 1000;
  private static final String PROPERTIES_FILE = "WEB-INF/classes/uploadprogress.properties";
  private static final Logger LOGGER = LoggerFactory.getLogger(UploadProgressServlet.class);
  private String uploadDirectory;

  @Override
  public void init() throws ServletException {
    Properties properties = new Properties();
    try {
      properties.load(getServletContext().getResourceAsStream(PROPERTIES_FILE));
    } catch (IOException ioe) {
      throw new ServletException(ioe);
    }

    uploadDirectory = properties.getProperty("upload.directory", "target");
  }

  @Override
  public void initialise() {
    getThreadLocalRequest().getSession(true);
  }

  @Override
  public List<FileDto> readFiles(final int page, final int pageSize) {

    File[] listFiles = readFiles(this.uploadDirectory);
    sortFiles(listFiles);

    int firstFile = pageSize * (page - 1);
    int lastFile = firstFile + pageSize;

    int fileCount = listFiles.length;
    if (fileCount < lastFile) {
      lastFile = fileCount;
    }

    if (firstFile < fileCount) {
      List<FileDto> files = new ArrayList<FileDto>();

      for (int i = firstFile; i < lastFile; i++) {

        File file = listFiles[i];
        FileDto fileDto = new FileDto();
        fileDto.setFilename(file.getName());
        fileDto.setDateUploaded(new Date(file.lastModified()));
        files.add(fileDto);
      }
      return files;
    } else {
      return Collections.EMPTY_LIST;
    }
  }

  @Override
  public List<Event> getEvents() {

    HttpSession session = getThreadLocalRequest().getSession();
    UploadProgress uploadProgress = UploadProgress.getUploadProgress(session);

    List<Event> events = null;
    if (null != uploadProgress) {
      if (uploadProgress.isEmpty()) {
        try {
          synchronized (uploadProgress) {
            LOGGER.debug("waiting...");
            uploadProgress.wait(EVENT_WAIT);
          }
        } catch (final InterruptedException ie) {
          LOGGER.debug("interrupted...");
        }
      }

      synchronized (uploadProgress) {
        events = uploadProgress.getEvents();
        uploadProgress.clear();
      }
    }

    return events;
  }

  @Override
  public int countFiles() {
    return readFiles(this.uploadDirectory).length;
  }

  private File[] readFiles(final String directory) {
    File uploadDirectory = new File(directory);
    return uploadDirectory.listFiles(new FileFilter() {

      @Override
      public boolean accept(final File file) {
        return null == file ? false : file.isFile();
      }
    });
  }

  private void sortFiles(final File[] listFiles) {
    Arrays.sort(listFiles, new Comparator<File>() {

      @Override
      public int compare(final File f1, final File f2) {
        return Long.valueOf(f2.lastModified()).compareTo(f1.lastModified());
      }
    });
  }
}

UploadProgress stores the list of events, and has a factory method for creating/retrieving an instance of its self in the session.

UploadProgress.java

package org.adrianwalker.gwt.uploadprogress.server;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpSession;
import org.adrianwalker.gwt.uploadprogress.common.event.Event;

public final class UploadProgress {

  private static final String SESSION_KEY = "uploadProgress";
  private List<Event> events = new ArrayList<Event>();

  private UploadProgress() {
  }

  public List<Event> getEvents() {

    return events;
  }

  public void add(final Event event) {
    events.add(event);
  }

  public void clear() {
    events = new ArrayList<Event>();
  }

  public boolean isEmpty() {
    return events.isEmpty();
  }

  public static UploadProgress getUploadProgress(final HttpSession session) {
    Object attribute = session.getAttribute(SESSION_KEY);
    if (null == attribute) {
      attribute = new UploadProgress();
      session.setAttribute(SESSION_KEY, attribute);
    }

    return null == attribute ? null : (UploadProgress) attribute;
  }
}

The UploadServlet uses Apache Commons FileUpload library to parse the multipart post request and stream the file to disk. An UploadProgressListener is added to a custom input stream, UploadProgressInputStream, which together report the status of the upload back to the client.

UploadServlet.java

package org.adrianwalker.gwt.uploadprogress.server;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Properties;
import javax.servlet.http.HttpServlet;

import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.util.Streams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public final class UploadServlet extends HttpServlet {

  private static final String PROPERTIES_FILE = "WEB-INF/classes/uploadprogress.properties";
  private static final Logger LOGGER = LoggerFactory.getLogger(UploadServlet.class);
  private static final String FILE_SEPERATOR = System.getProperty("file.separator");
  private String uploadDirectory;

  @Override
  public void init() throws ServletException {
    Properties properties = new Properties();
    try {
      properties.load(getServletContext().getResourceAsStream(PROPERTIES_FILE));
    } catch (IOException ioe) {
      throw new ServletException(ioe);
    }

    uploadDirectory = properties.getProperty("upload.directory", "target");
  }

  @Override
  protected void doPost(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
    try {
      uploadFile(request);
    } catch (FileUploadException fue) {
      throw new ServletException(fue);
    }
  }

  private void uploadFile(final HttpServletRequest request) throws FileUploadException, IOException {

    if (!ServletFileUpload.isMultipartContent(request)) {
      throw new FileUploadException("error multipart request not found");
    }

    FileItemFactory fileItemFactory = new DiskFileItemFactory();
    ServletFileUpload servletFileUpload = new ServletFileUpload(fileItemFactory);

    FileItemIterator fileItemIterator = servletFileUpload.getItemIterator(request);

    HttpSession session = request.getSession();
    UploadProgress uploadProgress = UploadProgress.getUploadProgress(session);

    while (fileItemIterator.hasNext()) {
      FileItemStream fileItemStream = fileItemIterator.next();

      String filePath = fileItemStream.getName();
      String fileName = filePath.substring(filePath.lastIndexOf(FILE_SEPERATOR) + 1);

      UploadProgressListener uploadProgressListener = new UploadProgressListener(fileName, uploadProgress);

      UploadProgressInputStream inputStream = new UploadProgressInputStream(fileItemStream.openStream(), request.getContentLength());
      inputStream.addListener(uploadProgressListener);

      File file = new File(uploadDirectory, fileName);

      Streams.copy(inputStream, new FileOutputStream(file), true);

      LOGGER.info(String.format("uploaded file %s", file.getAbsolutePath()));
    }
  }
}

UploadProgressInputStream is a FilterInputStream which reports the number of bytes read to attached listeners.

UploadProgressInputStream.java

package org.adrianwalker.gwt.uploadprogress.server;

import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.fileupload.ProgressListener;

public final class UploadProgressInputStream extends FilterInputStream {

  private List<ProgressListener> listeners;
  private long bytesRead = 0;
  private long totalBytes = 0;

  public UploadProgressInputStream(final InputStream in, final long totalBytes) {
    super(in);

    this.totalBytes = totalBytes;

    listeners = new ArrayList<ProgressListener>();
  }

  public void addListener(final ProgressListener listener) {
    listeners.add(listener);
  }

  @Override
  public int read() throws IOException {
    int b = super.read();

    this.bytesRead++;

    updateListeners(bytesRead, totalBytes);

    return b;
  }

  @Override
  public int read(final byte b[]) throws IOException {
    return read(b, 0, b.length);
  }

  @Override
  public int read(final byte b[], final int off, final int len) throws IOException {
    int bytesRead = in.read(b, off, len);

    this.bytesRead = this.bytesRead + bytesRead;

    updateListeners(this.bytesRead, totalBytes);

    return bytesRead;
  }

  @Override
  public void close() throws IOException {
    super.close();

    updateListeners(totalBytes, totalBytes);
  }

  private void updateListeners(final long bytesRead, final long totalBytes) {

    for (ProgressListener listener : listeners) {

      listener.update(bytesRead, totalBytes, listeners.size());
    }
  }
}

An UploadProgressListener attached to the input stream, adds UploadProgressChangeEvent events to the event list and calls notifyAll() to reply to the waiting client.

UploadProgressListener.java

package org.adrianwalker.gwt.uploadprogress.server;

import org.adrianwalker.gwt.uploadprogress.common.event.UploadProgressChangeEvent;
import org.apache.commons.fileupload.ProgressListener;

public final class UploadProgressListener implements ProgressListener {

  private static final double COMPLETE_PERECENTAGE = 100d;
  private int percentage = -1;
  private String fileName;
  private UploadProgress uploadProgress;

  public UploadProgressListener(final String fileName, final UploadProgress uploadProgress) {
    this.fileName = fileName;
    this.uploadProgress = uploadProgress;
  }

  @Override
  public void update(final long bytesRead, final long totalBytes, final int items) {
    int percentage = (int) Math.floor(((double) bytesRead / (double) totalBytes) * COMPLETE_PERECENTAGE);

    if (this.percentage == percentage) {
      return;
    }

    this.percentage = percentage;

    UploadProgressChangeEvent event = new UploadProgressChangeEvent();
    event.setFilename(this.fileName);
    event.setPercentage(percentage);

    synchronized (this.uploadProgress) {
      this.uploadProgress.add(event);
      this.uploadProgress.notifyAll();
    }
  }
}

The client side ProgressController is responsible for calling the getEvents() method on the service servlet, and calling it again after a successful response; creating an event loop.

ProgressController.java

package org.adrianwalker.gwt.uploadprogress.client.controller;

import com.google.gwt.core.client.GWT;
import java.util.List;

import com.google.gwt.user.client.rpc.AsyncCallback;
import org.adrianwalker.gwt.uploadprogress.client.state.UploadProgressState;
import org.adrianwalker.gwt.uploadprogress.common.dto.FileDto;
import org.adrianwalker.gwt.uploadprogress.common.event.Event;
import org.adrianwalker.gwt.uploadprogress.common.event.UploadProgressChangeEvent;

public final class ProgressController extends AbstractController {

  public static final ProgressController INSTANCE = new ProgressController();

  private ProgressController() {
  }

  public void findFiles(final int page, final int pageSize) {
    SERVICE.readFiles(page, pageSize, new AsyncCallback<List<FileDto>>() {

      @Override
      public void onFailure(final Throwable t) {
        GWT.log("error find files", t);
      }

      @Override
      public void onSuccess(final List<FileDto> files) {
        UploadProgressState.INSTANCE.setFiles(files);
      }
    });
  }

  private void getEvents() {

    SERVICE.getEvents(new AsyncCallback<List<Event>>() {

      @Override
      public void onFailure(final Throwable t) {
        GWT.log("error get events", t);
      }

      @Override
      public void onSuccess(final List<Event> events) {

        for (Event event : events) {
          handleEvent(event);
        }
        SERVICE.getEvents(this);
      }

      private void handleEvent(final Event event) {

        if (event instanceof UploadProgressChangeEvent) {
          UploadProgressChangeEvent uploadPercentChangeEvent = (UploadProgressChangeEvent) event;
          String filename = uploadPercentChangeEvent.getFilename();
          Integer percentage = uploadPercentChangeEvent.getPercentage();

          UploadProgressState.INSTANCE.setUploadProgress(filename, percentage);
        }
      }
    });
  }

  public void initialise() {
    SERVICE.initialise(new AsyncCallback<Void>() {

      @Override
      public void onFailure(final Throwable t) {
        GWT.log("error initialise", t);
      }

      @Override
      public void onSuccess(final Void result) {
        getEvents();
      }
    });
  }

  public void countFiles() {
    SERVICE.countFiles(new AsyncCallback<Integer>() {

      @Override
      public void onFailure(final Throwable t) {
        GWT.log("error count files", t);
      }

      @Override
      public void onSuccess(final Integer result) {
        int pageSize = UploadProgressState.INSTANCE.getPageSize();
        int pages = (int) Math.ceil((double) result / (double) pageSize);
        UploadProgressState.INSTANCE.setPages(pages);
      }
    });
  }
}

Changes to the files upload percentage are stored in UploadProgressState which can have listerners attached to be notified of updates to its fields.

UploadProgressState.java

package org.adrianwalker.gwt.uploadprogress.client.state;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.adrianwalker.gwt.uploadprogress.common.dto.FileDto;

public final class UploadProgressState extends PageableState {

  public static final UploadProgressState INSTANCE = new UploadProgressState();
  private Map<String, Integer> uploadProgress;
  private List<FileDto> files;

  private UploadProgressState() {
    uploadProgress = new HashMap<String, Integer>();
  }

  public List<FileDto> getFiles() {
    return files;
  }

  public void setFiles(final List<FileDto> files) {
    List<FileDto> old = this.files;
    this.files = files;
    firePropertyChange("files", old, files);
  }

  public Integer getUploadProgress(final String filename) {
    return uploadProgress.get(filename);
  }

  public void setUploadProgress(final String filename, final Integer percentage) {
    Integer old = this.uploadProgress.get(filename);
    uploadProgress.put(filename, percentage);
    firePropertyChange("uploadProgress", old, uploadProgress);
  }
}

Finally, UploadProgress listens for changes to UploadProgressState and updates the UploadPanel with percentage changes for the file upload, which in turn updates the panels ProgressBar.

UploadProgress.java

package org.adrianwalker.gwt.uploadprogress.client.view;

import com.google.gwt.user.client.Timer;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.VerticalPanel;
import java.util.HashMap;
import java.util.Map;
import org.adrianwalker.gwt.uploadprogress.client.state.UploadProgressState;

public final class UploadProgress extends Composite {

  private Panel panel;
  private Map<String, UploadPanel> uploads;

  public UploadProgress() {

    panel = new VerticalPanel();
    panel.setStyleName("UploadProgress");
    uploads = new HashMap<String, UploadPanel>();

    this.initWidget(panel);

    UploadProgressState.INSTANCE.addPropertyChangeListener("uploadProgress", new UploadProgressListener());
  }

  private final class UploadProgressListener implements PropertyChangeListener {

    private static final int COMPLETE_PERECENTAGE = 100;
    private static final int REMOVE_DELAY = 3000;

    @Override
    public void propertyChange(final PropertyChangeEvent event) {

      Map<String, Integer> uploadPercentage = (Map<String, Integer>) event.getNewValue();

      for (Map.Entry<String, Integer> entry : uploadPercentage.entrySet()) {
        String file = entry.getKey();
        Integer percentage = entry.getValue();

        final UploadPanel uploadPanel;
        if (!uploads.containsKey(file)) {
          uploadPanel = new UploadPanel(file);
          uploads.put(file, uploadPanel);
          panel.add(uploadPanel);
        } else {
          uploadPanel = uploads.get(file);
        }

        uploadPanel.update(percentage);

        if (percentage == COMPLETE_PERECENTAGE) {
          Timer timer = new Timer() {

            @Override
            public void run() {
              panel.remove(uploadPanel);
            }
          };
          timer.schedule(REMOVE_DELAY);
        }
      }
    }
  }

  private static final class UploadPanel extends HorizontalPanel {

    private ProgressBar bar;
    private Label label;

    public UploadPanel(final String file) {

      setStyleName("UploadPanel");

      bar = new ProgressBar();
      label = new Label(file);

      add(bar);
      add(label);
    }

    public void update(final int percentage) {
      bar.update(percentage);
    }
  }
}

Source Code

Usage

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

Update - 29/7/2012

If you're seeing the following exception during compilation try this updated pom.xml :

[gwt:compile]
auto discovered modules [org.adrianwalker.gwt.uploadprogress.UploadProgress]
You're project declares dependency on gwt-user 2.1.0. This plugin is designed for at least gwt version 2.5.0-rc1
You're project declares dependency on gwt-user 2.1.0. This plugin is designed for at least gwt version 2.5.0-rc1
Compiling module org.adrianwalker.gwt.uploadprogress.UploadProgress
   Validating units:
      Ignored 50 units with compilation errors in first pass.
Compile with -strict or with -logLevel set to TRACE or DEBUG to see all errors.
   [ERROR] Unexpected internal compiler error
java.lang.NullPointerException
 at com.google.gwt.dev.javac.CompilationProblemReporter.reportErrors(CompilationProblemReporter.java:200)
 at com.google.gwt.dev.jjs.impl.UnifyAst.assimilateUnit(UnifyAst.java:666)
 at com.google.gwt.dev.jjs.impl.UnifyAst.searchForTypeBySource(UnifyAst.java:985)
 at com.google.gwt.dev.jjs.impl.UnifyAst.addRootTypes(UnifyAst.java:530)
 at com.google.gwt.dev.jjs.JavaToJavaScriptCompiler.precompile(JavaToJavaScriptCompiler.java:601)
 at com.google.gwt.dev.jjs.JavaScriptCompiler.precompile(JavaScriptCompiler.java:33)
 at com.google.gwt.dev.Precompile.precompile(Precompile.java:278)
 at com.google.gwt.dev.Precompile.precompile(Precompile.java:229)
 at com.google.gwt.dev.Precompile.precompile(Precompile.java:141)
 at com.google.gwt.dev.Compiler.run(Compiler.java:232)
 at com.google.gwt.dev.Compiler.run(Compiler.java:198)
 at com.google.gwt.dev.Compiler$1.run(Compiler.java:170)
 at com.google.gwt.dev.CompileTaskRunner.doRun(CompileTaskRunner.java:88)
 at com.google.gwt.dev.CompileTaskRunner.runWithAppropriateLogger(CompileTaskRunner.java:82)
 at com.google.gwt.dev.Compiler.main(Compiler.java:177)

Monday, 7 February 2011

Maven Dependency Finder

My friend Mark had an idea for a utility to search a list of your favourite repositories for a given dependency. So I thought I'd code up a version.

Maven Dependency Finder is a command line utility that takes a list of repositories and a dependency group id,artifact id and version, and returns the repositories which contain the JAR you want.

The utility reads a list of repositories from a file, and uses multiple threads to check for the required JARs, outputting the URLs where the JARs are hosted.

MavenDependencyFinder.java

package org.adrianwalker.maven;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public final class MavenDependencyFinder {

  private static final int EXIT_FAILURE = -1;
  private static final int EXIT_SUCCESS = 0;
  private static final String FLAG_START = "-";
  private static final String GROUP_FLAG = FLAG_START + "g";
  private static final String ARTIFACT_FLAG = FLAG_START + "a";
  private static final String VERSION_FLAG = FLAG_START + "v";
  private static final String MAX_THREADS_FLAG = FLAG_START + "t";
  private static final String REPOSITORY_FILE_FLAG = FLAG_START + "r";
  private static final String HELP_FLAG = FLAG_START + "h";
  private static final String DEFAULT_VERSION = "";
  private static final List<String> DEFAULT_REPOSITORIES = Arrays.asList(new String[]{
            "http://mirrors.ibiblio.org/pub/mirrors/maven2/"
          });
  private static final int DEFAULT_MAX_THREADS = 2;
  private static final String ARTIFACT_VERSION_SEPERATOR = "-";
  private static final String EMPTY_STRING = "";
  private static final String HTTP_METHOD = "GET";
  private static final String JAR_FILE_EXTENSION = ".jar";
  private static final String PACKAGE_SEPERATOR = "\\.";
  private static final int TIMEOUT_MILLISECONDS = 10000;
  private static final String URL_SEPERATOR = "/";

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


    Map<String, String> parameters = readParameters(args);

    if (parameters.isEmpty() || parameters.containsKey(HELP_FLAG)) {
      printHelp();
      System.exit(EXIT_SUCCESS);
    }

    if (!parameters.containsKey(GROUP_FLAG)) {
      printError("Required parameter '-g' missing");
      System.exit(EXIT_FAILURE);
    }

    if (!parameters.containsKey(ARTIFACT_FLAG)) {
      printError("Required parameter '-a' missing");
      System.exit(EXIT_FAILURE);
    }

    String groupId = parameters.get(GROUP_FLAG);
    String artifactId = parameters.get(ARTIFACT_FLAG);

    String version = DEFAULT_VERSION;
    if (parameters.containsKey(VERSION_FLAG)) {
      version = parameters.get(VERSION_FLAG);
    }

    int maxThreads = DEFAULT_MAX_THREADS;
    if (parameters.containsKey(MAX_THREADS_FLAG)) {
      maxThreads = Integer.parseInt(parameters.get(MAX_THREADS_FLAG));
    }

    List<String> repositories = DEFAULT_REPOSITORIES;
    if (parameters.containsKey(REPOSITORY_FILE_FLAG)) {
      String fileName = parameters.get(REPOSITORY_FILE_FLAG);
      try {
        repositories = readRepositoriesFile(fileName);
      } catch (Throwable t) {
        printError(String.format("Error reading file '%s'", fileName));
        System.exit(EXIT_FAILURE);
      }
    }

    findDependency(maxThreads, repositories, groupId, artifactId, version);

    System.exit(EXIT_SUCCESS);
  }

  private MavenDependencyFinder() {
  }

  private static void findDependency(final int maxThreads, final List<String> repositories, final String groupId, final String artifactId, final String version) {

    ExecutorService threadPool = Executors.newFixedThreadPool(maxThreads);
    Map<String, Future<Boolean>> results = new HashMap<String, Future<Boolean>>();
    for (String repository : repositories) {
      Future<Boolean> isHostingDependancy = threadPool.submit(new CallableUrlFinder(repository, groupId, artifactId, version));
      results.put(repository, isHostingDependancy);
    }
    Set<String> responses = new HashSet<String>();
    while (results.size() > responses.size()) {

      for(Entry<String, Future<Boolean>> entry : results.entrySet()) {
          
        String repository = entry.getKey();
        Future<Boolean> isHostingDependancy = entry.getValue();
          
        if (!responses.contains(repository) && isHostingDependancy.isDone()) {
          Boolean found;
          try {
            found = isHostingDependancy.get();
            responses.add(repository);
          } catch (Throwable t) {
            found = false;
          }
          if (found) {
            System.out.println(repository);
          }
        }
      }
      Thread.yield();
    }
    threadPool.shutdown();
  }

  private static Map<String, String> readParameters(final String[] args) {
    int argsCount = args.length;

    if (argsCount == 1 && args[0].equals(HELP_FLAG)) {
      printHelp();
      System.exit(EXIT_SUCCESS);
    }else if (argsCount % 2 == 1) {
      printError("Invalid number of arguments");
      System.exit(EXIT_FAILURE);
    }

    Map<String, String> parameters = new HashMap<String, String>();
    for (int i = 0; i < argsCount; i = i + 2) {

      String flag = args[i];

      if (!flag.startsWith(FLAG_START)) {
        printError(String.format("Invalid flag '%s'", flag));
        System.exit(EXIT_FAILURE);
      }

      String value = args[i + 1];
      if (value.startsWith(FLAG_START)) {
        printError(String.format("Invalid value '%s' for flag '%s'", value, flag));
        System.exit(EXIT_FAILURE);
      }


      parameters.put(flag, value);
    }

    return parameters;
  }

  private static List readRepositoriesFile(final String fileName) throws IOException {
    List<String> repositories = new ArrayList<String>();
    BufferedReader reader = null;
    try {
      reader = new BufferedReader(new FileReader(fileName));
      String line;
      while ((line = reader.readLine()) != null) {
        repositories.add(line.trim());
      }
    } finally {
      if (null != reader) {
        reader.close();
      }
    }
    return repositories;
  }

  private static void printHelp() {
    String help = "Usage: mdf -g <group id> -a <artifact id> [-v <version>] [-r <filename>] [-t <threads>] [-h]\n\n"
            + "  -g\tDependency group id\n"
            + "  -a\tDependency artifact id\n"
            + "  -v\tDependency version\n"
            + "  -r\tRepository file\n"
            + "  -t\tMaximum threads\n"
            + "  -h\tHelp";
    System.out.println(help);
  }

  private static void printError(final String message) {
    System.out.println(message);
  }

  private static final class CallableUrlFinder implements Callable<Boolean> {

    private final String repository;
    private final String groupId;
    private final String artifactId;
    private final String version;

    public CallableUrlFinder(final String repository, final String groupId, final String artifactId, final String version) {
      this.repository = repository;
      this.groupId = groupId;
      this.artifactId = artifactId;
      this.version = version;
    }

    @Override
    public Boolean call() throws Exception {
      return isHostingDependency(repository, groupId, artifactId, version);
    }

    private boolean isHostingDependency(final String repository, final String groupId, final String artifactId, final String version) {

      String dependencyUrl = gavToUrl(repository, groupId, artifactId, version);

      HttpURLConnection connection = null;
      try {
        URL url = new URL(dependencyUrl);
        connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod(HTTP_METHOD);
        connection.setReadTimeout(TIMEOUT_MILLISECONDS);
        connection.connect();
        return connection.getResponseCode() == HttpURLConnection.HTTP_OK;
      } catch (Throwable t) {
        return false;
      } finally {
        if (null != connection) {
          connection.disconnect();
        }
      }
    }

    private String gavToUrl(final String repository, final String groupId, final String artifactId, final String version) {
      /*
       * Format:
       *
       * http://mirrors.ibiblio.org/pub/mirrors/maven2/log4j/log4j/1.2.13/log4j-1.2.13.jar
       *
       */

      StringBuilder builder = new StringBuilder();
      builder.append(repository) //
              .append(repository.endsWith(URL_SEPERATOR) ? EMPTY_STRING : URL_SEPERATOR) //
              .append(groupId.replaceAll(PACKAGE_SEPERATOR, URL_SEPERATOR)) //
              .append(URL_SEPERATOR).append(artifactId);
      if (version != null && !version.isEmpty()) {
        builder.append(URL_SEPERATOR) //
                .append(version) //
                .append(URL_SEPERATOR) //
                .append(artifactId) //
                .append(ARTIFACT_VERSION_SEPERATOR) //
                .append(version).append(JAR_FILE_EXTENSION);
      }

      return builder.toString();
    }
  }
}


Build the project with:

mvn clean install
to build a directory and zip assembly containing the executable JAR file, a batch file and shell script to run the utility from the command line.

Running the command with the -h flag shows the help for the utility:

Usage: mdf -g <group id> -a <artifact id> [-v <version>] [-r <filename>] [-t <threads>] [-h]

  -g    Dependency group id
  -a    Dependency artifact id
  -v    Dependency version
  -r    Repository file
  -t    Maximum threads
  -h    Help

To run the utility with a list of repositories and search for log4j:

mdf -g log4j -a log4j -r repositories.txt

outputs:

http://mirrors.ibiblio.org/pub/mirrors/maven2/
http://repository.jboss.org/maven2/

where the repositories list input file contains:

repositories.txt

http://mirrors.ibiblio.org/pub/mirrors/maven2/
http://repository.jboss.org/maven2/
http://download.java.net/maven/2
http://repository.codehaus.org

Source Code