Rapid JCR Applications Development with Sling

7,126 views
7,042 views

Published on

Apache Sling is an OSGi-based, scriptable applications layer, using REST principles, that runs on top of a JCR content repository. In this talk, we'll see how Sling enables rapid development of JCR-based content applications, by leveraging the JSR 223 scripting framework. We'll also look at the rich set of OSGi components provided by Sling. We will create a simple application from scratch in a few minutes, and explain a more complex multimedia application that does a lot with just a few lines of code. This talk will help you get started with Sling and understand how the different components fit together.

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

No Downloads
Views
Total views
7,126
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
255
Comments
0
Likes
5
Embeds 0
No embeds

No notes for slide

Rapid JCR Applications Development with Sling

  1. 1. Rapid JCR Applications Development with Sling Felix Meschberger ApacheCon US 09
  2. 2. About <ul><li>Senior Developer at Day
  3. 3. fmeschbe@apache.org
  4. 4. http://blog.meschberger.ch
  5. 5. Apache Projects: </li><ul><li>Sling
  6. 6. Felix
  7. 7. Jackrabbit </li></ul></ul>
  8. 8. Contents <ul><li>Introduction to JCR
  9. 9. Sling Overview
  10. 10. Single Script Blog
  11. 11. ESP Blog
  12. 12. Questions </li></ul>
  13. 13. Intro to JCR <ul><li>JCR = Java Content Repository
  14. 14. Specifications </li><ul><li>JSR-170 / JCR 1.0
  15. 15. JSR-283 / JCR 2.0 </li></ul><li>Everything is Content </li><ul><li>JCR manages it in trees of Nodes & Properties
  16. 16. Rich Data Types </li></ul></ul>
  17. 17. JSR-170 <ul><li>Content Repository for Java TM technology API
  18. 18. Spec Lead: David Nüscheler, Day Software
  19. 19. Final Release: 17 June 2005
  20. 20. Expert Group: </li></ul>
  21. 21. JSR-283 <ul><li>Content Repository for Java TM Technology API Version 2.0
  22. 22. Spec Lead: David Nüscheler, Day Software
  23. 23. Final Release: 25 September 2009
  24. 24. Expert Group: </li></ul>
  25. 25. JCR in 21 Words <ul>The API should be a standard, implementation independent way to access content bidirectionally on a granular level to a Content Repository. </ul>
  26. 26. JCR = DB + FS + more access control streams locking hierar-chies write read query structure tx obser-vation full text versions item order unstruc-tured multi value integrity JCR DB FS
  27. 27. Implementations <ul><li>Implementations </li><ul><li>Apache Jackrabbit
  28. 28. Day CRX
  29. 29. Oracle XML DB
  30. 30. IBM FileNet P8
  31. 31. IBM CM
  32. 32. Exo ECMS Platform
  33. 33. Xythos Repository
  34. 34. Alfresco ECM </li></ul></ul><ul><li>Connectors: </li><ul><li>Interwoven Repository
  35. 35. EMC Documentum
  36. 36. MS Sharepoint
  37. 37. OpenText Livelink
  38. 38. Vignette V7 </li></ul></ul>100s of TCKs registered
  39. 39. Some known Applications
  40. 40. JCR code excerpt <ul>Repository repository = new TransientRepository(); Session session = repository. login (...); // Create content Node root = session. getRootNode (); Node hello = root. addNode (&quot;hello&quot;); Node world = hello.addNode(&quot;world&quot;); world. setProperty (&quot;message&quot;, &quot;Hello, World!&quot;); session. save (); // Retrieve content Node node = root. getNode (&quot;hello/world&quot;); print(node. getPath ()); print(node. getProperty (&quot;message&quot;).getString()); </ul>
  41. 41. Contents <ul><li>Introduction to JCR
  42. 42. Sling Overview
  43. 43. Single Script Blog
  44. 44. ESP Blog
  45. 45. Questions </li></ul>
  46. 46. Sling builds on top of JCR <ul><li>Scriptable applications layer
  47. 47. OSGi based industrial strength
  48. 48. Simple, powerful, JCR inside
  49. 49. Runs on Apache Jackrabbit by default
  50. 50. Sling == REST over JCR </li></ul>
  51. 51. Sling URL decomposition /content/cars/audi/s4.details.html
  52. 52. Sling Architecture OSGi Framework Felix Web Console WebDAV Server browser filesystem debugger HTTP JSR-170/283 API JCR repository resource resolution servlet resolution standard servlets custom servlets JSR223 Scripting JSP javascript etc.
  53. 53. Demo Preparation <ul><li>Get Sling 5 Standalone from http://sling.apache.org/site/downloads.cgi
  54. 54. Upgrade to Web Console 2.0.2 from http://felix.apache.org/site/downloads.cgi
  55. 55. Compile and Install the Path based Resource Type Provider (from the samples/path-based-rtp folder) </li></ul>
  56. 56. Contents <ul><li>Introduction to JCR
  57. 57. Sling Overview
  58. 58. Single Script Blog
  59. 59. ESP Blog
  60. 60. Questions </li></ul>
  61. 61. Minimal Sling blog <ul><li>One Script (blog.esp)
  62. 62. JavaScript
  63. 63. 46 Lines of Code (!)
  64. 64. http://sling.apache.org/site/46-line-blog.html </li></ul>
  65. 65. Sling POST servlet <ul># POST to Sling curl -F title=hello http://localhost:8888/foo -> 200 OK # GET created node in json format curl http:/ /localhost:8888/foo.tidy.json { &quot;jcr:primaryType&quot;: &quot;nt:unstructured&quot;, &quot;title&quot;: &quot;hello&quot; } </ul>
  66. 66. Blog Step 1: Create Content <ul><form method=“ POST “> Title: <input type=“text“ name=“ title “ /> Text: <textarea name=“ text ></textarea> <input type=“submit“ /> <input type=“hidden“ name=“:redirect“ value=“*.html“ /> </form> </ul>
  67. 67. Blog Step 2: Retrieve Content <ul><script src=“ /system/sling.js “></script> <form method=“POST“> ... </form> <!-- init form fields from current node --> <script> Sling.wizard(); </script> </ul>
  68. 68. Blog Step 3: Navigation <ul><ul> <li> <a href=“ /content/blog/*.html “> [Create new post]</a> </li> <script> var posts = Sling. getContent („/content/blog“, 2); for (var post in posts) { document.write(„<li><a href='“ + post + „.html'>“ + posts[post].title </a></li>“); } </script> </ul> </ul>
  69. 69. We Got a Blog ! html form + Sling.wizard() + Sling.getContent()
  70. 70. Contents <ul><li>Introduction to JCR
  71. 71. Sling Overview
  72. 72. Single Script Blog
  73. 73. ESP Blog
  74. 74. Questions </li></ul>
  75. 75. The ESP Blog Sample <ul><li>ESP (Server side JavaScript) scripting
  76. 76. Java
  77. 77. JavaScript
  78. 78. OSGi Bundle
  79. 79. Initial Content
  80. 80. WebDAV
  81. 81. Observation
  82. 82. http://tinyurl.com/slingblogesp2 </li></ul>
  83. 83. ESP Blog Demo
  84. 84. ESP Blog Source Files <ul><li>admin.esp
  85. 85. edit.esp
  86. 86. html.esp
  87. 87. list.esp
  88. 88. menu.esp
  89. 89. xml.esp (RSS)
  90. 90. constants.esp
  91. 91. header.esp </li></ul><ul><li>pom.xml
  92. 92. ThumbnailGenerator.java
  93. 93. espblog.css
  94. 94. sling-logo.png </li></ul>
  95. 95. ESP Blog Content Structure
  96. 96. ESP Blog Edit Script <ul><% pageTitle = currentNode.title; load(&quot;header.esp&quot;); %> <body> <form method=&quot;POST&quot; action=&quot;<%= currentNode%>&quot;> <p><label>Title</label> <input name=&quot;title&quot; type=&quot;text&quot; value=&quot;<%= currentNode.title %>&quot;></p> ... <input type=&quot;hidden&quot; name=&quot;created&quot;/> <input name=&quot;:redirect&quot; type=&quot;hidden&quot; value=&quot;/content/espblog/posts.admin.html&quot;/> <input type=&quot;submit&quot; value=&quot;Post&quot; class=&quot;button&quot;> </form> ... </ul>
  97. 97. ESP Blog Thumbnails: OSGi DS Component <ul>/** * Observe the espblog content for changes, and generate * thumbnails when images are added. * * maven-scr-plugin uses annotations to generate the OSGi * Declarative Services XML configuration files * @scr.component immediate=&quot;true&quot; * */ public class ThumbnailGenerator implements EventListener { </ul>
  98. 98. ESP Blog Thumbnails: JCR Observation <ul>/** @scr.reference (framework injects it automatically) */ private SlingRepository repository; /** called by framework when service is activated */ protected void activate(ComponentContext context) { Session s = repository.loginAdministrative(null); // Listen for nt:file NODE_ADDED repository events ObservationManager m = s.getWorkspace().getObservationManager(); String[] types = { &quot;nt:file&quot; }; m.addEventListener( this, Event.NODE_ADDED, contentPath, true, null, types, false); </ul>
  99. 99. ESP Blog Thumbnails: NODE_ADDED <ul>/** Called by JCR Observation manager for events that this * EventListener registered for */ public void onEvent(EventIterator it) { while (it.hasNext()) { Event event = it.nextEvent(); if (event.getType() == Event.NODE_ADDED && !(event.getPath().contains(&quot;thumbnails&quot;))) { String p = event.getPath(); Node n = session.getRootNode().getNode(p); createThumbnails(addedNode); } } ... </ul>
  100. 100. We got a typical Sling Application! <ul><li>JCR Features: WebDAV, Observation, nt:unstructured
  101. 101. Sling Goodies: Simple Script mappings (BYOL), POST Servlet, RESTful interface
  102. 102. OSGi Bundle, Code + Initial Content, Maven Plugins </li></ul>
  103. 103. Where is Sling going ? <ul><li>First Web Framework designed for JCR
  104. 104. Embrace the web, act like a very clever web server !
  105. 105. Intelligent HTTP/JSON storage
  106. 106. OSGi
  107. 107. Organic Application Growth
  108. 108. Sling graduated as a TLP in May 2009 </li></ul>
  109. 109. Links <ul><li>http://sling.apache.org/
  110. 110. http://dev.day.com/
  111. 111. http://contentcentric.org/ </li></ul>
  112. 112. Questions ?
  113. 113. Thank You !

×