Weaver 3.1.0a1-D1

com.oldlight.weaver.clientbinding
Class WeaverAttributeAssertion

java.lang.Object
  extended bycom.oldlight.weaver.clientbinding.WeaverAttributeAssertion

public class WeaverAttributeAssertion
extends Object

The WeaverAttributeAssertion class checks values supplied to W-Beans. This class provides a convenient mechanism to check that the values needed by your W-Beans exist and are of the correct type. If values are not as expected then a WeaverClientException containing a suitable error message is automatically thrown. You may verify any of the following:

Typical usage is as follows:
 ...
 
 private static final WeaverAttributeAssertion[] assertions =
     new WeaverAttributeAssertion[] {
         new WeaverAttributeAssertion("var", WeaverAttributeUtil.SCOPE_BEAN, 
                                      java.lang.String.class, true),
         new WeaverAttributeAssertion("colour", WeaverAttributeUtil.SCOPE_BEAN, 
                                      java.lang.String.class, true, 
                                      new String[] { "red", "green" } ),
 };
 
 ...
                
     public void handle(WeaverExecutionContext context) 
         throws WeaverClientException {
         
         WeaverAttributeAssertion.assertAttributes(null, context, assertions);   
 ...
 
The first assertion declares that a value called var must be found in the Bean scope (that is must have been supplied via the param tag in the WAD file) and that it must be a String. The second assertion is similar to the first but insists that the value of colour attribute is take from the values red and green. Attribute assertions can be a great help during development but can be an overhead in a production deployment.

Author:
Paul Harvey

Field Summary
private  Class attrClass
           
private static boolean checking
           
private  boolean mandatory
           
private  String name
           
private  String scope
           
private  String[] values
           
 
Constructor Summary
WeaverAttributeAssertion(String name, String scope, Class attrClass, boolean mandatory)
          Create an attribute assertion.
WeaverAttributeAssertion(String name, String scope, Class attrClass, boolean mandatory, String[] values)
          Create an attribute assertion.
 
Method Summary
private  void append(String toAppend, StringBuffer buffer)
           
static void assertAttributes(String id, WeaverExecutionContext context, WeaverAttributeAssertion[] assertions)
          Check the current execution context against the supplied assertions.
private  boolean check(WeaverExecutionContext context, StringBuffer message)
           
private static boolean isA(Class valueClass, Class attrClass)
           
static void setChecking(boolean doChecking)
          Enable or disable checking attribute assertions.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

attrClass

private Class attrClass

mandatory

private boolean mandatory

scope

private String scope

name

private String name

values

private String[] values

checking

private static boolean checking
Constructor Detail

WeaverAttributeAssertion

public WeaverAttributeAssertion(String name,
                                String scope,
                                Class attrClass,
                                boolean mandatory,
                                String[] values)
Create an attribute assertion.

Parameters:
name - The name of the attribute to check.
scope - The scope of the attribute to check - or null to check all scopes via WeaverExecutionContext.findAttribute.
attrClass - The class the attribute must be compatible with - or null to not check.
mandatory - Whether existance of the value is mandatory.
values - A set of String values from which the attribute value must be taken.

WeaverAttributeAssertion

public WeaverAttributeAssertion(String name,
                                String scope,
                                Class attrClass,
                                boolean mandatory)
Create an attribute assertion.

Parameters:
name - The name of the attribute to check.
scope - The scope of the attribute to check - or null to check all scopes via WeaverExecutionContext.findAttribute.
attrClass - The class the attribute must be compatible with - or null to not check.
mandatory - Whether existance of the value is mandatory.
Method Detail

setChecking

public static void setChecking(boolean doChecking)
Enable or disable checking attribute assertions.

Parameters:
doChecking - Whether to enable or disable attribute assertions.

isA

private static boolean isA(Class valueClass,
                           Class attrClass)

append

private void append(String toAppend,
                    StringBuffer buffer)

check

private boolean check(WeaverExecutionContext context,
                      StringBuffer message)

assertAttributes

public static void assertAttributes(String id,
                                    WeaverExecutionContext context,
                                    WeaverAttributeAssertion[] assertions)
                             throws WeaverClientException
Check the current execution context against the supplied assertions. The current execution context is checked against the supplied assertions. If assertion checking is enabled and any of the assertions are not satisfied then an appropriate WeaverClientException is thrown.

Parameters:
id - The id parameter to pass to any WeaverClientException that may be thrown (may be null).
context - The execution context against which to check the assertions.
assertions - The assertions themselves.
Throws:
WeaverClientException - Thrown if any of the assertions are not satisfied.

Copyright ©Paul Harvey