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

Más contenido relacionado

La actualidad más candente

SPA Editor - Adobe Experience Manager Sites
SPA Editor - Adobe Experience Manager SitesSPA Editor - Adobe Experience Manager Sites
SPA Editor - Adobe Experience Manager SitesGabriel 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 DiveHanish Bansal
 
HTL(Sightly) - All you need to know
HTL(Sightly) - All you need to knowHTL(Sightly) - All you need to know
HTL(Sightly) - All you need to knowPrabhdeep Singh
 
Dynamic Components using Single-Page-Application Concepts in AEM/CQ
Dynamic Components using Single-Page-Application Concepts in AEM/CQDynamic Components using Single-Page-Application Concepts in AEM/CQ
Dynamic Components using Single-Page-Application Concepts in AEM/CQNetcetera
 
AEM Best Practices for Component Development
AEM Best Practices for Component DevelopmentAEM Best Practices for Component Development
AEM Best Practices for Component DevelopmentGabriel Walt
 
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
 
Adobe AEM - From Eventing to Job Processing
Adobe AEM - From Eventing to Job ProcessingAdobe AEM - From Eventing to Job Processing
Adobe AEM - From Eventing to Job ProcessingCarsten Ziegeler
 
AEM Sightly Deep Dive
AEM Sightly Deep DiveAEM Sightly Deep Dive
AEM Sightly Deep DiveGabriel Walt
 
AEM (CQ) Dispatcher Security and CDN+Browser Caching
AEM (CQ) Dispatcher Security and CDN+Browser CachingAEM (CQ) Dispatcher Security and CDN+Browser Caching
AEM (CQ) Dispatcher Security and CDN+Browser CachingAndrew Khoury
 
Integration patterns in AEM 6
Integration patterns in AEM 6Integration patterns in AEM 6
Integration patterns in AEM 6Yuval Ararat
 
Adobe Experience Manager Core Components
Adobe Experience Manager Core ComponentsAdobe Experience Manager Core Components
Adobe Experience Manager Core ComponentsGabriel Walt
 
Basics of Solr and Solr Integration with AEM6
Basics of Solr and Solr Integration with AEM6Basics of Solr and Solr Integration with AEM6
Basics of Solr and Solr Integration with AEM6DEEPAK KHETAWAT
 
Architecting an Highly Available and Scalable WordPress Site in AWS
Architecting an Highly Available and Scalable WordPress Site in AWS Architecting an Highly Available and Scalable WordPress Site in AWS
Architecting an Highly Available and Scalable WordPress Site in AWS Harish Ganesan
 
Rest and Sling Resolution
Rest and Sling ResolutionRest and Sling Resolution
Rest and Sling ResolutionDEEPAK KHETAWAT
 
ES6 presentation
ES6 presentationES6 presentation
ES6 presentationritika1
 
AEM GEMs Session Oak Lucene Indexes
AEM GEMs Session Oak Lucene IndexesAEM GEMs Session Oak Lucene Indexes
AEM GEMs Session Oak Lucene IndexesAdobeMarketingCloud
 
게임서비스를 위한 ElastiCache 활용 전략 :: 구승모 솔루션즈 아키텍트 :: Gaming on AWS 2016
게임서비스를 위한 ElastiCache 활용 전략 :: 구승모 솔루션즈 아키텍트 :: Gaming on AWS 2016게임서비스를 위한 ElastiCache 활용 전략 :: 구승모 솔루션즈 아키텍트 :: Gaming on AWS 2016
게임서비스를 위한 ElastiCache 활용 전략 :: 구승모 솔루션즈 아키텍트 :: Gaming on AWS 2016Amazon Web Services Korea
 

La actualidad más candente (20)

SPA Editor - Adobe Experience Manager Sites
SPA Editor - Adobe Experience Manager SitesSPA Editor - Adobe Experience Manager Sites
SPA Editor - Adobe Experience Manager Sites
 
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
 
HTL(Sightly) - All you need to know
HTL(Sightly) - All you need to knowHTL(Sightly) - All you need to know
HTL(Sightly) - All you need to know
 
Dynamic Components using Single-Page-Application Concepts in AEM/CQ
Dynamic Components using Single-Page-Application Concepts in AEM/CQDynamic Components using Single-Page-Application Concepts in AEM/CQ
Dynamic Components using Single-Page-Application Concepts in AEM/CQ
 
AEM Best Practices for Component Development
AEM Best Practices for Component DevelopmentAEM Best Practices for Component Development
AEM Best Practices for Component Development
 
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
 
Adobe AEM - From Eventing to Job Processing
Adobe AEM - From Eventing to Job ProcessingAdobe AEM - From Eventing to Job Processing
Adobe AEM - From Eventing to Job Processing
 
AEM Sightly Deep Dive
AEM Sightly Deep DiveAEM Sightly Deep Dive
AEM Sightly Deep Dive
 
AEM (CQ) Dispatcher Security and CDN+Browser Caching
AEM (CQ) Dispatcher Security and CDN+Browser CachingAEM (CQ) Dispatcher Security and CDN+Browser Caching
AEM (CQ) Dispatcher Security and CDN+Browser Caching
 
Integration patterns in AEM 6
Integration patterns in AEM 6Integration patterns in AEM 6
Integration patterns in AEM 6
 
Spring boot
Spring bootSpring boot
Spring boot
 
Angular Directives
Angular DirectivesAngular Directives
Angular Directives
 
Adobe Experience Manager Core Components
Adobe Experience Manager Core ComponentsAdobe Experience Manager Core Components
Adobe Experience Manager Core Components
 
Basics of Solr and Solr Integration with AEM6
Basics of Solr and Solr Integration with AEM6Basics of Solr and Solr Integration with AEM6
Basics of Solr and Solr Integration with AEM6
 
Introduction to Spring Boot
Introduction to Spring BootIntroduction to Spring Boot
Introduction to Spring Boot
 
Architecting an Highly Available and Scalable WordPress Site in AWS
Architecting an Highly Available and Scalable WordPress Site in AWS Architecting an Highly Available and Scalable WordPress Site in AWS
Architecting an Highly Available and Scalable WordPress Site in AWS
 
Rest and Sling Resolution
Rest and Sling ResolutionRest and Sling Resolution
Rest and Sling Resolution
 
ES6 presentation
ES6 presentationES6 presentation
ES6 presentation
 
AEM GEMs Session Oak Lucene Indexes
AEM GEMs Session Oak Lucene IndexesAEM GEMs Session Oak Lucene Indexes
AEM GEMs Session Oak Lucene Indexes
 
게임서비스를 위한 ElastiCache 활용 전략 :: 구승모 솔루션즈 아키텍트 :: Gaming on AWS 2016
게임서비스를 위한 ElastiCache 활용 전략 :: 구승모 솔루션즈 아키텍트 :: Gaming on AWS 2016게임서비스를 위한 ElastiCache 활용 전략 :: 구승모 솔루션즈 아키텍트 :: Gaming on AWS 2016
게임서비스를 위한 ElastiCache 활용 전략 :: 구승모 솔루션즈 아키텍트 :: Gaming on AWS 2016
 

Destacado

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

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

Similar a Sling Models Overview

Design Patterns - Part 2 of 2
Design Patterns - Part 2 of 2Design Patterns - Part 2 of 2
Design Patterns - Part 2 of 2Savio Sebastian
 
AEM and Sling
AEM and SlingAEM and Sling
AEM and SlingLokesh BS
 
Deepak khetawat sling_models_sightly_jsp
Deepak khetawat sling_models_sightly_jspDeepak khetawat sling_models_sightly_jsp
Deepak khetawat sling_models_sightly_jspDEEPAK KHETAWAT
 
Design Patterns - Part 1 of 2
Design Patterns - Part 1 of 2Design Patterns - Part 1 of 2
Design Patterns - Part 1 of 2Savio Sebastian
 
Efficient content structures and queries in CRX/CQ
Efficient content structures and queries in CRX/CQEfficient content structures and queries in CRX/CQ
Efficient content structures and queries in CRX/CQconnectwebex
 
CloudStack Meetup Santa Clara
CloudStack Meetup Santa Clara CloudStack Meetup Santa Clara
CloudStack Meetup Santa Clara NetApp
 
Intro to iOS Development • Made by Many
Intro to iOS Development • Made by ManyIntro to iOS Development • Made by Many
Intro to iOS Development • Made by Manykenatmxm
 
Mastering the Sling Rewriter by Justin Edelson
Mastering the Sling Rewriter by Justin EdelsonMastering the Sling Rewriter by Justin Edelson
Mastering the Sling Rewriter by Justin EdelsonAEM HUB
 
Mastering the Sling Rewriter
Mastering the Sling RewriterMastering the Sling Rewriter
Mastering the Sling RewriterJustin 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 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
 

Similar a Sling Models Overview (20)

Sightly_techInsight
Sightly_techInsightSightly_techInsight
Sightly_techInsight
 
Design Patterns - Part 2 of 2
Design Patterns - Part 2 of 2Design Patterns - Part 2 of 2
Design Patterns - Part 2 of 2
 
AEM and Sling
AEM and SlingAEM and Sling
AEM and Sling
 
Deepak khetawat sling_models_sightly_jsp
Deepak khetawat sling_models_sightly_jspDeepak khetawat sling_models_sightly_jsp
Deepak khetawat sling_models_sightly_jsp
 
Design Patterns - Part 1 of 2
Design Patterns - Part 1 of 2Design Patterns - Part 1 of 2
Design Patterns - Part 1 of 2
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 
Efficient content structures and queries in CRX/CQ
Efficient content structures and queries in CRX/CQEfficient content structures and queries in CRX/CQ
Efficient content structures and queries in CRX/CQ
 
Encapsulation
EncapsulationEncapsulation
Encapsulation
 
CloudStack Meetup Santa Clara
CloudStack Meetup Santa Clara CloudStack Meetup Santa Clara
CloudStack Meetup Santa Clara
 
Intro to iOS Development • Made by Many
Intro to iOS Development • Made by ManyIntro to iOS Development • Made by Many
Intro to iOS Development • Made by Many
 
Mastering the Sling Rewriter by Justin Edelson
Mastering the Sling Rewriter by Justin EdelsonMastering the Sling Rewriter by Justin Edelson
Mastering the Sling Rewriter by Justin Edelson
 
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
 

Último

Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceanilsa9823
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 

Último (20)

Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 

Sling Models Overview

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

Notas del editor

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