[CONF] Apache Sling YAMF - Yet Another Model Factory

2014-01-22 Thread Justin Edelson (Confluence)














  


Justin Edelson edited the page:
 


YAMF - Yet Another Model Factory   






 



 Table of Contents








 Info




  This functionality has been renamed Sling Models and is documented at http://sling.staging.apache.org/documentation/bundles/models.html  



  
  
Many Sling projects want to be able to create model objects - POJOs which are automatically mapped from Sling objects, typically resources, but also request objects. Sometimes these POJOs need OSGi services as well.
...






 View Online   Like   View Changes   Add Comment  
 Stop watching space   Manage Notifications  


 


 


  This message was sent by Atlassian Confluence 5.0.3, Team Collaboration Software  






[CONF] Apache Sling YAMF - Yet Another Model Factory

2014-01-07 Thread Justin Edelson (Confluence)














  


Justin Edelson edited the page:
 


YAMF - Yet Another Model Factory   




 Comment: changed @Projection to @Via 


...
   @PostConstruct methods in a super class will be invoked first. Using projection, you can inject If the injection should be based on a child object JavaBean property of the adaptable., you can indicate this using the @Via annotation:   



 Code Block








language
java


 




 @Model(adaptables=SlingHttpServletRequest.class)
public interface MyModel {

// will return request.getResource().adaptTo(ValueMap.class).get(propertyName, String.class)
 @Inject @Projection@Via(resource)
  String getPropertyName();
} 



...

 @Model - declares a model class or interface  
 @Inject - marks a field or method as injectable 
 @Named - declare a name for the injection (otherwise, defaults based on field or method name).  
 @Optional - marks a field or method injection as optional  
 @Source - explictly tie an injected field or method to a particular injector (by name). Can also be on other annotations.  
 @Filter - an OSGi service filter  
 @PostConstruct - methods to call upon model option creation (only for model classes)  
 @Projection @Via - project use a JavaBean property of the adaptable as the adaptable.source of the injection   
 @Default - set default values for a field or method 

...





[CONF] Apache Sling YAMF - Yet Another Model Factory

2014-01-07 Thread Justin Edelson (Confluence)














  


Justin Edelson edited the page:
 


YAMF - Yet Another Model Factory   




 Comment: add @Source example 


...



 Code Block








language
java


 




 @Model(adaptables=SlingHttpServletRequest.class)
public interface MyModel {

// will return request.getResource().adaptTo(ValueMap.class).get(propertyName, String.class)
 @Inject @Via(resource)
  String getPropertyName();
} 



 If there is ambiguity where a given injection could be handled by more than one injector, the@Source annotation can be used to define which injector is responsible: 




 Code Block








language
java


 




 
@Model(adaptables=SlingHttpServletRequest.class)
public interface MyModel {

// Ensure that resource is retrived from the bindings, not a request attribute
  

[CONF] Apache Sling YAMF - Yet Another Model Factory

2014-01-07 Thread Justin Edelson (Confluence)














  


Justin Edelson edited the page:
 


YAMF - Yet Another Model Factory   




 Comment: convert injectors to table 


...

 @Model - declares a model class or interface  
 @Inject - marks a field or method as injectable 
 @Named - declare a name for the injection (otherwise, defaults based on field or method name).  
 @Optional - marks a field or method injection as optional  
 @Source - explictly tie an injected field or method to a particular injector (by name). Can also be on other annotations.  
 @Filter - an OSGi service filter  
 @PostConstruct - methods to call upon model option creation (only for model classes)  
 @Via - use a JavaBean property of the adaptable as the source of the injection  
 @Default - set default values for a field or method 

 Available Injectors 




Title
Name (for @Source)
Description
Applicable To (including using @Via)
Notes


Value Map




...





valuemap
Gets a property from a Value Map
Any object which is or can be adapted to a ValueMap




...






 

[CONF] Apache Sling YAMF - Yet Another Model Factory

2014-01-06 Thread Justin Edelson (Confluence)














  


Justin Edelson added a comment to the page:
 


YAMF - Yet Another Model Factory   





HiVasily Lazerko, these questions are better posted to the sling-dev or sling-users mailing lists.
I'm not sure what you mean by TLD is not ready. There's no TLD for YAMF and there shouldn't need to be one. This would violate one of the design goals (Client doesn't know/care that YAMF is involved) 
Your description is a bit vague and references a class which doesn't exist. Can you explain in a bit more detail? Is anything logged?





 View Online   Like  
 Stop watching space   Manage Notifications  


 


 


  This message was sent by Atlassian Confluence 5.0.3, Team Collaboration Software  






[CONF] Apache Sling YAMF - Yet Another Model Factory

2013-12-23 Thread Justin Edelson (Confluence)














  


Justin Edelson edited the page:
 


YAMF - Yet Another Model Factory   






 



 Table of Contents




  
Many Sling projects want to be able to create model objects - POJOs which are automatically mapped from Sling objects, typically resources, but also request objects. Sometimes these POJOs need OSGi services as well.
...



 Code Block








language
java


 




 @Model(adaptables=SlingHttpServletRequest.class)
public interface MyModel {

 @Inject @Projection(resource)
  String getPropertyName();
} 



 Annotation Reference 

 @Model - declares a model class or interface   
 @Inject - marks a field or method as injectable 
 @Named - declare a name for the injection (otherwise, defaults based on field or method name).   
 @Optional - marks a field or method injection as optional   
 @Source - explictly tie an injected field or method to a particular injector (by name). Can also be on other annotations.   
 @Filter - an OSGi service filter   
 @PostConstruct - methods to call upon model option creation (only for model classes)   
 @Projection - project a JavaBean property as the adaptable.   
 

[CONF] Apache Sling YAMF - Yet Another Model Factory

2013-12-23 Thread Justin Edelson (Confluence)














  


Justin Edelson edited the page:
 


YAMF - Yet Another Model Factory   






...

 Value Map (valuemap) - adapt the adaptable to a ValueMap and retrive properties. 
 OSGI Services (osgi-services) - lookup services based on class name (including List and Array support). Effectively ignores name. 
 Script Bindings (script-bindings) - gets script bindings from request and looks up by name. 
 Child Resources (child-resources) - gets child resources by name. 
 Request Attributes (request-atttributes) - gets request attributes 
  







 View Online   Like   View Changes   Add Comment  
 Stop watching space   Manage Notifications  


 


 


  This message was sent by Atlassian Confluence 5.0.3, Team Collaboration Software  






[CONF] Apache Sling YAMF - Yet Another Model Factory

2013-12-22 Thread Vasily Lazerko (Confluence)














  


Vasily Lazerko added a comment to the page:
 


YAMF - Yet Another Model Factory   





I tried to use YAMF in my JSP as c:set var=myModel value=%=resource.adaptTo('org.apache.sling.yamf.it.models.MyModel')%/ while TLD is not ready. 
It worked almost fine except returning null when I addedorg.apache.sling.api.resource.Resource or any other required non-property field.
 c:set var=myModel value=%=slingRequest.adaptTo('org.apache.sling.yamf.it.models.MyModel')%/injects Resource but not simple String properties. 
What can you suggest? I can contribute with some coding if you gave me your solution.





 View Online   Like   Reply To This  
 Stop watching space   Manage Notifications  


 


 


  This message was sent by Atlassian Confluence 5.0.3, Team Collaboration Software  






[CONF] Apache Sling YAMF - Yet Another Model Factory

2013-12-22 Thread Vasily Lazerko (Confluence)














  


Vasily Lazerko edited a comment on the page:
 


YAMF - Yet Another Model Factory   






I tried to use YAMF in my JSP as c:set var=myModel value=%=resource.adaptTo('org.apache.sling.yamf.it.models.MyModel')%/ while TLD is not ready. 
...
 c:set var=myModel value=%=slingRequest.adaptTo('org.apache.sling.yamf.it.models.MyModel')%/ is vice-versa:injects Resource but not simple String properties. 
...






 View Online   Like   Reply To This  
 Stop watching space   Manage Notifications  


 


 


  This message was sent by Atlassian Confluence 5.0.3, Team Collaboration Software  






[CONF] Apache Sling YAMF - Yet Another Model Factory

2013-12-20 Thread Justin Edelson (Confluence)














  


Justin Edelson edited the page:
 


YAMF - Yet Another Model Factory   






...



 Code Block








language
java


 




 @Model(adaptables=SlingHttpServletRequest.class)
public class MyModel {

 @Inject
 private PrintWriter out;

 @Inject
 @Named(log)
 private Logger logger;

 @Inject
 @Filter((paths=/bin/something))
 private ListServlet servlets;
} 



...






 View Online   Like   View Changes   Add Comment  
 Stop watching space   Manage Notifications  


 


 


  This message was sent by Atlassian Confluence 5.0.3, Team Collaboration Software  






[CONF] Apache Sling YAMF - Yet Another Model Factory

2013-12-19 Thread Justin Edelson (Confluence)














  


Justin Edelson edited the page:
 


YAMF - Yet Another Model Factory   






...



 Code Block








language
java


 




 @Model(adaptableadaptables=Resource.class)
public class MyModel {

 @Inject
 private String propertyName;
} 



...



 Code Block








language
java


 




 @Model(adaptableadaptables=Resource.class)
public interface MyModel {

 @Inject
  String getPropertyName();
} 



...



 

[CONF] Apache Sling YAMF - Yet Another Model Factory

2013-12-19 Thread Justin Edelson (Confluence)














  


Justin Edelson created a page:
 


YAMF - Yet Another Model Factory   





Many Sling projects want to be able to create model objects - POJOs which are automatically mapped from Sling objects, typically resources, but also request objects. Sometimes these POJOs need OSGi services as well.
YAMF is an attempt to consolidate the various approaches I have seen to this problem.

 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 YAMF is involved 
 Support both classes and interfaces. 
 Work with existing Sling infrastructure (i.e. not require changes to other bundles). 

In the simplest case, the class is annotated with @Model and the adaptable class. Fields which need to be injected are annotated with @Inject: 


@Model(adaptable=Resource.class)
public class MyModel {

 @Inject
 private String propertyName;
}


 In this case, a property named 'propertyName' will be looked up from the Resource (after first adapting it to a ValueMap) and it is injected.For an interface, it is similar: 


@Model(adaptable=Resource.class)
public interface MyModel {

 @Inject
  String getPropertyName();
}


 In order for these classes to be picked up, there is a header which must be added to the bundle's manifest: 


Sling-YAMF-Packages
  org.apache.sling.yamf.it.models
/Sling-YAMF-Packages


   If the field or method name doesn't exactly match the property name, @Named can be used: 


@Model(adaptable=Resource.class)
public class MyModel {

 @Inject @Named(secondPropertyName)
 private String otherName;
}



   @Injected fields/methods are assumed to be required. To mark them as optional, use @Optional: 


  

[CONF] Apache Sling YAMF - Yet Another Model Factory

2013-12-19 Thread Justin Edelson (Confluence)














  


Justin Edelson edited the page:
 


YAMF - Yet Another Model Factory   






...
YAMF is an attempt to consolidate the various approaches I have seen to this problem.
  
 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 YAMF is involved 
 Support both classes and interfaces. 
 Work with existing Sling infrastructure (i.e. not require changes to other bundles). 

 Basic Usage 
In the simplest case, the class is annotated with @Model and the adaptable class. Fields which need to be injected are annotated with @Inject:  



 Code Block








language
java


 




 @Model(adaptables=Resource.class)
public class MyModel {

 @Inject
 private String propertyName;
} 



In this case, a property named 'propertyName' will be looked up from the Resource (after first adapting it to a ValueMap) and it is injected.For an interface, it is similar:  



  

[CONF] Apache Sling YAMF - Yet Another Model Factory

2013-12-19 Thread Justin Edelson (Confluence)














  


Justin Edelson edited the page:
 


YAMF - Yet Another Model Factory   






...
 Client code doesn't need to be aware that YAMF is being used. It just uses the Sling Adapter framework: 
  



 Code Block




 
MyModel model = resource.adaptTo(MyModel.class)

OR
sling:adaptTo adaptable=${resource} adaptTo=org.apache.sling.yamf.it.models.MyModel var=model/

OR
${sling:adaptTo(resource, 'org.apache.sling.yamf.it.models.MyModel')}
 



  

 Other Options 
 If the field or method name doesn't exactly match the property name, @Named can be used:   
...






 View Online   Like   View Changes   Add Comment  
 Stop watching space   Manage Notifications  


 


 


  This message was sent by Atlassian Confluence 5.0.3, Team Collaboration Software  






[CONF] Apache Sling YAMF - Yet Another Model Factory

2013-12-19 Thread Justin Edelson (Confluence)














  


Justin Edelson edited the page:
 


YAMF - Yet Another Model Factory   






...



 Code Block




 MyModel model = resource.adaptTo(MyModel.class)

OR

 



 Or 



 Code Block








language
xml


 




 sling:adaptTo adaptable=${resource} adaptTo=org.apache.sling.yamf.it.models.MyModel var=model/

OR

 



 Or 



 Code Block








language
xml