/hazelcast-client/src/main/java/com/hazelcast/client/ClientConfigBuilder.java

https://bitbucket.org/gabral6_gmailcom/hazelcast · Java · 131 lines · 98 code · 15 blank · 18 comment · 22 complexity · c8200e2c7a9b9e8546daf8bb48f0d59a MD5 · raw file

  1. /*
  2. * Copyright (c) 2008-2013, Hazelcast, Inc. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.hazelcast.client;
  17. import com.hazelcast.config.ConfigLoader;
  18. import com.hazelcast.logging.ILogger;
  19. import com.hazelcast.logging.Logger;
  20. import java.io.File;
  21. import java.io.FileInputStream;
  22. import java.io.IOException;
  23. import java.io.InputStream;
  24. import java.net.URL;
  25. import java.util.Properties;
  26. import java.util.logging.Level;
  27. /**
  28. * @mdogan 5/10/12
  29. */
  30. public class ClientConfigBuilder {
  31. private static final ILogger logger = Logger.getLogger(ClientConfigBuilder.class.getName());
  32. public final static String GROUP_NAME = "hazelcast.client.group.name";
  33. public final static String GROUP_PASS = "hazelcast.client.group.pass";
  34. public final static String CONNECTION_TIMEOUT = "hazelcast.client.connection.timeout";
  35. public final static String CONNECTION_ATTEMPT_LIMIT = "hazelcast.client.connection.attempts.limit";
  36. public final static String RECONNECTION_TIMEOUT = "hazelcast.client.reconnection.timeout";
  37. public final static String RECONNECTION_ATTEMPT_LIMIT = "hazelcast.client.reconnection.attempts.limit";
  38. public final static String SHUFFLE_ADDRESSES = "hazelcast.client.shuffle.addresses";
  39. public final static String UPDATE_AUTOMATIC = "hazelcast.client.update.automatic";
  40. public final static String ADDRESSES = "hazelcast.client.addresses";
  41. private final Properties props = new Properties();
  42. private final ClientConfig config = new ClientConfig();
  43. private String resource;
  44. public ClientConfigBuilder(String resource) throws IOException {
  45. super();
  46. URL url = ConfigLoader.locateConfig(resource);
  47. if (url == null) {
  48. throw new IllegalArgumentException("Could not load " + resource);
  49. }
  50. this.resource = resource;
  51. props.load(url.openStream());
  52. }
  53. public ClientConfigBuilder(File file) throws IOException {
  54. super();
  55. if (file == null) {
  56. throw new NullPointerException("File is null!");
  57. }
  58. this.resource = file.getAbsolutePath();
  59. props.load(new FileInputStream(file));
  60. }
  61. public ClientConfigBuilder(URL url) throws IOException {
  62. super();
  63. if (url == null) {
  64. throw new NullPointerException("URL is null!");
  65. }
  66. this.resource = url.toExternalForm();
  67. props.load(url.openStream());
  68. }
  69. public ClientConfigBuilder(InputStream in) throws IOException {
  70. super();
  71. if (in == null) {
  72. throw new NullPointerException("InputStream is null!");
  73. }
  74. props.load(in);
  75. }
  76. public ClientConfig build() {
  77. logger.log(Level.INFO, "Building ClientConfig " + (resource != null ? " using " + resource : "") + ".");
  78. if (props.containsKey(GROUP_NAME)) {
  79. config.getGroupConfig().setName(props.getProperty(GROUP_NAME));
  80. }
  81. if (props.containsKey(GROUP_PASS)) {
  82. config.getGroupConfig().setPassword(props.getProperty(GROUP_PASS));
  83. }
  84. if (props.containsKey(CONNECTION_TIMEOUT)) {
  85. config.setConnectionTimeout(Integer.parseInt(props.getProperty(CONNECTION_TIMEOUT)));
  86. }
  87. if (props.containsKey(CONNECTION_ATTEMPT_LIMIT)) {
  88. config.setInitialConnectionAttemptLimit(Integer.parseInt(props.getProperty(CONNECTION_ATTEMPT_LIMIT)));
  89. }
  90. if (props.containsKey(RECONNECTION_TIMEOUT)) {
  91. config.setReConnectionTimeOut(Integer.parseInt(props.getProperty(RECONNECTION_TIMEOUT)));
  92. }
  93. if (props.containsKey(RECONNECTION_ATTEMPT_LIMIT)) {
  94. config.setReconnectionAttemptLimit(Integer.parseInt(props.getProperty(RECONNECTION_ATTEMPT_LIMIT)));
  95. }
  96. if (props.containsKey(SHUFFLE_ADDRESSES)) {
  97. config.setShuffle(Boolean.valueOf(props.getProperty(SHUFFLE_ADDRESSES)));
  98. }
  99. if (props.containsKey(UPDATE_AUTOMATIC)) {
  100. config.setUpdateAutomatic(Boolean.valueOf(props.getProperty(UPDATE_AUTOMATIC)));
  101. }
  102. if (props.containsKey(ADDRESSES)) {
  103. final String addressesProp = props.getProperty(ADDRESSES);
  104. if (addressesProp != null) {
  105. final String[] addresses = addressesProp.split("[,; ]");
  106. for (String address : addresses) {
  107. address = address.trim();
  108. if (address.length() > 0) {
  109. config.addAddress(address);
  110. }
  111. }
  112. }
  113. }
  114. return config;
  115. }
  116. }