001: /**
002: * hammurapi-rules @mesopotamia.version@
003: * Hammurapi rules engine.
004: * Copyright (C) 2005 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://http://www.hammurapi.biz
021: * e-Mail: support@hammurapi.biz
022: */package biz.hammurapi.rules;
023:
024: import java.io.Serializable;
025: import java.util.ArrayList;
026: import java.util.Collection;
027: import java.util.HashMap;
028: import java.util.Iterator;
029: import java.util.LinkedHashMap;
030: import java.util.Map;
031:
032: import javax.rules.Handle;
033:
034: import biz.hammurapi.config.Component;
035: import biz.hammurapi.config.ComponentBase;
036: import biz.hammurapi.config.ConfigurationException;
037:
038: /**
039: * Handle manager implementation which keeps handles in memory.
040: * Objects are compared by identity, not by eqals().
041: * @author Pavel Vlasov
042: * @version ${Revision}
043: */
044: public class IdentityHandleManager extends ComponentBase implements
045: HandleManager, Component {
046:
047: private Map handleMap;
048:
049: private String storageReference;
050:
051: /**
052: * Path to object storage.
053: * @param storageReference
054: */
055: public void setStorageReference(String storageReference) {
056: this .storageReference = storageReference;
057: }
058:
059: private static class HandleImpl implements Handle, Serializable {
060: private static final long serialVersionUID = -6198273232968377370L;
061: private Object master;
062:
063: HandleImpl(Object master) {
064: this .master = master;
065: }
066:
067: public boolean equals(Object obj) {
068: return obj instanceof HandleImpl
069: && master == ((HandleImpl) obj).master;
070: }
071:
072: public int hashCode() {
073: return master == null ? 0 : master.hashCode();
074: }
075:
076: public String toString() {
077: return getClass().getName() + " -> " + master;
078: }
079: }
080:
081: synchronized public Handle addObject(Object object) {
082: Handle newHandle = new HandleImpl(object);
083: Handle existingHandle = (Handle) handleMap.get(newHandle);
084: if (existingHandle == null) {
085: handleMap.put(newHandle, newHandle);
086: return newHandle;
087: }
088:
089: return existingHandle;
090: }
091:
092: synchronized public Object getObject(Handle handle) {
093: if (handle instanceof HandleImpl) {
094: return ((HandleImpl) handle).master;
095: }
096:
097: throw new IllegalArgumentException("Foreign handle: " + handle);
098: }
099:
100: public Collection getObjects() {
101: ArrayList ret = new ArrayList();
102: Iterator it = handleMap.keySet().iterator();
103: while (it.hasNext()) {
104: ret.add(((HandleImpl) it.next()).master);
105: }
106: return ret;
107: }
108:
109: public Collection getHandles() {
110: return handleMap.keySet();
111: }
112:
113: synchronized public void remove(Handle handle) {
114: handleMap.remove(handle);
115: }
116:
117: public boolean contains(Handle handle) {
118: return handleMap.containsKey(handle);
119: }
120:
121: synchronized public void rebind(Handle handle, Object object) {
122: if (handle instanceof HandleImpl) {
123: handleMap.remove(handle);
124: ((HandleImpl) handle).master = object;
125: handleMap.put(handle, handle);
126: } else {
127: throw new IllegalArgumentException("Foreign handle: "
128: + handle);
129: }
130: }
131:
132: public void remove(Object obj) {
133: handleMap.remove(new HandleImpl(obj));
134: }
135:
136: public synchronized void clear() {
137: handleMap.clear();
138: }
139:
140: public void start() throws ConfigurationException {
141: if (storageReference == null) {
142: handleMap = new LinkedHashMap();
143: } else {
144: handleMap = (Map) ((ObjectStorage) get(storageReference))
145: .get("handle-manager");
146: if (handleMap == null) {
147: handleMap = new HashMap();
148: ((ObjectStorage) get(storageReference)).put(
149: "handle-manager", handleMap);
150: }
151: }
152: }
153:
154: public void stop() throws ConfigurationException {
155: // Nothing
156: }
157:
158: public synchronized boolean isNegatedBy(Negator negator) {
159: Iterator it = handleMap.keySet().iterator();
160: while (it.hasNext()) {
161: HandleImpl handle = (HandleImpl) it.next();
162: if (Conclusion.object2Negator(handle.master, negator)) {
163: it.remove();
164: }
165: }
166:
167: return false;
168: }
169: }
|