| Drools-OSGi Integration |
|
|
| Written by Valery Abu-Eid | |
| Friday, 17 October 2008 00:11 | |
Integration ScenariosWhile Drools (aka. JBoss Rules) already offers mechanisms for updating rules at runtime, OSGi can be used as an alternative/complimentary way to achieve dynamicity and to enable class versioning. Below are some variants of OSGi-Drools integration that seemed most relevant to me:
Integration IssuesPrior to working on OSGi-Drools integration scenarios you need to address few concerns:
Example ApplicationThe Example Application consists of four application bundles:
The consumer bundle invokes the Price Rule Service (if available) every 5 seconds so the change in the behavior could be noted when Rules Provider bundles are installed and uninstalled at runtime. Note worthy about this example is that dynamicity achieved by providing rules as OSGi Services (the second approach) and that an OSGi-aware Class Loader provided by the ClassLoading-Utils project was used. The code below demonstrates how the rule was created in the OSGi Environment and how I used the OSGi-aware Class Loader.
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Properties;
import org.drools.RuleBase;
import org.drools.RuleBaseConfiguration;
import org.drools.RuleBaseFactory;
import org.drools.compiler.PackageBuilder;
import org.drools.compiler.PackageBuilderConfiguration;
import org.drools.rule.Package;
import org.dynamicjava.osgi.classloading_utils.OsgiEnvironmentClassLoader;
import org.osgi.framework.BundleContext;
public class RuleCreator {
public RuleBase createRule(BundleContext bundleContext) throws Exception {
OsgiEnvironmentClassLoader classLoader =
new OsgiEnvironmentClassLoader(bundleContext,
Thread.currentThread().getContextClassLoader(),
bundleContext.getBundle());
Reader drlSource = new InputStreamReader(
bundleContext.getBundle().getResource(
"advanced_rule.dslr").openStream());
Reader dslSource = new InputStreamReader(
bundleContext.getBundle().getResource(
"advanced_rule.dsl").openStream());
Properties properties = new Properties();
properties.setProperty("drools.dialect.java.compiler", "JANINO");
PackageBuilderConfiguration config =
new PackageBuilderConfiguration(classLoader, properties);
PackageBuilder builder = new PackageBuilder(config);
builder.addPackageFromDrl(drlSource, dslSource);
Package pkg = builder.getPackage();
RuleBaseConfiguration ruleBaseConfig = new RuleBaseConfiguration();
RuleBase ruleBase = RuleBaseFactory.newRuleBase(ruleBaseConfig);
ruleBase.addPackage(pkg);
return ruleBase;
}
}
I left the OSGi-incompliant Drools libraries and their dependencies for DA-Launcher to generate bundles from them. The Example Application can be downloaded from the link below. The example application is available from drools-osgi-example.zip. The source code drools-osgi-example-src.zip. Currently, this example is only compatible with Equinox, the work for supporting Felix and Knopflerfish is in-progress and the example application will be updated soon. |