PageRenderTime 54ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/client/hotrod-client/src/main/java/org/infinispan/client/hotrod/configuration/ConfigurationBuilder.java

https://github.com/an1310/infinispan
Java | 363 lines | 309 code | 43 blank | 11 comment | 22 complexity | 5336c933a4fee3f2bea0f578c53e99c0 MD5 | raw file
  1. package org.infinispan.client.hotrod.configuration;
  2. import java.lang.ref.WeakReference;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Properties;
  6. import java.util.regex.Matcher;
  7. import java.util.regex.Pattern;
  8. import org.infinispan.client.hotrod.RemoteCacheManager;
  9. import org.infinispan.client.hotrod.impl.ConfigurationProperties;
  10. import org.infinispan.client.hotrod.impl.TypedProperties;
  11. import org.infinispan.client.hotrod.impl.consistenthash.ConsistentHash;
  12. import org.infinispan.client.hotrod.impl.consistenthash.ConsistentHashV1;
  13. import org.infinispan.client.hotrod.impl.consistenthash.ConsistentHashV2;
  14. import org.infinispan.client.hotrod.impl.consistenthash.SegmentConsistentHash;
  15. import org.infinispan.client.hotrod.impl.transport.TransportFactory;
  16. import org.infinispan.client.hotrod.impl.transport.tcp.RequestBalancingStrategy;
  17. import org.infinispan.client.hotrod.impl.transport.tcp.RoundRobinBalancingStrategy;
  18. import org.infinispan.client.hotrod.impl.transport.tcp.TcpTransportFactory;
  19. import org.infinispan.client.hotrod.logging.Log;
  20. import org.infinispan.client.hotrod.logging.LogFactory;
  21. import org.infinispan.commons.configuration.Builder;
  22. import org.infinispan.commons.marshall.Marshaller;
  23. import org.infinispan.commons.marshall.jboss.GenericJBossMarshaller;
  24. import org.infinispan.commons.util.Util;
  25. /**
  26. * ConfigurationBuilder used to generate immutable {@link Configuration} objects to pass to the
  27. * {@link RemoteCacheManager#RemoteCacheManager(Configuration)} constructor.
  28. *
  29. * @author Tristan Tarrant
  30. * @since 5.3
  31. */
  32. public class ConfigurationBuilder implements ConfigurationChildBuilder, Builder<Configuration> {
  33. private static final Log log = LogFactory.getLog(ConfigurationBuilder.class, Log.class);
  34. // Match IPv4 (host:port) or IPv6 ([host]:port) addresses
  35. private static final Pattern ADDRESS_PATTERN = Pattern
  36. .compile("(\\[([0-9A-Fa-f:]+)\\]|([^:/?#]*))(?::(\\d*))?");
  37. private WeakReference<ClassLoader> classLoader;
  38. private final ExecutorFactoryConfigurationBuilder asyncExecutorFactory;
  39. private Class<? extends RequestBalancingStrategy> balancingStrategy = RoundRobinBalancingStrategy.class;
  40. private final ConnectionPoolConfigurationBuilder connectionPool;
  41. private int connectionTimeout = ConfigurationProperties.DEFAULT_CONNECT_TIMEOUT;
  42. @SuppressWarnings("unchecked")
  43. private final Class<? extends ConsistentHash> consistentHashImpl[] = new Class[] {
  44. ConsistentHashV1.class, ConsistentHashV2.class, SegmentConsistentHash.class
  45. };
  46. private boolean forceReturnValues;
  47. private int keySizeEstimate = ConfigurationProperties.DEFAULT_KEY_SIZE;
  48. private Class<? extends Marshaller> marshallerClass = GenericJBossMarshaller.class;
  49. private Marshaller marshaller;
  50. private boolean pingOnStartup = true;
  51. private String protocolVersion = ConfigurationProperties.DEFAULT_PROTOCOL_VERSION;
  52. private final List<ServerConfigurationBuilder> servers = new ArrayList<ServerConfigurationBuilder>();
  53. private int socketTimeout = ConfigurationProperties.DEFAULT_SO_TIMEOUT;
  54. private final SecurityConfigurationBuilder security;
  55. private boolean tcpNoDelay = true;
  56. private boolean tcpKeepAlive = false;
  57. private Class<? extends TransportFactory> transportFactory = TcpTransportFactory.class;
  58. private int valueSizeEstimate = ConfigurationProperties.DEFAULT_VALUE_SIZE;
  59. private int maxRetries = ConfigurationProperties.DEFAULT_MAX_RETRIES;
  60. private final NearCacheConfigurationBuilder nearCache;
  61. public ConfigurationBuilder() {
  62. this.classLoader = new WeakReference<ClassLoader>(Thread.currentThread().getContextClassLoader());
  63. this.connectionPool = new ConnectionPoolConfigurationBuilder(this);
  64. this.asyncExecutorFactory = new ExecutorFactoryConfigurationBuilder(this);
  65. this.security = new SecurityConfigurationBuilder(this);
  66. this.nearCache = new NearCacheConfigurationBuilder(this);
  67. }
  68. @Override
  69. public ServerConfigurationBuilder addServer() {
  70. ServerConfigurationBuilder builder = new ServerConfigurationBuilder(this);
  71. this.servers.add(builder);
  72. return builder;
  73. }
  74. @Override
  75. public ConfigurationBuilder addServers(String servers) {
  76. for (String server : servers.split(";")) {
  77. Matcher matcher = ADDRESS_PATTERN.matcher(server);
  78. if (matcher.matches()) {
  79. String v6host = matcher.group(2);
  80. String v4host = matcher.group(3);
  81. String host = v6host != null ? v6host : v4host;
  82. String portString = matcher.group(4);
  83. int port = portString == null
  84. ? ConfigurationProperties.DEFAULT_HOTROD_PORT
  85. : Integer.parseInt(portString);
  86. this.addServer().host(host).port(port);
  87. } else {
  88. throw log.parseErrorServerAddress(server);
  89. }
  90. }
  91. return this;
  92. }
  93. @Override
  94. public ExecutorFactoryConfigurationBuilder asyncExecutorFactory() {
  95. return this.asyncExecutorFactory;
  96. }
  97. @Override
  98. public ConfigurationBuilder balancingStrategy(String balancingStrategy) {
  99. this.balancingStrategy = Util.loadClass(balancingStrategy, this.classLoader());
  100. return this;
  101. }
  102. @Override
  103. public ConfigurationBuilder balancingStrategy(Class<? extends RequestBalancingStrategy> balancingStrategy) {
  104. this.balancingStrategy = balancingStrategy;
  105. return this;
  106. }
  107. @Override
  108. public ConfigurationBuilder classLoader(ClassLoader cl) {
  109. this.classLoader = new WeakReference<ClassLoader>(cl);
  110. return this;
  111. }
  112. ClassLoader classLoader() {
  113. return classLoader != null ? classLoader.get() : null;
  114. }
  115. @Override
  116. public ConnectionPoolConfigurationBuilder connectionPool() {
  117. return connectionPool;
  118. }
  119. @Override
  120. public ConfigurationBuilder connectionTimeout(int connectionTimeout) {
  121. this.connectionTimeout = connectionTimeout;
  122. return this;
  123. }
  124. @Override
  125. public ConfigurationBuilder consistentHashImpl(int version, Class<? extends ConsistentHash> consistentHashClass) {
  126. this.consistentHashImpl[version - 1] = consistentHashClass;
  127. return this;
  128. }
  129. @Override
  130. public ConfigurationBuilder consistentHashImpl(int version, String consistentHashClass) {
  131. this.consistentHashImpl[version - 1] = Util.loadClass(consistentHashClass, classLoader());
  132. return this;
  133. }
  134. @Override
  135. public ConfigurationBuilder forceReturnValues(boolean forceReturnValues) {
  136. this.forceReturnValues = forceReturnValues;
  137. return this;
  138. }
  139. @Override
  140. public ConfigurationBuilder keySizeEstimate(int keySizeEstimate) {
  141. this.keySizeEstimate = keySizeEstimate;
  142. return this;
  143. }
  144. @Override
  145. public ConfigurationBuilder marshaller(String marshaller) {
  146. this.marshallerClass = Util.loadClass(marshaller, this.classLoader());
  147. return this;
  148. }
  149. @Override
  150. public ConfigurationBuilder marshaller(Class<? extends Marshaller> marshaller) {
  151. this.marshallerClass = marshaller;
  152. return this;
  153. }
  154. @Override
  155. public ConfigurationBuilder marshaller(Marshaller marshaller) {
  156. this.marshaller = marshaller;
  157. return this;
  158. }
  159. public NearCacheConfigurationBuilder nearCache() {
  160. return nearCache;
  161. }
  162. @Override
  163. public ConfigurationBuilder pingOnStartup(boolean pingOnStartup) {
  164. this.pingOnStartup = pingOnStartup;
  165. return this;
  166. }
  167. @Override
  168. public ConfigurationBuilder protocolVersion(String protocolVersion) {
  169. this.protocolVersion = protocolVersion;
  170. return this;
  171. }
  172. @Override
  173. public SecurityConfigurationBuilder security() {
  174. return security;
  175. }
  176. @Override
  177. public ConfigurationBuilder socketTimeout(int socketTimeout) {
  178. this.socketTimeout = socketTimeout;
  179. return this;
  180. }
  181. /**
  182. * @deprecated Use security().ssl() instead
  183. */
  184. @Deprecated
  185. public SslConfigurationBuilder ssl() {
  186. return security.ssl();
  187. }
  188. @Override
  189. public ConfigurationBuilder tcpNoDelay(boolean tcpNoDelay) {
  190. this.tcpNoDelay = tcpNoDelay;
  191. return this;
  192. }
  193. @Override
  194. public ConfigurationBuilder tcpKeepAlive(boolean keepAlive) {
  195. this.tcpKeepAlive = keepAlive;
  196. return this;
  197. }
  198. @Override
  199. public ConfigurationBuilder transportFactory(String transportFactory) {
  200. this.transportFactory = Util.loadClass(transportFactory, this.classLoader());
  201. return this;
  202. }
  203. @Override
  204. public ConfigurationBuilder transportFactory(Class<? extends TransportFactory> transportFactory) {
  205. this.transportFactory = transportFactory;
  206. return this;
  207. }
  208. @Override
  209. public ConfigurationBuilder valueSizeEstimate(int valueSizeEstimate) {
  210. this.valueSizeEstimate = valueSizeEstimate;
  211. return this;
  212. }
  213. @Override
  214. public ConfigurationBuilder maxRetries(int maxRetries) {
  215. this.maxRetries = maxRetries;
  216. return this;
  217. }
  218. @Override
  219. public ConfigurationBuilder withProperties(Properties properties) {
  220. TypedProperties typed = TypedProperties.toTypedProperties(properties);
  221. if (typed.containsKey(ConfigurationProperties.ASYNC_EXECUTOR_FACTORY)) {
  222. this.asyncExecutorFactory().factoryClass(typed.getProperty(ConfigurationProperties.ASYNC_EXECUTOR_FACTORY));
  223. }
  224. this.asyncExecutorFactory().withExecutorProperties(typed);
  225. this.balancingStrategy(typed.getProperty(ConfigurationProperties.REQUEST_BALANCING_STRATEGY, balancingStrategy.getName()));
  226. this.connectionPool.withPoolProperties(typed);
  227. this.connectionTimeout(typed.getIntProperty(ConfigurationProperties.CONNECT_TIMEOUT, connectionTimeout));
  228. for (int i = 1; i <= consistentHashImpl.length; i++) {
  229. this.consistentHashImpl(i, typed.getProperty(ConfigurationProperties.HASH_FUNCTION_PREFIX + "." + i, consistentHashImpl[i - 1].getName()));
  230. }
  231. this.forceReturnValues(typed.getBooleanProperty(ConfigurationProperties.FORCE_RETURN_VALUES, forceReturnValues));
  232. this.keySizeEstimate(typed.getIntProperty(ConfigurationProperties.KEY_SIZE_ESTIMATE, keySizeEstimate));
  233. if (typed.containsKey(ConfigurationProperties.MARSHALLER)) {
  234. this.marshaller(typed.getProperty(ConfigurationProperties.MARSHALLER));
  235. }
  236. this.pingOnStartup(typed.getBooleanProperty(ConfigurationProperties.PING_ON_STARTUP, pingOnStartup));
  237. this.protocolVersion(typed.getProperty(ConfigurationProperties.PROTOCOL_VERSION, protocolVersion));
  238. this.servers.clear();
  239. this.addServers(typed.getProperty(ConfigurationProperties.SERVER_LIST, ""));
  240. this.socketTimeout(typed.getIntProperty(ConfigurationProperties.SO_TIMEOUT, socketTimeout));
  241. this.tcpNoDelay(typed.getBooleanProperty(ConfigurationProperties.TCP_NO_DELAY, tcpNoDelay));
  242. this.tcpKeepAlive(typed.getBooleanProperty(ConfigurationProperties.TCP_KEEP_ALIVE, tcpKeepAlive));
  243. if (typed.containsKey(ConfigurationProperties.TRANSPORT_FACTORY)) {
  244. this.transportFactory(typed.getProperty(ConfigurationProperties.TRANSPORT_FACTORY));
  245. }
  246. this.valueSizeEstimate(typed.getIntProperty(ConfigurationProperties.VALUE_SIZE_ESTIMATE, valueSizeEstimate));
  247. this.maxRetries(typed.getIntProperty(ConfigurationProperties.MAX_RETRIES, maxRetries));
  248. return this;
  249. }
  250. @Override
  251. public void validate() {
  252. connectionPool.validate();
  253. asyncExecutorFactory.validate();
  254. security.validate();
  255. nearCache.validate();
  256. if (maxRetries < 0) {
  257. throw log.invalidMaxRetries(maxRetries);
  258. }
  259. }
  260. @Override
  261. public Configuration create() {
  262. List<ServerConfiguration> servers = new ArrayList<ServerConfiguration>();
  263. if (this.servers.size() > 0)
  264. for (ServerConfigurationBuilder server : this.servers) {
  265. servers.add(server.create());
  266. }
  267. else {
  268. servers.add(new ServerConfiguration("127.0.0.1", ConfigurationProperties.DEFAULT_HOTROD_PORT));
  269. }
  270. if (marshaller == null) {
  271. return new Configuration(asyncExecutorFactory.create(), balancingStrategy, classLoader == null ? null : classLoader.get(), connectionPool.create(), connectionTimeout,
  272. consistentHashImpl, forceReturnValues, keySizeEstimate, marshallerClass, pingOnStartup, protocolVersion, servers, socketTimeout, security.create(), tcpNoDelay, tcpKeepAlive, transportFactory,
  273. valueSizeEstimate, maxRetries, nearCache.create());
  274. } else {
  275. return new Configuration(asyncExecutorFactory.create(), balancingStrategy, classLoader == null ? null : classLoader.get(), connectionPool.create(), connectionTimeout,
  276. consistentHashImpl, forceReturnValues, keySizeEstimate, marshaller, pingOnStartup, protocolVersion, servers, socketTimeout, security.create(), tcpNoDelay, tcpKeepAlive, transportFactory,
  277. valueSizeEstimate, maxRetries, nearCache.create());
  278. }
  279. }
  280. @Override
  281. public Configuration build() {
  282. return build(true);
  283. }
  284. public Configuration build(boolean validate) {
  285. if (validate) {
  286. validate();
  287. }
  288. return create();
  289. }
  290. @Override
  291. public ConfigurationBuilder read(Configuration template) {
  292. this.classLoader = new WeakReference<ClassLoader>(template.classLoader());
  293. this.asyncExecutorFactory.read(template.asyncExecutorFactory());
  294. this.balancingStrategy = template.balancingStrategy();
  295. this.connectionPool.read(template.connectionPool());
  296. this.connectionTimeout = template.connectionTimeout();
  297. for (int i = 0; i < consistentHashImpl.length; i++) {
  298. this.consistentHashImpl[i] = template.consistentHashImpl()[i];
  299. }
  300. this.forceReturnValues = template.forceReturnValues();
  301. this.keySizeEstimate = template.keySizeEstimate();
  302. this.marshaller = template.marshaller();
  303. this.marshallerClass = template.marshallerClass();
  304. this.pingOnStartup = template.pingOnStartup();
  305. this.protocolVersion = template.protocolVersion();
  306. this.servers.clear();
  307. for (ServerConfiguration server : template.servers()) {
  308. this.addServer().host(server.host()).port(server.port());
  309. }
  310. this.socketTimeout = template.socketTimeout();
  311. this.security.read(template.security());
  312. this.tcpNoDelay = template.tcpNoDelay();
  313. this.tcpKeepAlive = template.tcpKeepAlive();
  314. this.transportFactory = template.transportFactory();
  315. this.valueSizeEstimate = template.valueSizeEstimate();
  316. this.maxRetries = template.maxRetries();
  317. this.nearCache.read(template.nearCache());
  318. return this;
  319. }
  320. }