YourKit Java Profiler |
JFacets /
HelloJFacetsProfiled Hello World !This very simple example demonstrates how you can write code that's profile-aware easily with facets ! Here, we're gonna change the formatting of a We'll write 2 simple facets (both extending a common base class), and assign them to these profiles and the This example uses the SimpleProfileRepository for the sake of simplicity. Have a quick look to the default profiles hierarchy in there if not yet done... Main classHere is the main class, which retrieves the facets and uses them for users with profiles package net.sourceforge.jfacets.examples.hello; import java.util.Date; import net.sourceforge.jfacets.JFacets; public class Main { public static void main(String[] args) { // create the target object (a dummy date) Date targetObject = new Date(); // get the JFacets bean JFacets jFacets = JFacets.get("net/sourceforge/jfacets/examples/hello/helloFacetsAppCtx.xml"); // get the hello facet for ivar (standard_role)... BaseHelloFacet helloFacet = (BaseHelloFacet)jFacets.getFacet("hello", "ivar", targetObject); System.out.println("Facet obtained for (hello,ivar,Date) : " + helloFacet); // and invoke hello() String res = helloFacet.hello(); System.out.println("helloFacet.hello() = " + res); // get the hello facet for john (admin_role) helloFacet = (BaseHelloFacet)jFacets.getFacet("hello", "john", targetObject); System.out.println("Facet obtained for (hello,john,Date) : " + helloFacet); // and invoke hello() res = helloFacet.hello(); System.out.println("helloFacet.hello() = " + res); } } As you can see, there is 'no conditional statement '(if/then/else or switch) in order to get the code to be executed for a particular profile ! We simply ask for some code for a profile and an object, and use it : JFacets handles its retrievement for us. BaseHelloFacetThis is the base class for our facets. The aim is to factorize facet context stuff, and to define the contract of the hello facets (i.e. they have a package net.sourceforge.jfacets.examples.hello; import net.sourceforge.jfacets.impl.DefaultFacet; /** * Base class for facets that say hello ! */ public abstract class BaseHelloFacet extends DefaultFacet { public abstract String hello(); } This could also be an interface in case you don't have anything to factorize... (hello, root_profile, java.util.Date)This is our first facet, assigned to package net.sourceforge.jfacets.examples.hello; import java.util.Date; import net.sourceforge.jfacets.annotations.FacetKey; /** * Hello Facet for all users (root_profile is the * top-level profile in the profiles hierarchy) and date object. */ @FacetKey(name="hello", profileId="root_profile", targetObjectType=Date.class) public class ViewDateRootProfile extends BaseHelloFacet { public String hello() { // get the facet's target object (a Date) Date d = (Date)getContext().getTargetObject(); // return a dummy message return "Hello ! the time is " + d.toString(); } } (hello, admin_role, java.util.Date)This is our second facet, which overrides (hello, root_profile, Date). It's defined for a sub-profile ( package net.sourceforge.jfacets.examples.hello; import java.util.Date; import net.sourceforge.jfacets.annotations.FacetKey; @FacetKey(name="hello", profileId="admin_role", targetObjectType=Date.class) public class ViewDateAdmin extends BaseHelloFacet { public String hello() { Date date = (Date)getContext().getTargetObject(); return "Hello, admin ! here is the time, in ms = " + date.getTime(); } } As usual, the "hello" example is not very useful per se, but it gives a good glance of how you can assign and execute code in a profiled way. |