$BlogRSDURL$>
applicationContext-rules.xml
, into [appfuse]\web\WEB-INF\
:
<?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="ruleServiceProvider"
class="org.springmodules.jsr94.factory.DefaultRuleServiceProviderFactoryBean">
<property name="provider">http://drools.org/</value></property>
<property name="providerClass"><value>org.drools.jsr94.rules.RuleServiceProviderImpl</value>
</property>
</bean>
<bean id="ruleRuntime"
class="org.springmodules.jsr94.factory.RuleRuntimeFactoryBean">
<property name="serviceProvider"><ref local="ruleServiceProvider"/></property>
</bean>
<bean id="ruleAdministrator"
class="org.springmodules.jsr94.factory.RuleAdministratorFactoryBean">
<ref local="ruleServiceProvider"/></property>
</bean>
<bean id="ruleSource" class="org.springmodules.jsr94.rulesource.DefaultRuleSource">
<property name="ruleRuntime"><ref local="ruleRuntime"/></property>
<property name="ruleAdministrator"><ref local="ruleAdministrator"/></property>
<property name="source"><value>/WEB-INF/authorizedUsers.drl</value></property>
<property name="bindUri"><value>authorizedUsers</value></property>
</bean>
<bean id="rulesService" class="org.appfuse.service.impl.RulesServiceDrools">
<property name="ruleSource"><ref local="ruleSource"/></property>
</bean>
</beans>
authorizedUsers.drl
, is the ruleset, and RulesServiceDrools
is the Drools rule implementation we have in this sample setup.authorizedUsers.drl
, in [appfuse]\web\WEB-INF\
:
<?xml version="1.0" encoding="UTF-8"?>
<rule-set
name="Get authorized users"
description="Rules to retrieve authorized users"
xmlns="http://drools.org/rules"
xmlns:java="http://drools.org/semantics/java"
xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
xs:schemaLocation="http://drools.org/rules rules.xsd
http://drools.org/semantics/java java.xsd">
<rule name="Enabled users">
<parameter identifier="user">
<class>org.appfuse.model.User</class>
</parameter>
<java:condition> user.isEnabled() != true </java:condition>
<java:consequence> drools.retractObject(user); </java:consequence>
</rule>
<rule name="Valid credentials">
<parameter identifier="user">
<class>org.appfuse.model.User</class>
</parameter>
<java:condition>
user.isCredentialsExpired() == true || user.getRoles().size() == 0
</java:condition>
<java:consequence>
drools.retractObject(user);
</java:consequence>
</rule>
</rule-set>
if (!user.isEnabled() && (user.isCredentialsExpired()
|| user.getRoles().size() == 0))
userList.remove(user);
RulesService
, is simple enough:
public interface RulesService {
public List getAuthorizedUsers(List users);
}
RulesServiceDrools
:
public class RulesServiceDrools extends Jsr94RuleSupport implements RulesService {
public final static String ACTIVE_USERS_URI="authorizedUsers";
public List getAuthorizedUsers(List users) {
return executeStateless(ACTIVE_USERS_URI, users);
}
}
public List getAuthorizedUsers(List users) {
List outputList = getTemplate().executeStateless(ACTIVE_USERS_URI,null,
new StatelessRuleSessionCallback() {
public Object execute(StatelessRuleSession session)
throws InvalidRuleSessionException, RemoteException {
return session.executeRules(users);
}
}
);
return outputList;
}
public abstract class AuthorizedUsers extends BasePage implements PageRenderListener {
public abstract UserManager getUserManager();
public abstract void setUserManager(UserManager manager);
public abstract RulesService getRulesService();
public abstract void setRulesService(RulesService svr);
public void pageBeginRender(PageEvent event) {
List authorizedUsers = getRulesService().getAuthorizedUsers(getUserManager().getUsers(null));
...
}
}