YourKit Java Profiler |
JFacets /
ProfiledCommandsTODO : rewrite, this example really sucks ! This example shows a very simple implementation of the Command Pattern. The game here is to "profilize" commands ! We'll use ExecutableFacets, a built-in feature of JFacets that allows to make facets executable. The scenario is to execute commands on some objects (
Here is the content of the XML facets descriptor : <facet name="increment" profile="standard_role" object_type="java.lang.Integer" class="net.sourceforge.jfacets.examples.command.IncrementIntegerStandard"/> <facet name="increment" profile="admin_role" object_type="java.lang.Integer" class="net.sourceforge.jfacets.examples.command.IncrementIntegerAdmin"/> And here is the code for our facets : As you can see they both inherit from package net.sourceforge.jfacets.examples.command; import net.sourceforge.jfacets.IExecutable; import net.sourceforge.jfacets.IFacet; import net.sourceforge.jfacets.IFacetContext; public abstract class BaseCommandFacet implements IFacet, IExecutable { private IFacetContext ctx; public void setContext(IFacetContext ctx) { this.ctx = ctx; } public IFacetContext getContext() { return ctx; } public abstract Object execute(); } And now, a package net.sourceforge.jfacets.examples.command; import net.sourceforge.jfacets.JFacets; public class Main { public static void main(String[] args) { // the target object Integer i = new Integer(10); // get the JFacets bean JFacets jFacets = JFacets.get("net/sourceforge/jfacets/examples/command/jFacetsAppCtx.xml"); // execute commands System.out.println("i = " + i); System.out.println("Executing command (increment, john, " + i + ")"); Integer i1 = (Integer)jFacets.execFacet("increment", "john", i); print(i1); System.out.println("Executing command (increment, ivar, " + i + ")"); Integer i2 = (Integer)jFacets.execFacet("increment", "ivar", i); print(i2); System.out.println("Command example OK : check the logs !"); } private static void print(Integer i) { System.out.println("Result = " + i); } } ... and the console logs : i = 10 Executing command (increment, john, 10) Result = 11 Executing command (increment, ivar, 10) Result = 20 Command example OK : check the logs ! |