YourKit Java Profiler |
JFacets /
WritingFacetsWriting and assigning facets to their Here under is an example facet that uses the @FacetKey(name="purchase", profileId="role_customer", targetObjectType=MyProduct.class) public class MyFacet implements IFacet { private IFacetContext context; public IFacetContext getContext() { return context; } public void setContext(IFacetContext context) { this.context = context; } public void purchase() { MyProduct p = (MyProduct)context.getTargetObject(); p.setPurchaseDate(new Date()); ... } } As you can see, it's really no big deal : a POJO, and a one-liner annotation... could hardly be simpler ! Several other options are available for writing and assigning facets. You are not tied to annotations if you don't like them, and even more, you aren't even tied to writing your facets in Java !! JFacets uses pluggable Facet Descriptor Managers (FDM) and Facet Factories (FF), assembled via Spring, which allows you to implement virtually any facet definition mechanism (see the Architecture docs for more infos). A few implementations are bundled with the JFacets core, which you can benefit directly by just hooking them in via Spring. The JFacets distribution contains a few XML application contexts that can be used for inspiration (look into the test resources)... Also, for a more involved example, you can have a look at Woko, which has a pretty cool implementation, where facets are Groovy code stored into the database ! Currently supported...The included FDMs and FFs support the following ways of writing/assigning facets :
Each of the different options have pros and cons, but choice is always a good thing... You can use either only one "mode" or mix them in the same application, and of course, this does not have any impact on the way you use facets. XML facet descriptor / Java ClassesIn this "mode", you write the facets in pure Java (or in Groovy, but then you have to compile your Groovy code into .class files), as POJOs that can implement some of the JFacets interfaces. Then you assign the facets to the Here under is some code showing the definition of our example facet in the XML file : <facets> <facet name="purchase" profile="role_customer" object_type="com.xyz.model.MyProduct" class="com.xyz.facets.MyFacet"/> <!-- some other facets here... --> </facets> As you can see, the facet's name, profileId and targetObjectType are specified as the The path to the XML facets descriptors file is specified via Spring as a property of the Facet Descriptor Manager. Pros And ConsWhat's cool :
What sucks :
Using annotationsThis mode leverages Java annotations and auto discovery. You write facets in Java just like when you use the XML descriptor mode, but this time don't have to maintain an XML file. It's a tendency these days to have those auto discovery features, and it's very handy in some situations. This mode uses another Facet Descriptor Manager which is configured in the Spring context, just like the other modes. You can use the annotations with or without other facet descriptor managers, it's just about geting it configured. Annotated classes are looked up from the CLASSPATH. You have to provide at least one base package in the facet descriptor manager's Spring bean definition (look to the javadocs for more infos). The @FacetKey annotationDefining a facet using @FacetKey(name="purchase", profileId="role_customer", targetObjectType=MyProduct.class) public class MyFacet ... { ... } As you can see, the
And that's all !
Pros And ConsWhat's cool :
What sucks :
Facet discovery / Groovy Classes and ScriptsThis mode provides full support for writing your facets in Groovy as facet Classes or as plain scripts ! Moreover, it also uses auto-discovery in order to find your facets and theis assignations without any external configuration required. Automatic Facet Discovery and AssignationFacets that you write in Groovy have to be available as CLASSPATH resources. At startup, the JFacets Groovy FDM scans the CLASSPATH in order to find some resources with the following naming convention : /
The base package is not important : it's only an utility in order to put your facet scripts where you want to. This can be configured in the Spring context, depending on your application. What's important is :
Here under are a few examples of valid Groovy facet files (once again, they have to be available as CLASSPATH resources), and their translated
As you can see, facet assignation follows the convention over configuration paradigm : the information is specified by the file/folders structure itself, instead of being duplicated in the XML file. You don't have to add anything to the file when using Groovy facets, you simply have to put it at the right place !
Groovy Classes or Executable ScriptsWhen writing Groovy facets, you have two options :
Groovy ClassesDefining the facet class in the Groovy file is strictly equivalent to coding the facet in Java. You simply declare write the class the Java way or the Groovy way, the choice is yours. Here below is our "purchase" example in Groovy, located in file /** * A Groovy facet class */ class MyGroovyFacet implements IFacet { // GroovyBeans generates the contract of IFacet IFacetContext context void purchase() { // we do stuff with the target object // using a superclass method def product = ctx.targetObject product.purchaseDate = new Date() ... } } As you can see, the facet file simply defines the facet implementation class, the one that will get instanciated at run time when facets are looked up. Another feature of Groovy Facets is the ability to to implement facets as plain scripts instead of classes... Plain ScriptsYou can also write your facets as plain scripts, with no class declaration. If you do so, the facet is automatically considered as executable, and thereby script is executed when you execute the facet. The FacetContext is available as a variable, and the script can return a value that becomes the result of the facet's execution. Here under is an example of such a Plain Script Groovy Facet : /* * plain script facet that increments and return an Integer * passed as the facet's target object */ i = context.targetObject; i = new Integer(i.intValue() + 1); return i;
Pros And ConsWhat's cool :
What sucks :
|