01: package org.drools.agent;
02:
03: import java.util.Arrays;
04: import java.util.Collection;
05: import java.util.Iterator;
06: import java.util.Properties;
07:
08: import org.drools.RuleBase;
09: import org.drools.RuntimeDroolsException;
10: import org.drools.rule.Package;
11:
12: /**
13: * All sources of packages must implement this.
14: * @author Michael Neale
15: *
16: */
17: public abstract class PackageProvider {
18:
19: protected AgentEventListener listener;
20:
21: /**
22: * Perform the scan, adding in any packages changed.
23: * if there are no changes, null should be returned.
24: * If there was an error reading the packages, this will not fail, it will
25: * just do nothing (as there may be a temporary IO issue).
26: */
27: abstract Package[] loadPackageChanges();
28:
29: /**
30: * This will be passed the entire config.
31: */
32: abstract void configure(Properties config);
33:
34: /**
35: * Remove the package from the rulebase if it exists in it.
36: * If it does not, does nothing.
37: */
38: static void removePackage(String name, RuleBase rb) {
39: Package[] ps = rb.getPackages();
40: if (ps == null)
41: return;
42: for (int i = 0; i < ps.length; i++) {
43: Package p = ps[i];
44: if (p.getName().equals(name)) {
45: rb.removePackage(name);
46: return;
47: }
48: }
49: }
50:
51: static void applyChanges(RuleBase rb,
52: boolean removeExistingPackages, Package[] ps,
53: AgentEventListener listener) {
54: applyChanges(rb, removeExistingPackages, Arrays.asList(ps),
55: listener);
56: }
57:
58: static void applyChanges(RuleBase rb,
59: boolean removeExistingPackages, Collection changes,
60: AgentEventListener listener) {
61: if (changes == null)
62: return;
63: for (Iterator iter = changes.iterator(); iter.hasNext();) {
64: Package p = (Package) iter.next();
65:
66: if (removeExistingPackages) {
67: removePackage(p.getName(), rb);
68: }
69: try {
70: listener.info("Adding package called " + p.getName());
71: rb.addPackage(p);
72: } catch (Exception e) {
73: throw new RuntimeDroolsException(e);
74: }
75: }
76: }
77:
78: public void setAgentListener(AgentEventListener listener) {
79: this.listener = listener;
80: }
81:
82: }
|