01: package org.drools.brms.server.selector;
02:
03: import java.io.BufferedReader;
04: import java.io.IOException;
05: import java.io.InputStream;
06: import java.io.InputStreamReader;
07:
08: import org.apache.log4j.Logger;
09: import org.drools.CheckedDroolsException;
10: import org.drools.RuleBase;
11: import org.drools.RuntimeDroolsException;
12: import org.drools.StatelessSession;
13: import org.drools.StatelessSessionResult;
14: import org.drools.compiler.RuleBaseLoader;
15: import org.drools.repository.AssetItem;
16:
17: /**
18: * This uses rules to decide if an asset is to be included in a build.
19: *
20: * @author Michael Neale
21: *
22: */
23: public class RuleBasedSelector implements AssetSelector {
24:
25: private static final Logger log = Logger
26: .getLogger(RuleBasedSelector.class);
27:
28: String ruleFile;
29: private RuleBase ruleBase;
30:
31: public RuleBasedSelector(String val) {
32: this .ruleFile = val;
33:
34: InputStream ins = this .getClass().getResourceAsStream(ruleFile);
35: InputStreamReader reader = new InputStreamReader(ins);
36:
37: try {
38: this .ruleBase = RuleBaseLoader.getInstance()
39: .loadFromReader(reader);
40: } catch (CheckedDroolsException e) {
41: log.error(e);
42: throw new RuntimeDroolsException(e);
43: } catch (IOException e) {
44: log.error(e);
45: throw new RuntimeDroolsException(e);
46: }
47:
48: }
49:
50: public boolean isAssetAllowed(AssetItem asset) {
51: return evalRules(asset);
52: }
53:
54: boolean evalRules(Object asset) {
55: StatelessSession session = ruleBase.newStatelessSession();
56: StatelessSessionResult result = session
57: .executeWithResults(asset);
58:
59: java.util.Iterator objects = result.iterateObjects();
60: while (objects.hasNext()) {
61: if (objects.next() instanceof Allow) {
62: return true;
63: }
64: }
65: return false;
66: }
67:
68: }
|