001: package org.drools.jsr94.rules.admin;
002:
003: /*
004: * Copyright 2005 JBoss Inc
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import java.io.IOException;
020: import java.io.InputStream;
021: import java.io.InputStreamReader;
022: import java.io.Reader;
023: import java.io.Serializable;
024: import java.net.URL;
025: import java.util.Map;
026:
027: import javax.rules.admin.RuleExecutionSet;
028: import javax.rules.admin.RuleExecutionSetCreateException;
029: import javax.rules.admin.RuleExecutionSetProvider;
030: import javax.xml.transform.Source;
031: import javax.xml.transform.Transformer;
032: import javax.xml.transform.TransformerException;
033: import javax.xml.transform.TransformerFactory;
034: import javax.xml.transform.dom.DOMSource;
035: import javax.xml.transform.sax.SAXResult;
036:
037: import org.drools.compiler.PackageBuilder;
038: import org.drools.lang.descr.PackageDescr;
039: import org.drools.rule.Package;
040: import org.drools.xml.XmlPackageReader;
041: import org.w3c.dom.Element;
042:
043: /**
044: * The Drools implementation of the <code>RuleExecutionSetProvider</code>
045: * interface which defines <code>RuleExecutionSet</code> creation methods for
046: * defining <code>RuleExecutionSet</code>s from potentially serializable
047: * resources.
048: *
049: * @see RuleExecutionSetProvider
050: *
051: * @author N. Alex Rupp (n_alex <at>codehaus.org)
052: * @author <a href="mailto:thomas.diesler@softcon-itec.de">thomas diesler </a>
053: * @author <a href="mailto:michael.frandsen@syngenio.de">michael frandsen </a>
054: */
055: public class RuleExecutionSetProviderImpl implements
056: RuleExecutionSetProvider {
057: /**
058: * Creates a <code>RuleExecutionSet</code> implementation from an XML
059: * Document and additional Drools-specific properties. A Drools-specific
060: * rule execution set is read from the supplied XML Document.
061: *
062: * @param ruleExecutionSetElement the XML element that is the source of the
063: * rule execution set
064: * @param properties additional properties used to create the
065: * <code>RuleExecutionSet</code> implementation.
066: * May be <code>null</code>.
067: *
068: * @throws RuleExecutionSetCreateException on rule execution set creation
069: * error.
070: *
071: * @return The created <code>RuleExecutionSet</code>.
072: */
073: public RuleExecutionSet createRuleExecutionSet(
074: final Element ruleExecutionSetElement, final Map properties)
075: throws RuleExecutionSetCreateException {
076: try {
077: // Prepare the DOM source
078: final Source source = new DOMSource(ruleExecutionSetElement);
079:
080: final XmlPackageReader xmlPackageReader = new XmlPackageReader();
081: // Prepare the result
082: final SAXResult result = new SAXResult(xmlPackageReader);
083:
084: // Create a transformer
085: final Transformer xformer = TransformerFactory
086: .newInstance().newTransformer();
087:
088: // Traverse the DOM tree
089: xformer.transform(source, result);
090:
091: final PackageDescr packageDescr = xmlPackageReader
092: .getPackageDescr();
093:
094: // pre build the package
095: final PackageBuilder builder = new PackageBuilder();
096: builder.addPackage(packageDescr);
097: final Package pkg = builder.getPackage();
098:
099: final LocalRuleExecutionSetProviderImpl localRuleExecutionSetProvider = new LocalRuleExecutionSetProviderImpl();
100: return localRuleExecutionSetProvider
101: .createRuleExecutionSet(pkg, properties);
102: } catch (final TransformerException e) {
103: throw new RuleExecutionSetCreateException(
104: "could not create RuleExecutionSet: " + e);
105: }
106:
107: }
108:
109: /**
110: * Creates a <code>RuleExecutionSet</code> implementation from a
111: * Drools-specific Abstract Syntax Tree (AST) representation and
112: * Drools-specific properties.
113: * <p/>
114: * This method accepts a <code>org.drools.RuleBase</code> object as its
115: * vendor-specific AST representation.
116: *
117: * @param ruleExecutionSetAst the Drools representation of a
118: * rule execution set
119: * @param properties additional properties used to create the
120: * <code>RuleExecutionSet</code> implementation.
121: * May be <code>null</code>.
122: *
123: * @throws RuleExecutionSetCreateException on rule execution set creation
124: * error.
125: *
126: * @return The created <code>RuleExecutionSet</code>.
127: */
128: public RuleExecutionSet createRuleExecutionSet(
129: final Serializable ruleExecutionSetAst, final Map properties)
130: throws RuleExecutionSetCreateException {
131: if (ruleExecutionSetAst instanceof Package) {
132: final LocalRuleExecutionSetProviderImpl localRuleExecutionSetProvider = new LocalRuleExecutionSetProviderImpl();
133: return localRuleExecutionSetProvider
134: .createRuleExecutionSet(ruleExecutionSetAst,
135: properties);
136: } else {
137: throw new IllegalArgumentException(
138: "Serializable object must be "
139: + "an instance of org.drools.rule.RuleSet. It was "
140: + ruleExecutionSetAst.getClass().getName());
141: }
142: }
143:
144: /**
145: * Creates a <code>RuleExecutionSet</code> implementation from a URI.
146: * The URI is opaque to the specification and may be used to refer to the
147: * file system, a database, or Drools-specific datasource.
148: *
149: * @param ruleExecutionSetUri the URI to load the rule execution set from
150: * @param properties additional properties used to create the
151: * <code>RuleExecutionSet</code> implementation.
152: * May be <code>null</code>.
153: *
154: * @throws RuleExecutionSetCreateException on rule execution set creation
155: * error.
156: * @throws IOException if an I/O error occurs while accessing the URI
157: *
158: * @return The created <code>RuleExecutionSet</code>.
159: */
160: public RuleExecutionSet createRuleExecutionSet(
161: final String ruleExecutionSetUri, final Map properties)
162: throws RuleExecutionSetCreateException, IOException {
163: InputStream in = null;
164: try {
165: final LocalRuleExecutionSetProviderImpl localRuleExecutionSetProvider = new LocalRuleExecutionSetProviderImpl();
166: in = new URL(ruleExecutionSetUri).openStream();
167: final Reader reader = new InputStreamReader(in);
168: return localRuleExecutionSetProvider
169: .createRuleExecutionSet(reader, properties);
170: } catch (final IOException ex) {
171: throw ex;
172: } finally {
173: if (in != null) {
174: in.close();
175: }
176: }
177: }
178: }
|