| /** |
| * |
| */ |
| package org.eclipse.smila.solr; |
| |
| import java.io.IOException; |
| import java.io.InputStream; |
| |
| import org.apache.commons.logging.Log; |
| import org.apache.commons.logging.LogFactory; |
| import org.eclipse.smila.common.exceptions.ConfigurationException; |
| import org.eclipse.smila.datamodel.Any; |
| import org.eclipse.smila.datamodel.AnyMap; |
| import org.eclipse.smila.datamodel.Record; |
| import org.eclipse.smila.datamodel.ipc.IpcAnyReader; |
| import org.eclipse.smila.utils.config.ConfigUtils; |
| |
| /** |
| * @author pwissel |
| * |
| */ |
| public class SolrConfiguration { |
| |
| private static final String BUNDLE = "org.eclipse.smila.solr"; |
| |
| private static final String CONFIGURATION_PATH = "solr-configuration.json"; |
| |
| public static final String DEFAULTS = "defaults"; |
| |
| public static final String CLIENT = "client"; |
| |
| static final String CLIENT_DEFAULT = "SMILA"; |
| |
| public static final String INDEX = "index"; |
| |
| static final String INDEX_DEFAULT = "smila"; |
| |
| public static final String UNIQUE_KEY = "uniqueKey"; |
| |
| static final String UNIQUE_KEY_DEFAULT = Record.RECORD_ID; |
| |
| public static final String FETCH_FACET_FIELD_TYPE = "fetchFacetFieldType"; |
| |
| static final boolean FETCH_FACET_FIELD_TYPE_DEFAULT = false; |
| |
| public static final String PROCESS_GROUP_VALUE_NULL = "processGroupValueNull"; |
| |
| static final boolean PROCESS_GROUP_VALUE_NULL_DEFAULT = true; |
| |
| public static final String GROUP_VALUE_NULL = "groupValueNull"; |
| |
| static final String GROUP_VALUE_NULL_DEFAULT = "null"; |
| |
| public static final String CLIENTS = "clients"; |
| |
| public static final String CONNECTION = "connection"; |
| |
| public static final String IMPL = "impl"; |
| |
| public static final String INDICES = "indices"; |
| |
| public static final String SEARCH = "search"; |
| |
| private final Log _log = LogFactory.getLog(getClass()); |
| |
| private final AnyMap _configuration; |
| |
| public SolrConfiguration() { |
| _configuration = loadConfiguration(); |
| } |
| |
| public SolrConfiguration(final AnyMap configuration) { |
| _configuration = configuration; |
| } |
| |
| private AnyMap loadConfiguration() { |
| Any configuration = null; |
| try (final InputStream inputStream = ConfigUtils.getConfigStream(BUNDLE, CONFIGURATION_PATH)) { |
| configuration = new IpcAnyReader().readJsonStream(inputStream); |
| } catch (final IOException exception) { |
| final String message = |
| String.format("Cannot load Solr configuration. bundle: '%s'; configurationPath: '%s'", BUNDLE, |
| CONFIGURATION_PATH); |
| if (_log.isFatalEnabled()) { |
| _log.fatal(message, exception); |
| } |
| throw new ConfigurationException(message, exception); |
| } |
| return (AnyMap) configuration; |
| } |
| |
| public AnyMap getConfiguration() { |
| if (_configuration == null) { |
| throw new ConfigurationException("No Solr configuration available"); |
| } |
| return _configuration; |
| } |
| |
| public AnyMap getDefaults() { |
| final AnyMap defaults = getConfiguration().getMap(DEFAULTS); |
| if (defaults == null) { |
| throw new ConfigurationException("No defaults configured"); |
| } |
| return defaults; |
| } |
| |
| public String getClientDefault() { |
| String client = null; |
| try { |
| client = getDefaults().getStringValue(CLIENT); |
| } catch (final ConfigurationException exception) { |
| if (_log.isDebugEnabled()) { |
| _log.debug("Cannot get client from defaults", exception); |
| } |
| } |
| return client != null ? client : CLIENT_DEFAULT; |
| } |
| |
| public String getIndexDefault() { |
| String index = null; |
| try { |
| index = getDefaults().getStringValue(INDEX); |
| } catch (final ConfigurationException exception) { |
| if (_log.isDebugEnabled()) { |
| _log.debug("Cannot get index from defaults", exception); |
| } |
| } |
| return index != null ? index : INDEX_DEFAULT; |
| } |
| |
| public String getUniqueKeyDefault() { |
| String uniqueKey = null; |
| try { |
| uniqueKey = getDefaults().getStringValue(UNIQUE_KEY); |
| } catch (final ConfigurationException exception) { |
| if (_log.isDebugEnabled()) { |
| _log.debug("Cannot get uniqueKey from defaults", exception); |
| } |
| } |
| return uniqueKey != null ? uniqueKey : UNIQUE_KEY_DEFAULT; |
| } |
| |
| public boolean isFetchFacetFieldTypeDefault() { |
| Boolean fetchFacetFieldType = null; |
| try { |
| fetchFacetFieldType = getDefaults().getBooleanValue(FETCH_FACET_FIELD_TYPE); |
| } catch (final ConfigurationException exception) { |
| if (_log.isDebugEnabled()) { |
| _log.debug("Cannot get fetchFacetFieldType from defaults", exception); |
| } |
| } |
| return fetchFacetFieldType != null ? fetchFacetFieldType.booleanValue() : FETCH_FACET_FIELD_TYPE_DEFAULT; |
| } |
| |
| public boolean isProcessGroupValueNullDefault() { |
| Boolean processGroupValueNull = null; |
| try { |
| processGroupValueNull = getDefaults().getBooleanValue(PROCESS_GROUP_VALUE_NULL); |
| } catch (final ConfigurationException exception) { |
| if (_log.isDebugEnabled()) { |
| _log.debug("Cannot get fetchFacetFieldType from defaults", exception); |
| } |
| } |
| return processGroupValueNull != null ? processGroupValueNull.booleanValue() : PROCESS_GROUP_VALUE_NULL_DEFAULT; |
| } |
| |
| public String getGroupValueNullDefault() { |
| String groupValueNull = null; |
| try { |
| groupValueNull = getDefaults().getStringValue(GROUP_VALUE_NULL); |
| } catch (final ConfigurationException exception) { |
| if (_log.isDebugEnabled()) { |
| _log.debug("Cannot get groupValueNull from defaults", exception); |
| } |
| } |
| return groupValueNull != null ? groupValueNull : GROUP_VALUE_NULL_DEFAULT; |
| } |
| |
| public AnyMap getClient(String clientName) { |
| final AnyMap clients = getConfiguration().getMap(CLIENTS); |
| if (clients == null) { |
| throw new ConfigurationException("No remote clients configured"); |
| } |
| final AnyMap client = clients.getMap(clientName); |
| if (client == null) { |
| final String message = String.format("No client configuration found. clientName: '%s'", clientName); |
| throw new ConfigurationException(message); |
| } |
| return client; |
| } |
| |
| public AnyMap getConnection(String clientName) { |
| final AnyMap connection = getClient(clientName).getMap(CONNECTION); |
| if (connection == null) { |
| final String message = String.format("No connection found. clientName: '%s'", clientName); |
| throw new ConfigurationException(message); |
| } |
| return connection; |
| } |
| |
| public AnyMap getIndices(String clientName) { |
| final AnyMap indices = getClient(clientName).getMap(INDICES); |
| if (indices == null) { |
| final String message = String.format("No indices found. clientName: '%s'", clientName); |
| throw new ConfigurationException(message); |
| } |
| return indices; |
| } |
| |
| public AnyMap getIndex(String clientName, String indexName) { |
| final AnyMap index = getIndices(clientName).getMap(indexName); |
| if (index == null) { |
| final String message = |
| String.format("No index found. clientName: '%s'; indexName: '%s'", clientName, indexName); |
| throw new ConfigurationException(message); |
| } |
| return index; |
| } |
| |
| public String getUniqueKey(final String clientName, final String indexName, final boolean returnDefault) { |
| String uniqueKey = null; |
| try { |
| uniqueKey = getIndex(clientName, indexName).getStringValue(UNIQUE_KEY); |
| if (uniqueKey == null) { |
| final String message = |
| String.format("uniqueKey is not configured. clientName: '%s'; indexName: '%s'", clientName, indexName); |
| throw new ConfigurationException(message); |
| } |
| } catch (final ConfigurationException exception) { |
| if (returnDefault) { |
| return getUniqueKeyDefault(); |
| } |
| throw exception; |
| } |
| return uniqueKey; |
| } |
| |
| public boolean isFetchFacetFieldType(final String clientName, final String indexName, final boolean returnDefault) { |
| Boolean fetchFacetFieldType = null; |
| try { |
| fetchFacetFieldType = getIndex(clientName, indexName).getBooleanValue(FETCH_FACET_FIELD_TYPE); |
| if (fetchFacetFieldType == null) { |
| final String message = |
| String.format("fetchFacetFieldType is not configured. clientName: '%s'; indexName: '%s'"); |
| throw new ConfigurationException(message); |
| } |
| } catch (final ConfigurationException exception) { |
| if (returnDefault) { |
| return isFetchFacetFieldTypeDefault(); |
| } |
| throw exception; |
| } |
| return fetchFacetFieldType.booleanValue(); |
| } |
| |
| public boolean isProcessGroupValueNull(final String clientName, final String indexName, |
| final boolean returnDefault) { |
| Boolean processGroupValueNull = null; |
| try { |
| processGroupValueNull = getIndex(clientName, indexName).getBooleanValue(PROCESS_GROUP_VALUE_NULL); |
| if (processGroupValueNull == null) { |
| final String message = |
| String.format("processGroupValueNull is not configured. clientName: '%s'; indexName: '%s'"); |
| throw new ConfigurationException(message); |
| } |
| } catch (final ConfigurationException exception) { |
| if (returnDefault) { |
| return isProcessGroupValueNullDefault(); |
| } |
| throw exception; |
| } |
| return processGroupValueNull.booleanValue(); |
| } |
| |
| public String getGroupValueNull(final String clientName, final String indexName, boolean returnDefault) { |
| String groupValueNull = null; |
| try { |
| groupValueNull = getIndex(clientName, indexName).getStringValue(GROUP_VALUE_NULL); |
| if (groupValueNull == null) { |
| final String message = String.format("groupValueNull is not configured. clientName: '%s'; indexName: '%s'"); |
| throw new ConfigurationException(message); |
| } |
| } catch (final ConfigurationException exception) { |
| if (returnDefault) { |
| return getGroupValueNullDefault(); |
| } |
| throw exception; |
| } |
| return groupValueNull; |
| } |
| |
| public AnyMap getSearch() { |
| final AnyMap search = getConfiguration().getMap(SEARCH); |
| if (search == null) { |
| throw new ConfigurationException("No search configuration exists"); |
| } |
| return search; |
| } |
| |
| } |