Recent Changes - Search:


SourceForge Logo









YourKit Java Profiler

ProfiledCommands

TODO : 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 (java.lang.Integers) for different profiles. We'll use 2 commands (one per role) :

  • (increment, standard_role, java.lang.Integer) : increments of 1 ;
  • (increment, admin_role, java.lang.Integer) : increments of 10 ! ;

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 :

package net.sourceforge.jfacets.examples.command;

public class IncrementIntegerStandard extends BaseCommandFacet {

        public Object execute() {
                // we increment of 1
                Integer i = (Integer)getContext().getTargetObject();
                return new Integer(i.intValue() + 1);
        }

}

package net.sourceforge.jfacets.examples.command;

public class IncrementIntegerAdmin extends BaseCommandFacet {

        public Object execute() {
                // we increment of ten !
                Integer i = (Integer)getContext().getTargetObject();
                return new Integer(i.intValue() + 10);
        }

}

As you can see they both inherit from BaseCommandFacet. Here is the code :

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 main() that executes profile-based commands :

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 !

Edit - History - Print - Recent Changes - Search
Page last modified on December 21, 2006, at 07:29 PM