Ow2 Utilities - The Swiss Army Knife Of Ow2 Projects

605 views

Published on

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.

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
605
On SlideShare
0
From Embeds
0
Number of Embeds
58
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Ow2 Utilities - The Swiss Army Knife Of Ow2 Projects

  1. 1. OW2 UTILITIES The Swiss Army Knife Of OW2 Projects Guillaume SauthierOW2 Annual Conference 2012, 27-29 November, Orange Labs, Paris www.ow2.org 1
  2. 2. OW2 Utilities Because
  3. 3.  code
  4. 4.  should
  5. 5.  be
  6. 6.  written
  7. 7.  only
  8. 8.  once Because
  9. 9.  code
  10. 10.  should
  11. 11.  be
  12. 12.  well
  13. 13.  tested Because
  14. 14.  code
  15. 15.  should
  16. 16.  be
  17. 17.  sharedOW2 Annual Conference 2012, 27-29 November, Orange Labs, Paris www.ow2.org 2
  18. 18. Genesis How
  19. 19.  did
  20. 20.  we
  21. 21.  get
  22. 22.  there
  23. 23.  ?OW2 Annual Conference 2012, 27-29 November, Orange Labs, Paris www.ow2.org 3
  24. 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
  25. 25. Modules 15
  26. 26.  minutes
  27. 27.  is
  28. 28.  so
  29. 29.  short,
  30. 30.  and
  31. 31.  we
  32. 32.  have
  33. 33.  so
  34. 34.  many
  35. 35.  modulesOW2 Annual Conference 2012, 27-29 November, Orange Labs, Paris www.ow2.org 5
  36. 36. QUITE BASIC STUFF Just
  37. 37.  to
  38. 38.  be
  39. 39.  mentioned• I18n and Logger • 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. 40. ANNOTATION PROCESSOR Want
  41. 41.  a
  42. 42.  nice
  43. 43.  way
  44. 44.  to
  45. 45.  visit
  46. 46.  annotations
  47. 47.  of
  48. 48.  an
  49. 49.  instance
  50. 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); } @OverrideIAnnotationProcessor 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. 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. 52. // Create a Dispatcher EVENT SYSTEM Some
  53. 53.  codeEventDispatcher dispatcher = new EventDispatcher();dispatcher.setNbWorkers(3); // Listen to all Event published in /result/**dispatcher.start(); eventService.registerListener(listener, /result/.*); // Topic registrationeventService.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. 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. 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. 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. 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. 58. Contributions What
  59. 59.  about
  60. 60.  you
  61. 61.  ?OW2 Annual Conference 2012, 27-29 November, Orange Labs, Paris www.ow2.org 14
  62. 62. ANONYMOUS QUOTES‘I
  63. 63.  want
  64. 64.  to
  65. 65.  code,
  66. 66.  where
  67. 67.  are
  68. 68.  the
  69. 69.  repositories
  70. 70.  ?’ http://gitorious.ow2.org/ow2-utilities ‘You
  71. 71.  convinced
  72. 72.  me,
  73. 73.  let
  74. 74.  me
  75. 75.  write
  76. 76.  some
  77. 77.  documentation’ http://utilities.ow2.org‘Wow,
  78. 78.  they
  79. 79.  even
  80. 80.  have
  81. 81.  a
  82. 82.  Bamboo
  83. 83.  for
  84. 84.  CI’ http://bamboo.ow2.org/browse/UTIL ‘Do
  85. 85.  they
  86. 86.  really
  87. 87.  need
  88. 88.  a
  89. 89.  JIRA,
  90. 90.  it’s
  91. 91.  working
  92. 92.  so
  93. 93.  great
  94. 94.  !’ http://jira.ow2.org/browse/UTIL OW2 Annual Conference 2012, 27-29 November, Orange Labs, Paris www.ow2.org 15
  95. 95. QuestionsOW2 Annual Conference 2012, 27-29 November, Orange Labs, Paris www.ow2.org 16
  96. 96. utilities.ow2.org guillaume.sauthier@peergreen.com @sauthiegOW2 Annual Conference 2012, 27-29 November, Orange Labs, Paris www.ow2.org 17
  97. 97. RESOURCES• http://www.flickr.com/photos/emagic/56206868/ OW2 Annual Conference 2012, 27-29 November, Orange Labs, Paris www.ow2.org 18

×