- Notifications
You must be signed in to change notification settings - Fork26
Slice - a framework which simplifies Sling/AEM development by using dependency injection pattern and mapping Sling resources into Java objects
License
Apache-2.0 and 2 other licenses found
Licenses found
wttech/Slice
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Slice is a framework which simplifies Sling/Adobe AEM development by using dependency injection pattern (DI). It glues Sling and Google Guice together, allowing developers to create a code with a clean separation of concerns. You can map resources to Java models seamlessly and thanks to DI arrange your application in easily testable and maintainable code.
What can you gain?
- Lean and neat code, slicker design!
- Improved testability of your code - thanks to dependency injection it can be easily unit-tested.
- Improved maintenance of your code - all business logic arranged in clean and simple Java classes (POJOs)
- Easy to start with! Slice is easy to learn and if you use it in all your projects, your developers know exactly how to start.
- Faster development – code reuse, dependency injection and simplicity make you more efficient than ever.
- Overall costs reduced!
No more business logic in your view (JSP,HTL scripts) - business logic's place is in Java classes and Slice knows it!
Slice loves HTL. HTL loves Slice. They go together like strawberries and cream! Seamless integration you will love:
<divdata-sly-use.model="com.example.components.text.TextModel"><p>${model.text}<p></div>
JSPs made clean and tidy - no more ugly scriptlets.
<slice:lookupvar="model"type="<%=com.example.components.text.TextModel%>" /><p>${model.text}</p>
Reusable Java models which expose data for your view - note that the same model can be used by HTL and JSP components - one model to feed them all!
@SliceResourcepublicclassTextModel {@JcrPropertyprivateStringtext;publicStringgetText() {returntext; }}
Interested in details? Read aboutSlice concepts andhow it works internally on our Wiki.
Slice allows you to map a resource to a plain Java object. It's annotation-driven, very easy to use and fully extensible, so you can write your own ways of mapping if a set of available features is not enough for your needs. Slice supports mapping of:
- simple properties (
String
,Long
,Boolean
, etc.) into primitives and objects - simple properties into enums.
- multi-value properties to arrays or lists
- child resources to a Java object or list of objects
- nested resources/classes hierarchy
The following code snippet demonstrates all of the above features in one model. It's simple - just annotate a class with@SliceResource
and its fields with@JcrProperty
to get auto mapping of resource properties to class fields:
@SliceResourcepublicclassComplexTextModel {@JcrPropertyprivateStringtext;@JcrPropertyprivateString[]styles;@JcrPropertyprivateAlignmentEnumalignment;@Children(LinkModel.class)@JcrPropertyprivateList<LinkModel>links;@JcrPropertyprivateImageModelimage;//... do whatever business logic you want in your model}publicenumAlignmentEnum {LEFT,RIGHT,CENTER;}@SliceResource(MappingStrategy.ALL)publicclassLinkModel {privateStringpath;privateStringlabel;//...}@SliceResource(MappingStrategy.ALL)publicclassImageModel {privateStringimagePath;privateStringaltText;//...}
Read more about mapping on ourWiki.
If your AEM components are more than simple text/image/title components (and they certainly are), then you probably need to combine their functionality with some external data or more complex business logic provided by other classes. Dependency injection allows you to do this easily and keep your code testable without boiler-plate code and unneeded arguments in methods used only to propagate a value down into the class hierarchy.
We took Guice as a DI container. Why it's awesome? Take a look here:Google I/O 2009 - Big Modular Java with Guice, and here to checkmotivation of Google Guice creators
To demonstrate an example of a complex component which combines use of Slice features with power of DI, take a look at the following code which is an implementation of a Twitter component.
<divdata-sly-use.model="com.example.components.twitter.TwitterModel"><uldata-sly-list="${model.tweets}"><li>${item}</li></ul></div>
@SliceResourcepublicclassTwitterModel {@JcrPropertyprivateintlimit;privatefinalTwitterHandlertwitterHandler;@InjectpublicTwitterModel(TwitterHandlertwitterHandler) {this.twitterHandler =twitterHandler;}//returns list of tweets limited by number configurable by authorspublicList<String>getTweets() {List<String>tweets =twitterHandler.readTweets();returntweets.subList(0,Math.max(tweets.size(),limit));}//...}
The model of the component is fairly simple and fully testable because you can easily mock the TwitterHandler in your unit test case.
TwitterHandler is also very simple as it uses Twitter client (from Twitter4j library). Please note that this client is injected by Guice and you don't have to care about its configuration in the handler itself.
publicclassTwitterHandler {@InjectprivateTwittertwitterClient;//Twitter4j clientpublicList<String>readTweets() {List<String>tweets =newArrayList<String>();List<Status>statuses =twitterClient.getHomeTimeline();for (Statusstatus :statuses) {tweets.add(status.getText());}returntweets;}}
The configuration is set while instantiating the twitter client by Guice. To instruct Guice how to create the client object we need to create a so calledprovider. You can do this in module configuration. It reads some configuration properties from repository (using ModelProvider). ContextScope instructs Guice to create such an object only once per request or OSGi service call - yes, you can reuse the TwitterHandler in you OSGi services which are request/resource agnostic - that's the power of Slice!.
publicclassMyModuleextendsAbstractModule {privatestaticfinalStringTWITTER_CONFIG_PATH ="/etc/twitter/configuration/jcr:content/twitterConfig";@Provides@ContextScopedpublicTwittergetTwitter(ModelProvidermodelProvider) {TwitterConfigurationconfig =modelProvider.get(TwitterConfiguration.class,TWITTER_CONFIG_PATH);ConfigurationBuilderbuilder =newConfigurationBuilder();// from Twitter4jbuilder.setOAuthConsumerKey(config.getOAuthKey()).setOAuthConsumerSecret(config.getOAuthSecret());TwitterFactoryfactory =newTwitterFactory(builder.build());returnfactory.getInstance();}//...}
- AEM / Apache Sling 2
- Maven 2.x, 3.x
Slice is available from the Maven Central Repo. However if you want to check out the newest development version, do the following:
Checkout the source code:
cd [folder of your choice]git clone git://github.com/wttech/Slice.gitcd Slice
Compile and install:
mvn install
Add dependencies to your POM file:
(...)<dependency><groupId>com.cognifide.slice</groupId><artifactId>slice-core-api</artifactId><version>4.4.0</version></dependency><dependency><groupId>com.cognifide.slice</groupId><artifactId>slice-core</artifactId><version>4.4.0</version></dependency><dependency><groupId>com.cognifide.slice</groupId><artifactId>slice-mapper</artifactId><version>4.4.0</version></dependency><dependency><groupId>com.cognifide.slice</groupId><artifactId>slice-mapper-api</artifactId><version>4.4.0</version></dependency>(...)
The last thing you need to do is to prepare anInjector
of your application in itsBundleActivator
. Read more on how to do this on ourWiki
AEM/CQ related add-ons:
- Slice AEM v6.3, 6.2, 6.1, 6.0 Addon:https://github.com/Cognifide/Slice-AEM60
- Slice CQ v5.6 Addon:https://github.com/Cognifide/Slice-CQ56/
- Slice CQ v5.5 Addon:https://github.com/Cognifide/Slice-CQ55/
Technical support can be made available if needed. Pleasecontact us for more details.
We can:
- prioritize your feature request,
- tailor the product to your needs,
- provide a training for your engineers,
- support your development teams.
- Full documentation of Slice 4.5
- Slice Wiki
- Slice users mailing group if you have any question on how to use it
- Slice issue tracking
About
Slice - a framework which simplifies Sling/AEM development by using dependency injection pattern and mapping Sling resources into Java objects