Accessing a user-defined policy by using ESQL

Use ESQL to query properties dynamically at run time, in user-defined policies.

Before you begin

About this task

If you created a user-defined policy, and created properties for that policy, you can query those properties by using a Java class method that can be called from ESQL. For example, you can create a user-defined policy to set timeouts for processing HTTP messages.

Procedure

  1. Create a Java class with a static method that can be called from the ESQL mypolicyaccessclass.getpolicyproperty().
    In this method, you can access the policy by using the provided Java class mbpolicy, as shown in Accessing a user-defined policy from a JavaCompute node.
  2. Right-click the Compute node and click Open ESQL to create and open an ESQL file in the Editor view, or open an existing file.
  3. Create a Java class called MyPolicyAccessClass with a static method called getPolicyProperty().
    This method uses the MbPolicy Java class to return a Java string that contains the policy property. For more information, see Policies overview.
  4. Add user code as shown below. In the example shown, a user-defined policy called MyUDCS has been defined in a policy project called PP1.
    CREATE FUNCTION getPolicyProperty( IN policyName CHARACTER, IN propertyName CHARACTER )
      RETURNS CHARACTER
      LANGUAGE JAVA
      EXTERNAL NAME "MyPolicyAccessClass.getPolicyProperty";
    
    DECLARE Property2 CHARACTER;
    SET Property2=getPolicyProperty('{PP1}:MyUDCS', 'prop2');
    

    This lookup syntax can be qualified by policy project. If no policy project is qualified, the policy project that is defined on the defaultPolicyProject property in the node.conf.yaml configuration file is used. Null is returned if either the property or the policy does not exist.

  5. Create and deploy the Java class that is called from the ESQL code that you added in step 2, to access and return the policy property.
    For example:
    
    import com.ibm.broker.plugin.MbPolicy;
    public class MyPolicyAccessClass {
      public static String getPolicyProperty(String policyName, String propertyName ) {
        String resultPropertyValue = null; 
        try {
          MbPolicy myPol = MbPolicy.getPolicy("UserDefined", policyName);
          if (myPol != null) {
            resultPropertyValue = myPol.getPropertyValueAsString(propertyName);
            if (resultPropertyValue == null) {
              System.out.println("Unable to find property '"+propertyName+"' in UserDefined policy with name '"+policyName+"'");
            } else {
              System.out.println("Found property '"+propertyName+"' with value '"+resultPropertyValue+"' in UserDefined policy with name '"+policyName+"'");
            }
          } else {
            System.out.println("Unable to find UserDefined policy with name '"+policyName+"'");
          }
        } catch (MbException mbe) {
          System.out.println("Exception caught trying to find UserDefined policy with name '"+policyName+"'. Exception details: '"+mbe.toString()+"'"); 
        }
        return resultPropertyValue;
    } }