Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Factory to populate Config object from well-known locations #16386

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import com.hazelcast.client.Client;
import com.hazelcast.client.LoadBalancer;
import com.hazelcast.client.config.impl.XmlClientConfigLocator;
import com.hazelcast.client.config.impl.YamlClientConfigLocator;
import com.hazelcast.config.Config;
import com.hazelcast.config.ConfigPatternMatcher;
import com.hazelcast.config.InvalidConfigurationException;
Expand Down Expand Up @@ -48,6 +50,8 @@
import java.util.concurrent.ConcurrentMap;

import static com.hazelcast.internal.config.ConfigUtils.lookupByPattern;
import static com.hazelcast.internal.config.DeclarativeConfigUtil.SYSPROP_CLIENT_CONFIG;
import static com.hazelcast.internal.config.DeclarativeConfigUtil.validateSuffixInSystemProperty;
import static com.hazelcast.internal.util.Preconditions.checkFalse;
import static com.hazelcast.internal.util.Preconditions.isNotNull;
import static com.hazelcast.partition.strategy.StringPartitioningStrategy.getBaseName;
Expand Down Expand Up @@ -168,6 +172,45 @@ public ClientConfig(ClientConfig config) {
metricsConfig = new ClientMetricsConfig(config.metricsConfig);
}

/**
* Populates Hazelcast {@link ClientConfig} object from an external configuration file.
* <p>
* It tries to load Hazelcast Client configuration from a list of well-known locations.
* When no location contains Hazelcast Client configuration then it returns default.
* <p>
* Note that the same mechanism is used when calling
* {@link com.hazelcast.client.HazelcastClient#newHazelcastClient()}.
*
* @return ClientConfig created from a file when exists, otherwise default.
*/
public static ClientConfig load() {
validateSuffixInSystemProperty(SYSPROP_CLIENT_CONFIG);

XmlClientConfigLocator xmlConfigLocator = new XmlClientConfigLocator();
YamlClientConfigLocator yamlConfigLocator = new YamlClientConfigLocator();

ClientConfig config;
if (xmlConfigLocator.locateFromSystemProperty()) {
// 1. Try loading XML config from the configuration provided in system property
config = new XmlClientConfigBuilder(xmlConfigLocator).build();
} else if (yamlConfigLocator.locateFromSystemProperty()) {
// 2. Try loading YAML config from the configuration provided in system property
config = new YamlClientConfigBuilder(yamlConfigLocator).build();
} else if (xmlConfigLocator.locateInWorkDirOrOnClasspath()) {
// 3. Try loading XML config from the working directory or from the classpath
config = new XmlClientConfigBuilder(xmlConfigLocator).build();
} else if (yamlConfigLocator.locateInWorkDirOrOnClasspath()) {
// 4. Try loading YAML config from the working directory or from the classpath
config = new YamlClientConfigBuilder(yamlConfigLocator).build();
} else {
// 5. Loading the default XML configuration file
xmlConfigLocator.locateDefault();
config = new XmlClientConfigBuilder(xmlConfigLocator).build();
}

return config;
}

/**
* Sets the pattern matcher which is used to match item names to
* configuration objects.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,16 @@

package com.hazelcast.client.config;

import com.hazelcast.client.config.impl.XmlClientFailoverConfigLocator;
import com.hazelcast.client.config.impl.YamlClientFailoverConfigLocator;
import com.hazelcast.core.HazelcastException;

import java.util.LinkedList;
import java.util.List;

import static com.hazelcast.internal.config.DeclarativeConfigUtil.SYSPROP_CLIENT_FAILOVER_CONFIG;
import static com.hazelcast.internal.config.DeclarativeConfigUtil.validateSuffixInSystemProperty;

/**
* Config class to configure multiple client configs to be used by single client instance
* The client will try to connect them in given order.
Expand All @@ -34,6 +41,42 @@ public ClientFailoverConfig() {

}

/**
* Populates Hazelcast {@link ClientFailoverConfig} object from an external configuration file.
* <p>
* It tries to load Hazelcast Failover Client configuration from a list of well-known locations.
* When no location contains Hazelcast Failover Client configuration then it returns default.
* <p>
* Note that the same mechanism is used when calling
* {@link com.hazelcast.client.HazelcastClient#newHazelcastFailoverClient()}.
*
* @return ClientFailoverConfig created from a file when exists, otherwise default.
*/
public static ClientFailoverConfig load() {
validateSuffixInSystemProperty(SYSPROP_CLIENT_FAILOVER_CONFIG);

XmlClientFailoverConfigLocator xmlConfigLocator = new XmlClientFailoverConfigLocator();
YamlClientFailoverConfigLocator yamlConfigLocator = new YamlClientFailoverConfigLocator();

ClientFailoverConfig config;
if (xmlConfigLocator.locateFromSystemProperty()) {
// 1. Try loading XML config from the configuration provided in system property
config = new XmlClientFailoverConfigBuilder(xmlConfigLocator).build();
} else if (yamlConfigLocator.locateFromSystemProperty()) {
// 2. Try loading YAML config from the configuration provided in system property
config = new YamlClientFailoverConfigBuilder(yamlConfigLocator).build();
} else if (xmlConfigLocator.locateInWorkDirOrOnClasspath()) {
// 3. Try loading XML config from the working directory or from the classpath
config = new XmlClientFailoverConfigBuilder(xmlConfigLocator).build();
} else if (yamlConfigLocator.locateInWorkDirOrOnClasspath()) {
// 4. Try loading YAML config from the working directory or from the classpath
config = new YamlClientFailoverConfigBuilder(yamlConfigLocator).build();
} else {
throw new HazelcastException("Failed to load ClientFailoverConfig");
}
return config;
}

public ClientFailoverConfig addClientConfig(ClientConfig clientConfig) {
clientConfigs.add(clientConfig);
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,11 @@
import com.hazelcast.client.config.ClientConfig;
import com.hazelcast.client.config.ClientFailoverConfig;
import com.hazelcast.client.config.ClientNetworkConfig;
import com.hazelcast.client.config.XmlClientConfigBuilder;
import com.hazelcast.client.config.impl.XmlClientConfigLocator;
import com.hazelcast.client.config.XmlClientFailoverConfigBuilder;
import com.hazelcast.client.config.impl.XmlClientFailoverConfigLocator;
import com.hazelcast.client.config.YamlClientConfigBuilder;
import com.hazelcast.client.config.impl.YamlClientConfigLocator;
import com.hazelcast.client.config.YamlClientFailoverConfigBuilder;
import com.hazelcast.client.config.impl.YamlClientFailoverConfigLocator;
import com.hazelcast.config.InvalidConfigurationException;
import com.hazelcast.core.HazelcastException;

import java.util.List;

import static com.hazelcast.internal.config.DeclarativeConfigUtil.SYSPROP_CLIENT_CONFIG;
import static com.hazelcast.internal.config.DeclarativeConfigUtil.SYSPROP_CLIENT_FAILOVER_CONFIG;
import static com.hazelcast.internal.config.DeclarativeConfigUtil.validateSuffixInSystemProperty;

/**
* Static methods to resolve and validate multiple client configs for blue green feature
*/
Expand All @@ -55,7 +43,7 @@ private FailoverClientConfigSupport() {
* @throws InvalidConfigurationException when given config is not valid
*/
public static ClientFailoverConfig resolveClientFailoverConfig() {
return resolveClientFailoverConfig(locateAndCreateClientFailoverConfig());
return resolveClientFailoverConfig(null);
}

/**
Expand All @@ -72,7 +60,7 @@ public static ClientFailoverConfig resolveClientFailoverConfig() {
*/
public static ClientFailoverConfig resolveClientFailoverConfig(ClientFailoverConfig clientFailoverConfig) {
if (clientFailoverConfig == null) {
clientFailoverConfig = locateAndCreateClientFailoverConfig();
clientFailoverConfig = ClientFailoverConfig.load();
}
checkValidAlternative(clientFailoverConfig.getClientConfigs());
return clientFailoverConfig;
Expand All @@ -92,69 +80,7 @@ public static ClientFailoverConfig resolveClientFailoverConfig(ClientFailoverCon
*/
public static ClientConfig resolveClientConfig(ClientConfig config) {
if (config == null) {
return locateAndCreateClientConfig();
}
return config;
}

private static ClientFailoverConfig locateAndCreateClientFailoverConfig() {
ClientFailoverConfig config;

validateSuffixInSystemProperty(SYSPROP_CLIENT_FAILOVER_CONFIG);

XmlClientFailoverConfigLocator xmlConfigLocator = new XmlClientFailoverConfigLocator();
YamlClientFailoverConfigLocator yamlConfigLocator = new YamlClientFailoverConfigLocator();

if (xmlConfigLocator.locateFromSystemProperty()) {
// 1. Try loading XML config from the configuration provided in system property
config = new XmlClientFailoverConfigBuilder(xmlConfigLocator).build();

} else if (yamlConfigLocator.locateFromSystemProperty()) {
// 2. Try loading YAML config from the configuration provided in system property
config = new YamlClientFailoverConfigBuilder(yamlConfigLocator).build();

} else if (xmlConfigLocator.locateInWorkDirOrOnClasspath()) {
// 3. Try loading XML config from the working directory or from the classpath
config = new XmlClientFailoverConfigBuilder(xmlConfigLocator).build();

} else if (yamlConfigLocator.locateInWorkDirOrOnClasspath()) {
// 4. Try loading YAML config from the working directory or from the classpath
config = new YamlClientFailoverConfigBuilder(yamlConfigLocator).build();

} else {
throw new HazelcastException("Failed to load ClientFailoverConfig");
}
return config;
}

private static ClientConfig locateAndCreateClientConfig() {
ClientConfig config;

validateSuffixInSystemProperty(SYSPROP_CLIENT_CONFIG);

XmlClientConfigLocator xmlConfigLocator = new XmlClientConfigLocator();
YamlClientConfigLocator yamlConfigLocator = new YamlClientConfigLocator();

if (xmlConfigLocator.locateFromSystemProperty()) {
// 1. Try loading XML config from the configuration provided in system property
config = new XmlClientConfigBuilder(xmlConfigLocator).build();

} else if (yamlConfigLocator.locateFromSystemProperty()) {
// 2. Try loading YAML config from the configuration provided in system property
config = new YamlClientConfigBuilder(yamlConfigLocator).build();

} else if (xmlConfigLocator.locateInWorkDirOrOnClasspath()) {
// 3. Try loading XML config from the working directory or from the classpath
config = new XmlClientConfigBuilder(xmlConfigLocator).build();

} else if (yamlConfigLocator.locateInWorkDirOrOnClasspath()) {
// 4. Try loading YAML config from the working directory or from the classpath
config = new YamlClientConfigBuilder(yamlConfigLocator).build();

} else {
// 5. Loading the default XML configuration file
xmlConfigLocator.locateDefault();
config = new XmlClientConfigBuilder(xmlConfigLocator).build();
return ClientConfig.load();
}
return config;
}
Expand Down
41 changes: 41 additions & 0 deletions hazelcast/src/main/java/com/hazelcast/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
import com.hazelcast.internal.config.ServicesConfig;
import com.hazelcast.internal.config.SetConfigReadOnly;
import com.hazelcast.internal.config.TopicConfigReadOnly;
import com.hazelcast.internal.config.XmlConfigLocator;
import com.hazelcast.internal.config.YamlConfigLocator;
import com.hazelcast.internal.util.Preconditions;
import com.hazelcast.map.IMap;
import com.hazelcast.multimap.MultiMap;
Expand All @@ -64,6 +66,8 @@

import static com.hazelcast.config.NearCacheConfigAccessor.initDefaultMaxSizeForOnHeapMaps;
import static com.hazelcast.internal.config.ConfigUtils.lookupByPattern;
import static com.hazelcast.internal.config.DeclarativeConfigUtil.SYSPROP_MEMBER_CONFIG;
import static com.hazelcast.internal.config.DeclarativeConfigUtil.validateSuffixInSystemProperty;
import static com.hazelcast.internal.util.Preconditions.checkNotNull;
import static com.hazelcast.internal.util.Preconditions.isNotNull;
import static com.hazelcast.partition.strategy.StringPartitioningStrategy.getBaseName;
Expand Down Expand Up @@ -181,6 +185,43 @@ public Config(String instanceName) {
this.instanceName = instanceName;
}

/**
* Populates Hazelcast {@link Config} object from an external configuration file.
* <p>
* It tries to load Hazelcast configuration from a list of well-known locations.
* When no location contains Hazelcast configuration then it returns default.
* <p>
* Note that the same mechanism is used when calling {@link com.hazelcast.core.Hazelcast#newHazelcastInstance()}.
*
* @return Config created from a file when exists, otherwise default.
*/
public static Config load() {
validateSuffixInSystemProperty(SYSPROP_MEMBER_CONFIG);

XmlConfigLocator xmlConfigLocator = new XmlConfigLocator();
YamlConfigLocator yamlConfigLocator = new YamlConfigLocator();

Config config;
if (xmlConfigLocator.locateFromSystemProperty()) {
// 1. Try loading XML config from the configuration provided in system property
config = new XmlConfigBuilder(xmlConfigLocator).build();
} else if (yamlConfigLocator.locateFromSystemProperty()) {
// 2. Try loading YAML config from the configuration provided in system property
config = new YamlConfigBuilder(yamlConfigLocator).build();
} else if (xmlConfigLocator.locateInWorkDirOrOnClasspath()) {
// 3. Try loading XML config from the working directory or from the classpath
config = new XmlConfigBuilder(xmlConfigLocator).build();
} else if (yamlConfigLocator.locateInWorkDirOrOnClasspath()) {
// 4. Try loading YAML config from the working directory or from the classpath
config = new YamlConfigBuilder(yamlConfigLocator).build();
} else {
// 5. Loading the default XML configuration file
xmlConfigLocator.locateDefault();
config = new XmlConfigBuilder(xmlConfigLocator).build();
}
return config;
}

/**
* Returns the class-loader that will be used in serialization.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@
import com.hazelcast.config.Config;
import com.hazelcast.config.InvalidConfigurationException;
import com.hazelcast.config.XmlConfigBuilder;
import com.hazelcast.config.YamlConfigBuilder;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.internal.config.XmlConfigLocator;
import com.hazelcast.internal.config.YamlConfigLocator;
import com.hazelcast.internal.jmx.ManagementService;
import com.hazelcast.internal.util.ExceptionUtil;
import com.hazelcast.internal.util.ModularJavaUtils;
Expand All @@ -40,8 +37,6 @@
import java.util.concurrent.atomic.AtomicInteger;

import static com.hazelcast.core.LifecycleEvent.LifecycleState.STARTED;
import static com.hazelcast.internal.config.DeclarativeConfigUtil.SYSPROP_MEMBER_CONFIG;
import static com.hazelcast.internal.config.DeclarativeConfigUtil.validateSuffixInSystemProperty;
import static com.hazelcast.internal.util.EmptyStatement.ignore;
import static com.hazelcast.internal.util.Preconditions.checkHasText;
import static com.hazelcast.internal.util.SetUtil.createHashSet;
Expand Down Expand Up @@ -127,32 +122,7 @@ public static HazelcastInstance getOrCreateHazelcastInstance(Config config) {
*/
public static HazelcastInstance newHazelcastInstance(Config config) {
if (config == null) {
validateSuffixInSystemProperty(SYSPROP_MEMBER_CONFIG);

XmlConfigLocator xmlConfigLocator = new XmlConfigLocator();
YamlConfigLocator yamlConfigLocator = new YamlConfigLocator();

if (xmlConfigLocator.locateFromSystemProperty()) {
// 1. Try loading XML config from the configuration provided in system property
config = new XmlConfigBuilder(xmlConfigLocator).build();

} else if (yamlConfigLocator.locateFromSystemProperty()) {
// 2. Try loading YAML config from the configuration provided in system property
config = new YamlConfigBuilder(yamlConfigLocator).build();

} else if (xmlConfigLocator.locateInWorkDirOrOnClasspath()) {
// 3. Try loading XML config from the working directory or from the classpath
config = new XmlConfigBuilder(xmlConfigLocator).build();

} else if (yamlConfigLocator.locateInWorkDirOrOnClasspath()) {
// 4. Try loading YAML config from the working directory or from the classpath
config = new YamlConfigBuilder(yamlConfigLocator).build();

} else {
// 5. Loading the default XML configuration file
xmlConfigLocator.locateDefault();
config = new XmlConfigBuilder(xmlConfigLocator).build();
}
config = Config.load();
}

return newHazelcastInstance(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void testAllClientConfigsAreHandledInMultipleClientConfigSupport() {
"setQueryCacheConfigs", "getInstanceName", "setInstanceName", "getConnectionStrategyConfig",
"setConnectionStrategyConfig", "getUserCodeDeploymentConfig", "setUserCodeDeploymentConfig",
"getOrCreateQueryCacheConfig", "getOrNullQueryCacheConfig", "addLabel", "setLabels",
"setUserContext", "getUserContext", "setMetricsConfig",
"setUserContext", "getUserContext", "setMetricsConfig", "load",
"setBackupAckToClientEnabled", "isBackupAckToClientEnabled", "getMetricsConfig", "equals", "hashCode", "toString");
Method[] declaredMethods = ClientConfig.class.getDeclaredMethods();
for (Method method : declaredMethods) {
Expand Down
Loading