• No results found

CQCON 2013: five Sling features you should know

N/A
N/A
Protected

Academic year: 2021

Share "CQCON 2013: five Sling features you should know"

Copied!
17
0
0

Loading.... (view fulltext now)

Full text

(1)

CQCON 2013: five Sling features you should know

Olaf Otto

(2)

1: Write custom tag libraries

<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>

META-INF/<name>.tld

Version your API! Fully qualified identifier Use current XSD

Taglibs are not exactly the latest trend, but they get the job done properly –

re-usable, testable

implementations for view-related tasks such as rich

text transformation, i18n, date formatting etc.

Multiple TLDs allowed

(3)

1.1 Talking of versioned taglibs:

<sling:include /> <sling:forward /> <sling:defineObjects /> http://sling.apache.org/taglibs/sling/1.0 <sling:call />

<sling:eval /> 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)}

<sling:getProperty /> <sling:getResource />

http://sling.apache.org/taglibs/sling/1.3 Coming

(4)

2: Enriching the view

@Service

@Component(immediate = true)

public class Provider implements BindingsValuesProvider {

@Override

public void addBindings(Bindings bindings) { } } View (JSP) anything Map<String, ?> ${bindings.key}

The BindingsValuesProvider allows you to inject anything into any view and thus aides in

de-coupling view and backend layer. © computing.co.uk

(5)

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-scr-annotations and supported by the maven-scr-plugin.

(6)

Click to edit Master title style

++complexity

Sling has a simple generic resource model (Resource, ValueMap), which

may not suffice when resources require complex operations on their

(7)

3: When

Resource

is not enough

CUG protection Persistence Sitemap entry Page cq:Page Properties Link

These are independent aspects of the cq:Page resource type taken from

an actual project. Each aspect requires an

(8)

3.1 .adaptTo(TheRescue)

No more util-class projects, please.

{

data, Operations

} : Mode

l

.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

Please do not implement such aspects in static util functions. This is a terrible

(9)

3.2: .adaptTo & OSGi best practices

© dadcando.com Implementation API Feature .adaptTo Provides API implementation + AdapterFactory Clean separation

.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 Contains adapter interface © computing.co.uk

(10)

3.3: Boilerplate, anyone?

@Component

@Service

@Properties({

@Property(name = "adaptables",

value = "org.apache.sling.api.resource.Resource"),

@Property(name = "adapters",

value = "fully.qualified.type.Name")

})

public class MyFactory implements AdapterFactory {

public <T> T getAdapter(Object from, Class<T> toType) {

… } } = from = to If () else if () … Stringly typed

Such boilerplate may well be the reason developers avoid

(11)

3.4: Choices!

Generic Adapterfactory Improve declaration

© onespoonatatime.com

Luckily, boilerplate can be circumvented by either simplifying declaration or programmatically publishing

a generic adapter factory

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

(12)

3.5 Custom SCR annotation for adapter factories

@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

Typesafe!

The simple most approach is to exchange the boilerplate SCR

metadata with a semantic annotation

Refer to: http://felix.apache.org/documentation/subprojects/apache-felix-maven-scr-plugin/extending-scr-annotations.html

(13)

4: Decorating resources

• Manipulation of any Resource

• Transparent (.adaptTo, Views)

@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

(everything is a resource!).

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

(14)

5: Providing custom resources

ResourceProvider /custom /custom/<child> JCR /content /site Refer to http://sling.apache.org/documentation/the-sling-engine/resources.html , «Providing Resources»

(15)

• Bad fit for thirdparty integration (tight coupling!). Better:

• Good fit for local integration (DBs, Filesystem…). Upcoming Sling version features provider for MongoDB.

5.1: The pros an cons

soaml ESB

Advertise Advertise

Advertise UseUseUse

Enterprise serv ice bus

Advertise

Advertise UseUse

System System System Client Client Client RESTful support – e.g. ServiceMix

JSONP for cross-domain integration

(16)

• CRUD support with PersistableValueMap

• Deployment of custom node types in bundles (CND)

• Static resource support: Serve static resource from bundles

(17)

Belpstrasse 48 3007 Bern Tel +41 31 560 12 12 Fax +41 31 560 12 13 [email protected] www.unic.com

Unic AG

References

Related documents

Cilj doktorske disertacije je predstaviti problematiko in preiskovanje kriminalitete zoper umetnine, opraviti pregled obstoječega stanja na področju proučevanja, raziskovanja

To receive credit on a final draft, you must submit a hard copy of your paper directly to me in class and upload another copy of the same paper into Blackboard’s SafeAssign

In SEM (structural equation model), the test of marketing capability construct shows that the variable with highest impact on firm performance is social capital (weight

Sketch Manhattan from the observation deck of the Empire State Building, looking south toward the Statue of Liberty.. CitySketch_NY_prfs05.indd 20-21 05/11/2013

Therefore, determining the number of people who will be served, their per capita use, and the factors that who will be served, their per capita use, and the factors that affect

Further, Customers must fully indemnify Diodes Incorporated and its representatives against any damages arising out of the use of Diodes Incorporated products in such

considerations impacting the officer, the officer’s family, and other police officers involved in the aftermath of a shooting incident; relevant theoretical considerations of