PageRenderTime 31ms CodeModel.GetById 19ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

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