SlideShare a Scribd company logo
© 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

Introduction to Sightly and Sling Models
Introduction to Sightly and Sling ModelsIntroduction to Sightly and Sling Models
Introduction to Sightly and Sling Models
Stefano Celentano
 
AEM Sightly Deep Dive
AEM Sightly Deep DiveAEM Sightly Deep Dive
AEM Sightly Deep Dive
Gabriel Walt
 
AEM Best Practices for Component Development
AEM Best Practices for Component DevelopmentAEM Best Practices for Component Development
AEM Best Practices for Component Development
Gabriel Walt
 
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
Hanish Bansal
 
Integration patterns in AEM 6
Integration patterns in AEM 6Integration patterns in AEM 6
Integration patterns in AEM 6
Yuval Ararat
 
Aem dispatcher – tips & tricks
Aem dispatcher – tips & tricksAem dispatcher – tips & tricks
Aem dispatcher – tips & tricks
Ashokkumar T A
 
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
 
Heap Dump Analysis - AEM: Real World Issues
Heap Dump Analysis - AEM: Real World IssuesHeap Dump Analysis - AEM: Real World Issues
Heap Dump Analysis - AEM: Real World Issues
Kanika Gera
 
Spring boot jpa
Spring boot jpaSpring boot jpa
Spring boot jpa
Hamid Ghorbani
 
Rest and Sling Resolution
Rest and Sling ResolutionRest and Sling Resolution
Rest and Sling ResolutionDEEPAK KHETAWAT
 
Apache Sling Generic Validation Framework
Apache Sling Generic Validation FrameworkApache Sling Generic Validation Framework
Apache Sling Generic Validation Framework
Radu Cotescu
 
Spring Boot Tutorial
Spring Boot TutorialSpring Boot Tutorial
Spring Boot Tutorial
Naphachara Rattanawilai
 
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 HUB
 
Java Persistence API (JPA) Step By Step
Java Persistence API (JPA) Step By StepJava Persistence API (JPA) Step By Step
Java Persistence API (JPA) Step By StepGuo Albert
 
Spring Framework - Spring Security
Spring Framework - Spring SecuritySpring Framework - Spring Security
Spring Framework - Spring Security
Dzmitry Naskou
 
Spring Framework
Spring Framework  Spring Framework
Spring Framework
tola99
 
Spring MVC
Spring MVCSpring MVC
Spring MVC
Aaron Schram
 
Ask the expert AEM Assets best practices 092016
Ask the expert  AEM Assets best practices 092016Ask the expert  AEM Assets best practices 092016
Ask the expert AEM Assets best practices 092016
AdobeMarketingCloud
 
Spring MVC
Spring MVCSpring MVC
Spring MVC
Emprovise
 

What's hot (20)

Introduction to Sightly and Sling Models
Introduction to Sightly and Sling ModelsIntroduction to Sightly and Sling Models
Introduction to Sightly and Sling Models
 
AEM Sightly Deep Dive
AEM Sightly Deep DiveAEM Sightly Deep Dive
AEM Sightly Deep Dive
 
AEM Best Practices for Component Development
AEM Best Practices for Component DevelopmentAEM Best Practices for Component Development
AEM Best Practices for Component Development
 
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
 
Integration patterns in AEM 6
Integration patterns in AEM 6Integration patterns in AEM 6
Integration patterns in AEM 6
 
Aem dispatcher – tips & tricks
Aem dispatcher – tips & tricksAem dispatcher – tips & tricks
Aem dispatcher – tips & tricks
 
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 - Client Libraries
AEM - Client LibrariesAEM - Client Libraries
AEM - Client Libraries
 
Heap Dump Analysis - AEM: Real World Issues
Heap Dump Analysis - AEM: Real World IssuesHeap Dump Analysis - AEM: Real World Issues
Heap Dump Analysis - AEM: Real World Issues
 
Spring boot jpa
Spring boot jpaSpring boot jpa
Spring boot jpa
 
Rest and Sling Resolution
Rest and Sling ResolutionRest and Sling Resolution
Rest and Sling Resolution
 
Apache Sling Generic Validation Framework
Apache Sling Generic Validation FrameworkApache Sling Generic Validation Framework
Apache Sling Generic Validation Framework
 
Spring Boot Tutorial
Spring Boot TutorialSpring Boot Tutorial
Spring Boot Tutorial
 
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
 
Java Persistence API (JPA) Step By Step
Java Persistence API (JPA) Step By StepJava Persistence API (JPA) Step By Step
Java Persistence API (JPA) Step By Step
 
Spring Framework - Spring Security
Spring Framework - Spring SecuritySpring Framework - Spring Security
Spring Framework - Spring Security
 
Spring Framework
Spring Framework  Spring Framework
Spring Framework
 
Spring MVC
Spring MVCSpring MVC
Spring MVC
 
Ask the expert AEM Assets best practices 092016
Ask the expert  AEM Assets best practices 092016Ask the expert  AEM Assets best practices 092016
Ask the expert AEM Assets best practices 092016
 
Spring MVC
Spring MVCSpring MVC
Spring MVC
 

Viewers also liked

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
Ashokkumar T A
 
Extra AEM Development Tools
Extra AEM Development ToolsExtra AEM Development Tools
Extra AEM Development Tools
Justin 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 Things
Justin Edelson
 
Declarative Services - Dependency Injection OSGi Style
Declarative Services - Dependency Injection OSGi StyleDeclarative Services - Dependency Injection OSGi Style
Declarative Services - Dependency Injection OSGi Style
Felix Meschberger
 
SCR Annotations for Fun and Profit
SCR Annotations for Fun and ProfitSCR Annotations for Fun and Profit
SCR Annotations for Fun and Profit
Mike Pfaff
 
Reactive applications
Reactive applicationsReactive applications
Reactive applications
Ashokkumar T A
 
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 Sling
Carsten Ziegeler
 
RESTful Web Applications with Apache Sling
RESTful Web Applications with Apache SlingRESTful Web Applications with Apache Sling
RESTful Web Applications with Apache Sling
Bertrand Delacretaz
 
Shooting rabbits with sling
Shooting rabbits with slingShooting rabbits with sling
Shooting rabbits with sling
Tomasz Rękawek
 
Aem maintenance
Aem maintenanceAem maintenance
Aem maintenance
Ashokkumar T A
 
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 MONGODB
Nate Nelson
 
Intro to OSGi
Intro to OSGiIntro to OSGi
Intro to OSGi
Tricode (part of Dept)
 
AEM (CQ) Dispatcher Caching Webinar 2013
AEM (CQ) Dispatcher Caching Webinar 2013AEM (CQ) Dispatcher Caching Webinar 2013
AEM (CQ) Dispatcher Caching Webinar 2013
Andrew 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 Docker
David Currie
 

Viewers also liked (17)

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
 
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 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 2
Savio Sebastian
 
AEM and Sling
AEM and SlingAEM and Sling
AEM and Sling
Lokesh BS
 
Deepak khetawat sling_models_sightly_jsp
Deepak khetawat sling_models_sightly_jspDeepak khetawat sling_models_sightly_jsp
Deepak khetawat sling_models_sightly_jsp
DEEPAK KHETAWAT
 
Design Patterns - Part 1 of 2
Design Patterns - Part 1 of 2Design Patterns - Part 1 of 2
Design Patterns - Part 1 of 2
Savio Sebastian
 
Design Patterns
Design PatternsDesign 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
connectwebex
 
Encapsulation
EncapsulationEncapsulation
Encapsulation
Ducat India
 
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 Many
kenatmxm
 
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
AEM HUB
 
Mastering the Sling Rewriter
Mastering the Sling RewriterMastering the Sling Rewriter
Mastering the Sling Rewriter
Justin Edelson
 
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 CONCEPT
Ajay 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 Bosschaert
mfrancis
 
Android architecture
Android architecture Android architecture
Android architecture
Trong-An Bui
 
SOLID
SOLIDSOLID
Introduction to Spring Boot.pdf
Introduction to Spring Boot.pdfIntroduction to Spring Boot.pdf
Introduction to Spring Boot.pdf
ShaiAlmog1
 
Design patterns
Design patternsDesign patterns
Design patterns
Anas Alpure
 
Bring the fun back to java
Bring the fun back to javaBring the fun back to java
Bring the fun back to java
ciklum_ods
 

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
 
Mastering the Sling Rewriter
Mastering the Sling RewriterMastering the Sling Rewriter
Mastering the Sling Rewriter
 
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
 

Recently uploaded

Corporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMSCorporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMS
Tendenci - The Open Source AMS (Association Management Software)
 
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
Tier1 app
 
Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
Paco van Beckhoven
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
Globus
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Shahin Sheidaei
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Globus
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
Fermin Galan
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
XfilesPro
 
RISE with SAP and Journey to the Intelligent Enterprise
RISE with SAP and Journey to the Intelligent EnterpriseRISE with SAP and Journey to the Intelligent Enterprise
RISE with SAP and Journey to the Intelligent Enterprise
Srikant77
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
Cyanic lab
 
Graphic Design Crash Course for beginners
Graphic Design Crash Course for beginnersGraphic Design Crash Course for beginners
Graphic Design Crash Course for beginners
e20449
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Globus
 
Enterprise Resource Planning System in Telangana
Enterprise Resource Planning System in TelanganaEnterprise Resource Planning System in Telangana
Enterprise Resource Planning System in Telangana
NYGGS Automation Suite
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
Matt Welsh
 
GlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote sessionGlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote session
Globus
 
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
informapgpstrackings
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
Globus
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Globus
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
IES VE
 

Recently uploaded (20)

Corporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMSCorporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMS
 
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
 
Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
 
RISE with SAP and Journey to the Intelligent Enterprise
RISE with SAP and Journey to the Intelligent EnterpriseRISE with SAP and Journey to the Intelligent Enterprise
RISE with SAP and Journey to the Intelligent Enterprise
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
 
Graphic Design Crash Course for beginners
Graphic Design Crash Course for beginnersGraphic Design Crash Course for beginners
Graphic Design Crash Course for beginners
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
 
Enterprise Resource Planning System in Telangana
Enterprise Resource Planning System in TelanganaEnterprise Resource Planning System in Telangana
Enterprise Resource Planning System in Telangana
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
 
GlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote sessionGlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote session
 
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBroker
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
 

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