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.Iterator;
020: import java.util.Map;
021:
022: import javax.rules.InvalidRuleSessionException;
023: import javax.rules.RuleExecutionSetMetadata;
024: import javax.rules.RuleSession;
025: import javax.rules.admin.RuleExecutionSet;
026:
027: import org.drools.jsr94.rules.admin.RuleExecutionSetImpl;
028: import org.drools.jsr94.rules.admin.RuleExecutionSetRepository;
029:
030: /**
031: * The Drools implementation of the <code>RuleSession</code> interface which is
032: * a representation of a client session with a rules engine. A rules engine
033: * session serves as an entry point into an underlying rules engine. The
034: * <code>RuleSession</code> is bound to a rules engine instance and exposes a
035: * vendor-neutral rule processing API for executing <code>Rule</code>s within a
036: * bound <code>RuleExecutionSet</code>.
037: * <p/>
038: * Note: the <code>release</code> method must be called to clean up all
039: * resources used by the <code>RuleSession</code>. Calling <code>release</code>
040: * may make the <code>RuleSession</code> eligible to be returned to a
041: * <code>RuleSession</code> pool.
042: *
043: * @see RuleSession
044: *
045: * @author N. Alex Rupp (n_alex <at>codehaus.org)
046: * @author <a href="mailto:thomas.diesler@softcon-itec.de">thomas diesler </a>
047: */
048: abstract class AbstractRuleSessionImpl implements RuleSession {
049: private RuleExecutionSetRepository repository;
050:
051: public AbstractRuleSessionImpl(
052: final RuleExecutionSetRepository repository) {
053: this .repository = repository;
054: }
055:
056: /**
057: * The Drools <code>RuleExecutionSet</code> associated
058: * with this <code>RuleSession</code>.
059: */
060: private RuleExecutionSetImpl ruleExecutionSet;
061:
062: /**
063: * A <code>Map</code> of <code>String</code>/<code>Object</code> pairs
064: * passed as application data to the Drools <code>WorkingMemory</code>.
065: */
066: private Map properties;
067:
068: /**
069: * Sets additional properties used to create this <code>RuleSession</code>.
070: *
071: * @param properties additional properties used to create the
072: * <code>RuleSession</code> implementation.
073: */
074: protected void setProperties(final Map properties) {
075: this .properties = properties;
076: }
077:
078: /**
079: * Returns the additional properties used to create this
080: * <code>RuleSession</code>.
081: *
082: * @return the additional properties used to create this
083: * <code>RuleSession</code>.
084: */
085: protected Map getProperties() {
086: return this .properties;
087: }
088:
089: /**
090: * Sets the Drools <code>RuleExecutionSet</code> associated
091: * with this <code>RuleSession</code>.
092: *
093: * @param ruleExecutionSet the Drools <code>RuleExecutionSet</code> to associate
094: * with this <code>RuleSession</code>.
095: */
096: protected void setRuleExecutionSet(
097: final RuleExecutionSetImpl ruleExecutionSet) {
098: this .ruleExecutionSet = ruleExecutionSet;
099: }
100:
101: /**
102: * Returns the Drools <code>RuleExecutionSet</code> associated
103: * with this <code>RuleSession</code>.
104: *
105: * @return the Drools <code>RuleExecutionSet</code> associated
106: * with this <code>RuleSession</code>.
107: */
108: protected RuleExecutionSetImpl getRuleExecutionSet() {
109: return this .ruleExecutionSet;
110: }
111:
112: protected abstract void checkRuleSessionValidity()
113: throws InvalidRuleSessionException;
114:
115: // JSR94 interface methods start here -------------------------------------
116:
117: /**
118: * Returns the meta data for the rule execution set bound to this rule
119: * session.
120: *
121: * @return the RuleExecutionSetMetaData bound to this rule session.
122: */
123: public RuleExecutionSetMetadata getRuleExecutionSetMetadata() {
124: String theBindUri = null;
125: for (final Iterator i = this .repository.getRegistrations()
126: .iterator(); i.hasNext();) {
127: final String aBindUri = (String) i.next();
128: final RuleExecutionSet aRuleSet = this .repository
129: .getRuleExecutionSet(aBindUri);
130: if (aRuleSet == this .ruleExecutionSet) {
131: theBindUri = aBindUri;
132: break;
133: }
134: }
135:
136: return new RuleExecutionSetMetadataImpl(theBindUri,
137: this .ruleExecutionSet.getName(), this .ruleExecutionSet
138: .getDescription());
139: }
140:
141: /**
142: * Releases all resources used by this rule session.
143: * This method renders this rule session unusable until
144: * it is reacquired through the <code>RuleRuntime</code>.
145: */
146: public void release() {
147: setProperties(null);
148: setRuleExecutionSet(null);
149: }
150: }
|