I believe it is essential for any self-respecting Java developer to occasionaly browse through the Apache Commons library in order to determine whether there isn’t anything he could or should be using to assist with his coding. It happens to me each time I visit the site – I find something that I’ve either written myself, or found that could make my life a whole lot easier.
I sincerely apologize for my blatant “content-theft” – all credit and content belongs unreservedly to Apache Commons. All I have attempted to do is provide a short summary of what each of the libaries consist of, so that I can perhaps assist in redirecting more of you to the Apache Commons website.
I suspect some of these may be older and no longer in use, similarly some of them might have been incorporated into the official JDK. I have just attempted to take a snalpshot of what is on the Apache Commons website at this point in time.
Commons Attributes enables Java programmers to use C#/.Net-style attributes in their code.
The Byte Code Engineering Library (Apache Commons BCEL™) is intended to give users a convenient way to analyze, create, and manipulate (binary) Java class files (those ending with .class). Classes are represented by objects which contain all the symbolic information of the given class: methods, fields and byte code instructions, in particular.
Such objects can be read from an existing file, be transformed by a program (e.g. a class loader at run-time) and written to a file again. An even more interesting application is the creation of classes from scratch at run-time. The Byte Code Engineering Library (BCEL) may be also useful if you want to learn about the Java Virtual Machine (JVM) and the format of Java .class files.
Most Java developers are used to creating Java classes that conform to the JavaBeans naming patterns for property getters and setters. It is natural to then access these methods directly, using calls to the corresponding
setXxx methods. However, there are some occasions where dynamic access to Java object properties (without compiled-in knowledge of the property getter and setter methods to be called) is needed. Example use cases include:
Building scripting languages that interact with the Java object model (such as the Bean Scripting Framework).
Building template language processors for web presentation and similar uses (such as JSP or Velocity).
Building custom tag libraries for JSP and XSP environments (such as Jakarta Taglibs, Struts, Cocoon).
Consuming XML-based configuration resources (such as Ant build scripts, web application deployment descriptors, Tomcat’s
The Java language provides Reflection and Introspection APIs (see the
java.beans packages in the JDK Javadocs). However, these APIs can be quite complex to understand and utilize. The BeanUtils component provides easy-to-use wrappers around these capabilities.
The Betwixt library provides an XML introspection mechanism for mapping beans to XML in a flexible way. It is implemented using an XMLIntrospector and XMLBeanInfo classes which are similar to the standard Introspector and BeanInfo from the Java Beans specification.
Betwixt provides a way of turning beans into XML as well as automatically generating digester rules in a way that can be customized on a per type manner in the same way that the BeanInfo mechanism can be used to customize the default introspection on a java object.
Bean Scripting Framework (BSF) is a set of Java classes which provides scripting language support within Java applications, and access to Java objects and methods from scripting languages. BSF allows one to write JSPs in languages other than Java while providing access to the Java class library. In addition, BSF permits any Java application to be implemented in part (or dynamically extended) by a language that is embedded within it. This is achieved by providing an API that permits calling scripting language engines from within Java, as well as an object registry that exposes Java objects to these scripting language engines.
A popular technique for organizing the execution of complex processing flows is the “Chain of Responsibility” pattern, as described (among many other places) in the classic “Gang of Four” design patterns book. Although the fundamental API contracts required to implement this design patten are extremely simple, it is useful to have a base API that facilitates using the pattern, and (more importantly) encouraging composition of command implementations from multiple diverse sources.
Towards that end, the Chain API models a computation as a series of “commands” that can be combined into a “chain”. The API for a command consists of a single method (
execute()), which is passed a “context” parameter containing the dynamic state of the computation, and whose return value is a boolean that determines whether or not processing for the current chain has been completed (true), or whether processing should be delegated to the next command in the chain (false).
The “context” abstraction is designed to isolate command implementations from the environment in which they are run (such as a command that can be used in either a Servlet or Portlet, without being tied directly to the API contracts of either of these environments). For commands that need to allocate resources prior to delegation, and then release them upon return (even if a delegated-to command throws an exception), the “filter” extension to “command” provides a
postprocess() method for this cleanup. Finally, commands can be stored and looked up in a “catalog” to allow deferral of the decision on which command (or chain) is actually executed.
The Apache Commons CLI library provides an API for parsing command line options passed to programs. It’s also able to print help messages detailing the options available for a command line tool.
Commons CLI supports different types of options:
POSIX like options (ie.
tar -zxvf foo.tar.gz)
GNU like long options (ie.
du --human-readable --max-depth=1)
Java like properties (ie.
java -Djava.awt.headless=true -Djava.net.useSystemProxies=true Foo)
Short options with value attached (ie.
gcc -O2 foo.c)
long options with single hyphen (ie.
Commons Codec provides implementations of common encoders and decoders such as Base64, Hex, Phonetic and URLs.
The Java Collections Framework was a major addition in JDK 1.2. It added many powerful data structures that accelerate development of most significant Java applications. Since that time it has become the recognised standard for collection handling in Java.
The Apache Commons Compress library defines an API for working with ar, cpio, Unix dump, tar, zip, gzip, Pack200 and bzip2 files.
The Commons Configuration software library provides a generic configuration interface which enables a Java application to read configuration data from a variety of sources. Commons Configuration provides typed access to single, and multi-valued configuration parameters as demonstrated by the following code:
Double double = config.getDouble("number");
Integer integer = config.getInteger("number");
Configuration parameters may be loaded from the following sources:
Different configuration sources can be mixed using a ConfigurationFactory and a CompositeConfiguration. Additional sources of configuration parameters can be created by using custom configuration objects. This customization can be achieved by extending AbstractConfiguration or AbstractFileConfiguration.
Most multi-user operating systems already have a way in which server applications are started and stopped. Under Unix based operating systems non interactive server applications are called daemons and are controlled by the operating system with a set of specified signals. Under Windows such programs are called services and are controlled by appropriate calls to specific functions defined in the application binary, but although the ways of dealing with the problem are different, in both cases the operating system can notify a server application of its imminent shutdown, and the application has the ability to perform certain tasks before its process of execution is destroyed.
Many Apache projects support interaction with a relational database. Creating a new connection for each user can be time consuming (often requiring multiple seconds of clock time), in order to perform a database transaction that might take milliseconds. Opening a connection per user can be unfeasible in a publicly-hosted Internet application where the number of simultaneous users can be very large. Accordingly, developers often wish to share a “pool” of open connections between all of the application’s current users. The number of users actually performing a request at any given time is usually a very small percentage of the total number of active users, and during request processing is the only time that a database connection is required. The application itself logs into the DBMS, and handles any user account issues internally.
The Commons DbUtils library is a small set of classes designed to make working with JDBC easier. JDBC resource cleanup code is mundane, error prone work so these classes abstract out all of the cleanup tasks from your code leaving you with what you really wanted to do with JDBC in the first place: query and update data.
Some of the advantages of using DbUtils are:
No possibility for resource leaks. Correct JDBC coding isn’t difficult but it is time-consuming and tedious. This often leads to connection leaks that may be difficult to track down.
Cleaner, clearer persistence code. The amount of code needed to persist data in a database is drastically reduced. The remaining code clearly expresses your intention without being cluttered with resource cleanup.
Automatically populate JavaBean properties from ResultSets. You don’t need to manually copy column values into bean instances by calling setter methods. Each row of the ResultSet can be represented by one fully populated bean instance.
Many projects read XML configuration files to provide initialization of various Java objects within the system. There are several ways of doing this, and the Digester component was designed to provide a common implementation that can be used in many different projects.
Basically, the Digester package lets you configure an XML -> Java object mapping module, which triggers certain actions called rules whenever a particular pattern of nested XML elements is recognized. A rich set of predefined rules is available for your use, or you can also create your own.
The Discovery component is about discovering, or finding, implementations for pluggable interfaces. It provides facilities for instantiating classes in general, and for lifecycle management of singleton (factory) classes.
Fundamentally, Discovery locates classes that implement a given Java interface. The discovery pattern, though not necessarily this package, is used in many projects including JAXP (SaxParserFactory and others) and commons-logging (LogFactory). By extracting this pattern, other projects can (re)use it and take advantage of improvements to the pattern as Discovery evolves.
EL is the JSP 2.0 Expression Language Interpreter from Apache.
Commons Email aims to provide a API for sending email. It is built on top of the Java Mail API, which it aims to simplify.
Some of the mail classes that are provided are as follows:
SimpleEmail – This class is used to send basic text based emails.
MultiPartEmail – This class is used to send multipart messages. This allows a text message with attachments either inline or attached.
HtmlEmail – This class is used to send HTML formatted emails. It has all of the capabilities as MultiPartEmail allowing attachments to be easily added. It also supports embedded images.
EmailAttachment – This is a simple container class to allow for easy handling of attachments. It is for use with instances of MultiPartEmail and HtmlEmail.
Executing external processes from Java is a well-known problem area. It is inheriently platform dependent and requires the developer to know and test for platform specific behaviors, for example using cmd.exe on Windows or limited buffer sizes causing deadlocks. The JRE support for this is very limited, albeit better with the new Java SE 1.5 ProcessBuilder class.
Reliably executing external processes can also require knowledge of the environment variables before or after the command is executed. In J2SE 1.1-1.4 there is not support for this, since the method, System.getenv(), for retriving environment variables is deprecated.
The Commons FileUpload package makes it easy to add robust, high-performance, file upload capability to your servlets and web applications.
FileUpload parses HTTP requests which conform to RFC 1867, “Form-based File Upload in HTML”. That is, if an HTTP request is submitted using the POST method, and with a content type of “multipart/form-data”, then FileUpload can parse that request, and make the results available in a manner easily used by the caller.
A functor is a function that can be manipulated as an object, or an object representing a single, generic function.
Functors support and encourage a number of powerful programming techniques including:
programming in a functional style
higher order functions
reuse and specialization through composition rather than inheritance and overloading
generic “callback” or “extension point” APIs
generic “filters” or predicate APIs
many “behavioral” design patterns, such as Visitor, Strategy, Chain of Responsibility, etc.
Commons IO is a library of utilities to assist with developing IO functionality.
There are six main areas included:
– useful Input Stream and Reader implementations
– useful Output Stream and Writer implementations
– various implementations of file filters
– various implementations of java.util.Comparator
– a component for monitoring file system events
JCS is a distributed caching system written in java. It is intended to speed up applications by providing a means to manage cached data of various dynamic natures. Like any caching system, JCS is most useful for high read, low put applications. Latency times drop sharply and bottlenecks move away from the database in an effectively cached system.
Jelly is a tool for turning XML into executable code. So Jelly is a Java and XML based scripting and processing engine. Jelly can be used as a more flexible and powerful front end to Ant such as in the Maven project, as a testing framework such as JellyUnit, in an intergration or workflow system such as werkflow or as a page templating system inside engines like Cocoon.
Jelly borrows many good ideas from both JSP custom tags, Velocity, Cocoon, Ant. Jelly can be used from the command line, inside Ant and Maven or inside a Servlet, Web Service, JMS MessageListener or embedded directly into your software.
JEXL is a library intended to facilitate the implementation of dynamic and scripting features in applications and frameworks.
It is a small footprint “glueing” API – the core features fit in 3 classes and 10 methods – that can be used in various conditions:
Module or component configuration:
Your application has configuration files (eventually generated by a design module) consumed by the end-user module that would benefit from variables and expressions.
When it would be convenient to use IOC but overall complexity doesn’t require (or can’t depend upon) a full-blown library (Spring, Guice…).
Loose-coupling of interfaces and implementations or duck-typing:
Basic scripting features:
Simple template capabilities:
Its name stands for Java EXpression Language, a simple expression language inspired by Apache Velocity and the Expression Language defined in the JavaServer Pages Standard Tag Library version 1.1 (JSTL) and JavaServer Pages version 2.0 (JSP). JEXL 2.0 adds features inspired by Unified EL.
org.apache.commons.jxpath package defines a simple interpreter of an expression language called XPath. JXPath applies XPath expressions to graphs of objects of all kinds: JavaBeans, Maps, Servlet contexts, DOM etc, including mixtures thereof.
Consider this example:
Address address = (Address)JXPathContext.newContext(vendor).
This XPath expression is equivalent to the following Java code:
Address address = null;
Collection locations = vendor.getLocations();
Iterator it = locations.iterator();
Location location = (Location)it.next();
String zipCode = location.getAddress().getZipCode();
address = location.getAddress();
The standard Java libraries fail to provide enough methods for manipulation of its core classes. Apache Commons Lang provides these extra methods.
Lang provides a host of helper utilities for the java.lang API, notably String manipulation methods, basic numerical methods, object reflection, concurrency, creation and serialization and System properties. Additionally it contains basic enhancements to java.util.Date and a series of utilities dedicated to help with building methods, such as hashCode, toString and equals.
The Launcher Component is designed to be a cross platform Java application launcher.
Commons-launcher eliminates the need for a batch or shell script to launch a Java class. Some situations where elimination of a batch or shell script may be desirable are:
You want to avoid having to determining where certain application paths are e.g. your application’s home directory, etc. Determining this dynamically in a Windows batch scripts is very tricky on some versions of Windows or when softlinks are used on Unix platforms.
You want to avoid having to handle native file and path separators or native path quoting issues.
You need to enforce certain system properties e.g.
java.endorsed.dirs when running with JDK 1.4.
You want to allow users to pass in custom JVM arguments or system properties without having to parse and reorder arguments in your script. This can be tricky and/or messy in batch and shell scripts.
You want to bootstrap system properties from a configuration file instead hard-coding them in your batch and shell scripts.
You want to provide localized error messages which is very tricky to do in batch and shell scripts.
When writing a library it is very useful to log information. However there are many logging implementations out there, and a library cannot impose the use of a particular one on the overall application that the library is a part of.
The Logging package is an ultra-thin bridge between different logging implementations. A library that uses the commons-logging API can be used with any logging implementation at runtime. Commons-logging comes with support for a number of popular logging implementations, and writing adapters for others is a reasonably simple task.
Applications (rather than libraries) may also choose to use commons-logging. While logging-implementation independence is not as important for applications as it is for libraries, using commons-logging does allow the application to change to a different logging implementation without recompiling code.
Note that commons-logging does not attempt to initialise or terminate the underlying logging implementation that is used at runtime; that is the responsibility of the application. However many popular logging implementations do automatically initialise themselves; in this case an application may be able to avoid containing any code that is specific to the logging implementation used.
Commons Math is a library of lightweight, self-contained mathematics and statistics components addressing the most common problems not available in the Java programming language or Commons Lang.
Real-world application use cases determine development priority.
This package emphasizes small, easily integrated components rather than large libraries with complex dependencies and configurations.
All algorithms are fully documented and follow generally accepted best practices.
In situations where multiple standard algorithms exist, a Strategy pattern is used to support multiple implementations.
Limited dependencies. No external dependencies beyond Commons components and the core Java platform (at least Java 1.3 up to version 1.2 of the library, at least Java 5 starting with version 2.0 of the library).
Java Management Extensions (JMX) is an API that facilitates building management applications that can configure, and perform operations on, a server application. In general, each manageable component of the server application is represented by a Management Bean (or MBean, for short). JMX defines three types of MBeans, of which Model MBeans are the most flexible. Model MBeans provide a way to define MBeans for many different components, without having to write a specific MBean implementation class for each one.
However, this power comes at a price. It is necessary to set up a substantial amount of metadata about each MBean, including the attributes it should expose (similar to JavaBeans properties), the operations it should make available (similar to calling methods of a Java object via reflection), and other related information. The Modeler component is designed to make this process fairly painless — the required metadata is configured from an XML description of each Model MBean to be supported. In addition, Modeler provides a factory mechanism to create the actual Model MBean instances themselves.
Apache Commons Net™ library implements the client side of many basic Internet protocols. The purpose of the library is to provide fundamental protocol access, not higher-level abstractions. Therefore, some of the design violates object-oriented design principles. Our philosophy is to make the global functionality of a protocol accessible (e.g., TFTP send file and receive file) when possible, but also provide access to the fundamental protocols where applicable so that the programmer may construct his own custom implementations (e.g, the TFTP packet classes and the TFTP packet send and receive methods are exposed).
OGNL stands for Object-Graph Navigation Language; it is an expression language for getting and setting properties of Java objects, plus other extras such as list projection and selection and lambda expressions. You use the same expression for both getting and setting the value of a property.
The Ognl class contains convenience methods for evaluating OGNL expressions. You can do this in two stages, parsing an expression into an internal form and then using that internal form to either set or get the value of a property; or you can do it in a single stage, and get or set a property using the String form of the expression directly.
Pool provides an Object-pooling API, with three major aspects:
A generic object pool interface that clients and implementors can use to provide easily interchangable pooling implementations.
A toolkit for creating modular object pools.
Several general purpose pool implementations.
The Java language is mostly Object based, however the original designers chose to include eight primitive types – boolean, byte, char, double, float, int, long and short. Commons Primitives provides a library of collections and utilities specially designed for use with primitive types.
JDK 1.5 added auto-boxing to simplify working with primitive types. This is simply a mechanism of automatically converting a primitive to an Object and vice versa, an operation with potentially large memory and performance issues. Commons Primitives offers collections that hold the primitive type directly, and as a result they are usually smaller, faster and easier to work with than their purely Object based alternatives.
The Proxy design pattern (GoF ) allows you to provide “a surrogate or placeholder for another object to control access to it”. Proxies can be used in many ways. Some of which are:
Deferred Initialization – the proxy acts as a “stand-in” for the actual implementation allowing it to be instantiated only when absolutely necessary.
Security – the proxy object can verify that the user actually has the permission to execute the method (a la EJB).
Logging – the proxy can log evey method invocation, providing valuable debugging information.
Performance Monitoring – the proxy can log each method invocation to a performance monitor allowing system administrators to see what parts of the system are potentially bogged down.
Commons Proxy supports dynamic proxy generation using proxy factories, object providers, invokers, and interceptors.
This Pure-Java library reads and writes a variety of image formats, including fast parsing of image info (size, color space, icc profile, etc.) and metadata.
This library is pure Java. It’s slow, consequently, but perfectly portable. It’s easier to use than ImageIO/JAI/Toolkit (Sun/Java’s image support), supports more formats (and supports them more correctly). It also provides easy access to metadata.
Although not yet version 1.0, sanselan is working and is used by a number of projects in production.
State Chart XML (SCXML) is currently a Working Draft published by the World Wide Web Consortium (W3C). SCXML provides a generic state-machine based execution environment based on Harel State Tables. SCXML is a candidate for the control language within multiple markup languages coming out of the W3C (see Working Draft for details). Commons SCXML is an implementation aimed at creating and maintaining a Java SCXML engine capable of executing a state machine defined using a SCXML document, while abstracting out the environment interfaces.
Commons Transaction aims at providing lightweight, standardized, well tested and efficient implementations of utility classes commonly used in transactional Java programming. Initially there are implementations for multi level locks, transactional collections and transactional file access. There may be additional implementations when the common need for them becomes obvious. However, the complete component shall remain compatible to JDK1.2 and should have minimal dependencies.
These are the initial parts in detail:
A common issue when receiving data either electronically or from user input is verifying the integrity of the data. This work is repetitive and becomes even more complicated when different sets of validation rules need to be applied to the same set of data based on locale. Error messages may also vary by locale. This package addresses some of these issues to speed development and maintenance of validation rules.
Commons VFS provides a single API for accessing various different file systems. It presents a uniform view of the files from various different sources, such as the files on local disk, on an HTTP server, or inside a Zip archive.
Some of the features of Commons VFS are:
A single consistent API for accessing files of different types.
Caching of file information. Caches information in-JVM, and optionally can cache remote file information on the local file system.
Support for logical file systems made up of files from various different file systems.
Utilities for integrating Commons VFS into applications, such as a VFS-aware ClassLoader and URLStreamHandlerFactory.
Powered by Zoundry Raven