diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/EhCacheCacheConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/EhCacheCacheConfiguration.java index ee3642c084..0fcdb9f5d9 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/EhCacheCacheConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/EhCacheCacheConfiguration.java @@ -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; diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/HazelcastCacheConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/HazelcastCacheConfiguration.java index 137de63213..712e4d9d41 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/HazelcastCacheConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/HazelcastCacheConfiguration.java @@ -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. *
- * 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,19 +64,16 @@ 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);
- }
-
+ return new HazelcastCacheManager(existingHazelcastInstance);
}
}
@@ -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();
}
+
}
}
diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ResourceCondition.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ResourceCondition.java
index bb4afce96e..7c68b2ec23 100644
--- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ResourceCondition.java
+++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ResourceCondition.java
@@ -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);
diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfiguration.java
index e6a1e92ef6..95f65ad6aa 100644
--- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfiguration.java
+++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastAutoConfiguration.java
@@ -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");
}
+
}
}
diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastConfigResourceCondition.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastConfigResourceCondition.java
index f0d2499698..373c7c9844 100644
--- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastConfigResourceCondition.java
+++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastConfigResourceCondition.java
@@ -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);
}
diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastInstanceFactory.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastInstanceFactory.java
new file mode 100644
index 0000000000..07c29c65af
--- /dev/null
+++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastInstanceFactory.java
@@ -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);
+ }
+
+}
diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastProperties.java
index ba33e4f458..61ef267818 100644
--- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastProperties.java
+++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastProperties.java
@@ -49,12 +49,12 @@ public class HazelcastProperties {
* location
*/
public Resource resolveConfigLocation() {
- if (this.config != null) {
- Assert.isTrue(this.config.exists(), "Hazelcast configuration does not exist '"
- + this.config.getDescription() + "'");
- return this.config;
+ if (this.config == null) {
+ return null;
}
- return null;
+ Assert.isTrue(this.config.exists(), "Hazelcast configuration does not exist '"
+ + this.config.getDescription() + "'");
+ return this.config;
}
}
diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfigurationTests.java
index ff57a16f7e..b9a5cd5efa 100644
--- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfigurationTests.java
+++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfigurationTests.java
@@ -375,13 +375,12 @@ public class CacheAutoConfigurationTests {
Collection