001: package org.drools.util;
002:
003: import java.io.IOException;
004: import java.io.InputStream;
005: import java.io.ObjectInputStream;
006:
007: import org.drools.RuleBase;
008: import org.drools.RuleBaseFactory;
009: import org.drools.RuntimeDroolsException;
010: import org.drools.common.DroolsObjectInputStream;
011: import org.drools.rule.Package;
012:
013: /**
014: * This loads up rulebases from binary packages.
015: * Can work with an existing or a new rulebase.
016: * This is useful for deployment.
017: *
018: * @author Michael Neale
019: */
020: public class BinaryRuleBaseLoader {
021:
022: private RuleBase ruleBase;
023: private ClassLoader classLoader;
024:
025: /**
026: * This will create a new default rulebase (which is initially empty).
027: * Optional parent classLoader for the Package's internal ClassLoader
028: * is Thread.currentThread.getContextClassLoader()
029: */
030: public BinaryRuleBaseLoader() {
031: this (RuleBaseFactory.newRuleBase(), null);
032: }
033:
034: /**
035: * This will add any binary packages to the rulebase.
036: * Optional parent classLoader for the Package's internal ClassLoader
037: * is Thread.currentThread.getContextClassLoader()
038: */
039: public BinaryRuleBaseLoader(RuleBase rb) {
040: this (rb, null);
041: }
042:
043: /**
044: * This will add any binary packages to the rulebase.
045: * Optional classLoader to be used as the parent ClassLoader
046: * for the Package's internal ClassLoader, is Thread.currentThread.getContextClassLoader()
047: * if not user specified.
048: */
049: public BinaryRuleBaseLoader(RuleBase rb, ClassLoader classLoader) {
050: if (classLoader == null) {
051: classLoader = Thread.currentThread()
052: .getContextClassLoader();
053: if (classLoader == null) {
054: classLoader = this .getClass().getClassLoader();
055: }
056: }
057: this .ruleBase = rb;
058: this .classLoader = classLoader;
059: }
060:
061: /**
062: * This will add the BINARY package to the rulebase.
063: * Uses the member ClassLoader as the Package's internal parent classLoader
064: * which is Thread.currentThread.getContextClassLoader if not user specified
065: * @param in An input stream to the serialized package.
066: */
067: public void addPackage(InputStream in) {
068: addPackage(in, this .classLoader);
069: }
070:
071: /**
072: * This will add the BINARY package to the rulebase.
073: * @param in An input stream to the serialized package.
074: * @param optional classLoader used as the parent ClassLoader for the Package's internal ClassLaoder
075: */
076: public void addPackage(InputStream in, ClassLoader classLoader) {
077: if (classLoader == null) {
078: classLoader = this .classLoader;
079: }
080:
081: try {
082: ObjectInputStream oin = new DroolsObjectInputStream(in,
083: classLoader);
084: Object opkg = oin.readObject();
085: if (!(opkg instanceof Package)) {
086: throw new IllegalArgumentException(
087: "Can only add instances of org.drools.rule.Package to a rulebase instance.");
088: }
089: Package binPkg = (Package) opkg;
090:
091: if (!binPkg.isValid()) {
092: throw new IllegalArgumentException(
093: "Can't add a non valid package to a rulebase.");
094: }
095:
096: try {
097: this .ruleBase.addPackage(binPkg);
098: } catch (Exception e) {
099: throw new RuntimeDroolsException(
100: "Unable to add package to the rulebase.", e);
101: }
102:
103: } catch (IOException e) {
104: throw new RuntimeDroolsException(e);
105: } catch (ClassNotFoundException e) {
106: throw new RuntimeDroolsException(e);
107: } finally {
108: try {
109: in.close();
110: } catch (IOException e) {
111: throw new RuntimeException(e);
112: }
113: }
114:
115: }
116:
117: public RuleBase getRuleBase() {
118: return this.ruleBase;
119: }
120:
121: }
|