SlideShare a Scribd company logo
1 of 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.

More Related Content

What's hot

AEM Sightly Deep Dive
AEM Sightly Deep DiveAEM Sightly Deep Dive
AEM Sightly Deep DiveGabriel Walt
 
Adobe Experience Manager Core Components
Adobe Experience Manager Core ComponentsAdobe Experience Manager Core Components
Adobe Experience Manager Core ComponentsGabriel Walt
 
Introduction to Sightly
Introduction to SightlyIntroduction to Sightly
Introduction to SightlyAnkit Gubrani
 
Modernizing Adobe Experience Manager (AEM)
Modernizing Adobe Experience Manager (AEM)Modernizing Adobe Experience Manager (AEM)
Modernizing Adobe Experience Manager (AEM)Gabriel Walt
 
Integration patterns in AEM 6
Integration patterns in AEM 6Integration patterns in AEM 6
Integration patterns in AEM 6Yuval Ararat
 
AEM Sightly Template Language
AEM Sightly Template LanguageAEM Sightly Template Language
AEM Sightly Template LanguageGabriel Walt
 
Mastering the Sling Rewriter
Mastering the Sling RewriterMastering the Sling Rewriter
Mastering the Sling RewriterJustin Edelson
 
Spring Framework - Core
Spring Framework - CoreSpring Framework - Core
Spring Framework - CoreDzmitry Naskou
 
AEM & Single Page Applications (SPAs) 101
AEM & Single Page Applications (SPAs) 101AEM & Single Page Applications (SPAs) 101
AEM & Single Page Applications (SPAs) 101Adobe
 
Spring - Part 1 - IoC, Di and Beans
Spring - Part 1 - IoC, Di and Beans Spring - Part 1 - IoC, Di and Beans
Spring - Part 1 - IoC, Di and Beans Hitesh-Java
 
Rest and Sling Resolution
Rest and Sling ResolutionRest and Sling Resolution
Rest and Sling ResolutionDEEPAK KHETAWAT
 
Reactjs workshop (1)
Reactjs workshop (1)Reactjs workshop (1)
Reactjs workshop (1)Ahmed rebai
 
SPA Editor - Adobe Experience Manager Sites
SPA Editor - Adobe Experience Manager SitesSPA Editor - Adobe Experience Manager Sites
SPA Editor - Adobe Experience Manager SitesGabriel Walt
 
Spring Framework - AOP
Spring Framework - AOPSpring Framework - AOP
Spring Framework - AOPDzmitry Naskou
 

What's hot (20)

AEM Sightly Deep Dive
AEM Sightly Deep DiveAEM Sightly Deep Dive
AEM Sightly Deep Dive
 
Adobe Experience Manager Core Components
Adobe Experience Manager Core ComponentsAdobe Experience Manager Core Components
Adobe Experience Manager Core Components
 
Introduction to Sightly
Introduction to SightlyIntroduction to Sightly
Introduction to Sightly
 
Modernizing Adobe Experience Manager (AEM)
Modernizing Adobe Experience Manager (AEM)Modernizing Adobe Experience Manager (AEM)
Modernizing Adobe Experience Manager (AEM)
 
Integration patterns in AEM 6
Integration patterns in AEM 6Integration patterns in AEM 6
Integration patterns in AEM 6
 
Sightly - Part 2
Sightly - Part 2Sightly - Part 2
Sightly - Part 2
 
AEM Sightly Template Language
AEM Sightly Template LanguageAEM Sightly Template Language
AEM Sightly Template Language
 
Spring Boot
Spring BootSpring Boot
Spring Boot
 
Mastering the Sling Rewriter
Mastering the Sling RewriterMastering the Sling Rewriter
Mastering the Sling Rewriter
 
Spring User Guide
Spring User GuideSpring User Guide
Spring User Guide
 
Spring Framework - Core
Spring Framework - CoreSpring Framework - Core
Spring Framework - Core
 
AEM & Single Page Applications (SPAs) 101
AEM & Single Page Applications (SPAs) 101AEM & Single Page Applications (SPAs) 101
AEM & Single Page Applications (SPAs) 101
 
Spring - Part 1 - IoC, Di and Beans
Spring - Part 1 - IoC, Di and Beans Spring - Part 1 - IoC, Di and Beans
Spring - Part 1 - IoC, Di and Beans
 
Rest and Sling Resolution
Rest and Sling ResolutionRest and Sling Resolution
Rest and Sling Resolution
 
Reactjs workshop (1)
Reactjs workshop (1)Reactjs workshop (1)
Reactjs workshop (1)
 
Spring Core
Spring CoreSpring Core
Spring Core
 
Maven Introduction
Maven IntroductionMaven Introduction
Maven Introduction
 
SPA Editor - Adobe Experience Manager Sites
SPA Editor - Adobe Experience Manager SitesSPA Editor - Adobe Experience Manager Sites
SPA Editor - Adobe Experience Manager Sites
 
Spring Framework - AOP
Spring Framework - AOPSpring Framework - AOP
Spring Framework - AOP
 
Spring aop
Spring aopSpring aop
Spring aop
 

Viewers also liked

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

Viewers also liked (20)

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

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

Recently uploaded

Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odishasmiwainfosol
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprisepreethippts
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalLionel Briand
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作qr0udbr0
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Velvetech LLC
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Hr365.us smith
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfMarharyta Nedzelska
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfFerryKemperman
 
Powering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsPowering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsSafe Software
 
VK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web DevelopmentVK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web Developmentvyaparkranti
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Matt Ray
 
cpct NetworkING BASICS AND NETWORK TOOL.ppt
cpct NetworkING BASICS AND NETWORK TOOL.pptcpct NetworkING BASICS AND NETWORK TOOL.ppt
cpct NetworkING BASICS AND NETWORK TOOL.pptrcbcrtm
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfDrew Moseley
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...OnePlan Solutions
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmSujith Sukumaran
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtimeandrehoraa
 

Recently uploaded (20)

Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprise
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive Goal
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdf
 
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdf
 
Powering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsPowering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data Streams
 
VK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web DevelopmentVK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web Development
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
 
cpct NetworkING BASICS AND NETWORK TOOL.ppt
cpct NetworkING BASICS AND NETWORK TOOL.pptcpct NetworkING BASICS AND NETWORK TOOL.ppt
cpct NetworkING BASICS AND NETWORK TOOL.ppt
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdf
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalm
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtime
 

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.

Editor's Notes

  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