Apache Commons libraries

Posted by batman on Nov 26th, 2011
2011
Nov 26

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.

Attributes

Commons Attributes enables Java programmers to use C#/.Net-style attributes in their code.

BCEL

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.

BeanUtils

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 getXxx and 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 server.xml file).

The Java language provides Reflection and Introspection APIs (see the java.lang.reflect and 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.

Betwixt

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.

BSF

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.

Chain

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.

CLI

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. ant -projecthelp)

Codec

Commons Codec provides implementations of common encoders and decoders such as Base64, Hex, Phonetic and URLs.

Collections

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.

Compress

The Apache Commons Compress library defines an API for working with ar, cpio, Unix dump, tar, zip, gzip, Pack200 and bzip2 files.

Configuration

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:

  • Properties files
  • XML documents
  • Windows INI files
  • Property list files (plist)
  • JNDI
  • JDBC Datasource
  • System properties
  • Applet parameters
  • Servlet parameters

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.

Daemon

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.

DBCP

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.

Dbutils

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.

Digester

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.

Discovery

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

EL is the JSP 2.0 Expression Language Interpreter from Apache.

Email

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.

Exec

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.

FileUpload

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.

Functor

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
  • internal iterators
  • 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.

IO

Commons IO is a library of utilities to assist with developing IO functionality.

There are six main areas included:

  • Utility classes – with static methods to perform common tasks
  • Input – useful Input Stream and Reader implementations
  • Output – useful Output Stream and Writer implementations
  • Filters – various implementations of file filters
  • Comparators – various implementations of java.util.Comparator for files
  • File Monitor – a component for monitoring file system events

JCI

JCI is a java compiler interface. It can be used to either compile java (or any other language that can be compiled to java classes like e.g. groovy or javascript) to java. It is well integrated with a FAM (FilesystemAlterationMonitor) that can be used with the JCI compiling/reloading classloader. All the currently supported compilers (even javac before java6) feature in-memory compilation.

JCS

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

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.

Jelly has native support for a Velocity-like expression language called Jexl which is a superset of the JSP, JSTL and JSF expression languages as well as support for other pluggable expression languages like XPath via Jaxen, JavaScript, beanshell and Jython.

JEXL

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:
    • You have optional classes that your code cant consider as compilation dependencies.
    • You have to integrate and call “legacy” code or use components that you dont want to strongly depend upon.
  • Basic scripting features:
    • Your application lets (advanced) users evaluate or define some simple expressions like computation formulas.
  • Simple template capabilities:
    • Your application has basic template requirements and JSPs or Velocity would be overkill or too inconvenient to deploy.

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.

JXPath

The 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).
         getValue("locations[address/zipCode='90210']/address");

This XPath expression is equivalent to the following Java code:

Address address = null;
Collection locations = vendor.getLocations();
Iterator it = locations.iterator();
while (it.hasNext()){
    Location location = (Location)it.next();
    String zipCode = location.getAddress().getZipCode();
    if (zipCode.equals("90210")){
      address = location.getAddress();
      break;
    }
}

Lang

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.

Launcher

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.

Logging

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.

Math

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.

Guiding principles:

  1. Real-world application use cases determine development priority.
  2. This package emphasizes small, easily integrated components rather than large libraries with complex dependencies and configurations.
  3. All algorithms are fully documented and follow generally accepted best practices.
  4. In situations where multiple standard algorithms exist, a Strategy pattern is used to support multiple implementations.
  5. 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).

Modeler

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.

Net

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

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

Pool provides an Object-pooling API, with three major aspects:

  1. A generic object pool interface that clients and implementors can use to provide easily interchangable pooling implementations.
  2. A toolkit for creating modular object pools.
  3. Several general purpose pool implementations.

Primitives

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.

Proxy

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.

Sanselan

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.

SCXML

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.

Transaction

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:

  • memory package: Contains a wrapper to make any map implementing interface java.util.Map transactional. Depending on the type of the map that is wrapped this can either work as a transactional cache or some sort of volatile memory store.
  • locking package: Interfaces and implementations for locks that can have more than one owner at different compatible levels.
  • file package: Implementation of transaction file access. Using a pessimistic locking schema this implementation features serializable transactions.
  • util package: Contains a collection of utility classes used by the transaction package itself. Of more general interest could be a rendezvous barrier and a file utility class.

Validator

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.

VFS

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.
  • Support for numerous file system types .
  • Caching of file information. Caches information in-JVM, and optionally can cache remote file information on the local file system.
  • Event delivery.
  • 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.
  • A set of VFS-enabled Ant tasks .

Powered by Zoundry Raven

Share and Enjoy:
  • Facebook
  • Twitter
  • Digg
  • StumbleUpon
  • del.icio.us
  • Yahoo! Buzz
  • Google Bookmarks
  • LinkedIn
  • MySpace
  • laaik.it
  • Google Buzz
  • Technorati
  • Slashdot
  • Reddit

Leave a Comment




XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.