All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.alachisoft.ncache.client.CacheConnectionOptions Maven / Gradle / Ivy

package com.alachisoft.ncache.client;

import com.alachisoft.ncache.client.internal.caching.CacheHelper;
import com.alachisoft.ncache.client.internal.util.ClientConfiguration;
import com.alachisoft.ncache.client.internal.util.ConnectionStrings;
import com.alachisoft.ncache.runtime.exceptions.ConfigurationException;
import com.alachisoft.ncache.runtime.util.TimeSpan;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static tangible.DotNetToJavaStringHelper.isNullOrEmpty;

public class CacheConnectionOptions {




    //region Fields
    private Boolean _loadBalance = true;
    private Boolean _enableClientLogs = false;
    private int _port = 9800;
    private Integer _connectionRetries = 5;
    public TimeSpan _clientRequestTimeout = TimeSpan.FromSeconds(90);
    private TimeSpan _retryInterval = TimeSpan.FromSeconds(1);  //1 sec
    private TimeSpan _connectionTimeout = TimeSpan.FromSeconds(5);  //5 sec
    private TimeSpan _retryConnectionDelay = TimeSpan.FromMinutes(10);  //600 sec or 10 mins
    private String _appName;
    private String _clientBindIP = "";
    private LogLevel _logLevel = LogLevel.Info;
    private IsolationLevel _mode = IsolationLevel.Default;
    private List _serverList = new ArrayList();
    private HashMap _dirtyFlags = new HashMap();

    private String serverName;
    //endregion

    //region Public Properties
    public List getServerList() {
        return _serverList;
    }

    public void setServerList(List value) {
        if (value == null) {
            return;
        }
        if (_serverList.size() > 0) {
            _serverList.clear();
        }

        for (ServerInfo temp : value) {
            if (!_serverList.contains(temp)) {
                _serverList.add(temp);
            }
        }
        _dirtyFlags.put(ConnectionStrings.SERVERLIST, true);
    }

    public IsolationLevel  getIsolationMode() { return _mode; }

    public void setIsolationLevel(IsolationLevel value) {
        _mode = value;
    }

    public String getClientBindIP() {
        return _clientBindIP;
    }

    public void setClientBindIP(String value) {
        if (!isNullOrEmpty(value)) {
            _dirtyFlags.put(ConnectionStrings.BINDIP, true);
            _clientBindIP = value;
        }
    }

    public String getAppName() {
        return _appName;
    }

    public void setAppName(String value) {
        _appName = value;
    }

    public Boolean getLoadBalance() {
        return _loadBalance;
    }

    public void setLoadBalance(Boolean value) {
        _dirtyFlags.put(ConnectionStrings.LOADBALANCE, true);
        _loadBalance = value;
    }

    public TimeSpan getClientRequestTimeOut() {
        return _clientRequestTimeout;
    }

    public void setClientRequestTimeOut(TimeSpan value) {
        if (value.compareTo(TimeSpan.Zero) >= 0) {
            _dirtyFlags.put(ConnectionStrings.CLIENTREQUESTOPTIME, true);
            _clientRequestTimeout = value;
        }
    }

    public TimeSpan getConnectionTimeout() {
        return _connectionTimeout;
    }

    public void setConnectionTimeout(TimeSpan value) {
        if (value.compareTo(TimeSpan.Zero) >= 0) {
            _dirtyFlags.put(ConnectionStrings.CONNECTIONTIMEOUT, true);
            _connectionTimeout = value;
        }
    }

    public Integer getConnectionRetries() {
        return _connectionRetries;
    }

    public void setConnectionRetries(Integer value) {
        if (value > 0) {
            _dirtyFlags.put(ConnectionStrings.CONNECTIONRETRIES, true);
            _connectionRetries = value;
        }
    }

    public TimeSpan getRetryInterval() {
        return _retryInterval;
    }

    public void setRetryInterval(TimeSpan value) {
        _dirtyFlags.put(ConnectionStrings.RETRYINTERVAL, true);
        _retryInterval = value;
    }

    public TimeSpan getRetryConnectionDelay() {
        return _retryConnectionDelay;
    }

    public void setRetryConnectionDelay(TimeSpan value) {
        if (value.compareTo(TimeSpan.Zero) >= 0) {
            _dirtyFlags.put(ConnectionStrings.RETRYCONNECTIONDELAY, true);
            _retryConnectionDelay = value;
        }
    }

    public boolean IsSet(String paramId) {
        if (_dirtyFlags == null || _dirtyFlags.isEmpty()) {
            return false;
        }

        if (_dirtyFlags.containsKey(paramId)) {
            return (boolean) (_dirtyFlags.get(paramId));
        }

        return false;
    }

    public LogLevel getLogLevel() {
        return _logLevel;
    }

    public void setLogLevel(LogLevel value) {
        _logLevel = value;
        _dirtyFlags.put(ConnectionStrings.LOGLEVEL, true);
    }

    //endregion

    //region Internal Properties

    public Boolean getEnableClientLogs() {
        return _enableClientLogs;
    }

    public void setEnableClientLogs(Boolean value) {
        _enableClientLogs = value;
        _dirtyFlags.put(ConnectionStrings.ENABLECLIENTLOGS, true);
    }

    public Integer getPort() {
        if (_serverList.size() > 0) {
            return _serverList.get(0).getPort();
        }
        return _port;
    }

    void setPort(Integer value) {
        _dirtyFlags.put(ConnectionStrings.PORT, true);
        _port = value;
    }

    public String getServerName() {
        return serverName;
    }
    //endregion

    //region Clonable
    public Object clone() {
        CacheConnectionOptions _cloneParam = new CacheConnectionOptions();
        synchronized (this) {
            _cloneParam.setAppName(getAppName());
            _cloneParam.setClientBindIP(getClientBindIP());
            _cloneParam.setClientRequestTimeOut(getClientRequestTimeOut());
            _cloneParam.setConnectionRetries(getConnectionRetries());
            _cloneParam.setConnectionTimeout(getConnectionTimeout());
            _cloneParam.setLoadBalance(getLoadBalance());
            _cloneParam.setIsolationLevel(getIsolationMode());
            _cloneParam.setPort(getPort());
            _cloneParam.setRetryConnectionDelay(getRetryConnectionDelay());
            _cloneParam.setRetryInterval(getRetryInterval());
            _cloneParam.setServerList(getServerList());
            _cloneParam.serverName = getServerName();
            _cloneParam.setEnableClientLogs(getEnableClientLogs());
            _cloneParam.setLogLevel(getLogLevel());
        }
        return _cloneParam;
    }

    //endregion

    //region Internal Methods

    void initialize(String cacheId) throws ConfigurationException {
        boolean useDefault = false;
        ClientConfiguration config = new ClientConfiguration(cacheId);
        int retries = 3;
        while (true) {
            try {
                config.loadConfiguration();
                break;
            } catch (Exception ie) {
                if (--retries == 0) {
                    useDefault = true;
                    break;
                }
                try {
                    Thread.sleep(500);
                } catch (InterruptedException ignored) {
                }
            }
        }

        if (!useDefault) {
            if (!IsSet(ConnectionStrings.CLIENTREQUESTOPTIME)) {
                _clientRequestTimeout = TimeSpan.FromMilliseconds(config.getClientRequestTimeout());
            }
            if (!IsSet(ConnectionStrings.CONNECTIONTIMEOUT)) {
                this._connectionTimeout = TimeSpan.FromMilliseconds(config.getConnectionTimeout());
            }
            if (!IsSet(ConnectionStrings.BINDIP)) {
                this._clientBindIP = config.getBindIP();
            }
            if (!IsSet(ConnectionStrings.PORT)) {
                this._port = config.getServerPort();
            }
            if (!IsSet(ConnectionStrings.ENABLECLIENTLOGS)) {
                this._enableClientLogs = config.getEnableClientLogs();
            }
            if (!IsSet(ConnectionStrings.LOGLEVEL)) {
                this._logLevel = config.getLogLevels();
            }
            if (!IsSet(ConnectionStrings.LOADBALANCE)) {
                this._loadBalance = config.getBalanceNodes();
            }
        }
    }

    //endregion

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy