|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
RuleExecutionSetProviderImpl.java | 75% | 85.2% | 100% | 85.3% |
|
1 | package org.drools.jsr94.rules.admin; | |
2 | ||
3 | /* | |
4 | * $Id: RuleExecutionSetProviderImpl.java,v 1.19 2005/02/04 02:13:38 mproctor Exp $ | |
5 | * | |
6 | * Copyright 2002-2004 (C) The Werken Company. All Rights Reserved. | |
7 | * | |
8 | * Redistribution and use of this software and associated documentation | |
9 | * ("Software"), with or without modification, are permitted provided that the | |
10 | * following conditions are met: | |
11 | * | |
12 | * 1. Redistributions of source code must retain copyright statements and | |
13 | * notices. Redistributions must also contain a copy of this document. | |
14 | * | |
15 | * 2. Redistributions in binary form must reproduce the above copyright notice, | |
16 | * this list of conditions and the following disclaimer in the documentation | |
17 | * and/or other materials provided with the distribution. | |
18 | * | |
19 | * 3. The name "drools" must not be used to endorse or promote products derived | |
20 | * from this Software without prior written permission of The Werken Company. | |
21 | * For written permission, please contact bob@werken.com. | |
22 | * | |
23 | * 4. Products derived from this Software may not be called "drools" nor may | |
24 | * "drools" appear in their names without prior written permission of The Werken | |
25 | * Company. "drools" is a registered trademark of The Werken Company. | |
26 | * | |
27 | * 5. Due credit should be given to The Werken Company. | |
28 | * (http://drools.werken.com/). | |
29 | * | |
30 | * THIS SOFTWARE IS PROVIDED BY THE WERKEN COMPANY AND CONTRIBUTORS ``AS IS'' | |
31 | * AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
32 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
33 | * ARE DISCLAIMED. IN NO EVENT SHALL THE WERKEN COMPANY OR ITS CONTRIBUTORS BE | |
34 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
35 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
36 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
37 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
38 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
39 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
40 | * POSSIBILITY OF SUCH DAMAGE. | |
41 | * | |
42 | */ | |
43 | ||
44 | import java.io.IOException; | |
45 | import java.io.InputStream; | |
46 | import java.io.InputStreamReader; | |
47 | import java.io.Reader; | |
48 | import java.io.Serializable; | |
49 | import java.net.URL; | |
50 | import java.util.Map; | |
51 | ||
52 | import javax.rules.admin.RuleExecutionSet; | |
53 | import javax.rules.admin.RuleExecutionSetCreateException; | |
54 | import javax.rules.admin.RuleExecutionSetProvider; | |
55 | import javax.xml.transform.Source; | |
56 | import javax.xml.transform.Transformer; | |
57 | import javax.xml.transform.TransformerException; | |
58 | import javax.xml.transform.TransformerFactory; | |
59 | import javax.xml.transform.dom.DOMSource; | |
60 | import javax.xml.transform.sax.SAXResult; | |
61 | ||
62 | import org.drools.io.RuleSetReader; | |
63 | import org.drools.rule.RuleSet; | |
64 | import org.drools.smf.DefaultSemanticsRepository; | |
65 | import org.drools.smf.SemanticsReaderException; | |
66 | import org.w3c.dom.Element; | |
67 | ||
68 | /** | |
69 | * The Drools implementation of the <code>RuleExecutionSetProvider</code> | |
70 | * interface which defines <code>RuleExecutionSet</code> creation methods for | |
71 | * defining <code>RuleExecutionSet</code>s from potentially serializable | |
72 | * resources. | |
73 | * | |
74 | * @see RuleExecutionSetProvider | |
75 | * | |
76 | * @author N. Alex Rupp (n_alex <at>codehaus.org) | |
77 | * @author <a href="mailto:thomas.diesler@softcon-itec.de">thomas diesler </a> | |
78 | */ | |
79 | public class RuleExecutionSetProviderImpl implements RuleExecutionSetProvider | |
80 | { | |
81 | /** | |
82 | * Creates a <code>RuleExecutionSet</code> implementation from an XML | |
83 | * Document and additional Drools-specific properties. A Drools-specific | |
84 | * rule execution set is read from the supplied XML Document. | |
85 | * | |
86 | * @param ruleExecutionSetElement the XML element that is the source of the | |
87 | * rule execution set | |
88 | * @param properties additional properties used to create the | |
89 | * <code>RuleExecutionSet</code> implementation. | |
90 | * May be <code>null</code>. | |
91 | * | |
92 | * @throws RuleExecutionSetCreateException on rule execution set creation | |
93 | * error. | |
94 | * | |
95 | * @return The created <code>RuleExecutionSet</code>. | |
96 | */ | |
97 | 2 | public RuleExecutionSet createRuleExecutionSet( |
98 | Element ruleExecutionSetElement, Map properties ) | |
99 | throws RuleExecutionSetCreateException | |
100 | { | |
101 | // Prepare the DOM source | |
102 | 2 | Source source = new DOMSource( ruleExecutionSetElement ); |
103 | ||
104 | // Create a reader to handle the SAX events | |
105 | 2 | RuleSetReader reader; |
106 | 2 | try |
107 | { | |
108 | 2 | reader = |
109 | new RuleSetReader( DefaultSemanticsRepository.getInstance( ) ); | |
110 | } | |
111 | catch ( SemanticsReaderException e ) | |
112 | { | |
113 | 0 | throw new RuleExecutionSetCreateException( |
114 | "Couldn't get an instance of the DefaultSemanticsRepository: " | |
115 | + e ); | |
116 | } | |
117 | catch ( IOException e ) | |
118 | { | |
119 | 0 | throw new RuleExecutionSetCreateException( |
120 | "Couldn't get an instance of the DefaultSemanticsRepository: " | |
121 | + e ); | |
122 | } | |
123 | ||
124 | 2 | try |
125 | { | |
126 | // Prepare the result | |
127 | 2 | SAXResult result = new SAXResult( reader ); |
128 | ||
129 | // Create a transformer | |
130 | 2 | Transformer xformer = |
131 | TransformerFactory.newInstance( ).newTransformer( ); | |
132 | ||
133 | // Traverse the DOM tree | |
134 | 2 | xformer.transform( source, result ); |
135 | } | |
136 | catch ( TransformerException e ) | |
137 | { | |
138 | 0 | throw new RuleExecutionSetCreateException( |
139 | "could not create RuleExecutionSet: " + e ); | |
140 | } | |
141 | ||
142 | 2 | RuleSet ruleSet = reader.getRuleSet( ); |
143 | 2 | LocalRuleExecutionSetProviderImpl localRuleExecutionSetProvider = |
144 | new LocalRuleExecutionSetProviderImpl( ); | |
145 | 2 | return localRuleExecutionSetProvider.createRuleExecutionSet( |
146 | ruleSet, properties ); | |
147 | } | |
148 | ||
149 | /** | |
150 | * Creates a <code>RuleExecutionSet</code> implementation from a | |
151 | * Drools-specific Abstract Syntax Tree (AST) representation and | |
152 | * Drools-specific properties. | |
153 | * <p/> | |
154 | * This method accepts a <code>org.drools.RuleBase</code> object as its | |
155 | * vendor-specific AST representation. | |
156 | * | |
157 | * @param ruleExecutionSetAst the Drools representation of a | |
158 | * rule execution set | |
159 | * @param properties additional properties used to create the | |
160 | * <code>RuleExecutionSet</code> implementation. | |
161 | * May be <code>null</code>. | |
162 | * | |
163 | * @throws RuleExecutionSetCreateException on rule execution set creation | |
164 | * error. | |
165 | * | |
166 | * @return The created <code>RuleExecutionSet</code>. | |
167 | */ | |
168 | 2 | public RuleExecutionSet createRuleExecutionSet( |
169 | Serializable ruleExecutionSetAst, Map properties ) | |
170 | throws RuleExecutionSetCreateException | |
171 | { | |
172 | 2 | if ( ruleExecutionSetAst instanceof RuleSet ) |
173 | { | |
174 | 1 | LocalRuleExecutionSetProviderImpl localRuleExecutionSetProvider = |
175 | new LocalRuleExecutionSetProviderImpl( ); | |
176 | 1 | return localRuleExecutionSetProvider.createRuleExecutionSet( |
177 | ruleExecutionSetAst, properties ); | |
178 | } | |
179 | else | |
180 | { | |
181 | 1 | throw new IllegalArgumentException( "Serializable object must be " |
182 | + "an instance of org.drools.rule.RuleSet. It was " | |
183 | + ruleExecutionSetAst.getClass( ).getName( ) ); | |
184 | } | |
185 | } | |
186 | ||
187 | /** | |
188 | * Creates a <code>RuleExecutionSet</code> implementation from a URI. | |
189 | * The URI is opaque to the specification and may be used to refer to the | |
190 | * file system, a database, or Drools-specific datasource. | |
191 | * | |
192 | * @param ruleExecutionSetUri the URI to load the rule execution set from | |
193 | * @param properties additional properties used to create the | |
194 | * <code>RuleExecutionSet</code> implementation. | |
195 | * May be <code>null</code>. | |
196 | * | |
197 | * @throws RuleExecutionSetCreateException on rule execution set creation | |
198 | * error. | |
199 | * @throws IOException if an I/O error occurs while accessing the URI | |
200 | * | |
201 | * @return The created <code>RuleExecutionSet</code>. | |
202 | */ | |
203 | 2 | public RuleExecutionSet createRuleExecutionSet( |
204 | String ruleExecutionSetUri, Map properties ) | |
205 | throws RuleExecutionSetCreateException, IOException | |
206 | { | |
207 | 2 | InputStream in = null; |
208 | 2 | try |
209 | { | |
210 | 2 | LocalRuleExecutionSetProviderImpl localRuleExecutionSetProvider = |
211 | new LocalRuleExecutionSetProviderImpl( ); | |
212 | 2 | in = new URL( ruleExecutionSetUri ).openStream( ); |
213 | 2 | Reader reader = new InputStreamReader( in ); |
214 | 2 | return localRuleExecutionSetProvider.createRuleExecutionSet( |
215 | reader, properties ); | |
216 | } | |
217 | catch ( IOException ex ) | |
218 | { | |
219 | 0 | throw ex; |
220 | } | |
221 | finally | |
222 | { | |
223 | 2 | if ( in != null ) |
224 | { | |
225 | 2 | in.close( ); |
226 | } | |
227 | } | |
228 | } | |
229 | } |
|