OW2 Utilities is a newly accepted project in the consortium. It aims to be the OW2 toolkit catalog for common pieces of code that everybody rewrite for each new project. This presentation will start with a description of the goals of that project, a little bit of history, then we will explain why it's important to maximize re-use within the consortium (reliability, ...). In a second part we will focus to the most useful and/or interesting modules provided by this project. This session will be developer-oriented, with code samples and effective use cases.
24. A LITTLE BIT OF HISTORY
• Was born in 2006 inside the OW2 EasyBeans project
• Used in JOnAS, CAROL, CMI, EasyBeans, Shelbie, JASMINe for years
• Extracted as a separate maven project in 2008
• More than 35 releases up to today !
• Proposed as top level OW2 project in 2012
• We’re here now :)
OW2 Annual Conference 2012, 27-29 November, Orange Labs, Paris
www.ow2.org 4
53. code
EventDispatcher dispatcher = new EventDispatcher();
dispatcher.setNbWorkers(3); // Listen to all Event published in /result/**
dispatcher.start(); eventService.registerListener(listener, /result/.*);
// Topic registration
eventService.registerDispatcher(/result/success, dispatcher);
Dispatchers Loose Coupling Event Listeners
Event Source(s)
Asynchronous
Consumer(s)
/a/b
/a
/c
Event Service
// Dispatch an Event
dispatcher.dispatch(new MyEvent(/result/.*));
OW2 Annual Conference 2012, 27-29 November, Orange Labs, Paris
www.ow2.org 9
54. Re-using
POOL instances
public static void main(String[] args) throws Exception {
• Simple Pooling API
PoolFactoryPooled, Long poolFactory = new PooledFactory();
JPoolPooled,Long pool = new JPoolPooled, Long(poolFactory);
pool.start();
Pooled one = pool.get();
• Pool size, waiters, timeout
Pooled two = pool.get();
Pooled three = pool.get();
Pooled four = pool.get(); // Will block until timeout or a release
}
•
Thread-safe private static final class PooledFactory implements PoolFactoryPooled, Long {
public Pooled create(Long clue) throws PoolException {
return new Pooled(clue);
}
• Basic implementation
public boolean isMatching(Pooled instance, Long clue) {
}
return instance.id == clue;
public void remove(Pooled instance) { // Destroy instance }
•
public boolean validate(Pooled instance, PoolEntryStatistics stats) {
Synchronous
}
return true;
}
• Advanced implementation @Pool(min = 1, max = 3)
private static class Pooled {
long id;
public Pooled(Long clue) { id = clue; }
•
}
Asynchronous, composable
OW2 Annual Conference 2012, 27-29 November, Orange Labs, Paris
www.ow2.org 10
55. Variable
SUBSTITUTION processing
• Extract variables declaration from String
• ‘Hello ${speaker.name} !’ DefaultSubstitutionEngine engine = new DefaultSubstitutionEngine();
ChainedResolver chain = new ChainedResolver();
chain.getResolvers().add(new SpeakerResolver());
• PropertyResolvers
chain.getResolvers().add(new DateResolver());engine.setResolver(chain);
engine.substitute(Hello ${speaker.name} ! -- ${date});
public class SpeakerResolver implements IPropertyResolver {
public String resolve(String expression) {
return speaker.name.equals(expression) ? Guillaume : null;
• Provides value for expression
}
}
public class DateResolver implements IPropertyResolver {
public String resolve(String expression) {
• Composable
}
}
return date.equals(expression) ? (new Date()).toString() : null;
• Support recursion (variables in variable)
OW2 Annual Conference 2012, 27-29 November, Orange Labs, Paris
www.ow2.org 11
56. Resource
ARCHIVE Abstraction
• Uniform API for resource consumption
• Supporting (out of the box) public interface IArchive {
String getName();
URL getURL() throws ArchiveException;
• Directory URL getResource(String resourceName) throws ArchiveException;
IteratorURL getResources() throws ArchiveException;
• Jar IteratorURL getResources(String resourceName) throws ArchiveException;
IteratorString getEntries() throws ArchiveException;
boolean close();
• OSGi Bundle IArchiveMetadata getMetadata();
}
• Extensible
OW2 Annual Conference 2012, 27-29 November, Orange Labs, Paris
www.ow2.org 12
57. BUNDLES, BUNDLES, BUNDLES
• Around 20 common libraries wrapped as Bundles
• commons-{collections, logging, modeler}, javassist, jaxb2-ri, jgroups, jsch, opencsv,
weld, zookeeper, bouncycastle, ...
• Correct exported packages version, verified imports
• All other modules are OSGi Bundles
• Versioned API, content exported
OW2 Annual Conference 2012, 27-29 November, Orange Labs, Paris
www.ow2.org 13