Velocity A template engine OR A Rule engine OR Both
March 2005
Discussion
Velocity: A template engine OR A Rule engine OR Both?
Most of the developers must be familiar with Velocity as a great open source template engine and I don’t think I need to say much about its uses and features as a template engine. This paper compiles its features as a rule engine.
I have been working with java for the past 6 years and recently, I got an opportunity to design and develop an Entitlement Application.
An entitlement is the result of the execution of a formula, or rule that specifies what must be considered when deciding whether a given user has rights to manipulate or use a given resource (or object). This rule may be composed of one or more conditions that might only be evaluated with application context specific data. In addition, a rule evaluation may result in more than simple yes/no answers and contain additional information that would be required by an application to now perform the requested access to the resource. These are called obligations. Therefore an Entitlement Service can be used both as a rule evaluator and a privilege data service.
In its simplest form an Entitlement Service performs standard authorization calls. For example, can a user perform a simple action against a named resource? In its more complicated form an entitlement may be a rule on how an object such as a financial transaction may be manipulated. In this rule the specific parameters of the transaction may be compared against set limits or current financial data. The rule in this case may be a representation of business logic.
There are four types of entitlement:
Coarse-grained entitlement - The control of access to broad categories of resources, for example, an entire application, an entire subsystem, or access to specific web pages.
Fine-grained entitlement - The granting or withholding of individual resources like buttons, links, menu choices on a page or screen, or functions within a program.
Transactional entitlement - An entitlement that checks privilege to manipulate a resource by examining very specific parameters about a ‘transaction’ upon that resource. The characteristics of or relationships between these parameters could be expressed numerically (<, >, =, …), using date and time information, or even as part of a formula (*,%,/,-,+).
Dynamic entitlement - Where a parameter of a ‘transaction’ represents information that must be calculated or retrieved from an external source. This information may be retrieved by a call to an application specific service.
So according to the requirements, this application has to have:
Some logic to return the stored data (Users, User Groups, Resources, Resource Groups, Permissions etc) based on some filters. For example return me all the users who have an access on this resource. Logic to check the permission. Permissions are Boolean in nature and don’t require any complex logic to process. Although, permission are very simple they are quiet powerful for building an entitlement application and cater to most of the cases of Coarse-grained and Fine-grained entitlements. A rule based engine to handle Transactional and Dynamic entitlements.
Now, we will directly focus on the uses of Velocity as a rule engine as this article is not about developing an entitlement application.
So, instead of developing my own rule engine I started looking around to find out a decent rule engine that can fulfill my requirements. And I began to explore rule engines like:
JEL ( http://galaxy.fzu.cz/JEL ), Bean Shell (http://www.beanshell.org/home.html), EL ( http://java.sun.com/j2ee/1.4/docs/tutorial/doc/JSPIntro7.html )
and many more, especially the ones with open sources because my management did not want to spend too much money on this application.
What I was looking for was an engine with the following features:
Ease of use in development. Should reduce my development time. Easy and flexible in terms of configuration. It should have very easy and familiar language to write rules especially for a non-technical admin to maintain. Easily extendable. Clear separation between admin (Rule author) and developer. Tested and reliable. Great community support. Nobody is perfect and everybody needs Help :)
But I couldn’t find anything good enough out there. Suddenly, Velocity got my attention. I have already used Velocity to generate my GUI. However, this time, I was looking at it with a different point of view. I was compiling its features in terms of a rule engine and it seemed to me a considerable fit for this new framework. Then it became a perfect fit when I realized its uses of toolbox concept as well. The toolbox concept provides the power to rule an author to access any EIS or application (almost anything) at runtime and with a complete transparency to the author.
Now lets go over a real life example and use Velocity as rule engine. Before we go ahead, lets configure Velocity.
Velocity can be used as a servlet in a web application or as a utility in a standalone application. In our case, we will use it as a standalone application. Now, as a standalone application, you can use it in two ways. One is singleton another is separate runtime instance. I used it as singleton. Following is the code sample:
public class RuleEngineVelocityImpl implements RuleEngine{public RuleEngineVelocityImpl{ try { // Initialize Velocity.Velocity.init(); }catch(Exception e){ //Do something. }} public String execute(String ruleId ,Map params){ // get the velocity template from some storage.String template = getFromSomewhere(ruleId );// Create a Context object.VelocityContext context = new VelocityContext( params );// Get a template as stream.StringWriter writer = new StringWriter(); StringReader reader = new StringR
推荐文章 |
