Your SlideShare is downloading. ×
SCR Annotations for Fun and Profit
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Saving this for later?

Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime - even offline.

Text the download link to your phone

Standard text messaging rates apply

SCR Annotations for Fun and Profit

2,562
views

Published on

Talk held by Mike Pfaff at the .adaptTo() conference 2012 in Berlin

Talk held by Mike Pfaff at the .adaptTo() conference 2012 in Berlin

Published in: Technology

0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,562
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
22
Comments
0
Likes
2
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. SCR Annotations for Fun and ProfitLightningtalks.adaptTo(Berlin, 2012) Mike Pfaff Freelance CQ Consultant mikepfaff83@gmail.com
  • 2. Huh?Short intro on how to use SCR annotations toeasily define servlets and services, usereferences to access services, defineconfigurable components, etc.Show ease of “new style” annotationscompared to “old style” JavaDoc comments
  • 3. How to: Use annotations...<dependencies> ... <dependency> <groupId>org.apache.felix</groupId> <artifactId>org.apache.felix.scr.annotations</artifactId> <version>1.7.0</version> <scope>provided</scope> </dependency> ...</dependencies>...
  • 4. How to: Maven plugin...<build> <plugins> <plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-scr-plugin</artifactId> <version>1.8.0</version> </plugin> ... </plugins></build>...
  • 5. Reference: Old Style.../*** @scr.reference*/private SlingRepository repo;...
  • 6. Reference: New Style...@Referenceprivate SlingRepository repo;...
  • 7. Servlet: Old Style/*** Foo Bar Servlet** @scr.component metatype="false"* @scr.service interface="javax.servlet.Servlet"* @scr.property name="sling.servlet.methods" value="POST"* @scr.property name="sling.servlet.selectors" value="foobar"* @scr.property name="sling.servlet.resourceTypes" value="adaptto2012/components/page"**/public class FooBarServlet extends SlingAllMethodsServlet { ...}
  • 8. Servlet: New Style/*** Foo Bar Servlet**/@SlingServlet( resourceTypes = {"adaptto2012/components/page"}, methods = {"POST"}, selectors = {"foobar"})public class FooBarServlet extends SlingAllMethodsServlet { ...}
  • 9. SlingServlet optionsBased on resourceType (recommended!) resourceTypes - Required selectors - Optional extensions - Optional methods - OptionalBased on path (caution - no access control!) paths - Required
  • 10. Configurable Component: Define@Component( label = "adaptTo() - Sample Configurable Component", description = "This is a sample for a configurable component", metatype = true)public class SampleConfigurableComponent { @Property( label = "Hostname", description = "Hostname of some external service" ) static final String PROP_HOSTNAME = "hostname";}
  • 11. Configurable Component: Configure
  • 12. Configuration: Retrieveprotected void activate(ComponentContext context) { Dictionary configProperties = context.getProperties(); String hostName = (String)configProperties.get(PROP_HOSTNAME);}
  • 13. Other annotationsDefine a service (which you can then @Reference) @ServiceSling filters: @SlingFilter @SlingFilterScopeOverride default method names: @Activate @Deactivate @Modified
  • 14. Some best practicesOnly set metatype to true if you want yourcomponent to be configurable through theApache Felix web console. Defaults are falsefor annotations (so better than JavaDoc tags)Always set nice labels and descriptions toavoid cryptic entries in the Apache Felix webconsole (also applies to non configurablecomponents!)
  • 15. DocumentationSCR Annotationshttp://felix.apache.org/site/scr-annotations.htmlSling SCR Annotationshttp://felix.apache.org/site/extending-scr-annotations.htmlMaven Pluginhttp://felix.apache.org/site/apache-felix-maven-scr-plugin.html
  • 16. Thank you!