Rapid JCR Applications Development with Sling - Presentation Transcript
Rapid JCR Applications Development with Sling Felix Meschberger ApacheCon US 09
About
Senior Developer at Day
fmeschbe@apache.org
http://blog.meschberger.ch
Apache Projects:
Sling
Felix
Jackrabbit
Contents
Introduction to JCR
Sling Overview
Single Script Blog
ESP Blog
Questions
Intro to JCR
JCR = Java Content Repository
Specifications
JSR-170 / JCR 1.0
JSR-283 / JCR 2.0
Everything is Content
JCR manages it in trees of Nodes & Properties
Rich Data Types
JSR-170
Content Repository for Java TM technology API
Spec Lead: David Nüscheler, Day Software
Final Release: 17 June 2005
Expert Group:
JSR-283
Content Repository for Java TM Technology API Version 2.0
Spec Lead: David Nüscheler, Day Software
Final Release: 25 September 2009
Expert Group:
JCR in 21 Words
The API should be a standard, implementation independent way to access content bidirectionally on a granular level to a Content Repository.
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
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.
Demo Preparation
Get Sling 5 Standalone from http://sling.apache.org/site/downloads.cgi
Upgrade to Web Console 2.0.2 from http://felix.apache.org/site/downloads.cgi
Compile and Install the Path based Resource Type Provider (from the samples/path-based-rtp folder)
Contents
Introduction to JCR
Sling Overview
Single Script Blog
ESP Blog
Questions
Minimal Sling blog
One Script (blog.esp)
JavaScript
46 Lines of Code (!)
http://sling.apache.org/site/46-line-blog.html
Sling POST servlet
# 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 { "jcr:primaryType": "nt:unstructured", "title": "hello" }
Blog Step 1: Create Content
<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>
Blog Step 2: Retrieve Content
<script src=“ /system/sling.js “></script> <form method=“POST“> ... </form> <!-- init form fields from current node --> <script> Sling.wizard(); </script>
Blog Step 3: Navigation
<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>
We Got a Blog ! html form + Sling.wizard() + Sling.getContent()
/** * 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="true" * */ public class ThumbnailGenerator implements EventListener {
ESP Blog Thumbnails: JCR Observation
/** @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 = { "nt:file" }; m.addEventListener( this, Event.NODE_ADDED, contentPath, true, null, types, false);
ESP Blog Thumbnails: NODE_ADDED
/** 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("thumbnails"))) { String p = event.getPath(); Node n = session.getRootNode().getNode(p); createThumbnails(addedNode); } } ...
Apache Sling is an OSGi-based, scriptable applicati more
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. less
0 comments
Post a comment