001    /*
002     * 
003     * $Revision: 13085 $ $Date: 2008-02-06 18:27:24 +0100 (Mi, 06 Feb 2008) $
004     *
005     * This file is part of ***  M y C o R e  ***
006     * See http://www.mycore.de/ for details.
007     *
008     * This program is free software; you can use it, redistribute it
009     * and / or modify it under the terms of the GNU General Public License
010     * (GPL) as published by the Free Software Foundation; either version 2
011     * of the License or (at your option) any later version.
012     *
013     * This program is distributed in the hope that it will be useful, but
014     * WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016     * GNU General Public License for more details.
017     *
018     * You should have received a copy of the GNU General Public License
019     * along with this program, in a file called gpl.txt or license.txt.
020     * If not, write to the Free Software Foundation Inc.,
021     * 59 Temple Place - Suite 330, Boston, MA  02111-1307 USA
022     */
023    
024    package org.mycore.services.fieldquery;
025    
026    import java.util.Enumeration;
027    import java.util.Hashtable;
028    import java.util.Properties;
029    
030    import org.apache.log4j.Logger;
031    import org.mycore.common.MCRConfiguration;
032    import org.mycore.common.MCRConfigurationException;
033    
034    /**
035     * This class manages instances of MCRSearcher and provides methods to get these
036     * for a given searcher ID. The class is responsible for looking up, loading,
037     * instantiating and remembering the implementations of MCRSearcher used in the
038     * system.
039     * 
040     * @author Frank Lützenkirchen
041     * @version $Revision: 13085 $ $Date: 2008-02-06 18:27:24 +0100 (Mi, 06 Feb 2008) $
042     */
043    public class MCRSearcherFactory {
044        /** Hashtable SearcherID to MCRSearcher instance */
045        protected static Hashtable<String,MCRSearcher> table = new Hashtable<String,MCRSearcher>();
046    
047        /** The logger */
048        private static final Logger LOGGER = Logger.getLogger(MCRSearcherFactory.class);
049    
050        /**
051         * Returns the MCRSearcher instance that is configured for this SearcherID.
052         * The instance that is returned is configured by the property
053         * <tt>MCR.Searcher.<ID>.Class</tt> in mycore.properties.
054         * 
055         * @param searcherID
056         *            the non-null ID of the MCRSearcher implementation
057         * @return the MCRSearcher instance that uses this searcherID
058         * @throws MCRConfigurationException
059         *             if no MCRSearcher implementation is configured for this ID
060         */
061        public static MCRSearcher getSearcher(String searcherID) {
062            if (!table.containsKey(searcherID)) {
063                try {
064                    String searcherClass = "MCR.Searcher." + searcherID + ".Class";
065                    LOGGER.debug("Reading searcher implementation for ID " + searcherID + ": " + searcherClass);
066    
067                    Object obj = MCRConfiguration.instance().getSingleInstanceOf(searcherClass);
068                    MCRSearcher s = (MCRSearcher) (obj);
069                    s.init(searcherID);
070    
071                    table.put(searcherID, s);
072                } catch (Exception ex) {
073                    String msg = "Could not load MCRSearcher with searcher ID = " + searcherID;
074                    throw new MCRConfigurationException(msg, ex);
075                }
076            }
077    
078            return table.get(searcherID);
079        }
080    
081        /**
082         * Returns the MCRSearcher instance that manages the given index.
083         * 
084         * @param indexID
085         *            the ID of the search index as declared in searchfields.xml
086         * @return the MCRSearcher instance that manages this index
087         * @throws MCRConfigurationException
088         *             if no MCRSearcher implementation is configured for this index
089         */
090        public static MCRSearcher getSearcherForIndex(String indexID) {
091            String prefix = "MCR.Searcher.";
092            String suffix = ".Index";
093    
094            Properties props = MCRConfiguration.instance().getProperties(prefix);
095            Enumeration names = props.keys();
096            while (names.hasMoreElements()) {
097                String name = (String) (names.nextElement());
098                if (name.endsWith(suffix) && MCRConfiguration.instance().getString(name).equals(indexID)) {
099                    String searcherID = name.substring(prefix.length(), name.indexOf(suffix));
100                    return getSearcher(searcherID);
101                }
102            }
103    
104            String msg = "No searcher configured for search index " + indexID;
105            throw new MCRConfigurationException(msg);
106        }
107    }