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     */
023    package biz.hammurapi.rules;
024    
025    import java.util.ArrayList;
026    import java.util.Collection;
027    import java.util.Iterator;
028    
029    import biz.hammurapi.config.ComponentBase;
030    import biz.hammurapi.config.ConfigurationException;
031    import biz.hammurapi.dispatch.InvocationTarget;
032    
033    public abstract class AbstractRule extends ComponentBase implements InvocationTarget, Constants {
034    
035            private String name;
036            private String description;
037    
038            public String getName() {
039                    return name;
040            }
041    
042            /**
043             * This method is invoked by rule container to set rule name.
044             * Rule name is used to retrieve collections from collection 
045             * manager.
046             * @param name
047             */
048            public void setName(String name) {
049                    this.name=name;
050            }
051    
052            public String getDescription() {
053                    return description;
054            }
055    
056            public void setDescription(String description) {
057                    this.description = description;
058            }
059    
060            public String toString() {
061                    return "["+getClass().getName()+"] "+getName()+" - "+getDescription();
062            }
063    
064            public abstract Collection getRemoveHandlers();
065            
066            public void start() throws ConfigurationException {
067                    this.collectionManager=(CollectionManager) get("/"+COLLECTION_MANAGER);
068                    if (getName()==null) {
069                            throw new ConfigurationException("Rule name must not be null");
070                    }               
071            }
072            
073            public void stop() {
074                    // Override this method to release resources.
075            }                       
076            
077            private Collection acquiredCollections = new ArrayList();
078            
079            /**
080             * Convenience method to retrieve collection from the collection manager.
081             * Collections retrieved through this method are cleared in reset();
082             * @param setName
083             * @return
084             */
085            protected Collection getCollection(String collectionName, Object lock) {
086                    Collection ret = collectionManager.get(name, collectionName, lock);
087                    acquiredCollections.add(ret);
088                    return ret;
089            }
090                    
091            private CollectionManager collectionManager;
092    
093            /**
094             * Invoked in rule session reset() method. Cleans acquired collections.
095             * Subclasses can put additional cleanup logic here.
096             */
097            public void reset() {
098                    // Rules session clears collection manager. This functionality
099                    // is a duplicate of reset() functionality in rules session, 
100                    // for a case if rules are ever reset on individual basis.
101                    Iterator cit = acquiredCollections.iterator();
102                    while (cit.hasNext()) {
103                            ((Collection) cit.next()).clear();
104                    }
105            }
106            
107    }