|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
LocalRuleExecutionSetProviderImpl.java | 50% | 66.7% | 100% | 71.4% |
|
1 | package org.drools.jsr94.rules.admin; | |
2 | ||
3 | /* | |
4 | * $Id: LocalRuleExecutionSetProviderImpl.java,v 1.20 2004/12/16 18:48:14 dbarnett 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.Reader; | |
47 | import java.util.Map; | |
48 | ||
49 | import javax.rules.admin.LocalRuleExecutionSetProvider; | |
50 | import javax.rules.admin.RuleExecutionSet; | |
51 | import javax.rules.admin.RuleExecutionSetCreateException; | |
52 | ||
53 | import org.drools.RuleIntegrationException; | |
54 | import org.drools.RuleSetIntegrationException; | |
55 | import org.drools.io.RuleSetReader; | |
56 | import org.drools.rule.RuleSet; | |
57 | import org.xml.sax.SAXException; | |
58 | ||
59 | /** | |
60 | * The Drools implementation of the <code>LocalRuleExecutionSetProvider</code> | |
61 | * interface which defines <code>RuleExecutionSet</code> creation methods for | |
62 | * defining <code>RuleExecutionSet</code>s from local (non-serializable) | |
63 | * resources. | |
64 | * | |
65 | * @see LocalRuleExecutionSetProvider | |
66 | * | |
67 | * @author N. Alex Rupp (n_alex <at>codehaus.org) | |
68 | * @author <a href="mailto:thomas.diesler@softcon-itec.de">thomas diesler </a> | |
69 | */ | |
70 | public class LocalRuleExecutionSetProviderImpl | |
71 | implements LocalRuleExecutionSetProvider | |
72 | { | |
73 | /** Default constructor. */ | |
74 | 61 | public LocalRuleExecutionSetProviderImpl( ) |
75 | { | |
76 | 61 | super( ); |
77 | } | |
78 | ||
79 | /** | |
80 | * Creates a <code>RuleExecutionSet</code> implementation using a | |
81 | * supplied input stream and additional Drools-specific properties. | |
82 | * A Drools-specific rule execution set is read from the supplied | |
83 | * InputStream. The method <code>createRuleExecutionSet</code> taking | |
84 | * a Reader instance should be used if the source is a character | |
85 | * stream and encoding conversion should be performed. | |
86 | * | |
87 | * @param ruleExecutionSetStream | |
88 | * an input stream used to read the rule execution set. | |
89 | * @param properties additional properties used to create the | |
90 | * <code>RuleExecutionSet</code> implementation. | |
91 | * May be <code>null</code>. | |
92 | * | |
93 | * @throws RuleExecutionSetCreateException | |
94 | * on rule execution set creation error. | |
95 | * | |
96 | * @return The created <code>RuleExecutionSet</code>. | |
97 | */ | |
98 | 19 | public RuleExecutionSet createRuleExecutionSet( |
99 | InputStream ruleExecutionSetStream, Map properties ) | |
100 | throws RuleExecutionSetCreateException | |
101 | { | |
102 | 19 | try |
103 | { | |
104 | 19 | RuleSetReader setReader = new RuleSetReader( ); |
105 | 19 | RuleSet ruleSet = setReader.read( ruleExecutionSetStream ); |
106 | 19 | return this.createRuleExecutionSet( ruleSet, properties ); |
107 | } | |
108 | catch ( SAXException e ) | |
109 | { | |
110 | 0 | throw new RuleExecutionSetCreateException( |
111 | "cannot create rule set", e ); | |
112 | } | |
113 | catch ( IOException e ) | |
114 | { | |
115 | 0 | throw new RuleExecutionSetCreateException( |
116 | "cannot create rule set", e ); | |
117 | } | |
118 | } | |
119 | ||
120 | /** | |
121 | * Creates a <code>RuleExecutionSet</code> implementation using a supplied | |
122 | * character stream Reader and additional Drools-specific properties. A | |
123 | * Drools-specific rule execution set is read from the supplied Reader. | |
124 | * | |
125 | * @param ruleExecutionSetReader | |
126 | * a Reader used to read the rule execution set. | |
127 | * @param properties additional properties used to create the | |
128 | * <code>RuleExecutionSet</code> implementation. | |
129 | * May be <code>null</code>. | |
130 | * | |
131 | * @throws RuleExecutionSetCreateException | |
132 | * on rule execution set creation error. | |
133 | * | |
134 | * @return The created <code>RuleExecutionSet</code>. | |
135 | */ | |
136 | 38 | public RuleExecutionSet createRuleExecutionSet( |
137 | Reader ruleExecutionSetReader, Map properties ) | |
138 | throws RuleExecutionSetCreateException | |
139 | { | |
140 | 38 | try |
141 | { | |
142 | 38 | RuleSetReader setReader = new RuleSetReader( ); |
143 | 38 | RuleSet ruleSet = setReader.read( ruleExecutionSetReader ); |
144 | 38 | return this.createRuleExecutionSet( ruleSet, properties ); |
145 | } | |
146 | catch ( SAXException e ) | |
147 | { | |
148 | 0 | throw new RuleExecutionSetCreateException( |
149 | "cannot create rule set", e ); | |
150 | } | |
151 | catch ( IOException e ) | |
152 | { | |
153 | 0 | throw new RuleExecutionSetCreateException( |
154 | "cannot create rule set", e ); | |
155 | } | |
156 | } | |
157 | ||
158 | /** | |
159 | * Creates a <code>RuleExecutionSet</code> implementation from a | |
160 | * Drools-specific AST representation and Drools-specific properties. | |
161 | * | |
162 | * @param ruleExecutionSetAst | |
163 | * the vendor representation of a rule execution set | |
164 | * @param properties additional properties used to create the | |
165 | * <code>RuleExecutionSet</code> implementation. | |
166 | * May be <code>null</code>. | |
167 | * | |
168 | * @throws RuleExecutionSetCreateException | |
169 | * on rule execution set creation error. | |
170 | * | |
171 | * @return The created <code>RuleExecutionSet</code>. | |
172 | */ | |
173 | 4 | public RuleExecutionSet createRuleExecutionSet( |
174 | Object ruleExecutionSetAst, Map properties ) | |
175 | throws RuleExecutionSetCreateException | |
176 | { | |
177 | 4 | if ( ruleExecutionSetAst instanceof RuleSet ) |
178 | { | |
179 | 4 | RuleSet ruleSet = ( RuleSet ) ruleExecutionSetAst; |
180 | 4 | return this.createRuleExecutionSet( ruleSet, properties ); |
181 | } | |
182 | 0 | throw new RuleExecutionSetCreateException( |
183 | " Incoming AST object must be an org.drools.rule.RuleSet. Was " | |
184 | + ruleExecutionSetAst.getClass( ) ); | |
185 | } | |
186 | ||
187 | /** | |
188 | * Creates a <code>RuleExecutionSet</code> implementation from a | |
189 | * <code>RuleSet</code> and Drools-specific properties. | |
190 | * | |
191 | * @param ruleSet a Drools <code>org.drools.rule.RuleSet</code> | |
192 | * representation of a rule execution set. | |
193 | * @param properties additional properties used to create the | |
194 | * RuleExecutionSet implementation. May be <code>null</code>. | |
195 | * | |
196 | * @throws RuleExecutionSetCreateException | |
197 | * on rule execution set creation error. | |
198 | * | |
199 | * @return The created <code>RuleExecutionSet</code>. | |
200 | */ | |
201 | 61 | private RuleExecutionSet createRuleExecutionSet( |
202 | RuleSet ruleSet, Map properties ) | |
203 | throws RuleExecutionSetCreateException | |
204 | { | |
205 | 61 | try |
206 | { | |
207 | 61 | return new RuleExecutionSetImpl( ruleSet, properties ); |
208 | } | |
209 | catch ( RuleIntegrationException e ) | |
210 | { | |
211 | 0 | throw new RuleExecutionSetCreateException( |
212 | "Failed to create RuleExecutionSet", e ); | |
213 | } | |
214 | catch ( RuleSetIntegrationException e ) | |
215 | { | |
216 | 0 | throw new RuleExecutionSetCreateException( |
217 | "Failed to create RuleExecutionSet", e ); | |
218 | } | |
219 | } | |
220 | } |
|