Recent Changes - Search:


SourceForge Logo









YourKit Java Profiler

InstanceFacets

Facets are assigned and searched for target types. You specify the facet's target type in its descriptor (annotation, XML, whatever), and at run time the type is used in order to lookup for the facet.

JFacets also includes (as of v2.1) a mechanism that allows to write and use instance facets, that are bound to a target type but also depend on the target object's (instance) state.

Writing Instance Facets

Writing instance facets is easy, you just have to implement the IInstanceFacet interface that defines the following method :

 public boolean matchesTargetObject(Object targetObject);

This callback method is invoked by JFacets when it traverses the profiles and types graphs in order to check wether or not the facets are matching for requested target object. This way, you can implement matchesTargetObject() in order to assign facets to specific instances !

The following example shows a confirm facet, assigned to the Order type, which matches only when the order isn't yet confirmed (you can't confirm an already confirmed order !) :

@FacetKey(name="confirm",profileId="ROLE_CUSTOMER",targetObjectType=Order.class)
public class ConfirmOrderFacet implements IFacet, IInstanceFacet {

  public boolean matchesTargetObject(Object targetObject) {
    Order order = (Order)targetObject;
    return !order.isConfirmed();
  }

  public void confirmOrder() {
    Order order = (Order)getContext().getTargetObject();
    order.setConfirmed(true);
    ...
  }

}

Retrieving Instance Facets

Retrievement of instance facets is identical to retrievement of type-wide facets. All the job of selecting matching facets is done for you by JFacets when you invoke getFacet(...) or execFacet(...) methods. If some instance facet(s) are found while traversing the profiles/types graphs, then JFacets checks wether or not it matches the target object. First matching instance facet is returned, and instance facets that don't match are just discarded for this lookup (just like if they were not there at all).

For more infos, have a look to the javadocs.

Edit - History - Print - Recent Changes - Search
Page last modified on May 11, 2007, at 08:08 AM