001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.search.facet.util;
016    
017    import com.liferay.portal.kernel.search.SearchContext;
018    import com.liferay.portal.kernel.search.facet.Facet;
019    import com.liferay.portal.kernel.search.facet.config.FacetConfiguration;
020    
021    import java.lang.reflect.Constructor;
022    
023    import java.util.Map;
024    import java.util.concurrent.ConcurrentHashMap;
025    
026    /**
027     * @author Raymond Aug??
028     */
029    public class FacetFactoryUtil {
030    
031            public static Facet create(
032                            SearchContext searchContext, FacetConfiguration facetConfiguration)
033                    throws Exception {
034    
035                    String className = facetConfiguration.getClassName();
036    
037                    Constructor<?> constructor = _constructorCache.get(className);
038    
039                    if (constructor == null) {
040                            constructor = Class.forName(className).getConstructor(
041                                    SearchContext.class);
042    
043                            _constructorCache.put(className, constructor);
044                    }
045    
046                    Facet facet = (Facet)constructor.newInstance(searchContext);
047    
048                    facet.setFacetConfiguration(facetConfiguration);
049    
050                    return facet;
051            }
052    
053            private static Map<String, Constructor<?>> _constructorCache =
054                    new ConcurrentHashMap<String, Constructor<?>>();
055    
056    }