SlideShare una empresa de Scribd logo
1 de 35
© 2013 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Justin Edelson | Technical Architect
Sling Models
© 2013 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Agenda
 Background & Goals
 Usage
 Extensions
2
© 2013 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
What is Sling Models?
 Let’s say you want to adapt a Resource into some domain object…
3
© 2013 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
What is Sling Models?
public class OldModel {
private String title;
private String description;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
4
© 2013 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
What is Sling Models?
@Component
@Service
@Properties({
@Property(name=AdapterFactory.ADAPTABLE_CLASSES, value="org.apache.sling.api.Resource"),
@Property(name=AdapterFactory.ADAPTER_CLASSES,
value="com.adobe.people.jedelson.slingmodels.demo.OldModel")
})
public class OldModelAdapterFactory implements AdapterFactory {
@SuppressWarnings("unchecked")
public <AdapterType> AdapterType getAdapter(Object adaptable, Class<AdapterType> type) {
if (adaptable instanceof Resource && type.equals(OldModel.class)) {
OldModel model = new OldModel();
ValueMap map = ResourceUtil.getValueMap((Resource) adaptable);
model.setTitle(map.get(”title", String.class));
model.setDescription(map.get(”description", String.class));
return (AdapterType) model;
} else {
return null;
}
}
}
5
© 2013 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
What is Sling Models?
OldModel myModel = resource.adaptTo(OldModel.class)
<sling:adaptTo adaptable="${resource}" adaptTo=”…
OldModel" var=”myModel" />
<div data-sly-use.myModel =“…OldModel”></div>
6
© 2013 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
What is Sling Models?
@Model(adaptables = Resource.class)
public class NewModel {
@Inject
private String title;
@Inject
private String description;
public String getTitle() {
return title;
}
public String getDescription() {
return description;
}
}
7
© 2013 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
What is Sling Models?
NewModel myModel = resource.adaptTo(NewModel.class)
<sling:adaptTo adaptable="${resource}" adaptTo=”…
NewModel" var=”myModel" />
<div data-sly-use.myModel=“…NewModel”></div>
8
© 2013 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
What is Sling Models?
 The “old” way: 30+ LOC
 The “new” way: 13 LOC
 Plus one extra bundle header:
<Sling-Model-Packages>com.adobe.people.jedelson.slingmodels.demo</Sling-Model-Packages>
9
© 2013 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
What is Sling Models?
@Model(adaptables = Resource.class)
public interface NewModel2 {
@Inject
public String getTitle();
@Inject
public String getDescription();
}
10
© 2013 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Design Goals
 Entirely annotation driven. "Pure" POJOs.
 Use standard annotations where possible.
 Pluggable
 OOTB, support resource properties (via ValueMap), SlingBindings, OSGi
services, request attributes
 Adapt multiple objects - minimal required Resource and
SlingHttpServletRequest
 Client doesn't know/care that these objects are different than any other
adapter factory
 Support both classes and interfaces.
 Work with existing Sling infrastructure (i.e. not require changes to other
bundles).
11
© 2013 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Timeline
 December 2013 – YAMF prototype announced on sling-dev
 January 2014 – API formalized and renamed to Sling Models
 Feburary 2014 – 1.0.0 release; Included in AEM 6.0 Beta
 March 2014 – 1.0.2 release; Included in AEM 6.0 Release
 May 2014 – 1.0.4 release; Memory leak bug fix.
12
© 2013 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Usage – What can be injected?
 In order…
 SlingBindings objects
 ValueMap properties (with Resource -> ValueMap adaptation)
 Child Resources
 Request Attributes
 OSGi Services
 This is just the default set.
13
© 2013 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Usage - Annotations
 @org.apache.sling.models.annotations.Model
 @javax.inject.Inject
 @javax.inject.Named
 @org.apache.sling.models.annotations.Optional
 @org.apache.sling.models.annotations.Source
 @org.apache.sling.models.annotations.Filter
 @javax.inject.PostConstruct
 @org.apache.sling.models.annotations.Via
 @org.apache.sling.models.annotations.Default
14
© 2013 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Usage - @Model
 @Model(adaptables = Resource.class)
 @Model(adaptables = SlingHttpServletRequest.class)
 @Model(adaptables = { Resource.class, ValueMap.class })
15
© 2013 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Usage - @Inject
 @Inject private String title;
 valueMap.get(“title”, String.class);
 @Inject public String getTitle();
 valueMap.get(“title”, String.class);
 @Inject private String[] columnNames;
 valueMap.get(“columnNames”, String[].class);
 @Inject private List<Filter> filters;
 bundleContext.getServiceReferences(“javax.servlet.Filter”)
16
© 2013 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Usage - @Named
 By default, the name of the field or method is used to perform the
injection.
 @Inject @Named(“jcr:title”) private String title;
 valueMap.get(“jcr:title”, String.class);
17
© 2013 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Usage - @Optional
 By default, all @Inject points are required.
 resource.adaptTo(Model.class) <- returns null
 @Inject @Optional private String title;
18
© 2013 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Usage - @Source
 request.getAttribute(“text”) <- returns “goodbye”
 slingBindings.get(“text”) <- returns “hello”
 @Inject private String text; <- “hello” (SlingBindings is checked first)
 @Inject @Source(“request-attributes”) private String text; <- “goodbye”
19
© 2013 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Usage - @Source
20
© 2013 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Usage - @Filter
 Specifically for OSGi services:
 @Inject @Filter("(sling.servlet.extensions=json)") private List<Servlet>
servlets;
 Implicitly applies @Source(“osgi-services”)
21
© 2013 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Usage - @PostConstruct
 @Inject private String text;
 @PostConstruct protected void doSomething() { log.info("text = {}", text);
};
 Superclass @PostConstruct methods called first.
22
© 2013 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Usage - @Via
@Model(adaptables = SlingHttpServletRequest.class)
public class ViaModel {
@Inject
@Via("resource")
private String firstProperty;
public String getFirstProperty() {
return firstProperty;
}
}
23
© 2013 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Usage - @Default
 @Inject @Default(values=“default text”) private String text;
 Also
 booleanValues
 doubleValues
 floatValues
 intValues
 longValues
 shortValues
24
© 2013 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Usage – Constructor Injection
 If you need the adaptable itself
@Model(adaptables = SlingHttpServletRequest.class)
public class WithOneConstructorModel {
private final SlingHttpServletRequest request;
@Inject
private int attribute;
public WithOneConstructorModel(SlingHttpServletRequest request) {
this.request = request;
}
public int getAttribute() {
return attribute;
}
public SlingHttpServletRequest getRequest() {
return request;
}
}
25
© 2013 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Usage – Child Adaptation
@Model(adaptables = Resource.class)
public interface ChildValueMapModel {
@Inject
public ValueMap getFirstChild();
}
 resource.getChild(“firstChild”).adaptTo(ValueMap.class)
26
© 2013 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Usage – Fancy Child Adaptation
@Model(adaptables = Resource.class)
public interface ParentModel {
@Inject
public ChildModel getFirstChild();
}
 Works even if resource.adaptTo(ChildModel.class) isn’t done by Sling
Models
27
© 2013 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Extensions – Custom Injectors
 Injectors are OSGi services implementing the
org.apache.sling.models.spi.Injector interface
 Object getValue(Object adaptable, String name, Type type,
AnnotatedElement element, DisposalCallbackRegistry callbackRegistry)
 adaptable – the object being adapted
 name – the name (either using @Named or the default name)
 element – the method or field
 callbackRegistry – Injector gets notified when the adapted model is
garbage collected
28
© 2013 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Extensions – Custom Injector
public Object getValue(Object adaptable, String name,
Type type, AnnotatedElement element,
DisposalCallbackRegistry callbackRegistry) {
Resource resource = getResource(adaptable);
if (resource == null) {
return null;
} else if (type instanceof Class<?>) {
InheritanceValueMap map = new
HierarchyNodeInheritanceValueMap(resource);
return map.getInherited(name, (Class<?>) type);
} else {
return null;
}
}
29
© 2013 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Extensions – Custom Annotation
 Some injectors need extra data
 Example: OSGi service filters
@Target({ ElementType.FIELD, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
@Source("resource-path")
public @interface ResourcePath {
String value();
}
30
© 2013 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Extensions – Custom Annotations
public Object getValue(Object adaptable, String name,
Type declaredType, AnnotatedElement element,
DisposalCallbackRegistry callbackRegistry) {
ResourcePath path =
element.getAnnotation(ResourcePath.class);
if (path == null) {
return null;
}
ResourceResolver resolver = getResourceResolver(adaptable);
if (resolver == null) {
return null;
}
return resolver.getResource(path.value());
}
31
© 2013 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Extensions – Custom Annotations
@Model(adaptables = Resource.class)
public interface ResourcePathModel {
@Inject @ResourcePath("/content/dam")
Resource getResource();
}
32
© 2013 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Availability
 Bundles can be downloaded from http://sling.apache.org/downloads.cgi
 Content Package can be downloaded from https://github.com/Adobe-
Consulting-Services/com.adobe.acs.bundles.sling-models/releases
 Bleeding edge code can be built from
http://svn.apache.org/repos/asf/sling/trunk/bundles/extensions/models
33
© 2013 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.
Future Roadmap
 Custom Annotations
 More Standard Injectors
 Grandchild Resource Lists
 AEM-specific injectors in ACS AEM Commons
 Pluggable @Via support
34
© 2013 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

Más contenido relacionado

La actualidad más candente

AEM Rich Text Editor (RTE) Deep Dive
AEM Rich Text Editor (RTE) Deep DiveAEM Rich Text Editor (RTE) Deep Dive
AEM Rich Text Editor (RTE) Deep DiveHanish Bansal
 
JCR, Sling or AEM? Which API should I use and when?
JCR, Sling or AEM? Which API should I use and when?JCR, Sling or AEM? Which API should I use and when?
JCR, Sling or AEM? Which API should I use and when?connectwebex
 
AEM Best Practices for Component Development
AEM Best Practices for Component DevelopmentAEM Best Practices for Component Development
AEM Best Practices for Component DevelopmentGabriel Walt
 
Dynamic Components using Single-Page-Application Concepts in AEM/CQ
Dynamic Components using Single-Page-Application Concepts in AEM/CQDynamic Components using Single-Page-Application Concepts in AEM/CQ
Dynamic Components using Single-Page-Application Concepts in AEM/CQNetcetera
 
HTL(Sightly) - All you need to know
HTL(Sightly) - All you need to knowHTL(Sightly) - All you need to know
HTL(Sightly) - All you need to knowPrabhdeep Singh
 
Adobe AEM - From Eventing to Job Processing
Adobe AEM - From Eventing to Job ProcessingAdobe AEM - From Eventing to Job Processing
Adobe AEM - From Eventing to Job ProcessingCarsten Ziegeler
 
AEM Sightly Deep Dive
AEM Sightly Deep DiveAEM Sightly Deep Dive
AEM Sightly Deep DiveGabriel Walt
 
Integration patterns in AEM 6
Integration patterns in AEM 6Integration patterns in AEM 6
Integration patterns in AEM 6Yuval Ararat
 
Angular Data Binding
Angular Data BindingAngular Data Binding
Angular Data BindingDuy Khanh
 
Sling Models Using Sightly and JSP by Deepak Khetawat
Sling Models Using Sightly and JSP by Deepak KhetawatSling Models Using Sightly and JSP by Deepak Khetawat
Sling Models Using Sightly and JSP by Deepak KhetawatAEM HUB
 
The secret life of a dispatcher (Adobe CQ AEM)
The secret life of a dispatcher (Adobe CQ AEM)The secret life of a dispatcher (Adobe CQ AEM)
The secret life of a dispatcher (Adobe CQ AEM)Venugopal Gummadala
 
Modernising AEM Sites Codebase (AEM Meetup 2019)
Modernising AEM Sites Codebase  (AEM Meetup 2019)Modernising AEM Sites Codebase  (AEM Meetup 2019)
Modernising AEM Sites Codebase (AEM Meetup 2019)Hanish Bansal
 
Rest and Sling Resolution
Rest and Sling ResolutionRest and Sling Resolution
Rest and Sling ResolutionDEEPAK KHETAWAT
 
Dynamic components using SPA concepts in AEM
Dynamic components using SPA concepts in AEMDynamic components using SPA concepts in AEM
Dynamic components using SPA concepts in AEMBojana Popovska
 
Angular 8
Angular 8 Angular 8
Angular 8 Sunil OS
 
Mastering the Sling Rewriter
Mastering the Sling RewriterMastering the Sling Rewriter
Mastering the Sling RewriterJustin Edelson
 

La actualidad más candente (20)

AEM Rich Text Editor (RTE) Deep Dive
AEM Rich Text Editor (RTE) Deep DiveAEM Rich Text Editor (RTE) Deep Dive
AEM Rich Text Editor (RTE) Deep Dive
 
JCR, Sling or AEM? Which API should I use and when?
JCR, Sling or AEM? Which API should I use and when?JCR, Sling or AEM? Which API should I use and when?
JCR, Sling or AEM? Which API should I use and when?
 
AEM Best Practices for Component Development
AEM Best Practices for Component DevelopmentAEM Best Practices for Component Development
AEM Best Practices for Component Development
 
Dynamic Components using Single-Page-Application Concepts in AEM/CQ
Dynamic Components using Single-Page-Application Concepts in AEM/CQDynamic Components using Single-Page-Application Concepts in AEM/CQ
Dynamic Components using Single-Page-Application Concepts in AEM/CQ
 
HTL(Sightly) - All you need to know
HTL(Sightly) - All you need to knowHTL(Sightly) - All you need to know
HTL(Sightly) - All you need to know
 
Adobe AEM - From Eventing to Job Processing
Adobe AEM - From Eventing to Job ProcessingAdobe AEM - From Eventing to Job Processing
Adobe AEM - From Eventing to Job Processing
 
AEM Sightly Deep Dive
AEM Sightly Deep DiveAEM Sightly Deep Dive
AEM Sightly Deep Dive
 
Integration patterns in AEM 6
Integration patterns in AEM 6Integration patterns in AEM 6
Integration patterns in AEM 6
 
Angular modules in depth
Angular modules in depthAngular modules in depth
Angular modules in depth
 
Angular Data Binding
Angular Data BindingAngular Data Binding
Angular Data Binding
 
AEM - Client Libraries
AEM - Client LibrariesAEM - Client Libraries
AEM - Client Libraries
 
Sling Models Using Sightly and JSP by Deepak Khetawat
Sling Models Using Sightly and JSP by Deepak KhetawatSling Models Using Sightly and JSP by Deepak Khetawat
Sling Models Using Sightly and JSP by Deepak Khetawat
 
The secret life of a dispatcher (Adobe CQ AEM)
The secret life of a dispatcher (Adobe CQ AEM)The secret life of a dispatcher (Adobe CQ AEM)
The secret life of a dispatcher (Adobe CQ AEM)
 
Modernising AEM Sites Codebase (AEM Meetup 2019)
Modernising AEM Sites Codebase  (AEM Meetup 2019)Modernising AEM Sites Codebase  (AEM Meetup 2019)
Modernising AEM Sites Codebase (AEM Meetup 2019)
 
Sightly - Part 2
Sightly - Part 2Sightly - Part 2
Sightly - Part 2
 
Rest and Sling Resolution
Rest and Sling ResolutionRest and Sling Resolution
Rest and Sling Resolution
 
Angular Data Binding
Angular Data BindingAngular Data Binding
Angular Data Binding
 
Dynamic components using SPA concepts in AEM
Dynamic components using SPA concepts in AEMDynamic components using SPA concepts in AEM
Dynamic components using SPA concepts in AEM
 
Angular 8
Angular 8 Angular 8
Angular 8
 
Mastering the Sling Rewriter
Mastering the Sling RewriterMastering the Sling Rewriter
Mastering the Sling Rewriter
 

Destacado

Aem dispatcher – tips & tricks
Aem dispatcher – tips & tricksAem dispatcher – tips & tricks
Aem dispatcher – tips & tricksAshokkumar T A
 
The six key steps to AEM architecture
The six key steps to AEM architectureThe six key steps to AEM architecture
The six key steps to AEM architectureAshokkumar T A
 
Extra AEM Development Tools
Extra AEM Development ToolsExtra AEM Development Tools
Extra AEM Development ToolsJustin Edelson
 
Omnisearch in AEM 6.2 - Search All the Things
Omnisearch in AEM 6.2 - Search All the ThingsOmnisearch in AEM 6.2 - Search All the Things
Omnisearch in AEM 6.2 - Search All the ThingsJustin Edelson
 
Apache Sling Generic Validation Framework
Apache Sling Generic Validation FrameworkApache Sling Generic Validation Framework
Apache Sling Generic Validation FrameworkRadu Cotescu
 
Declarative Services - Dependency Injection OSGi Style
Declarative Services - Dependency Injection OSGi StyleDeclarative Services - Dependency Injection OSGi Style
Declarative Services - Dependency Injection OSGi StyleFelix Meschberger
 
SCR Annotations for Fun and Profit
SCR Annotations for Fun and ProfitSCR Annotations for Fun and Profit
SCR Annotations for Fun and ProfitMike Pfaff
 
Server-side OSGi with Apache Sling (OSGiDevCon 2011)
Server-side OSGi with Apache Sling (OSGiDevCon 2011)Server-side OSGi with Apache Sling (OSGiDevCon 2011)
Server-side OSGi with Apache Sling (OSGiDevCon 2011)Felix Meschberger
 
Modern operations with Apache Sling (2014 adaptTo version)
Modern operations with Apache Sling (2014 adaptTo version)Modern operations with Apache Sling (2014 adaptTo version)
Modern operations with Apache Sling (2014 adaptTo version)Bertrand Delacretaz
 
OSGi, Scripting and REST, Building Webapps With Apache Sling
OSGi, Scripting and REST, Building Webapps With Apache SlingOSGi, Scripting and REST, Building Webapps With Apache Sling
OSGi, Scripting and REST, Building Webapps With Apache SlingCarsten Ziegeler
 
RESTful Web Applications with Apache Sling
RESTful Web Applications with Apache SlingRESTful Web Applications with Apache Sling
RESTful Web Applications with Apache SlingBertrand Delacretaz
 
Shooting rabbits with sling
Shooting rabbits with slingShooting rabbits with sling
Shooting rabbits with slingTomasz Rękawek
 
Apache Sling : JCR, OSGi, Scripting and REST
Apache Sling : JCR, OSGi, Scripting and RESTApache Sling : JCR, OSGi, Scripting and REST
Apache Sling : JCR, OSGi, Scripting and RESTCarsten Ziegeler
 
AEM WITH MONGODB
AEM WITH MONGODBAEM WITH MONGODB
AEM WITH MONGODBNate Nelson
 
AEM (CQ) Dispatcher Caching Webinar 2013
AEM (CQ) Dispatcher Caching Webinar 2013AEM (CQ) Dispatcher Caching Webinar 2013
AEM (CQ) Dispatcher Caching Webinar 2013Andrew Khoury
 
IBM WebSphere Application Server traditional and Docker
IBM WebSphere Application Server traditional and DockerIBM WebSphere Application Server traditional and Docker
IBM WebSphere Application Server traditional and DockerDavid Currie
 

Destacado (19)

Aem dispatcher – tips & tricks
Aem dispatcher – tips & tricksAem dispatcher – tips & tricks
Aem dispatcher – tips & tricks
 
The six key steps to AEM architecture
The six key steps to AEM architectureThe six key steps to AEM architecture
The six key steps to AEM architecture
 
Extra AEM Development Tools
Extra AEM Development ToolsExtra AEM Development Tools
Extra AEM Development Tools
 
Omnisearch in AEM 6.2 - Search All the Things
Omnisearch in AEM 6.2 - Search All the ThingsOmnisearch in AEM 6.2 - Search All the Things
Omnisearch in AEM 6.2 - Search All the Things
 
Apache Sling Generic Validation Framework
Apache Sling Generic Validation FrameworkApache Sling Generic Validation Framework
Apache Sling Generic Validation Framework
 
Declarative Services - Dependency Injection OSGi Style
Declarative Services - Dependency Injection OSGi StyleDeclarative Services - Dependency Injection OSGi Style
Declarative Services - Dependency Injection OSGi Style
 
SCR Annotations for Fun and Profit
SCR Annotations for Fun and ProfitSCR Annotations for Fun and Profit
SCR Annotations for Fun and Profit
 
Reactive applications
Reactive applicationsReactive applications
Reactive applications
 
Server-side OSGi with Apache Sling (OSGiDevCon 2011)
Server-side OSGi with Apache Sling (OSGiDevCon 2011)Server-side OSGi with Apache Sling (OSGiDevCon 2011)
Server-side OSGi with Apache Sling (OSGiDevCon 2011)
 
Modern operations with Apache Sling (2014 adaptTo version)
Modern operations with Apache Sling (2014 adaptTo version)Modern operations with Apache Sling (2014 adaptTo version)
Modern operations with Apache Sling (2014 adaptTo version)
 
OSGi, Scripting and REST, Building Webapps With Apache Sling
OSGi, Scripting and REST, Building Webapps With Apache SlingOSGi, Scripting and REST, Building Webapps With Apache Sling
OSGi, Scripting and REST, Building Webapps With Apache Sling
 
RESTful Web Applications with Apache Sling
RESTful Web Applications with Apache SlingRESTful Web Applications with Apache Sling
RESTful Web Applications with Apache Sling
 
Shooting rabbits with sling
Shooting rabbits with slingShooting rabbits with sling
Shooting rabbits with sling
 
Aem maintenance
Aem maintenanceAem maintenance
Aem maintenance
 
Apache Sling : JCR, OSGi, Scripting and REST
Apache Sling : JCR, OSGi, Scripting and RESTApache Sling : JCR, OSGi, Scripting and REST
Apache Sling : JCR, OSGi, Scripting and REST
 
AEM WITH MONGODB
AEM WITH MONGODBAEM WITH MONGODB
AEM WITH MONGODB
 
Intro to OSGi
Intro to OSGiIntro to OSGi
Intro to OSGi
 
AEM (CQ) Dispatcher Caching Webinar 2013
AEM (CQ) Dispatcher Caching Webinar 2013AEM (CQ) Dispatcher Caching Webinar 2013
AEM (CQ) Dispatcher Caching Webinar 2013
 
IBM WebSphere Application Server traditional and Docker
IBM WebSphere Application Server traditional and DockerIBM WebSphere Application Server traditional and Docker
IBM WebSphere Application Server traditional and Docker
 

Similar a Sling Models Overview

Design Patterns - Part 2 of 2
Design Patterns - Part 2 of 2Design Patterns - Part 2 of 2
Design Patterns - Part 2 of 2Savio Sebastian
 
AEM and Sling
AEM and SlingAEM and Sling
AEM and SlingLokesh BS
 
Deepak khetawat sling_models_sightly_jsp
Deepak khetawat sling_models_sightly_jspDeepak khetawat sling_models_sightly_jsp
Deepak khetawat sling_models_sightly_jspDEEPAK KHETAWAT
 
Design Patterns - Part 1 of 2
Design Patterns - Part 1 of 2Design Patterns - Part 1 of 2
Design Patterns - Part 1 of 2Savio Sebastian
 
Efficient content structures and queries in CRX/CQ
Efficient content structures and queries in CRX/CQEfficient content structures and queries in CRX/CQ
Efficient content structures and queries in CRX/CQconnectwebex
 
CloudStack Meetup Santa Clara
CloudStack Meetup Santa Clara CloudStack Meetup Santa Clara
CloudStack Meetup Santa Clara NetApp
 
Intro to iOS Development • Made by Many
Intro to iOS Development • Made by ManyIntro to iOS Development • Made by Many
Intro to iOS Development • Made by Manykenatmxm
 
Mastering the Sling Rewriter by Justin Edelson
Mastering the Sling Rewriter by Justin EdelsonMastering the Sling Rewriter by Justin Edelson
Mastering the Sling Rewriter by Justin EdelsonAEM HUB
 
Storage Plug-ins
Storage Plug-ins Storage Plug-ins
Storage Plug-ins buildacloud
 
4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPT4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPTAjay Chimmani
 
Leveraging the Latest OSGi R7 Specifications - C Ziegeler & D Bosschaert
Leveraging the Latest OSGi R7 Specifications - C Ziegeler & D BosschaertLeveraging the Latest OSGi R7 Specifications - C Ziegeler & D Bosschaert
Leveraging the Latest OSGi R7 Specifications - C Ziegeler & D Bosschaertmfrancis
 
Android architecture
Android architecture Android architecture
Android architecture Trong-An Bui
 
Introduction to Spring Boot.pdf
Introduction to Spring Boot.pdfIntroduction to Spring Boot.pdf
Introduction to Spring Boot.pdfShaiAlmog1
 
Bring the fun back to java
Bring the fun back to javaBring the fun back to java
Bring the fun back to javaciklum_ods
 
New and cool in OSGi R7 - David Bosschaert & Carsten Ziegeler
New and cool in OSGi R7 - David Bosschaert & Carsten ZiegelerNew and cool in OSGi R7 - David Bosschaert & Carsten Ziegeler
New and cool in OSGi R7 - David Bosschaert & Carsten Ziegelermfrancis
 

Similar a Sling Models Overview (20)

Sightly_techInsight
Sightly_techInsightSightly_techInsight
Sightly_techInsight
 
Design Patterns - Part 2 of 2
Design Patterns - Part 2 of 2Design Patterns - Part 2 of 2
Design Patterns - Part 2 of 2
 
AEM and Sling
AEM and SlingAEM and Sling
AEM and Sling
 
Deepak khetawat sling_models_sightly_jsp
Deepak khetawat sling_models_sightly_jspDeepak khetawat sling_models_sightly_jsp
Deepak khetawat sling_models_sightly_jsp
 
Design Patterns - Part 1 of 2
Design Patterns - Part 1 of 2Design Patterns - Part 1 of 2
Design Patterns - Part 1 of 2
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 
Efficient content structures and queries in CRX/CQ
Efficient content structures and queries in CRX/CQEfficient content structures and queries in CRX/CQ
Efficient content structures and queries in CRX/CQ
 
Encapsulation
EncapsulationEncapsulation
Encapsulation
 
CloudStack Meetup Santa Clara
CloudStack Meetup Santa Clara CloudStack Meetup Santa Clara
CloudStack Meetup Santa Clara
 
Intro to iOS Development • Made by Many
Intro to iOS Development • Made by ManyIntro to iOS Development • Made by Many
Intro to iOS Development • Made by Many
 
Mastering the Sling Rewriter by Justin Edelson
Mastering the Sling Rewriter by Justin EdelsonMastering the Sling Rewriter by Justin Edelson
Mastering the Sling Rewriter by Justin Edelson
 
Storage Plug-ins
Storage Plug-ins Storage Plug-ins
Storage Plug-ins
 
4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPT4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPT
 
Leveraging the Latest OSGi R7 Specifications - C Ziegeler & D Bosschaert
Leveraging the Latest OSGi R7 Specifications - C Ziegeler & D BosschaertLeveraging the Latest OSGi R7 Specifications - C Ziegeler & D Bosschaert
Leveraging the Latest OSGi R7 Specifications - C Ziegeler & D Bosschaert
 
Android architecture
Android architecture Android architecture
Android architecture
 
SOLID
SOLIDSOLID
SOLID
 
Introduction to Spring Boot.pdf
Introduction to Spring Boot.pdfIntroduction to Spring Boot.pdf
Introduction to Spring Boot.pdf
 
Design patterns
Design patternsDesign patterns
Design patterns
 
Bring the fun back to java
Bring the fun back to javaBring the fun back to java
Bring the fun back to java
 
New and cool in OSGi R7 - David Bosschaert & Carsten Ziegeler
New and cool in OSGi R7 - David Bosschaert & Carsten ZiegelerNew and cool in OSGi R7 - David Bosschaert & Carsten Ziegeler
New and cool in OSGi R7 - David Bosschaert & Carsten Ziegeler
 

Último

Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️anilsa9823
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfkalichargn70th171
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
Active Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfActive Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfCionsystems
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AIABDERRAOUF MEHENNI
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationkaushalgiri8080
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 

Último (20)

Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Active Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfActive Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdf
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanation
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 

Sling Models Overview

  • 1. © 2013 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. Justin Edelson | Technical Architect Sling Models
  • 2. © 2013 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. Agenda  Background & Goals  Usage  Extensions 2
  • 3. © 2013 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. What is Sling Models?  Let’s say you want to adapt a Resource into some domain object… 3
  • 4. © 2013 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. What is Sling Models? public class OldModel { private String title; private String description; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } } 4
  • 5. © 2013 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. What is Sling Models? @Component @Service @Properties({ @Property(name=AdapterFactory.ADAPTABLE_CLASSES, value="org.apache.sling.api.Resource"), @Property(name=AdapterFactory.ADAPTER_CLASSES, value="com.adobe.people.jedelson.slingmodels.demo.OldModel") }) public class OldModelAdapterFactory implements AdapterFactory { @SuppressWarnings("unchecked") public <AdapterType> AdapterType getAdapter(Object adaptable, Class<AdapterType> type) { if (adaptable instanceof Resource && type.equals(OldModel.class)) { OldModel model = new OldModel(); ValueMap map = ResourceUtil.getValueMap((Resource) adaptable); model.setTitle(map.get(”title", String.class)); model.setDescription(map.get(”description", String.class)); return (AdapterType) model; } else { return null; } } } 5
  • 6. © 2013 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. What is Sling Models? OldModel myModel = resource.adaptTo(OldModel.class) <sling:adaptTo adaptable="${resource}" adaptTo=”… OldModel" var=”myModel" /> <div data-sly-use.myModel =“…OldModel”></div> 6
  • 7. © 2013 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. What is Sling Models? @Model(adaptables = Resource.class) public class NewModel { @Inject private String title; @Inject private String description; public String getTitle() { return title; } public String getDescription() { return description; } } 7
  • 8. © 2013 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. What is Sling Models? NewModel myModel = resource.adaptTo(NewModel.class) <sling:adaptTo adaptable="${resource}" adaptTo=”… NewModel" var=”myModel" /> <div data-sly-use.myModel=“…NewModel”></div> 8
  • 9. © 2013 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. What is Sling Models?  The “old” way: 30+ LOC  The “new” way: 13 LOC  Plus one extra bundle header: <Sling-Model-Packages>com.adobe.people.jedelson.slingmodels.demo</Sling-Model-Packages> 9
  • 10. © 2013 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. What is Sling Models? @Model(adaptables = Resource.class) public interface NewModel2 { @Inject public String getTitle(); @Inject public String getDescription(); } 10
  • 11. © 2013 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. Design Goals  Entirely annotation driven. "Pure" POJOs.  Use standard annotations where possible.  Pluggable  OOTB, support resource properties (via ValueMap), SlingBindings, OSGi services, request attributes  Adapt multiple objects - minimal required Resource and SlingHttpServletRequest  Client doesn't know/care that these objects are different than any other adapter factory  Support both classes and interfaces.  Work with existing Sling infrastructure (i.e. not require changes to other bundles). 11
  • 12. © 2013 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. Timeline  December 2013 – YAMF prototype announced on sling-dev  January 2014 – API formalized and renamed to Sling Models  Feburary 2014 – 1.0.0 release; Included in AEM 6.0 Beta  March 2014 – 1.0.2 release; Included in AEM 6.0 Release  May 2014 – 1.0.4 release; Memory leak bug fix. 12
  • 13. © 2013 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. Usage – What can be injected?  In order…  SlingBindings objects  ValueMap properties (with Resource -> ValueMap adaptation)  Child Resources  Request Attributes  OSGi Services  This is just the default set. 13
  • 14. © 2013 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. Usage - Annotations  @org.apache.sling.models.annotations.Model  @javax.inject.Inject  @javax.inject.Named  @org.apache.sling.models.annotations.Optional  @org.apache.sling.models.annotations.Source  @org.apache.sling.models.annotations.Filter  @javax.inject.PostConstruct  @org.apache.sling.models.annotations.Via  @org.apache.sling.models.annotations.Default 14
  • 15. © 2013 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. Usage - @Model  @Model(adaptables = Resource.class)  @Model(adaptables = SlingHttpServletRequest.class)  @Model(adaptables = { Resource.class, ValueMap.class }) 15
  • 16. © 2013 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. Usage - @Inject  @Inject private String title;  valueMap.get(“title”, String.class);  @Inject public String getTitle();  valueMap.get(“title”, String.class);  @Inject private String[] columnNames;  valueMap.get(“columnNames”, String[].class);  @Inject private List<Filter> filters;  bundleContext.getServiceReferences(“javax.servlet.Filter”) 16
  • 17. © 2013 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. Usage - @Named  By default, the name of the field or method is used to perform the injection.  @Inject @Named(“jcr:title”) private String title;  valueMap.get(“jcr:title”, String.class); 17
  • 18. © 2013 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. Usage - @Optional  By default, all @Inject points are required.  resource.adaptTo(Model.class) <- returns null  @Inject @Optional private String title; 18
  • 19. © 2013 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. Usage - @Source  request.getAttribute(“text”) <- returns “goodbye”  slingBindings.get(“text”) <- returns “hello”  @Inject private String text; <- “hello” (SlingBindings is checked first)  @Inject @Source(“request-attributes”) private String text; <- “goodbye” 19
  • 20. © 2013 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. Usage - @Source 20
  • 21. © 2013 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. Usage - @Filter  Specifically for OSGi services:  @Inject @Filter("(sling.servlet.extensions=json)") private List<Servlet> servlets;  Implicitly applies @Source(“osgi-services”) 21
  • 22. © 2013 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. Usage - @PostConstruct  @Inject private String text;  @PostConstruct protected void doSomething() { log.info("text = {}", text); };  Superclass @PostConstruct methods called first. 22
  • 23. © 2013 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. Usage - @Via @Model(adaptables = SlingHttpServletRequest.class) public class ViaModel { @Inject @Via("resource") private String firstProperty; public String getFirstProperty() { return firstProperty; } } 23
  • 24. © 2013 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. Usage - @Default  @Inject @Default(values=“default text”) private String text;  Also  booleanValues  doubleValues  floatValues  intValues  longValues  shortValues 24
  • 25. © 2013 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. Usage – Constructor Injection  If you need the adaptable itself @Model(adaptables = SlingHttpServletRequest.class) public class WithOneConstructorModel { private final SlingHttpServletRequest request; @Inject private int attribute; public WithOneConstructorModel(SlingHttpServletRequest request) { this.request = request; } public int getAttribute() { return attribute; } public SlingHttpServletRequest getRequest() { return request; } } 25
  • 26. © 2013 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. Usage – Child Adaptation @Model(adaptables = Resource.class) public interface ChildValueMapModel { @Inject public ValueMap getFirstChild(); }  resource.getChild(“firstChild”).adaptTo(ValueMap.class) 26
  • 27. © 2013 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. Usage – Fancy Child Adaptation @Model(adaptables = Resource.class) public interface ParentModel { @Inject public ChildModel getFirstChild(); }  Works even if resource.adaptTo(ChildModel.class) isn’t done by Sling Models 27
  • 28. © 2013 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. Extensions – Custom Injectors  Injectors are OSGi services implementing the org.apache.sling.models.spi.Injector interface  Object getValue(Object adaptable, String name, Type type, AnnotatedElement element, DisposalCallbackRegistry callbackRegistry)  adaptable – the object being adapted  name – the name (either using @Named or the default name)  element – the method or field  callbackRegistry – Injector gets notified when the adapted model is garbage collected 28
  • 29. © 2013 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. Extensions – Custom Injector public Object getValue(Object adaptable, String name, Type type, AnnotatedElement element, DisposalCallbackRegistry callbackRegistry) { Resource resource = getResource(adaptable); if (resource == null) { return null; } else if (type instanceof Class<?>) { InheritanceValueMap map = new HierarchyNodeInheritanceValueMap(resource); return map.getInherited(name, (Class<?>) type); } else { return null; } } 29
  • 30. © 2013 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. Extensions – Custom Annotation  Some injectors need extra data  Example: OSGi service filters @Target({ ElementType.FIELD, ElementType.METHOD }) @Retention(RetentionPolicy.RUNTIME) @Qualifier @Source("resource-path") public @interface ResourcePath { String value(); } 30
  • 31. © 2013 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. Extensions – Custom Annotations public Object getValue(Object adaptable, String name, Type declaredType, AnnotatedElement element, DisposalCallbackRegistry callbackRegistry) { ResourcePath path = element.getAnnotation(ResourcePath.class); if (path == null) { return null; } ResourceResolver resolver = getResourceResolver(adaptable); if (resolver == null) { return null; } return resolver.getResource(path.value()); } 31
  • 32. © 2013 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. Extensions – Custom Annotations @Model(adaptables = Resource.class) public interface ResourcePathModel { @Inject @ResourcePath("/content/dam") Resource getResource(); } 32
  • 33. © 2013 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. Availability  Bundles can be downloaded from http://sling.apache.org/downloads.cgi  Content Package can be downloaded from https://github.com/Adobe- Consulting-Services/com.adobe.acs.bundles.sling-models/releases  Bleeding edge code can be built from http://svn.apache.org/repos/asf/sling/trunk/bundles/extensions/models 33
  • 34. © 2013 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential. Future Roadmap  Custom Annotations  More Standard Injectors  Grandchild Resource Lists  AEM-specific injectors in ACS AEM Commons  Pluggable @Via support 34
  • 35. © 2013 Adobe Systems Incorporated. All Rights Reserved. Adobe Confidential.

Notas del editor

  1. Or less…
  2. @Model determines what classes can be adapted from. It’s essentially the equivelant of the adaptables service property on an Adapter Factory
  3. @Inject marks an injection point. In a class, can be on a field. In an interface, on a method. Arrays and Lists are supported.
  4. Possibly in the future, a class-level annotation could be used to set the default to @Optional, at which point we’ll add @Required
  5. Source names are listed on http://sling.apache.org/documentation/bundles/models.html. Also http://localhost:4502/system/console/status-slingmodels
  6. Obviously only for classes. Doesn’t apply to interfaces
  7. Constructor must only have a single argument. The argument can be a superclass, i.e. HttpServletRequest