001: package biz.hammurapi.rules.jsr94.admin;
002:
003: import java.io.IOException;
004: import java.io.Serializable;
005: import java.io.StringReader;
006: import java.net.MalformedURLException;
007: import java.net.URL;
008: import java.rmi.RemoteException;
009: import java.util.HashMap;
010: import java.util.Map;
011:
012: import javax.rules.admin.RuleExecutionSet;
013: import javax.rules.admin.RuleExecutionSetCreateException;
014: import javax.xml.parsers.FactoryConfigurationError;
015: import javax.xml.parsers.ParserConfigurationException;
016: import javax.xml.transform.TransformerException;
017:
018: import org.w3c.dom.Element;
019: import org.xml.sax.SAXException;
020:
021: import biz.hammurapi.config.ConfigurationException;
022: import biz.hammurapi.xml.dom.DOMUtils;
023:
024: /**
025: * Rule execution set provider.
026: * @author Pavel Vlasov
027: *
028: */
029: class RuleExecutionSetProvider implements
030: javax.rules.admin.RuleExecutionSetProvider {
031:
032: private Map properties = new HashMap();
033:
034: /**
035: * Constructor.
036: * @param properties
037: */
038: public RuleExecutionSetProvider(Map properties) {
039: if (properties != null) {
040: this .properties.putAll(properties);
041: }
042: }
043:
044: /**
045: * Creates rule execution set from XML Element.
046: * Properties are ignored.
047: */
048: public RuleExecutionSet createRuleExecutionSet(Element holder,
049: Map properties) throws RuleExecutionSetCreateException,
050: RemoteException {
051: try {
052: return new biz.hammurapi.rules.jsr94.admin.RuleExecutionSet(
053: holder, properties);
054: } catch (ConfigurationException e) {
055: throw new RuleExecutionSetCreateException(
056: "Could not instantiate rules: " + e, e);
057: } catch (TransformerException e) {
058: throw new RuleExecutionSetCreateException(
059: "Could not parse XML definition: " + e, e);
060: }
061: }
062:
063: /**
064: * Creates rule execution set from XML String
065: * The first parameter shall be of type String.
066: */
067: public RuleExecutionSet createRuleExecutionSet(
068: Serializable xmlString, Map properties)
069: throws RuleExecutionSetCreateException, RemoteException {
070: try {
071: return createRuleExecutionSet(DOMUtils.parse(
072: new StringReader((String) xmlString))
073: .getDocumentElement(), properties);
074: } catch (SAXException e) {
075: throw new RuleExecutionSetCreateException(
076: "Could not parse XML String: " + e, e);
077: } catch (IOException e) {
078: throw new RuleExecutionSetCreateException(
079: "Should never happen: " + e, e);
080: } catch (ParserConfigurationException e) {
081: throw new RuleExecutionSetCreateException(
082: "Could not parse XML String: " + e, e);
083: } catch (FactoryConfigurationError e) {
084: throw new RuleExecutionSetCreateException(
085: "Could not parse XML String: " + e);
086: }
087: }
088:
089: /**
090: * Constructs rule execution set from URI. Prefix <code>resource:</code> in uri indicates that
091: * rule set shall be loaded from classloader, otherwise it is loaded from URL.
092: */
093: public RuleExecutionSet createRuleExecutionSet(String uri,
094: Map properties) throws RuleExecutionSetCreateException,
095: IOException, RemoteException {
096: try {
097: if (uri.startsWith("resource:")) {
098: return new biz.hammurapi.rules.jsr94.admin.RuleExecutionSet(
099: uri,
100: DOMUtils
101: .parse(
102: getClass()
103: .getClassLoader()
104: .getResourceAsStream(
105: uri
106: .substring("resource:"
107: .length())))
108: .getDocumentElement(), properties);
109: }
110:
111: return new biz.hammurapi.rules.jsr94.admin.RuleExecutionSet(
112: uri, DOMUtils.parse(new URL(uri).openStream())
113: .getDocumentElement(), properties);
114: } catch (MalformedURLException e) {
115: throw new RuleExecutionSetCreateException("Malformed URI: "
116: + uri, e);
117: } catch (SAXException e) {
118: throw new RuleExecutionSetCreateException(
119: "Could not parse XML definition: " + e, e);
120: } catch (IOException e) {
121: throw new RuleExecutionSetCreateException(
122: "Could not read XML definition: " + e, e);
123: } catch (ParserConfigurationException e) {
124: throw new RuleExecutionSetCreateException(
125: "Could not parse XML definition: " + e, e);
126: } catch (FactoryConfigurationError e) {
127: throw new RuleExecutionSetCreateException(
128: "Could not parse XML definition: " + e);
129: } catch (ConfigurationException e) {
130: throw new RuleExecutionSetCreateException(
131: "Could not instantiate rules: " + e, e);
132: } catch (TransformerException e) {
133: throw new RuleExecutionSetCreateException(
134: "Could not parse XML definition: " + e, e);
135: }
136: }
137:
138: }
|