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

https://bitbucket.org/gabral6_gmailcom/hazelcast · Java · 119 lines · 84 code · 20 blank · 15 comment · 9 complexity · 2ea50b7f9f7e954aaf8973cdc40fe0e2 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 java.util.HashMap;
  18. import java.util.Map;
  19. import java.util.Map.Entry;
  20. @Deprecated
  21. public final class ClientProperties {
  22. @Deprecated
  23. public static enum ClientPropertyName {
  24. GROUP_NAME("hazelcast.client.group.name", null),
  25. GROUP_PASSWORD("hazelcast.client.group.password", null),
  26. INIT_CONNECTION_ATTEMPTS_LIMIT("hazelcast.client.init.connection.attempts.limit", "5"),
  27. RECONNECTION_ATTEMPTS_LIMIT("hazelcast.client.reconnection.attempts.limit", "5"),
  28. CONNECTION_TIMEOUT("hazelcast.client.connection.timeout", "300000"),
  29. RECONNECTION_TIMEOUT("hazelcast.client.reconnection.timeout", "5000");
  30. private final String name;
  31. private final String defaultValue;
  32. private ClientPropertyName(final String name, final String defaultValue) {
  33. this.name = name;
  34. this.defaultValue = defaultValue;
  35. }
  36. public String getName() {
  37. return name;
  38. }
  39. public String getDefaultValue() {
  40. return defaultValue;
  41. }
  42. public static ClientPropertyName fromValue(final String name) {
  43. for (final ClientPropertyName clientPropertyName : ClientPropertyName.values()) {
  44. if (clientPropertyName.getName().equals(name)) {
  45. return clientPropertyName;
  46. }
  47. }
  48. throw new IllegalArgumentException("There is no client property that has name '" + name + "'");
  49. }
  50. }
  51. private final Map<ClientPropertyName, String> properties;
  52. public ClientProperties() {
  53. this.properties = new HashMap<ClientProperties.ClientPropertyName, String>();
  54. }
  55. public Map<ClientPropertyName, String> getProperties() {
  56. return this.properties;
  57. }
  58. public ClientProperties setProperties(final Map<ClientPropertyName, String> properties) {
  59. for (final Entry<ClientPropertyName, String> entry : properties.entrySet()) {
  60. setPropertyValue(entry.getKey(), entry.getValue());
  61. }
  62. return this;
  63. }
  64. public ClientProperties setPropertyValue(final String name, final String value) {
  65. return setPropertyValue(ClientPropertyName.fromValue(name), value);
  66. }
  67. public ClientProperties setPropertyValue(final ClientPropertyName name, final String value) {
  68. this.properties.put(name, value);
  69. return this;
  70. }
  71. public String getProperty(final String name) {
  72. return getProperty(ClientPropertyName.fromValue(name));
  73. }
  74. public String getProperty(final ClientPropertyName name) {
  75. String string = this.properties.get(name);
  76. if (string == null) {
  77. string = System.getProperty(name.getName());
  78. }
  79. if (string == null) {
  80. string = name.defaultValue;
  81. }
  82. if (string == null) {
  83. throw new IllegalStateException("property " + name.getName() + " is null");
  84. }
  85. return string;
  86. }
  87. public int getInteger(final ClientPropertyName name) {
  88. return Integer.parseInt(getProperty(name));
  89. }
  90. public long getLong(final ClientPropertyName name) {
  91. return Long.parseLong(getProperty(name));
  92. }
  93. public static ClientProperties createBaseClientProperties(final String groupName, final String groupPassword) {
  94. ClientProperties clientProperties = new ClientProperties();
  95. clientProperties.setPropertyValue(ClientPropertyName.GROUP_NAME, groupName);
  96. clientProperties.setPropertyValue(ClientPropertyName.GROUP_PASSWORD, groupPassword);
  97. return clientProperties;
  98. }
  99. }