001: package org.drools.jsr94.rules;
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.util.List;
020: import java.util.Map;
021:
022: import javax.rules.RuleExecutionSetNotFoundException;
023: import javax.rules.RuleRuntime;
024: import javax.rules.RuleSession;
025: import javax.rules.RuleSessionTypeUnsupportedException;
026:
027: import org.drools.jsr94.rules.admin.RuleExecutionSetRepository;
028:
029: /**
030: * The Drools implementation of the <code>RuleRuntime</code> interface which
031: * is the access point for runtime execution of <code>RuleExecutionSet</code>s.
032: * It provides methods to create <code>RuleSession</code> implementation as
033: * well as methods to retrieve <code>RuleExecutionSet</code>s that have been
034: * previously registered using the <code>RuleAdministrator</code>. <p/> The
035: * <code>RuleRuntime</code> should be accessed through the
036: * <code>RuleServiceProvider</code>. An instance of the
037: * <code>RuleRuntime</code> can be retrieved by calling: <p/> <code>
038: * RuleServiceProvider ruleServiceProvider =
039: * RuleServiceProvider.newInstance();<br/>
040: * RuleRuntime ruleRuntime = ruleServiceProvider.getRuleRuntime();
041: * </code>
042: * <p/> Note: the release method must be called on the <code>RuleSession</code>
043: * to clean up all resources used by the <code>RuleSession</code>.
044: *
045: * @see RuleRuntime
046: * @author N. Alex Rupp (n_alex <at>codehaus.org)
047: */
048: public class RuleRuntimeImpl implements RuleRuntime {
049: private static final long serialVersionUID = 400L;
050:
051: private RuleExecutionSetRepository repository;
052:
053: /**
054: * Create a new <code>RuleRuntimeImpl</code>.
055: */
056: public RuleRuntimeImpl(final RuleExecutionSetRepository repository) {
057: this .repository = repository;
058: // no special initialization required
059: }
060:
061: /**
062: * Creates a <code>RuleSession</code> implementation using the supplied
063: * Drools-specific rule execution set registration URI.
064: *
065: * @param uri
066: * the URI for the <code>RuleExecutionSet</code>
067: * @param properties
068: * additional properties used to create the
069: * <code>RuleSession</code> implementation.
070: * @param ruleSessionType
071: * the type of rule session to create.
072: *
073: * @throws RuleSessionTypeUnsupportedException
074: * if the ruleSessionType is not supported by Drools or the
075: * RuleExecutionSet
076: * @throws RuleExecutionSetNotFoundException
077: * if the URI could not be resolved into a
078: * <code>RuleExecutionSet</code>
079: *
080: * @return The created <code>RuleSession</code>.
081: */
082: public RuleSession createRuleSession(final String uri,
083: final Map properties, final int ruleSessionType)
084: throws RuleSessionTypeUnsupportedException,
085: RuleExecutionSetNotFoundException {
086:
087: if (ruleSessionType == RuleRuntime.STATELESS_SESSION_TYPE) {
088: final StatelessRuleSessionImpl session = new StatelessRuleSessionImpl(
089: uri, properties, this .repository);
090: return session;
091: } else if (ruleSessionType == RuleRuntime.STATEFUL_SESSION_TYPE) {
092: final StatefulRuleSessionImpl session = new StatefulRuleSessionImpl(
093: uri, properties, this .repository);
094: return session;
095: }
096:
097: throw new RuleSessionTypeUnsupportedException(
098: "invalid session type: " + ruleSessionType);
099: }
100:
101: /**
102: * Retrieves a <code>List</code> of the URIs that currently have
103: * <code>RuleExecutionSet</code>s associated with them. An empty list is
104: * returned is there are no associations.
105: *
106: * @return a <code>List</code> of <code>String</code>s (URIs)
107: */
108: public List getRegistrations() {
109: return this.repository.getRegistrations();
110: }
111: }
|