PageRenderTime 27ms CodeModel.GetById 14ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 17package com.hazelcast.client;
 18
 19import com.hazelcast.config.ConfigLoader;
 20import com.hazelcast.logging.ILogger;
 21import com.hazelcast.logging.Logger;
 22
 23import java.io.File;
 24import java.io.FileInputStream;
 25import java.io.IOException;
 26import java.io.InputStream;
 27import java.net.URL;
 28import java.util.Properties;
 29import java.util.logging.Level;
 30
 31/**
 32 * @mdogan 5/10/12
 33 */
 34public class ClientConfigBuilder {
 35
 36    private static final ILogger logger = Logger.getLogger(ClientConfigBuilder.class.getName());
 37
 38    public final static String GROUP_NAME = "hazelcast.client.group.name";
 39    public final static String GROUP_PASS = "hazelcast.client.group.pass";
 40    public final static String CONNECTION_TIMEOUT = "hazelcast.client.connection.timeout";
 41    public final static String CONNECTION_ATTEMPT_LIMIT = "hazelcast.client.connection.attempts.limit";
 42    public final static String RECONNECTION_TIMEOUT = "hazelcast.client.reconnection.timeout";
 43    public final static String RECONNECTION_ATTEMPT_LIMIT = "hazelcast.client.reconnection.attempts.limit";
 44    public final static String SHUFFLE_ADDRESSES = "hazelcast.client.shuffle.addresses";
 45    public final static String UPDATE_AUTOMATIC = "hazelcast.client.update.automatic";
 46    public final static String ADDRESSES = "hazelcast.client.addresses";
 47
 48    private final Properties props = new Properties();
 49
 50    private final ClientConfig config = new ClientConfig();
 51
 52    private String resource;
 53
 54    public ClientConfigBuilder(String resource) throws IOException {
 55        super();
 56        URL url = ConfigLoader.locateConfig(resource);
 57        if (url == null) {
 58            throw new IllegalArgumentException("Could not load " + resource);
 59        }
 60        this.resource = resource;
 61        props.load(url.openStream());
 62    }
 63
 64    public ClientConfigBuilder(File file) throws IOException {
 65        super();
 66        if (file == null) {
 67            throw new NullPointerException("File is null!");
 68        }
 69        this.resource = file.getAbsolutePath();
 70        props.load(new FileInputStream(file));
 71    }
 72
 73    public ClientConfigBuilder(URL url) throws IOException {
 74        super();
 75        if (url == null) {
 76            throw new NullPointerException("URL is null!");
 77        }
 78        this.resource = url.toExternalForm();
 79        props.load(url.openStream());
 80    }
 81
 82    public ClientConfigBuilder(InputStream in) throws IOException {
 83        super();
 84        if (in == null) {
 85            throw new NullPointerException("InputStream is null!");
 86        }
 87        props.load(in);
 88    }
 89
 90    public ClientConfig build() {
 91        logger.log(Level.INFO, "Building ClientConfig " + (resource != null ? " using " + resource : "") + ".");
 92        if (props.containsKey(GROUP_NAME)) {
 93            config.getGroupConfig().setName(props.getProperty(GROUP_NAME));
 94        }
 95        if (props.containsKey(GROUP_PASS)) {
 96            config.getGroupConfig().setPassword(props.getProperty(GROUP_PASS));
 97        }
 98        if (props.containsKey(CONNECTION_TIMEOUT)) {
 99            config.setConnectionTimeout(Integer.parseInt(props.getProperty(CONNECTION_TIMEOUT)));
100        }
101        if (props.containsKey(CONNECTION_ATTEMPT_LIMIT)) {
102            config.setInitialConnectionAttemptLimit(Integer.parseInt(props.getProperty(CONNECTION_ATTEMPT_LIMIT)));
103        }
104        if (props.containsKey(RECONNECTION_TIMEOUT)) {
105            config.setReConnectionTimeOut(Integer.parseInt(props.getProperty(RECONNECTION_TIMEOUT)));
106        }
107        if (props.containsKey(RECONNECTION_ATTEMPT_LIMIT)) {
108            config.setReconnectionAttemptLimit(Integer.parseInt(props.getProperty(RECONNECTION_ATTEMPT_LIMIT)));
109        }
110        if (props.containsKey(SHUFFLE_ADDRESSES)) {
111            config.setShuffle(Boolean.valueOf(props.getProperty(SHUFFLE_ADDRESSES)));
112        }
113        if (props.containsKey(UPDATE_AUTOMATIC)) {
114            config.setUpdateAutomatic(Boolean.valueOf(props.getProperty(UPDATE_AUTOMATIC)));
115        }
116        if (props.containsKey(ADDRESSES)) {
117            final String addressesProp = props.getProperty(ADDRESSES);
118            if (addressesProp != null) {
119                final String[] addresses = addressesProp.split("[,; ]");
120                for (String address : addresses) {
121                    address = address.trim();
122                    if (address.length() > 0) {
123                        config.addAddress(address);
124                    }
125                }
126            }
127        }
128        return config;
129    }
130
131}