Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/wttech/slice
Slice - a framework which simplifies Sling/AEM development by using dependency injection pattern and mapping Sling resources into Java objects
https://github.com/wttech/slice
aem apache-sling dependency-injection google-guice guice htl java sightly sling
Last synced: about 1 month ago
JSON representation
Slice - a framework which simplifies Sling/AEM development by using dependency injection pattern and mapping Sling resources into Java objects
- Host: GitHub
- URL: https://github.com/wttech/slice
- Owner: wttech
- License: apache-2.0
- Created: 2012-09-21T11:52:26.000Z (over 12 years ago)
- Default Branch: master
- Last Pushed: 2021-06-14T10:15:51.000Z (over 3 years ago)
- Last Synced: 2024-11-17T13:06:07.750Z (about 2 months ago)
- Topics: aem, apache-sling, dependency-injection, google-guice, guice, htl, java, sightly, sling
- Language: Java
- Homepage:
- Size: 11 MB
- Stars: 64
- Watchers: 57
- Forks: 26
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
![Wunderman Thompson Technology logo](./assets/wtt-logo.png)
[![Build Status](https://travis-ci.org/Cognifide/Slice.svg?branch=master)](https://travis-ci.org/Cognifide/Slice)
[![Coverity Status](https://scan.coverity.com/projects/4863/badge.svg)](https://scan.coverity.com/projects/4863)
[![Coverage Status](https://coveralls.io/repos/Cognifide/Slice/badge.svg)](https://coveralls.io/r/Cognifide/Slice)
[![Latest release](https://maven-badges.herokuapp.com/maven-central/com.cognifide.slice/slice-assembly/badge.svg)](http://mvnrepository.com/artifact/com.cognifide.slice/slice-assembly)# Slice
## Purpose
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!## Features
### Separation of concerns
No more business logic in your view (JSP, [HTL](https://docs.adobe.com/docs/en/htl/overview.html) 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:
```html
${model.text}
```**JSPs made clean and tidy** - no more ugly scriptlets.
```jsp${model.text}
```**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!
```java
@SliceResource
public class TextModel {
@JcrProperty
private String text;
public String getText() {
return text;
}
}
```Interested in details? Read about [Slice concepts](https://cognifide.atlassian.net/wiki/display/SLICE/Slice+concepts+-+4.4) and [how it works internally](https://cognifide.atlassian.net/wiki/display/SLICE/How+exactly+does+it+work+-+4.4) on our Wiki.
### Mapping resources to Java objects
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:```java
@SliceResource
public class ComplexTextModel {@JcrProperty
private String text;
@JcrProperty
private String[] styles;@JcrProperty
private AlignmentEnum alignment;@Children(LinkModel.class)
@JcrProperty
private List links;
@JcrProperty
private ImageModel image;//... do whatever business logic you want in your model
}public enum AlignmentEnum {
LEFT, RIGHT, CENTER;
}@SliceResource(MappingStrategy.ALL)
public class LinkModel {
private String path;
private String label;
//...
}@SliceResource(MappingStrategy.ALL)
public class ImageModel {
private String imagePath;
private String altText;
//...
}
```Read more about mapping on our [Wiki](https://cognifide.atlassian.net/wiki/display/SLICE/Mapper+-+4.4).
### Dependency Injection with Google Guice
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](https://www.youtube.com/watch?v=hBVJbzAagfs), and here to check [motivation of Google Guice creators](https://code.google.com/p/google-guice/wiki/)
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.
```html
- ${item}
``````java
@SliceResource
public class TwitterModel {@JcrProperty
private int limit;private final TwitterHandler twitterHandler;
@Inject
public TwitterModel(TwitterHandler twitterHandler) {
this.twitterHandler = twitterHandler;
}//returns list of tweets limited by number configurable by authors
public List getTweets() {
List tweets = twitterHandler.readTweets();
return tweets.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.
```java
public class TwitterHandler {@Inject
private Twitter twitterClient; //Twitter4j clientpublic List readTweets() {
List tweets = new ArrayList();
List statuses = twitterClient.getHomeTimeline();
for (Status status : statuses) {
tweets.add(status.getText());
}
return tweets;
}
}
```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 called [provider](https://code.google.com/p/google-guice/wiki/ProvidesMethods). 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!.
```java
public class MyModule extends AbstractModule {private static final String TWITTER_CONFIG_PATH = "/etc/twitter/configuration/jcr:content/twitterConfig";
@Provides
@ContextScoped
public Twitter getTwitter(ModelProvider modelProvider) {
TwitterConfiguration config = modelProvider.get(TwitterConfiguration.class,
TWITTER_CONFIG_PATH);
ConfigurationBuilder builder = new ConfigurationBuilder(); // from Twitter4j
builder.setOAuthConsumerKey(config.getOAuthKey())
.setOAuthConsumerSecret(config.getOAuthSecret());
TwitterFactory factory = new TwitterFactory(builder.build());
return factory.getInstance();
}//...
}
```## Prerequisites
* AEM / Apache Sling 2
* Maven 2.x, 3.x## Installation
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.git
cd SliceCompile and install:
mvn install
## Usage
Add dependencies to your POM file:
```xml
(...)com.cognifide.slice
slice-core-api
4.4.0com.cognifide.slice
slice-core
4.4.0com.cognifide.slice
slice-mapper
4.4.0com.cognifide.slice
slice-mapper-api
4.4.0(...)
```The last thing you need to do is to prepare an `Injector` of your application in its `BundleActivator`. Read more on how to do this on our [Wiki](https://cognifide.atlassian.net/wiki/display/SLICE/Setting+up+-+4.4)
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/# Commercial Support
Technical support can be made available if needed. Please [contact us](mailto:[email protected]) 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.# More documentation
------------------
* [Full documentation of Slice 4.5](https://cognifide.atlassian.net/wiki/display/SLICE/Slice+4.5)
* [Slice Wiki](https://cognifide.atlassian.net/wiki/display/SLICE)
* [Slice users mailing group](http://slice-users.2340343.n4.nabble.com/) if you have any question on how to use it
* [Slice issue tracking](https://cognifide.atlassian.net/browse/SLICE)