PageRenderTime 60ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/components/camel-solr/src/main/java/org/apache/camel/component/solr/SolrConfiguration.java

https://github.com/apache/camel
Java | 389 lines | 246 code | 61 blank | 82 comment | 5 complexity | 4a0cf3b92f7dab5a2f8110de6e379fae MD5 | raw file
Possible License(s): Apache-2.0
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.apache.camel.component.solr;
  18. import java.util.Optional;
  19. import org.apache.camel.spi.Metadata;
  20. import org.apache.camel.spi.UriParam;
  21. import org.apache.camel.spi.UriParams;
  22. import org.apache.camel.spi.UriPath;
  23. import org.apache.http.client.HttpClient;
  24. import org.apache.solr.client.solrj.SolrClient;
  25. import org.slf4j.Logger;
  26. import org.slf4j.LoggerFactory;
  27. @UriParams
  28. public class SolrConfiguration implements Cloneable {
  29. private static final Logger LOG = LoggerFactory.getLogger(SolrConfiguration.class);
  30. private boolean useConcurrentUpdateSolrClient;
  31. private SolrEndpoint solrEndpoint;
  32. private SolrScheme solrScheme;
  33. @UriPath(description = "Hostname and port for the Solr server(s). " +
  34. "Multiple hosts can be specified, separated with a comma. " +
  35. "See the solrClient parameter for more information on the SolrClient used to connect to Solr.")
  36. @Metadata(required = true)
  37. private final String url;
  38. @UriParam(defaultValue = "" + SolrConstants.DEFUALT_STREAMING_QUEUE_SIZE)
  39. private int streamingQueueSize = SolrConstants.DEFUALT_STREAMING_QUEUE_SIZE;
  40. @UriParam(defaultValue = "" + SolrConstants.DEFAULT_STREAMING_THREAD_COUNT)
  41. private int streamingThreadCount = SolrConstants.DEFAULT_STREAMING_THREAD_COUNT;
  42. @UriParam
  43. private Integer soTimeout;
  44. @UriParam
  45. private Integer connectionTimeout;
  46. @UriParam(label = "HttpSolrClient")
  47. private Boolean followRedirects;
  48. @UriParam(label = "HttpSolrClient")
  49. private Boolean allowCompression;
  50. @UriParam(label = "CloudSolrClient")
  51. private String zkHost;
  52. @UriParam(label = "CloudSolrClient")
  53. private String zkChroot;
  54. @UriParam(label = "CloudSolrClient")
  55. private String collection;
  56. @UriParam
  57. private SolrClient solrClient;
  58. @UriParam
  59. private HttpClient httpClient;
  60. @UriParam
  61. private String requestHandler;
  62. @UriParam(label = "security", secret = true)
  63. private String username;
  64. @UriParam(label = "security", secret = true)
  65. private String password;
  66. @UriParam(defaultValue = "false")
  67. private boolean autoCommit;
  68. @Deprecated
  69. @UriParam
  70. private Integer maxRetries;
  71. @Deprecated
  72. @UriParam
  73. private Integer defaultMaxConnectionsPerHost;
  74. @Deprecated
  75. @UriParam
  76. private Integer maxTotalConnections;
  77. public SolrConfiguration(SolrScheme solrScheme, String url, String zkChroot) {
  78. this.solrScheme = solrScheme;
  79. this.url = url;
  80. this.zkChroot = zkChroot;
  81. }
  82. public static SolrConfiguration newInstance(String endpointUri, String remaining) {
  83. SolrScheme solrScheme = SolrScheme.SOLR.getFrom(endpointUri);
  84. Optional<String> zkChrootOptional = SolrClientHandler.getZkChrootFromUrl(remaining);
  85. String url = SolrClientHandler.parseHostsFromUrl(remaining, zkChrootOptional);
  86. SolrConfiguration solrConfiguration = new SolrConfiguration(solrScheme, url, zkChrootOptional.orElse(null));
  87. // validate url
  88. SolrClientHandler.getUrlListFrom(solrConfiguration);
  89. // return configuration
  90. return solrConfiguration;
  91. }
  92. public SolrScheme getSolrScheme() {
  93. return solrScheme;
  94. }
  95. public void setSolrScheme(SolrScheme solrScheme) {
  96. this.solrScheme = solrScheme;
  97. }
  98. public String getUrl() {
  99. return url;
  100. }
  101. public int getStreamingQueueSize() {
  102. return streamingQueueSize;
  103. }
  104. /**
  105. * Sets the queue size for the ConcurrentUpdateSolrClient
  106. */
  107. public void setStreamingQueueSize(int streamingQueueSize) {
  108. this.streamingQueueSize = streamingQueueSize;
  109. }
  110. public int getStreamingThreadCount() {
  111. return streamingThreadCount;
  112. }
  113. /**
  114. * Sets the number of threads for the ConcurrentUpdateSolrClient
  115. */
  116. public void setStreamingThreadCount(int streamingThreadCount) {
  117. this.streamingThreadCount = streamingThreadCount;
  118. }
  119. public Integer getMaxRetries() {
  120. return maxRetries;
  121. }
  122. /**
  123. * Maximum number of retries to attempt in the event of transient errors
  124. */
  125. public void setMaxRetries(Integer maxRetries) {
  126. this.maxRetries = maxRetries;
  127. }
  128. public Integer getSoTimeout() {
  129. return soTimeout;
  130. }
  131. /**
  132. * Sets the socket timeout on the SolrClient
  133. */
  134. public void setSoTimeout(Integer soTimeout) {
  135. this.soTimeout = soTimeout;
  136. }
  137. public Integer getConnectionTimeout() {
  138. return connectionTimeout;
  139. }
  140. /**
  141. * Sets the connection timeout on the SolrClient
  142. */
  143. public void setConnectionTimeout(Integer connectionTimeout) {
  144. this.connectionTimeout = connectionTimeout;
  145. }
  146. public Integer getDefaultMaxConnectionsPerHost() {
  147. return defaultMaxConnectionsPerHost;
  148. }
  149. /**
  150. * maxConnectionsPerHost on the underlying HttpConnectionManager
  151. */
  152. public void setDefaultMaxConnectionsPerHost(Integer defaultMaxConnectionsPerHost) {
  153. this.defaultMaxConnectionsPerHost = defaultMaxConnectionsPerHost;
  154. }
  155. public Integer getMaxTotalConnections() {
  156. return maxTotalConnections;
  157. }
  158. /**
  159. * maxTotalConnection on the underlying HttpConnectionManager
  160. */
  161. public void setMaxTotalConnections(Integer maxTotalConnections) {
  162. this.maxTotalConnections = maxTotalConnections;
  163. }
  164. public Boolean getFollowRedirects() {
  165. return followRedirects;
  166. }
  167. /**
  168. * Indicates whether redirects are used to get to the Solr server
  169. */
  170. public void setFollowRedirects(Boolean followRedirects) {
  171. this.followRedirects = followRedirects;
  172. }
  173. public Boolean getAllowCompression() {
  174. return allowCompression;
  175. }
  176. /**
  177. * Server side must support gzip or deflate for this to have any effect
  178. */
  179. public void setAllowCompression(Boolean allowCompression) {
  180. this.allowCompression = allowCompression;
  181. }
  182. public String getZkHost() {
  183. return zkHost;
  184. }
  185. /**
  186. * Set the ZooKeeper host(s) urls which the CloudSolrClient uses, e.g. "zkHost=localhost:2181,localhost:2182".
  187. * Optionally add the chroot, e.g. "zkHost=localhost:2181,localhost:2182/rootformysolr". In case the first part of
  188. * the url path (='contextroot') is set to 'solr' (e.g. 'localhost:2181/solr' or 'localhost:2181/solr/..'), then
  189. * that path is not considered as zookeeper chroot for backward compatibility reasons (this behaviour can be
  190. * overridden via zkChroot parameter).
  191. */
  192. public void setZkHost(String zkHost) {
  193. Optional<String> zkChrootFromZkHost = SolrClientHandler.getZkChrootFromUrl(zkHost);
  194. if (zkChrootFromZkHost.isPresent() && getZkChroot() == null) {
  195. setZkChroot(zkChrootFromZkHost.get());
  196. }
  197. this.zkHost = SolrClientHandler.parseHostsFromUrl(zkHost, zkChrootFromZkHost);
  198. setSolrScheme(SolrScheme.SOLRCLOUD);
  199. }
  200. public String getZkChroot() {
  201. return zkChroot;
  202. }
  203. /**
  204. * Set the chroot of the zookeeper connection (include the leading slash; e.g. '/mychroot')
  205. */
  206. public void setZkChroot(String zkChroot) {
  207. this.zkChroot = zkChroot;
  208. setSolrScheme(SolrScheme.SOLRCLOUD);
  209. }
  210. public String getCollection() {
  211. return collection;
  212. }
  213. /**
  214. * Set the default collection for SolrCloud
  215. */
  216. public void setCollection(String collection) {
  217. this.collection = collection;
  218. }
  219. public String getRequestHandler() {
  220. return requestHandler;
  221. }
  222. /**
  223. * Set the request handler to be used
  224. */
  225. public void setRequestHandler(String requestHandler) {
  226. this.requestHandler = requestHandler;
  227. }
  228. public String getUsername() {
  229. return username;
  230. }
  231. /**
  232. * Sets username for basic auth plugin enabled servers
  233. */
  234. public void setUsername(String username) {
  235. this.username = username;
  236. }
  237. public String getPassword() {
  238. return password;
  239. }
  240. /**
  241. * Sets password for basic auth plugin enabled servers
  242. */
  243. public void setPassword(String password) {
  244. this.password = password;
  245. }
  246. public boolean isAutoCommit() {
  247. return autoCommit;
  248. }
  249. /**
  250. * If true, each producer operation will be automatically followed by a commit
  251. */
  252. public void setAutoCommit(boolean autoCommit) {
  253. this.autoCommit = autoCommit;
  254. }
  255. public SolrClient getSolrClient() {
  256. return solrClient;
  257. }
  258. /**
  259. * Uses the provided solr client to connect to solr. When this parameter is not specified, camel applies the
  260. * following rules to determine the SolrClient: 1) when zkHost or zkChroot (=zookeeper root) parameter is set, then
  261. * the CloudSolrClient is used. 2) when multiple hosts are specified in the uri (separated with a comma), then the
  262. * CloudSolrClient (uri scheme is 'solrCloud') or the LBHttpSolrClient (uri scheme is not 'solrCloud') is used. 3)
  263. * when the solr operation is INSERT_STREAMING, then the ConcurrentUpdateSolrClient is used. 4) otherwise, the
  264. * HttpSolrClient is used. Note: A CloudSolrClient should point to zookeeper endpoint(s); other clients point to
  265. * Solr endpoint(s). The SolrClient can also be set via the exchange header 'CamelSolrClient'.
  266. */
  267. public void setSolrClient(SolrClient solrClient) {
  268. this.solrClient = solrClient;
  269. }
  270. public HttpClient getHttpClient() {
  271. return httpClient;
  272. }
  273. /**
  274. * Sets the http client to be used by the solrClient. This is only applicable when solrClient is not set.
  275. */
  276. public void setHttpClient(HttpClient httpClient) {
  277. this.httpClient = httpClient;
  278. }
  279. public boolean getUseConcurrentUpdateSolrClient() {
  280. return useConcurrentUpdateSolrClient;
  281. }
  282. void setUseConcurrentUpdateSolrClient(boolean useConcurrentUpdateSolrClient) {
  283. this.useConcurrentUpdateSolrClient = useConcurrentUpdateSolrClient;
  284. }
  285. public SolrEndpoint getSolrEndpoint() {
  286. return solrEndpoint;
  287. }
  288. void setSolrEndpoint(SolrEndpoint solrEndpoint) {
  289. this.solrEndpoint = solrEndpoint;
  290. }
  291. public SolrConfiguration deepCopy() {
  292. try {
  293. return (SolrConfiguration) this.clone();
  294. } catch (CloneNotSupportedException e) {
  295. LOG.error(
  296. String.format(
  297. "Could not generate new configuration based on configuration of existing endpoint %s",
  298. getSolrEndpoint()),
  299. e);
  300. }
  301. return null;
  302. }
  303. public enum SolrScheme {
  304. SOLR("solr:", "http://"),
  305. SOLRS("solrs:", "https://"),
  306. SOLRCLOUD("solrCloud:", "");
  307. private final String uri;
  308. private final String scheme;
  309. SolrScheme(String uri, String scheme) {
  310. this.uri = uri;
  311. this.scheme = scheme;
  312. }
  313. public SolrScheme getFrom(String endpointUri) {
  314. for (SolrScheme solrScheme : SolrScheme.values()) {
  315. if (endpointUri.startsWith(solrScheme.uri)) {
  316. return solrScheme;
  317. }
  318. }
  319. throw new IllegalArgumentException("Invalid endpoint uri");
  320. }
  321. public String getUri() {
  322. return uri;
  323. }
  324. public String getScheme() {
  325. return scheme;
  326. }
  327. }
  328. }