CQCON 2013: five Sling features you should know Olaf Otto 19.06.2013
1: Write custom tag libraries Use current XSD Fully qualified identifier <taglib version="2.0" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"> <taglib> <uri>http://www.corp.com/taglibs/mytaglib/1.0</uri>... </taglib> Multiple TLDs allowed META-INF/<name>.tld computing.co.uk Version your API! Taglibs are not exactly the latest trend, but they get the job done properly reusable, testable implementations for viewrelated tasks such as rich text transformation, i18n, date formatting etc. Unic - Seite 2
1.1 Talking of versioned taglibs: <sling:include /> <sling:forward /> <sling:defineobjects /> <sling:call /> <sling:eval /> http://sling.apache.org/taglibs/sling/1.0 http://sling.apache.org/taglibs/sling/1.2 Current ${sling:adaptto(from, to) ${sling:findresources(resolver, query, language) ${sling:getrelativeresource(resource, path) ${sling:getresource(resolver, path) ${sling:listchildren(resource) Coming http://sling.apache.org/taglibs/sling/1.3 <sling:getproperty /> <sling:getresource /> Unic - Seite 3
2: Enriching the view @Service @Component(immediate = true) public class Provider implements BindingsValuesProvider { @Override public void addbindings(bindings bindings) { Map<String,?> ${bindings.key anything View (JSP) computing.co.uk The BindingsValuesProvider allows you to inject anything into any view and thus aides in decoupling view and backend layer. Unic - Seite 4
2.1 By the way: Semantic Annotations @SlingServlet(paths = "/bin/myservlet") public class MyServlet extends SlingSafeMethodsServlet { @Override public void doget( ) { @SlingFilter public class MyFilter implements Filter { @Override public void dofilter( ) { Use typesafe Java 5 annotations (not the JAVADoc ones). SlingFilter and SlingServlet are part of felix-scrannotations and supported by the maven-scr-plugin. Unic - Seite 5
Click ++complexity to edit Master title style Sling has a simple generic resource model (Resource, ValueMap), which may not suffice when resources require complex operations on their data Unic - Seite 6
3: When Resource is not enough Page cq:page These are independent aspects of the cq:page resource type taken from an actual project. Each aspect requires an implementation. CUG protection Sitemap entry Link Properties Persistence Unic - Seite 7
3.1.adaptTo(TheRescue) Please do not implement such aspects in static util functions. This is a terrible anti-pattern. No more util-class projects, please. {data, Operations : Model.adaptTo(X) Best pratice for Sling: write a POJO for an aspect and.adaptto it..adaptto s most valueable application is adaptation to type safe representations of aspects of resources Unic - Seite 8
3.2:.adaptTo & OSGi best practices.adapto is a perfect match for OSGi best practices. Refer to http://www.ibm.com/developerworks/websphere/techjournal/1007_charters/1007_charter s.html#sec5 Feature.adaptTo computing.co.uk Implementation dadcando.com API Provides API implementation + AdapterFactory Contains adapter interface Clean separation Unic - Seite 9
3.3: Boilerplate, anyone? = from @Component @Service @Properties({ = to @Property(name = "adaptables", value = "org.apache.sling.api.resource.resource"), @Property(name = "adapters", value = "fully.qualified.type.name") ) public class MyFactory implements AdapterFactory Stringly { typed public <T> T getadapter(object from, Class<T> totype) { If () else if () Such boilerplate may well be the reason developers avoid adapter factories Unic - Seite 10
3.4: Choices! Improve declaration Luckily, boilerplate can be circumvented by either simplifying declaration or programmatically publishing a generic adapter factory onespoonatatime.com Note: AdapterFactories are excellent extension points for Spring integration. When obtaining the Adapter from the Spring bean factory, you can transparently adapt to a Spring bean at any time from anywhere in you application. Generic Adapterfactory Unic - Seite 11
3.5 Custom SCR annotation for adapter factories Typesafe! The simple most approach is to exchange the boilerplate SCR metadata with a semantic annotation @Adapts(from = Resource.class, to = Name.class) public class MyFactory implements AdapterFactory { public <T> T getadapter(object from, Class<T> totype) { How? SCR plugin / task + AnnotationProcessor Refer to: http://felix.apache.org/documentation/subprojects/apache-felixmaven-scr-plugin/extending-scr-annotations.html Unic - Seite 12
4: Decorating resources @Service @Component(immediate = true) public class Decorator implements ResourceDecorator { @Override public Resource decorate(resource resource) { Note: This is one of the most powerful extension points allowing manipulating any resource even script and servlets Manipulation of any Resource (everything is a resource!). Transparent (.adaptto, Views) Sample application: Resources with a specific property (e.g. inherit=true) are wrapped with a resource that provides an InhertianceVauleMap. This way, you can enable transparent inheritance for any resource at any time without changing a single line of code. Source from CQCON, see: http://www.cqcon.eu/2013/en/speakers/olaf-otto.html Unic - Seite 13
5: Providing custom resources JCR ResourceProvider /custom /content /site /custom/<child> Refer to http://sling.apache.org/documentation/the-slingengine/resources.html, «Providing Resources» Unic - Seite 14
5.1: The pros an cons Bad fit for thirdparty integration (tight coupling!). Better: soaml ESB RESTful support e.g. ServiceMix System Client Advertise Use Enterprise serv ice bus System Client JSONP for crossdomain integration System Client Good fit for local integration (DBs, Filesystem ). Upcoming Sling version features provider for MongoDB. Unic - Seite 15
Some more things to take a look at: CRUD support with PersistableValueMap Deployment of custom node types in bundles (CND) Static resource support: Serve static resource from bundles Unic - Seite 16
Unic AG Belpstrasse 48 3007 Bern Tel +41 31 560 12 12 Fax +41 31 560 12 13 info@unic.com www.unic.com Unic - Seite 17