|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2022 the original author or authors. |
| 2 | + * Copyright 2012-2024 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
16 | 16 |
|
17 | 17 | package org.springframework.boot.autoconfigure.hazelcast;
|
18 | 18 |
|
19 |
| -import java.io.IOException; |
20 |
| -import java.net.URL; |
21 |
| - |
22 | 19 | import com.hazelcast.client.HazelcastClient;
|
23 | 20 | import com.hazelcast.client.config.ClientConfig;
|
24 |
| -import com.hazelcast.client.config.XmlClientConfigBuilder; |
25 |
| -import com.hazelcast.client.config.YamlClientConfigBuilder; |
26 | 21 | import com.hazelcast.core.HazelcastInstance;
|
27 | 22 |
|
28 | 23 | import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
|
31 | 26 | import org.springframework.context.annotation.Bean;
|
32 | 27 | import org.springframework.context.annotation.Conditional;
|
33 | 28 | import org.springframework.context.annotation.Configuration;
|
34 |
| -import org.springframework.core.io.Resource; |
| 29 | +import org.springframework.context.annotation.Import; |
35 | 30 | import org.springframework.core.io.ResourceLoader;
|
36 |
| -import org.springframework.util.StringUtils; |
37 | 31 |
|
38 | 32 | /**
|
39 | 33 | * Configuration for Hazelcast client.
|
|
44 | 38 | @Configuration(proxyBeanMethods = false)
|
45 | 39 | @ConditionalOnClass(HazelcastClient.class)
|
46 | 40 | @ConditionalOnMissingBean(HazelcastInstance.class)
|
| 41 | +@Import(HazelcastClientInstanceConfiguration.class) |
47 | 42 | class HazelcastClientConfiguration {
|
48 | 43 |
|
49 | 44 | static final String CONFIG_SYSTEM_PROPERTY = "hazelcast.client.config";
|
50 | 45 |
|
51 |
| - private static HazelcastInstance getHazelcastInstance(ClientConfig config) { |
52 |
| - if (StringUtils.hasText(config.getInstanceName())) { |
53 |
| - return HazelcastClient.getOrCreateHazelcastClient(config); |
54 |
| - } |
55 |
| - return HazelcastClient.newHazelcastClient(config); |
56 |
| - } |
57 |
| - |
58 | 46 | @Configuration(proxyBeanMethods = false)
|
59 |
| - @ConditionalOnMissingBean(ClientConfig.class) |
| 47 | + @ConditionalOnMissingBean({ ClientConfig.class, HazelcastConnectionDetails.class }) |
60 | 48 | @Conditional(HazelcastClientConfigAvailableCondition.class)
|
61 | 49 | static class HazelcastClientConfigFileConfiguration {
|
62 | 50 |
|
63 | 51 | @Bean
|
64 |
| - HazelcastInstance hazelcastInstance(HazelcastProperties properties, ResourceLoader resourceLoader) |
65 |
| - throws IOException { |
66 |
| - Resource configLocation = properties.resolveConfigLocation(); |
67 |
| - ClientConfig config = (configLocation != null) ? loadClientConfig(configLocation) : ClientConfig.load(); |
68 |
| - config.setClassLoader(resourceLoader.getClassLoader()); |
69 |
| - return getHazelcastInstance(config); |
70 |
| - } |
71 |
| - |
72 |
| - private ClientConfig loadClientConfig(Resource configLocation) throws IOException { |
73 |
| - URL configUrl = configLocation.getURL(); |
74 |
| - String configFileName = configUrl.getPath(); |
75 |
| - if (configFileName.endsWith(".yaml") || configFileName.endsWith(".yml")) { |
76 |
| - return new YamlClientConfigBuilder(configUrl).build(); |
77 |
| - } |
78 |
| - return new XmlClientConfigBuilder(configUrl).build(); |
| 52 | + HazelcastConnectionDetails hazelcastConnectionDetails(HazelcastProperties properties, |
| 53 | + ResourceLoader resourceLoader) { |
| 54 | + return new PropertiesHazelcastConnectionDetails(properties, resourceLoader); |
79 | 55 | }
|
80 | 56 |
|
81 | 57 | }
|
82 | 58 |
|
83 | 59 | @Configuration(proxyBeanMethods = false)
|
| 60 | + @ConditionalOnMissingBean(HazelcastConnectionDetails.class) |
84 | 61 | @ConditionalOnSingleCandidate(ClientConfig.class)
|
85 | 62 | static class HazelcastClientConfigConfiguration {
|
86 | 63 |
|
87 | 64 | @Bean
|
88 |
| - HazelcastInstance hazelcastInstance(ClientConfig config) { |
89 |
| - return getHazelcastInstance(config); |
| 65 | + HazelcastConnectionDetails hazelcastConnectionDetails(ClientConfig config) { |
| 66 | + return () -> config; |
90 | 67 | }
|
91 | 68 |
|
92 | 69 | }
|
|
0 commit comments