Comprehensive Java utility library providing infrastructure and helper classes for the Eclipse Jetty web server
—
Specialized data structures for high-performance web applications including multi-maps, tries, pools, and concurrent collections. These structures are optimized for the access patterns common in web servers and networking applications.
Container for name/value pairs with case sensitivity options, designed for HTTP header-like data structures.
/**
* A container for name/value pairs, commonly used for HTTP headers and form parameters
*/
public class Fields implements Iterable<Fields.Field> {
/** Create new Fields instance */
public Fields();
/** Create Fields with case sensitivity setting */
public Fields(boolean caseSensitive);
/** Add name/value pair */
public void add(String name, String value);
/** Get first value for name */
public String get(String name);
/** Get all values for name */
public List<String> getValues(String name);
/** Put single value (replaces existing) */
public void put(String name, String value);
/** Put multiple values (replaces existing) */
public void put(String name, List<String> values);
/** Remove all values for name */
public boolean remove(String name);
/** Check if name exists */
public boolean contains(String name);
/** Get number of name/value pairs */
public int size();
/** Check if empty */
public boolean isEmpty();
/** Clear all fields */
public void clear();
/** Get field names */
public Set<String> getFieldNames();
/** Individual field representation */
public static class Field {
public String getName();
public String getValue();
public List<String> getValues();
}
}Usage Examples:
import org.eclipse.jetty.util.Fields;
import java.util.List;
// HTTP headers example
Fields headers = new Fields();
headers.add("Content-Type", "text/html");
headers.add("Accept", "text/html");
headers.add("Accept", "application/json"); // Multiple values
String contentType = headers.get("Content-Type");
List<String> acceptTypes = headers.getValues("Accept");
// Form parameters
Fields params = new Fields();
params.put("username", "john");
params.put("roles", List.of("admin", "user"));
// Iteration
for (Fields.Field field : headers) {
System.out.println(field.getName() + ": " + field.getValue());
}Multi-valued map extending LinkedHashMap for maintaining insertion order with multiple values per key.
/**
* A Map that can hold multiple values for each key
*/
public class MultiMap<V> extends LinkedHashMap<String, List<V>> {
/** Create new MultiMap */
public MultiMap();
/** Create MultiMap with case sensitivity */
public MultiMap(boolean caseSensitive);
/** Add value to key (creates list if needed) */
public void add(String key, V value);
/** Get first value for key */
public V getValue(String key);
/** Get all values for key */
public List<V> getValues(String key);
/** Put single value (replaces existing) */
public V putValue(String key, V value);
/** Add all values from another MultiMap */
public void addAllValues(MultiMap<V> map);
/** Remove specific value from key */
public boolean removeValue(String key, V value);
/** Convert to single-valued map (first values only) */
public Map<String, V> toStringArrayMap();
}Usage Examples:
import org.eclipse.jetty.util.MultiMap;
import java.util.List;
// Query parameters with multiple values
MultiMap<String> params = new MultiMap<>();
params.add("tag", "java");
params.add("tag", "web");
params.add("tag", "server");
params.add("limit", "10");
// Access values
String limit = params.getValue("limit");
List<String> tags = params.getValues("tag");
// Check for values
if (params.containsKey("tag")) {
tags.forEach(tag -> System.out.println("Tag: " + tag));
}Generic object pooling interface with lifecycle management for resource reuse.
/**
* Generic object pool interface
*/
public interface Pool<P> {
/** Acquire entry from pool */
Pool.Entry<P> acquire();
/** Release entry back to pool */
boolean release(Pool.Entry<P> entry);
/** Get maximum pool capacity */
int getMaxEntries();
/** Get current pool size */
int size();
/** Get number of available entries */
int getIdleCount();
/** Get number of entries in use */
int getInUseCount();
/** Check if pool is closed */
boolean isClosed();
/** Close pool and release resources */
void close();
/** Pool entry interface */
interface Entry<P> {
/** Get pooled object */
P getPooled();
/** Release entry back to pool */
boolean release();
/** Remove entry from pool permanently */
boolean remove();
/** Check if entry is reserved */
boolean isReserved();
}
}
/**
* Thread-safe pool implementation
*/
public class ConcurrentPool<P> implements Pool<P> {
/** Create pool with factory and maximum size */
public ConcurrentPool(Pool.Factory<P> factory, int maxSize);
/** Create pool with factory, maximum size, and multiplexing */
public ConcurrentPool(Pool.Factory<P> factory, int maxSize, boolean multiplexed);
/** Pool factory for creating objects */
public interface Factory<P> {
P create();
boolean test(P pooled);
void destroy(P pooled);
}
}Usage Examples:
import org.eclipse.jetty.util.Pool;
import org.eclipse.jetty.util.ConcurrentPool;
// Database connection pool example
ConcurrentPool<Connection> connectionPool = new ConcurrentPool<>(
new Pool.Factory<Connection>() {
@Override
public Connection create() {
return DriverManager.getConnection(url, user, password);
}
@Override
public boolean test(Connection conn) {
try {
return !conn.isClosed() && conn.isValid(1);
} catch (SQLException e) {
return false;
}
}
@Override
public void destroy(Connection conn) {
try { conn.close(); } catch (SQLException e) {}
}
},
20 // max connections
);
// Using pooled connection
Pool.Entry<Connection> entry = connectionPool.acquire();
if (entry != null) {
try {
Connection conn = entry.getPooled();
// Use connection
PreparedStatement stmt = conn.prepareStatement("SELECT * FROM users");
// ...
} finally {
entry.release(); // Return to pool
}
}Immutable string lookup data structures optimized for fast prefix matching.
/**
* Immutable string-to-value lookup interface
*/
public interface Index<V> {
/** Get value for exact key match */
V get(String key);
/** Get value for ByteBuffer key */
V get(ByteBuffer key);
/** Get best matching value for key */
V getBest(String key);
/** Get best matching value with prefix length */
V getBest(String key, int offset, int length);
/** Check if key exists */
boolean isMutable();
/** Get all keys */
Set<String> keySet();
}
/**
* Abstract base for trie implementations
*/
public abstract class AbstractTrie<V> implements Index<V> {
/** Put key/value pair */
public abstract boolean put(String key, V value);
/** Check if case sensitive */
public abstract boolean isCaseInsensitive();
}
/**
* Array-based trie implementation
*/
public class ArrayTrie<V> extends AbstractTrie<V> {
/** Create case sensitive trie */
public ArrayTrie();
/** Create trie with case sensitivity option */
public ArrayTrie(boolean insensitive);
/** Create trie with initial capacity */
public ArrayTrie(int capacity);
}
/**
* Tree-based trie implementation
*/
public class TreeTrie<V> extends AbstractTrie<V> {
/** Create case sensitive trie */
public TreeTrie();
/** Create trie with case sensitivity option */
public TreeTrie(boolean insensitive);
}Usage Examples:
import org.eclipse.jetty.util.ArrayTrie;
import org.eclipse.jetty.util.Index;
// HTTP method lookup
ArrayTrie<String> methods = new ArrayTrie<>();
methods.put("GET", "GET");
methods.put("POST", "POST");
methods.put("PUT", "PUT");
methods.put("DELETE", "DELETE");
// Fast lookup
String method = methods.get("GET");
String bestMatch = methods.getBest("GETDATA"); // Returns "GET"
// MIME type lookup (case insensitive)
ArrayTrie<String> mimeTypes = new ArrayTrie<>(true);
mimeTypes.put("html", "text/html");
mimeTypes.put("css", "text/css");
mimeTypes.put("js", "application/javascript");
String mimeType = mimeTypes.get("HTML"); // Returns "text/html"Key-value attribute storage interface with implementations.
/**
* Key-value attribute storage interface
*/
public interface Attributes {
/** Get attribute value */
Object getAttribute(String name);
/** Get attribute names */
Set<String> getAttributeNames();
/** Set attribute value */
void setAttribute(String name, Object attribute);
/** Remove attribute */
Object removeAttribute(String name);
/** Clear all attributes */
void clearAttributes();
}
/**
* Map-based attributes implementation
*/
public class AttributesMap implements Attributes, Dumpable {
/** Create new attributes map */
public AttributesMap();
/** Create attributes map from existing map */
public AttributesMap(Map<String, Object> map);
/** Get underlying map */
public Map<String, Object> getMap();
}Usage Examples:
import org.eclipse.jetty.util.Attributes;
import org.eclipse.jetty.util.AttributesMap;
// Request attributes
Attributes attributes = new AttributesMap();
attributes.setAttribute("user", userObject);
attributes.setAttribute("startTime", System.currentTimeMillis());
attributes.setAttribute("requestId", UUID.randomUUID().toString());
// Later access
User user = (User) attributes.getAttribute("user");
Long startTime = (Long) attributes.getAttribute("startTime");
// Cleanup
attributes.removeAttribute("user");
attributes.clearAttributes(); // Remove allAdditional concurrent data structures for high-performance applications.
/**
* Blocking array queue implementation
*/
public class BlockingArrayQueue<E> extends AbstractList<E>
implements BlockingQueue<E> {
/** Create queue with initial capacity */
public BlockingArrayQueue(int capacity);
/** Create queue with capacity and growth factor */
public BlockingArrayQueue(int capacity, int growBy);
/** Create queue with capacity and max capacity */
public BlockingArrayQueue(int capacity, int growBy, int maxCapacity);
}
/**
* Atomic operations on two integers
*/
public class AtomicBiInteger {
/** Create with initial values */
public AtomicBiInteger(int encoded);
/** Create with separate hi/lo values */
public AtomicBiInteger(int hi, int lo);
/** Get hi value */
public int getHi();
/** Get lo value */
public int getLo();
/** Get encoded value */
public int get();
/** Set both values atomically */
public void set(int hi, int lo);
/** Compare and set both values */
public boolean compareAndSet(int expectedEncoded, int hi, int lo);
/** Add to both values */
public int addAndGet(int deltaHi, int deltaLo);
}Usage Examples:
import org.eclipse.jetty.util.BlockingArrayQueue;
import org.eclipse.jetty.util.AtomicBiInteger;
import java.util.concurrent.TimeUnit;
// Producer-consumer queue
BlockingArrayQueue<String> queue = new BlockingArrayQueue<>(100);
// Producer
queue.offer("task1");
queue.offer("task2", 1, TimeUnit.SECONDS);
// Consumer
String task = queue.take(); // Blocks until available
String nextTask = queue.poll(5, TimeUnit.SECONDS); // Timeout
// Atomic counters
AtomicBiInteger counters = new AtomicBiInteger(0, 0);
counters.addAndGet(1, 0); // Increment success count
counters.addAndGet(0, 1); // Increment failure count
int successes = counters.getHi();
int failures = counters.getLo();These data structures are specifically optimized for the access patterns common in web servers and high-performance networking applications.
Install with Tessl CLI
npx tessl i tessl/maven-org-eclipse-jetty--jetty-util