This tutorial demonstrates how to create a custom evaluator.
You will create a custom evaluator class, wire it into SkyVault as a Spring bean, and learn
how to use evaluator properties.
This tutorial assumes you have completed previous tutorials. This tutorial assumes
you will be using the admin account. For testing convenience it will be
useful if you create another user account before starting this tutorial.
In the previous tutorial you saw that the provided evaluators can be applied to a
module. Different modules can have different evaluators applied to them. As well as
using the provided evaluators, it is possible to create your own custom evaluator. This
tutorial shows how to create a simple custom evaluator.
- In Eclipse Package Explorer create a new class in the beans package called SimpleModuleEvaluator.
-
Add the following code to the class:
package beans; import java.util.Map; import org.springframework.extensions.surf.RequestContext; import org.springframework.extensions.surf.extensibility.ExtensionModuleEvaluator; public class SimpleModuleEvaluator implements ExtensionModuleEvaluator { public static final String USER_PROP = "user"; public boolean applyModule(RequestContext context, Map<String, String> evaluationProperties) { String currUser = context.getUser().getId(); String targetUser = evaluationProperties.get(USER_PROP); return (targetUser != null && targetUser.equals(currUser)); } public String[] getRequiredProperties() { return new String[] { USER_PROP}; } }
- In Package Explorer expand the org/springframework/extensions/surf folder to locate your spring-surf-extensibility-context.xml file.
-
Add a bean for your new evaluator:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN//EN' 'http://www.springframework.org/dtd/spring-beans.dtd'> <beans> <bean id="beans.evaluator" class="beans.SiteMembersEvaluator" /> <bean id="beans.simple.module.evaluator" class="beans.SimpleModuleEvaluator" /> </beans>
- Rebuild your project using the Ant build script as done in previous tutorials.
- Restart your application server.
- In a browser tab navigate to http://localhost:8080/share/page/modules/deploy.
- Click on a deployed module, to display the list of evaluators.
-
From the list of evaluators select the evaluator,
beans.simple.module.evaluator.
You will see the evaluator property "user".
Attention: If you do not see the properties then try the following: close the Module Deployment tab. Create a new tab. Reload the Module Deployment page. - Set the user evaluator property to admin.
- Click Update.
- Click Apply Changes.
-
In another tab navigate to the admin dashboard.
You will see that the module has been applied.
-
Log out from the admin account and log into another account.
On the new user account dashboard you will see that the module is not applied. This is because the module was applied conditionally to the user; the module is only applied for the admin user, as was set by using the evaluator property.