Polish Hazelcast auto-configuration
Extract a HazelcastInstanceFactory class and cleanup some formatting. See gh-2942
This commit is contained in:
		
							parent
							
								
									138d66706a
								
							
						
					
					
						commit
						db41fb16c0
					
				| 
						 | 
				
			
			@ -20,9 +20,9 @@ import net.sf.ehcache.Cache;
 | 
			
		|||
import net.sf.ehcache.CacheManager;
 | 
			
		||||
 | 
			
		||||
import org.springframework.beans.factory.annotation.Autowired;
 | 
			
		||||
import org.springframework.boot.autoconfigure.condition.ResourceCondition;
 | 
			
		||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
 | 
			
		||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
 | 
			
		||||
import org.springframework.boot.autoconfigure.condition.ResourceCondition;
 | 
			
		||||
import org.springframework.cache.ehcache.EhCacheCacheManager;
 | 
			
		||||
import org.springframework.cache.ehcache.EhCacheManagerUtils;
 | 
			
		||||
import org.springframework.context.annotation.Bean;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -19,10 +19,6 @@ package org.springframework.boot.autoconfigure.cache;
 | 
			
		|||
import java.io.Closeable;
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
 | 
			
		||||
import com.hazelcast.core.Hazelcast;
 | 
			
		||||
import com.hazelcast.core.HazelcastInstance;
 | 
			
		||||
import com.hazelcast.spring.cache.HazelcastCacheManager;
 | 
			
		||||
 | 
			
		||||
import org.springframework.beans.factory.annotation.Autowired;
 | 
			
		||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
 | 
			
		||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
 | 
			
		||||
| 
						 | 
				
			
			@ -30,26 +26,31 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
 | 
			
		|||
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
 | 
			
		||||
import org.springframework.boot.autoconfigure.hazelcast.HazelcastAutoConfiguration;
 | 
			
		||||
import org.springframework.boot.autoconfigure.hazelcast.HazelcastConfigResourceCondition;
 | 
			
		||||
import org.springframework.boot.autoconfigure.hazelcast.HazelcastInstanceFactory;
 | 
			
		||||
import org.springframework.cache.CacheManager;
 | 
			
		||||
import org.springframework.context.annotation.Bean;
 | 
			
		||||
import org.springframework.context.annotation.Conditional;
 | 
			
		||||
import org.springframework.context.annotation.Configuration;
 | 
			
		||||
import org.springframework.core.io.Resource;
 | 
			
		||||
 | 
			
		||||
import com.hazelcast.core.Hazelcast;
 | 
			
		||||
import com.hazelcast.core.HazelcastInstance;
 | 
			
		||||
import com.hazelcast.spring.cache.HazelcastCacheManager;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Hazelcast cache configuration. Can either reuse the {@link HazelcastInstance} that
 | 
			
		||||
 * has been configured by the general {@link HazelcastAutoConfiguration} or create
 | 
			
		||||
 * a separate one if the {@code spring.cache.hazelcast.config} property has been set.
 | 
			
		||||
 * Hazelcast cache configuration. Can either reuse the {@link HazelcastInstance} that has
 | 
			
		||||
 * been configured by the general {@link HazelcastAutoConfiguration} or create a separate
 | 
			
		||||
 * one if the {@code spring.cache.hazelcast.config} property has been set.
 | 
			
		||||
 * <p>
 | 
			
		||||
 * If the {@link HazelcastAutoConfiguration} has been disabled, an attempt to configure
 | 
			
		||||
 * a default {@link HazelcastInstance} is still made, using the same defaults.
 | 
			
		||||
 * If the {@link HazelcastAutoConfiguration} has been disabled, an attempt to configure a
 | 
			
		||||
 * default {@link HazelcastInstance} is still made, using the same defaults.
 | 
			
		||||
 *
 | 
			
		||||
 * @author Stephane Nicoll
 | 
			
		||||
 * @since 1.3.0
 | 
			
		||||
 * @see HazelcastConfigResourceCondition
 | 
			
		||||
 */
 | 
			
		||||
@Configuration
 | 
			
		||||
@ConditionalOnClass({HazelcastInstance.class, HazelcastCacheManager.class})
 | 
			
		||||
@ConditionalOnClass({ HazelcastInstance.class, HazelcastCacheManager.class })
 | 
			
		||||
@ConditionalOnMissingBean(CacheManager.class)
 | 
			
		||||
@Conditional(CacheCondition.class)
 | 
			
		||||
@AutoConfigureAfter(HazelcastAutoConfiguration.class)
 | 
			
		||||
| 
						 | 
				
			
			@ -63,20 +64,17 @@ class HazelcastCacheConfiguration {
 | 
			
		|||
		private CacheProperties cacheProperties;
 | 
			
		||||
 | 
			
		||||
		@Bean
 | 
			
		||||
		public HazelcastCacheManager cacheManager(HazelcastInstance existingHazelcastInstance)
 | 
			
		||||
				throws IOException {
 | 
			
		||||
			Resource location = this.cacheProperties
 | 
			
		||||
					.resolveConfigLocation(this.cacheProperties.getHazelcast().getConfig());
 | 
			
		||||
		public HazelcastCacheManager cacheManager(
 | 
			
		||||
				HazelcastInstance existingHazelcastInstance) throws IOException {
 | 
			
		||||
			Resource config = this.cacheProperties.getHazelcast().getConfig();
 | 
			
		||||
			Resource location = this.cacheProperties.resolveConfigLocation(config);
 | 
			
		||||
			if (location != null) {
 | 
			
		||||
				HazelcastInstance cacheHazelcastInstance =
 | 
			
		||||
						HazelcastAutoConfiguration.createHazelcastInstance(location);
 | 
			
		||||
				HazelcastInstance cacheHazelcastInstance = new HazelcastInstanceFactory(
 | 
			
		||||
						location).getHazelcastInstance();
 | 
			
		||||
				return new CloseableHazelcastCacheManager(cacheHazelcastInstance);
 | 
			
		||||
			}
 | 
			
		||||
			else {
 | 
			
		||||
			return new HazelcastCacheManager(existingHazelcastInstance);
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Configuration
 | 
			
		||||
| 
						 | 
				
			
			@ -89,10 +87,10 @@ class HazelcastCacheConfiguration {
 | 
			
		|||
 | 
			
		||||
		@Bean
 | 
			
		||||
		public HazelcastInstance hazelcastInstance() throws IOException {
 | 
			
		||||
			Resource location = this.cacheProperties
 | 
			
		||||
					.resolveConfigLocation(this.cacheProperties.getHazelcast().getConfig());
 | 
			
		||||
			Resource config = this.cacheProperties.getHazelcast().getConfig();
 | 
			
		||||
			Resource location = this.cacheProperties.resolveConfigLocation(config);
 | 
			
		||||
			if (location != null) {
 | 
			
		||||
				HazelcastAutoConfiguration.createHazelcastInstance(location);
 | 
			
		||||
				new HazelcastInstanceFactory(location).getHazelcastInstance();
 | 
			
		||||
			}
 | 
			
		||||
			return Hazelcast.newHazelcastInstance();
 | 
			
		||||
		}
 | 
			
		||||
| 
						 | 
				
			
			@ -116,7 +114,9 @@ class HazelcastCacheConfiguration {
 | 
			
		|||
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	private static class CloseableHazelcastCacheManager extends HazelcastCacheManager implements Closeable {
 | 
			
		||||
	private static class CloseableHazelcastCacheManager extends HazelcastCacheManager
 | 
			
		||||
			implements Closeable {
 | 
			
		||||
 | 
			
		||||
		private final HazelcastInstance hazelcastInstance;
 | 
			
		||||
 | 
			
		||||
		public CloseableHazelcastCacheManager(HazelcastInstance hazelcastInstance) {
 | 
			
		||||
| 
						 | 
				
			
			@ -128,6 +128,7 @@ class HazelcastCacheConfiguration {
 | 
			
		|||
		public void close() throws IOException {
 | 
			
		||||
			this.hazelcastInstance.shutdown();
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -60,8 +60,8 @@ public abstract class ResourceCondition extends SpringBootCondition {
 | 
			
		|||
			AnnotatedTypeMetadata metadata) {
 | 
			
		||||
		RelaxedPropertyResolver resolver = new RelaxedPropertyResolver(
 | 
			
		||||
				context.getEnvironment(), this.prefix);
 | 
			
		||||
		if (resolver.containsProperty(propertyName)) {
 | 
			
		||||
			return ConditionOutcome.match("A '" + this.prefix + propertyName +"' "
 | 
			
		||||
		if (resolver.containsProperty(this.propertyName)) {
 | 
			
		||||
			return ConditionOutcome.match("A '" + this.prefix + this.propertyName + "' "
 | 
			
		||||
					+ "property is specified");
 | 
			
		||||
		}
 | 
			
		||||
		return getResourceOutcome(context, metadata);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -17,12 +17,6 @@
 | 
			
		|||
package org.springframework.boot.autoconfigure.hazelcast;
 | 
			
		||||
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.net.URL;
 | 
			
		||||
 | 
			
		||||
import com.hazelcast.config.Config;
 | 
			
		||||
import com.hazelcast.config.XmlConfigBuilder;
 | 
			
		||||
import com.hazelcast.core.Hazelcast;
 | 
			
		||||
import com.hazelcast.core.HazelcastInstance;
 | 
			
		||||
 | 
			
		||||
import org.springframework.beans.factory.annotation.Autowired;
 | 
			
		||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
 | 
			
		||||
| 
						 | 
				
			
			@ -34,9 +28,10 @@ import org.springframework.context.annotation.Bean;
 | 
			
		|||
import org.springframework.context.annotation.Conditional;
 | 
			
		||||
import org.springframework.context.annotation.Configuration;
 | 
			
		||||
import org.springframework.core.io.Resource;
 | 
			
		||||
import org.springframework.util.Assert;
 | 
			
		||||
import org.springframework.util.ResourceUtils;
 | 
			
		||||
import org.springframework.util.StringUtils;
 | 
			
		||||
 | 
			
		||||
import com.hazelcast.config.Config;
 | 
			
		||||
import com.hazelcast.core.Hazelcast;
 | 
			
		||||
import com.hazelcast.core.HazelcastInstance;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * {@link EnableAutoConfiguration Auto-configuration} for Hazelcast. Creates a
 | 
			
		||||
| 
						 | 
				
			
			@ -53,37 +48,8 @@ import org.springframework.util.StringUtils;
 | 
			
		|||
@EnableConfigurationProperties(HazelcastProperties.class)
 | 
			
		||||
public class HazelcastAutoConfiguration {
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Create a {@link HazelcastInstance} based on the specified configuration location.
 | 
			
		||||
	 * @param location the location of the configuration file
 | 
			
		||||
	 * @return a {@link HazelcastInstance} for the specified configuration
 | 
			
		||||
	 * @throws IOException the configuration file could not be read
 | 
			
		||||
	 */
 | 
			
		||||
	public static HazelcastInstance createHazelcastInstance(Resource location)
 | 
			
		||||
			throws IOException {
 | 
			
		||||
		Assert.notNull(location, "Config must not be null");
 | 
			
		||||
		URL configUrl = location.getURL();
 | 
			
		||||
		Config config = new XmlConfigBuilder(configUrl).build();
 | 
			
		||||
		if (ResourceUtils.isFileURL(configUrl)) {
 | 
			
		||||
			config.setConfigurationFile(location.getFile());
 | 
			
		||||
		}
 | 
			
		||||
		else {
 | 
			
		||||
			config.setConfigurationUrl(configUrl);
 | 
			
		||||
		}
 | 
			
		||||
		return createHazelcastInstance(config);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	private static HazelcastInstance createHazelcastInstance(Config config) {
 | 
			
		||||
		if (StringUtils.hasText(config.getInstanceName())) {
 | 
			
		||||
			return Hazelcast.getOrCreateHazelcastInstance(config);
 | 
			
		||||
		}
 | 
			
		||||
		return Hazelcast.newHazelcastInstance(config);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	@Configuration
 | 
			
		||||
	@ConditionalOnMissingBean({HazelcastInstance.class, Config.class})
 | 
			
		||||
	@ConditionalOnMissingBean({ HazelcastInstance.class, Config.class })
 | 
			
		||||
	@Conditional(ConfigAvailableCondition.class)
 | 
			
		||||
	static class HazelcastConfigFileConfiguration {
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -95,7 +61,7 @@ public class HazelcastAutoConfiguration {
 | 
			
		|||
		public HazelcastInstance hazelcastInstance() throws IOException {
 | 
			
		||||
			Resource config = this.hazelcastProperties.resolveConfigLocation();
 | 
			
		||||
			if (config != null) {
 | 
			
		||||
				return createHazelcastInstance(config);
 | 
			
		||||
				return new HazelcastInstanceFactory(config).getHazelcastInstance();
 | 
			
		||||
			}
 | 
			
		||||
			return Hazelcast.newHazelcastInstance();
 | 
			
		||||
		}
 | 
			
		||||
| 
						 | 
				
			
			@ -109,7 +75,7 @@ public class HazelcastAutoConfiguration {
 | 
			
		|||
 | 
			
		||||
		@Bean
 | 
			
		||||
		public HazelcastInstance hazelcastInstance(Config config) {
 | 
			
		||||
			return createHazelcastInstance(config);
 | 
			
		||||
			return new HazelcastInstanceFactory(config).getHazelcastInstance();
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
	}
 | 
			
		||||
| 
						 | 
				
			
			@ -123,6 +89,7 @@ public class HazelcastAutoConfiguration {
 | 
			
		|||
		public ConfigAvailableCondition() {
 | 
			
		||||
			super("spring.hazelcast", "config");
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -23,9 +23,9 @@ import org.springframework.context.annotation.ConditionContext;
 | 
			
		|||
import org.springframework.core.type.AnnotatedTypeMetadata;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * {@link SpringBootCondition} used to check if the Hazelcast configuration is
 | 
			
		||||
 * available. This either kicks in if a default configuration has been found or
 | 
			
		||||
 * if configurable property referring to the resource to use has been set.
 | 
			
		||||
 * {@link SpringBootCondition} used to check if the Hazelcast configuration is available.
 | 
			
		||||
 * This either kicks in if a default configuration has been found or if configurable
 | 
			
		||||
 * property referring to the resource to use has been set.
 | 
			
		||||
 *
 | 
			
		||||
 * @author Stephane Nicoll
 | 
			
		||||
 * @since 1.3.0
 | 
			
		||||
| 
						 | 
				
			
			@ -43,8 +43,8 @@ public abstract class HazelcastConfigResourceCondition extends ResourceCondition
 | 
			
		|||
	protected ConditionOutcome getResourceOutcome(ConditionContext context,
 | 
			
		||||
			AnnotatedTypeMetadata metadata) {
 | 
			
		||||
		if (System.getProperty(CONFIG_SYSTEM_PROPERTY) != null) {
 | 
			
		||||
			return ConditionOutcome.match("System property '"
 | 
			
		||||
					+ CONFIG_SYSTEM_PROPERTY + "' is set.");
 | 
			
		||||
			return ConditionOutcome.match("System property '" + CONFIG_SYSTEM_PROPERTY
 | 
			
		||||
					+ "' is set.");
 | 
			
		||||
		}
 | 
			
		||||
		return super.getResourceOutcome(context, metadata);
 | 
			
		||||
	}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -0,0 +1,85 @@
 | 
			
		|||
/*
 | 
			
		||||
 * Copyright 2012-2015 the original author or authors.
 | 
			
		||||
 *
 | 
			
		||||
 * Licensed under the Apache License, Version 2.0 (the "License");
 | 
			
		||||
 * you may not use this file except in compliance with the License.
 | 
			
		||||
 * You may obtain a copy of the License at
 | 
			
		||||
 *
 | 
			
		||||
 *      http://www.apache.org/licenses/LICENSE-2.0
 | 
			
		||||
 *
 | 
			
		||||
 * Unless required by applicable law or agreed to in writing, software
 | 
			
		||||
 * distributed under the License is distributed on an "AS IS" BASIS,
 | 
			
		||||
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
			
		||||
 * See the License for the specific language governing permissions and
 | 
			
		||||
 * limitations under the License.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
package org.springframework.boot.autoconfigure.hazelcast;
 | 
			
		||||
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.net.URL;
 | 
			
		||||
 | 
			
		||||
import org.springframework.core.io.Resource;
 | 
			
		||||
import org.springframework.util.Assert;
 | 
			
		||||
import org.springframework.util.ResourceUtils;
 | 
			
		||||
import org.springframework.util.StringUtils;
 | 
			
		||||
 | 
			
		||||
import com.hazelcast.config.Config;
 | 
			
		||||
import com.hazelcast.config.XmlConfigBuilder;
 | 
			
		||||
import com.hazelcast.core.Hazelcast;
 | 
			
		||||
import com.hazelcast.core.HazelcastInstance;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Factory that can be used to create a {@link HazelcastInstance}.
 | 
			
		||||
 *
 | 
			
		||||
 * @author Stephane Nicoll
 | 
			
		||||
 * @author Phillip Webb
 | 
			
		||||
 * @since 1.3.0
 | 
			
		||||
 */
 | 
			
		||||
public class HazelcastInstanceFactory {
 | 
			
		||||
 | 
			
		||||
	private Config config;
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Create a {@link HazelcastInstanceFactory} for the specified configuration location.
 | 
			
		||||
	 * @param configLocation the location of the configuration file
 | 
			
		||||
	 * @throws IOException if the configuration location could not be read
 | 
			
		||||
	 */
 | 
			
		||||
	public HazelcastInstanceFactory(Resource configLocation) throws IOException {
 | 
			
		||||
		Assert.notNull(configLocation, "ConfigLocation must not be null");
 | 
			
		||||
		this.config = getConfig(configLocation);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Create a {@link HazelcastInstanceFactory} for the specified configuration.
 | 
			
		||||
	 * @param config the configuration
 | 
			
		||||
	 */
 | 
			
		||||
	public HazelcastInstanceFactory(Config config) {
 | 
			
		||||
		Assert.notNull(config, "Config must not be null");
 | 
			
		||||
		this.config = config;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	private Config getConfig(Resource configLocation) throws IOException {
 | 
			
		||||
		URL configUrl = configLocation.getURL();
 | 
			
		||||
		Config config = new XmlConfigBuilder(configUrl).build();
 | 
			
		||||
		if (ResourceUtils.isFileURL(configUrl)) {
 | 
			
		||||
			config.setConfigurationFile(configLocation.getFile());
 | 
			
		||||
		}
 | 
			
		||||
		else {
 | 
			
		||||
			config.setConfigurationUrl(configUrl);
 | 
			
		||||
		}
 | 
			
		||||
		return config;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Get the {@link HazelcastInstance}.
 | 
			
		||||
	 * @return the {@link HazelcastInstance}
 | 
			
		||||
	 */
 | 
			
		||||
	public HazelcastInstance getHazelcastInstance() {
 | 
			
		||||
		if (StringUtils.hasText(this.config.getInstanceName())) {
 | 
			
		||||
			return Hazelcast.getOrCreateHazelcastInstance(this.config);
 | 
			
		||||
		}
 | 
			
		||||
		return Hazelcast.newHazelcastInstance(this.config);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -49,12 +49,12 @@ public class HazelcastProperties {
 | 
			
		|||
	 * location
 | 
			
		||||
	 */
 | 
			
		||||
	public Resource resolveConfigLocation() {
 | 
			
		||||
		if (this.config != null) {
 | 
			
		||||
		if (this.config == null) {
 | 
			
		||||
			return null;
 | 
			
		||||
		}
 | 
			
		||||
		Assert.isTrue(this.config.exists(), "Hazelcast configuration does not exist '"
 | 
			
		||||
				+ this.config.getDescription() + "'");
 | 
			
		||||
		return this.config;
 | 
			
		||||
	}
 | 
			
		||||
		return null;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -375,13 +375,12 @@ public class CacheAutoConfigurationTests {
 | 
			
		|||
		Collection<Class<?>> configs = new ArrayList<Class<?>>();
 | 
			
		||||
		configs.add(DefaultCacheConfiguration.class);
 | 
			
		||||
		configs.add(HazelcastAutoConfiguration.class);
 | 
			
		||||
		String mainConfig =
 | 
			
		||||
				"org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.xml";
 | 
			
		||||
		doLoad(configs, "spring.cache.type=hazelcast",
 | 
			
		||||
				"spring.hazelcast.config=" + mainConfig);
 | 
			
		||||
 | 
			
		||||
		String mainConfig = "org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.xml";
 | 
			
		||||
		doLoad(configs, "spring.cache.type=hazelcast", "spring.hazelcast.config="
 | 
			
		||||
				+ mainConfig);
 | 
			
		||||
		HazelcastCacheManager cacheManager = validateCacheManager(HazelcastCacheManager.class);
 | 
			
		||||
		HazelcastInstance hazelcastInstance = this.context.getBean(HazelcastInstance.class);
 | 
			
		||||
		HazelcastInstance hazelcastInstance = this.context
 | 
			
		||||
				.getBean(HazelcastInstance.class);
 | 
			
		||||
		assertThat(
 | 
			
		||||
				new DirectFieldAccessor(cacheManager)
 | 
			
		||||
						.getPropertyValue("hazelcastInstance"),
 | 
			
		||||
| 
						 | 
				
			
			@ -398,17 +397,14 @@ public class CacheAutoConfigurationTests {
 | 
			
		|||
		configs.add(HazelcastAutoConfiguration.class);
 | 
			
		||||
		String mainConfig = "org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.xml";
 | 
			
		||||
		String cacheConfig = "org/springframework/boot/autoconfigure/cache/hazelcast-specific.xml";
 | 
			
		||||
		doLoad(configs, "spring.cache.type=hazelcast",
 | 
			
		||||
				"spring.cache.hazelcast.config=" + cacheConfig,
 | 
			
		||||
				"spring.hazelcast.config=" + mainConfig);
 | 
			
		||||
		HazelcastInstance hazelcastInstance = this.context.getBean(HazelcastInstance.class);
 | 
			
		||||
		doLoad(configs, "spring.cache.type=hazelcast", "spring.cache.hazelcast.config="
 | 
			
		||||
				+ cacheConfig, "spring.hazelcast.config=" + mainConfig);
 | 
			
		||||
		HazelcastInstance hazelcastInstance = this.context
 | 
			
		||||
				.getBean(HazelcastInstance.class);
 | 
			
		||||
		HazelcastCacheManager cacheManager = validateCacheManager(HazelcastCacheManager.class);
 | 
			
		||||
		HazelcastInstance cacheHazelcastInstance = (HazelcastInstance)
 | 
			
		||||
				new DirectFieldAccessor(cacheManager).getPropertyValue("hazelcastInstance");
 | 
			
		||||
 | 
			
		||||
		assertThat(
 | 
			
		||||
				cacheHazelcastInstance,
 | 
			
		||||
				is(not(hazelcastInstance))); // Our custom cache instance
 | 
			
		||||
		HazelcastInstance cacheHazelcastInstance = (HazelcastInstance) new DirectFieldAccessor(
 | 
			
		||||
				cacheManager).getPropertyValue("hazelcastInstance");
 | 
			
		||||
		assertThat(cacheHazelcastInstance, is(not(hazelcastInstance))); // Our custom
 | 
			
		||||
		assertThat(hazelcastInstance.getConfig().getConfigurationFile(),
 | 
			
		||||
				is(new ClassPathResource(mainConfig).getFile()));
 | 
			
		||||
		assertThat(cacheHazelcastInstance.getConfig().getConfigurationFile(),
 | 
			
		||||
| 
						 | 
				
			
			@ -552,8 +548,7 @@ public class CacheAutoConfigurationTests {
 | 
			
		|||
		doLoad(configs, environment);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	private void doLoad(Collection<Class<?>> configs,
 | 
			
		||||
			String... environment) {
 | 
			
		||||
	private void doLoad(Collection<Class<?>> configs, String... environment) {
 | 
			
		||||
		AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
 | 
			
		||||
		EnvironmentTestUtils.addEnvironment(applicationContext, environment);
 | 
			
		||||
		for (Class<?> config : configs) {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -18,7 +18,6 @@ package org.springframework.boot.autoconfigure.condition;
 | 
			
		|||
 | 
			
		||||
import org.junit.After;
 | 
			
		||||
import org.junit.Test;
 | 
			
		||||
 | 
			
		||||
import org.springframework.boot.test.EnvironmentTestUtils;
 | 
			
		||||
import org.springframework.context.ConfigurableApplicationContext;
 | 
			
		||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
 | 
			
		||||
| 
						 | 
				
			
			@ -92,8 +91,7 @@ public class ResourceConditionTests {
 | 
			
		|||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	private static class DefaultLocationResourceCondition extends
 | 
			
		||||
			ResourceCondition {
 | 
			
		||||
	private static class DefaultLocationResourceCondition extends ResourceCondition {
 | 
			
		||||
 | 
			
		||||
		public DefaultLocationResourceCondition() {
 | 
			
		||||
			super("test", "spring.foo.test.", "config", "classpath:/logging.properties");
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -19,15 +19,10 @@ package org.springframework.boot.autoconfigure.hazelcast;
 | 
			
		|||
import java.io.IOException;
 | 
			
		||||
import java.util.Map;
 | 
			
		||||
 | 
			
		||||
import com.hazelcast.config.Config;
 | 
			
		||||
import com.hazelcast.config.QueueConfig;
 | 
			
		||||
import com.hazelcast.core.Hazelcast;
 | 
			
		||||
import com.hazelcast.core.HazelcastInstance;
 | 
			
		||||
import org.junit.After;
 | 
			
		||||
import org.junit.Rule;
 | 
			
		||||
import org.junit.Test;
 | 
			
		||||
import org.junit.rules.ExpectedException;
 | 
			
		||||
 | 
			
		||||
import org.springframework.beans.factory.BeanCreationException;
 | 
			
		||||
import org.springframework.boot.test.EnvironmentTestUtils;
 | 
			
		||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
 | 
			
		||||
| 
						 | 
				
			
			@ -35,6 +30,11 @@ import org.springframework.context.annotation.Bean;
 | 
			
		|||
import org.springframework.context.annotation.Configuration;
 | 
			
		||||
import org.springframework.core.io.ClassPathResource;
 | 
			
		||||
 | 
			
		||||
import com.hazelcast.config.Config;
 | 
			
		||||
import com.hazelcast.config.QueueConfig;
 | 
			
		||||
import com.hazelcast.core.Hazelcast;
 | 
			
		||||
import com.hazelcast.core.HazelcastInstance;
 | 
			
		||||
 | 
			
		||||
import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
 | 
			
		||||
import static org.hamcrest.collection.IsMapContaining.hasKey;
 | 
			
		||||
import static org.hamcrest.core.Is.is;
 | 
			
		||||
| 
						 | 
				
			
			@ -62,8 +62,8 @@ public class HazelcastAutoConfigurationTests {
 | 
			
		|||
	@Test
 | 
			
		||||
	public void defaultConfigFile() throws IOException {
 | 
			
		||||
		load(); // hazelcast.xml present in root classpath
 | 
			
		||||
		HazelcastInstance hazelcastInstance = this.context.getBean(
 | 
			
		||||
				HazelcastInstance.class);
 | 
			
		||||
		HazelcastInstance hazelcastInstance = this.context
 | 
			
		||||
				.getBean(HazelcastInstance.class);
 | 
			
		||||
		assertThat(hazelcastInstance.getConfig().getConfigurationUrl(),
 | 
			
		||||
				is(new ClassPathResource("hazelcast.xml").getURL()));
 | 
			
		||||
	}
 | 
			
		||||
| 
						 | 
				
			
			@ -74,9 +74,10 @@ public class HazelcastAutoConfigurationTests {
 | 
			
		|||
				"classpath:org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.xml");
 | 
			
		||||
		try {
 | 
			
		||||
			load();
 | 
			
		||||
			HazelcastInstance hazelcastInstance = this.context.getBean(
 | 
			
		||||
					HazelcastInstance.class);
 | 
			
		||||
			Map<String, QueueConfig> queueConfigs = hazelcastInstance.getConfig().getQueueConfigs();
 | 
			
		||||
			HazelcastInstance hazelcastInstance = this.context
 | 
			
		||||
					.getBean(HazelcastInstance.class);
 | 
			
		||||
			Map<String, QueueConfig> queueConfigs = hazelcastInstance.getConfig()
 | 
			
		||||
					.getQueueConfigs();
 | 
			
		||||
			assertThat(queueConfigs.values(), hasSize(1));
 | 
			
		||||
			assertThat(queueConfigs, hasKey("foobar"));
 | 
			
		||||
		}
 | 
			
		||||
| 
						 | 
				
			
			@ -87,20 +88,21 @@ public class HazelcastAutoConfigurationTests {
 | 
			
		|||
 | 
			
		||||
	@Test
 | 
			
		||||
	public void explicitConfigFile() throws IOException {
 | 
			
		||||
		load("spring.hazelcast.config=org/springframework/boot/autoconfigure/hazelcast/" +
 | 
			
		||||
				"hazelcast-specific.xml");
 | 
			
		||||
		HazelcastInstance hazelcastInstance = this.context.getBean(
 | 
			
		||||
				HazelcastInstance.class);
 | 
			
		||||
		load("spring.hazelcast.config=org/springframework/boot/autoconfigure/hazelcast/"
 | 
			
		||||
				+ "hazelcast-specific.xml");
 | 
			
		||||
		HazelcastInstance hazelcastInstance = this.context
 | 
			
		||||
				.getBean(HazelcastInstance.class);
 | 
			
		||||
		assertThat(hazelcastInstance.getConfig().getConfigurationFile(),
 | 
			
		||||
				is(new ClassPathResource("org/springframework/boot/autoconfigure/hazelcast" +
 | 
			
		||||
						"/hazelcast-specific.xml").getFile()));
 | 
			
		||||
				is(new ClassPathResource(
 | 
			
		||||
						"org/springframework/boot/autoconfigure/hazelcast"
 | 
			
		||||
								+ "/hazelcast-specific.xml").getFile()));
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Test
 | 
			
		||||
	public void explicitConfigUrl() throws IOException {
 | 
			
		||||
		load("spring.hazelcast.config=hazelcast-default.xml");
 | 
			
		||||
		HazelcastInstance hazelcastInstance = this.context.getBean(
 | 
			
		||||
				HazelcastInstance.class);
 | 
			
		||||
		HazelcastInstance hazelcastInstance = this.context
 | 
			
		||||
				.getBean(HazelcastInstance.class);
 | 
			
		||||
		assertThat(hazelcastInstance.getConfig().getConfigurationUrl(),
 | 
			
		||||
				is(new ClassPathResource("hazelcast-default.xml").getURL()));
 | 
			
		||||
	}
 | 
			
		||||
| 
						 | 
				
			
			@ -115,13 +117,15 @@ public class HazelcastAutoConfigurationTests {
 | 
			
		|||
	@Test
 | 
			
		||||
	public void configInstanceWithName() {
 | 
			
		||||
		Config config = new Config("my-test-instance");
 | 
			
		||||
		HazelcastInstance existingHazelcastInstance = Hazelcast.newHazelcastInstance(config);
 | 
			
		||||
		HazelcastInstance existingHazelcastInstance = Hazelcast
 | 
			
		||||
				.newHazelcastInstance(config);
 | 
			
		||||
		try {
 | 
			
		||||
			load(HazelcastConfigWithName.class,
 | 
			
		||||
					"spring.hazelcast.config=this-is-ignored.xml");
 | 
			
		||||
			HazelcastInstance hazelcastInstance = this.context.getBean(
 | 
			
		||||
					HazelcastInstance.class);
 | 
			
		||||
			assertThat(hazelcastInstance.getConfig().getInstanceName(), is("my-test-instance"));
 | 
			
		||||
			HazelcastInstance hazelcastInstance = this.context
 | 
			
		||||
					.getBean(HazelcastInstance.class);
 | 
			
		||||
			assertThat(hazelcastInstance.getConfig().getInstanceName(),
 | 
			
		||||
					is("my-test-instance"));
 | 
			
		||||
			// Should reuse any existing instance by default.
 | 
			
		||||
			assertThat(hazelcastInstance, is(existingHazelcastInstance));
 | 
			
		||||
		}
 | 
			
		||||
| 
						 | 
				
			
			@ -132,11 +136,11 @@ public class HazelcastAutoConfigurationTests {
 | 
			
		|||
 | 
			
		||||
	@Test
 | 
			
		||||
	public void configInstanceWithoutName() {
 | 
			
		||||
		load(HazelcastConfigNoName.class,
 | 
			
		||||
				"spring.hazelcast.config=this-is-ignored.xml");
 | 
			
		||||
		HazelcastInstance hazelcastInstance = this.context.getBean(
 | 
			
		||||
				HazelcastInstance.class);
 | 
			
		||||
		Map<String, QueueConfig> queueConfigs = hazelcastInstance.getConfig().getQueueConfigs();
 | 
			
		||||
		load(HazelcastConfigNoName.class, "spring.hazelcast.config=this-is-ignored.xml");
 | 
			
		||||
		HazelcastInstance hazelcastInstance = this.context
 | 
			
		||||
				.getBean(HazelcastInstance.class);
 | 
			
		||||
		Map<String, QueueConfig> queueConfigs = hazelcastInstance.getConfig()
 | 
			
		||||
				.getQueueConfigs();
 | 
			
		||||
		assertThat(queueConfigs.values(), hasSize(1));
 | 
			
		||||
		assertThat(queueConfigs, hasKey("another-queue"));
 | 
			
		||||
	}
 | 
			
		||||
| 
						 | 
				
			
			@ -156,7 +160,6 @@ public class HazelcastAutoConfigurationTests {
 | 
			
		|||
		this.context = applicationContext;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
	@Configuration
 | 
			
		||||
	static class HazelcastConfigWithName {
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue