OW2 UTILITIES
 The Swiss Army Knife Of OW2 Projects
           Guillaume Sauthier




OW2 Annual Conference 2012, 27-29 November, Orange Labs, Paris
                      www.ow2.org                                1
OW2 Utilities
       Because
 code
 should
 be
 written
 only
 once
             Because
 code
 should
 be
 well
 tested
                                                   Because
 code
 should
 be
 shared

OW2 Annual Conference 2012, 27-29 November, Orange Labs, Paris
                      www.ow2.org                                                                                                                                                                                                                  2
Genesis
                                              How

Ow2 Utilities - The Swiss Army Knife Of Ow2 Projects

  • 1.
    OW2 UTILITIES TheSwiss Army Knife Of OW2 Projects Guillaume Sauthier OW2 Annual Conference 2012, 27-29 November, Orange Labs, Paris www.ow2.org 1
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
     once Because
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
     tested Because
  • 14.
  • 15.
  • 16.
  • 17.
     shared OW2 Annual Conference2012, 27-29 November, Orange Labs, Paris www.ow2.org 2
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
     ? OW2 Annual Conference2012, 27-29 November, Orange Labs, Paris www.ow2.org 3
  • 24.
    A LITTLE BITOF 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
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
     modules OW2 Annual Conference2012, 27-29 November, Orange Labs, Paris www.ow2.org 5
  • 36.
  • 37.
  • 38.
  • 39.
     mentioned • I18n andLogger • Simple Java/Xml mapper (read your Xml configuration files) • Internationalized loggers, Java 5 style • Base64 • Xml parsing • Encode, decode • Obtain a DocumentParser, extract values from Elements (trimmed, • File/URL conversion Properties, default values, ...) • Classloader aware • Xml Config ObjectInputStream OW2 Annual Conference 2012, 27-29 November, Orange Labs, Paris www.ow2.org 6
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
     ? • Traverse Class,Method and Field declaration • Support inheritance • Call interested AnnotationHandler(s) public class SampleAnnotationHandler extends AbstractAnnotationHandler { public boolean isSupported(Class? extends Annotation clazz) { return clazz.equals(Hello.class); } @Override IAnnotationProcessor processor = new DefaultAnnotationProcessor(); public void process(Annotation annotation, Class? clazz, Object target) processor.addAnnotationHandler(new SampleAnnotationHandler()); throws ProcessorException { processor.addAnnotationHandler(/* as many handler as you want */); System.out.printf(Type %s is annotated with @%s(%s), processor.process(/* some instance */); clazz, annotation.annotationType().getSimpleName() ((Hello) annotation).name()); } } OW2 Annual Conference 2012, 27-29 November, Orange Labs, Paris www.ow2.org 7
  • 51.
    Asynchronous EVENT SYSTEM Support • On steroid topic notification system // Simple asynchronous listener IEventListener listener = new IEventListener() { public void handle(IEvent event) { • Hierarchical, multi-threaded // Do your own casting here System.out.printf(Received Event %s, event); } public EventPriority getPriority() { • Dispatcher fires event on a topic return EventPriority.ASYNC_HIGH; } public boolean accept(IEvent event) { return true; } }; • Listeners receive events matching a topic pattern • Declares a priority (SYNC, ASYNC) OW2 Annual Conference 2012, 27-29 November, Orange Labs, Paris www.ow2.org 8
  • 52.
    // Create aDispatcher EVENT SYSTEM Some
  • 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
  • 58.
  • 59.
  • 60.
  • 61.
     ? OW2 Annual Conference2012, 27-29 November, Orange Labs, Paris www.ow2.org 14
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
     ?’ http://gitorious.ow2.org/ow2-utilities ‘You
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
     documentation’ http://utilities.ow2.org ‘Wow,
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
     CI’ http://bamboo.ow2.org/browse/UTIL ‘Do
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
     !’ http://jira.ow2.org/browse/UTIL OW2 Annual Conference 2012, 27-29 November, Orange Labs, Paris www.ow2.org 15
  • 95.
    Questions OW2 Annual Conference2012, 27-29 November, Orange Labs, Paris www.ow2.org 16
  • 96.
    utilities.ow2.org guillaume.sauthier@peergreen.com @sauthieg OW2 Annual Conference 2012, 27-29 November, Orange Labs, Paris www.ow2.org 17
  • 97.
    RESOURCES • http://www.flickr.com/photos/emagic/56206868/ OW2 Annual Conference 2012, 27-29 November, Orange Labs, Paris www.ow2.org 18