Clone wiki

AspectFaces / 8.Static-Weaving

AspectFaces is not bound to any specific framework and the use with JSF is an extension, but it can work for REST WS or for the static generation. This section describes a little about its use as the core for transformations.

You basically need to import AFWeaver, Context, and JavaInspector.

Static usage

// register all annotation descriptors
AFWeaver.registerAllAnnotations();

// context
Context context = new Context();
context.setProfiles(new String[]{"US"});
context.getVariables().put("myVariable","CustomValue");


// config with mappings
File file = new File(getResource("/aspectFaces.config.xml"));
Configuration configuration = new StaticConfiguration(file.getName());

// add mapping manually
//configuration.addMapping(new Mapping("Long", "path"));
context.setConfiguration(configuration);

// link to the config
AFWeaver weaver = new AFWeaver("myConfigName");
// passing what to process to the inspection
weaver.setInspector(new JavaInspector(Address.class));

// output
String out = weaver.generate(context);

It is possible to expose more details to the AF Context and use it for the transformation.

Basic information passing to the context

Context context = new Context();
context.setProfiles(new String[]{"US"}); // profiles
context.setRoles(new String[]{"Admin"}); // roles
context.setLayout("specialLayout.xhtml"); // layout
context.getVariables().put("myContextVar", "AF is cool!"); // custom constant
context.getVariables().put("utils", new Utils()); // custom class with util functions for EL

AFWeaver weaver = new AFWeaver("myConfigName"); 
weaver.setInspector(new JavaInspector(Address.class));

// output
String out = weaver.generate(context);

Use with REST - get stages

When you have AspectFaces configured in your Java EE, you may use it for WS.

Basic information passing to the context

//init context
private Context initAFContext() throws Exception {
  Context context = new Context();
  // use existing config registered at aspectfaces-config.xml 
  Configuration configuration = ConfigurationStorage.getInstance().getConfiguration("primeFaces");
  context.setConfiguration(configuration);
  return context;
}

// get UI applicable entity properties
@SuppressWarnings("unchecked")
private List<MetaProperty> getEntityProperties(Class<?> entity, Context context) throws Exception {
  JavaInspector inspector = new JavaInspector(entity);
  List<MetaProperty> metaProperties = inspector.inspect(context);

  // Steal Ordered properties :) 
  UIFragmentComposer composer = new UIFragmentComposer();

  // reduce non-applicable fields
  metaProperties = composer.filterApplicable(metaProperties);

  // apply order
  metaProperties = composer.getOrderedFields(metaProperties,context);

  return metaProperties;
}

// print mappings
public void printFieldMappings(List<MetaProperty> metaProperties, Context context) throws Exception {
  for (MetaProperty metaProperty : metaProperties) {
    // apply mapping
    String tagPath = context.getConfiguration().getTagPath(metaProperty, context);
    System.out.println(metaProperty.getName() + " " + tagPath);
    for (Variable var : metaProperty.getTemplateVariables()) {
       System.out.println(var.getName() + " " + var.getValue());
    }
  }
}
public void bootstrap() {
  Context context = initAFContext();
  List<MetaProperty> props = getEntityProperties(Person.class, Context context);
  printFieldMappings(props, context);
}

Updated