|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| DroolsJSR94RuleProcessor.java | 50% | 71.4% | 16.7% | 58.6% |
|
||||||||||||||
| 1 |
/*
|
|
| 2 |
* DroolsJSR94RuleProcessor.java
|
|
| 3 |
* Copyright 2002-2004 Bill2, Inc.
|
|
| 4 |
*
|
|
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
| 6 |
* you may not use this file except in compliance with the License.
|
|
| 7 |
* You may obtain a copy of the License at
|
|
| 8 |
*
|
|
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
| 10 |
*
|
|
| 11 |
* Unless required by applicable law or agreed to in writing, software
|
|
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
| 14 |
* See the License for the specific language governing permissions and
|
|
| 15 |
* limitations under the License.
|
|
| 16 |
*/
|
|
| 17 |
package org.dentaku.services.rules;
|
|
| 18 |
|
|
| 19 |
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
|
|
| 20 |
import org.codehaus.plexus.util.DirectoryScanner;
|
|
| 21 |
import org.drools.io.RuleSetReader;
|
|
| 22 |
import org.drools.rule.RuleSet;
|
|
| 23 |
|
|
| 24 |
import javax.rules.RuleRuntime;
|
|
| 25 |
import javax.rules.RuleServiceProvider;
|
|
| 26 |
import javax.rules.RuleServiceProviderManager;
|
|
| 27 |
import javax.rules.RuleSession;
|
|
| 28 |
import javax.rules.StatefulRuleSession;
|
|
| 29 |
import javax.rules.StatelessRuleSession;
|
|
| 30 |
import javax.rules.admin.LocalRuleExecutionSetProvider;
|
|
| 31 |
import javax.rules.admin.RuleAdministrator;
|
|
| 32 |
import javax.rules.admin.RuleExecutionSet;
|
|
| 33 |
import java.io.InputStream;
|
|
| 34 |
import java.io.InputStreamReader;
|
|
| 35 |
import java.io.Reader;
|
|
| 36 |
import java.net.URL;
|
|
| 37 |
|
|
| 38 |
/**
|
|
| 39 |
* Implementation of a JSR-94 Rules processor with Drools
|
|
| 40 |
*
|
|
| 41 |
* @todo this is a poor implementation WRT container neutrality. The ideal setup would be to have URLs from getResource
|
|
| 42 |
* managing the location of the configuration rules. That way, we don't have to change configuraions when we redeploy.
|
|
| 43 |
*/
|
|
| 44 |
public class DroolsJSR94RuleProcessor implements JSR94RuleProcessor, Initializable { |
|
| 45 |
/** Drools <code>RuleServiceProvider</code> URI. */
|
|
| 46 |
public static final String RULE_SERVICE_PROVIDER = "http://drools.org/"; |
|
| 47 |
|
|
| 48 |
protected RuleServiceProvider ruleServiceProvider;
|
|
| 49 |
private RuleRuntime ruleRuntime;
|
|
| 50 |
|
|
| 51 |
// configuration parameters
|
|
| 52 |
String ruleFilesDirectory; |
|
| 53 |
String ruleFilesIncludes; |
|
| 54 |
String bindUri; |
|
| 55 |
|
|
| 56 | 3 |
public void initialize() throws Exception { |
| 57 | 3 |
RuleServiceProviderManager.registerRuleServiceProvider( RULE_SERVICE_PROVIDER, org.drools.jsr94.rules.RuleServiceProviderImpl.class );
|
| 58 | 3 |
ruleServiceProvider = RuleServiceProviderManager.getRuleServiceProvider( RULE_SERVICE_PROVIDER ); |
| 59 |
|
|
| 60 | 3 |
RuleAdministrator ruleAdministrator = ruleServiceProvider.getRuleAdministrator(); |
| 61 | 3 |
LocalRuleExecutionSetProvider ruleSetProvider = ruleAdministrator.getLocalRuleExecutionSetProvider(null);
|
| 62 | 3 |
ruleRuntime = ruleServiceProvider.getRuleRuntime(); |
| 63 |
|
|
| 64 | 3 |
DirectoryScanner scanner = new DirectoryScanner();
|
| 65 | 3 |
scanner.setBasedir( ruleFilesDirectory ); |
| 66 | 3 |
scanner.setIncludes( new String[]{ ruleFilesIncludes } );
|
| 67 | 3 |
scanner.scan(); |
| 68 |
|
|
| 69 | 3 |
String[] fileNames = scanner.getIncludedFiles(); |
| 70 | 3 |
for (int i = 0; i < fileNames.length; i++) { |
| 71 | 3 |
String fileName = fileNames[i]; |
| 72 |
// read rules and register with administrator
|
|
| 73 | 3 |
InputStream resourceAsStream = this.getClass().getResourceAsStream(fileName);
|
| 74 | 3 |
Reader ruleReader = new InputStreamReader(resourceAsStream);
|
| 75 | 3 |
RuleExecutionSet ruleSet = ruleSetProvider.createRuleExecutionSet(ruleReader, null);
|
| 76 | 0 |
ruleAdministrator.registerRuleExecutionSet(bindUri, ruleSet, null);
|
| 77 |
} |
|
| 78 |
} |
|
| 79 |
|
|
| 80 | 0 |
public StatefulRuleSession getStatefulRuleSession() throws Exception { |
| 81 |
// obtain the stateless rule session
|
|
| 82 | 0 |
return (StatefulRuleSession)ruleRuntime.createRuleSession(bindUri, null, RuleRuntime.STATEFUL_SESSION_TYPE); |
| 83 |
} |
|
| 84 |
|
|
| 85 | 0 |
public StatelessRuleSession getStatelessRuleSession() throws Exception { |
| 86 |
// obtain the stateless rule session
|
|
| 87 | 0 |
return (StatelessRuleSession)ruleRuntime.createRuleSession(bindUri, null, RuleRuntime.STATELESS_SESSION_TYPE); |
| 88 |
} |
|
| 89 |
|
|
| 90 | 0 |
public void releaseRuleSession(RuleSession session) throws Exception { |
| 91 | 0 |
session.release(); |
| 92 |
} |
|
| 93 |
|
|
| 94 |
/**
|
|
| 95 |
* Get the requested resource from the ClassLoader.
|
|
| 96 |
*
|
|
| 97 |
* @see ClassLoader#getResource
|
|
| 98 |
*/
|
|
| 99 | 0 |
protected URL getResource( String res )
|
| 100 |
{
|
|
| 101 | 0 |
return getClass().getClassLoader().getResource( res );
|
| 102 |
} |
|
| 103 |
|
|
| 104 |
/**
|
|
| 105 |
* Get the requested resource from the ClassLoader.
|
|
| 106 |
*
|
|
| 107 |
* @see ClassLoader#getResourceAsStream
|
|
| 108 |
*/
|
|
| 109 | 0 |
protected InputStream getResourceAsStream( String res )
|
| 110 |
{
|
|
| 111 | 0 |
return getClass().getClassLoader().getResourceAsStream( res );
|
| 112 |
} |
|
| 113 |
} |
|
| 114 |
|
|
| 115 |
|
|
||||||||||