001/*
002 * Copyright 2012-2016 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.boot.autoconfigure.hazelcast;
018
019import java.io.IOException;
020
021import com.hazelcast.config.Config;
022import com.hazelcast.core.Hazelcast;
023import com.hazelcast.core.HazelcastInstance;
024
025import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
026import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
027import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
028import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
029import org.springframework.boot.context.properties.EnableConfigurationProperties;
030import org.springframework.context.annotation.Bean;
031import org.springframework.context.annotation.Conditional;
032import org.springframework.context.annotation.Configuration;
033import org.springframework.core.io.Resource;
034
035/**
036 * {@link EnableAutoConfiguration Auto-configuration} for Hazelcast. Creates a
037 * {@link HazelcastInstance} based on explicit configuration or when a default
038 * configuration file is found in the environment.
039 *
040 * @author Stephane Nicoll
041 * @since 1.3.0
042 * @see HazelcastConfigResourceCondition
043 */
044@Configuration
045@ConditionalOnClass(HazelcastInstance.class)
046@ConditionalOnMissingBean(HazelcastInstance.class)
047@EnableConfigurationProperties(HazelcastProperties.class)
048public class HazelcastAutoConfiguration {
049
050        @Configuration
051        @ConditionalOnMissingBean(Config.class)
052        @Conditional(ConfigAvailableCondition.class)
053        static class HazelcastConfigFileConfiguration {
054
055                private final HazelcastProperties hazelcastProperties;
056
057                HazelcastConfigFileConfiguration(HazelcastProperties hazelcastProperties) {
058                        this.hazelcastProperties = hazelcastProperties;
059                }
060
061                @Bean
062                public HazelcastInstance hazelcastInstance() throws IOException {
063                        Resource config = this.hazelcastProperties.resolveConfigLocation();
064                        if (config != null) {
065                                return new HazelcastInstanceFactory(config).getHazelcastInstance();
066                        }
067                        return Hazelcast.newHazelcastInstance();
068                }
069
070        }
071
072        @Configuration
073        @ConditionalOnSingleCandidate(Config.class)
074        static class HazelcastConfigConfiguration {
075
076                @Bean
077                public HazelcastInstance hazelcastInstance(Config config) {
078                        return new HazelcastInstanceFactory(config).getHazelcastInstance();
079                }
080
081        }
082
083        /**
084         * {@link HazelcastConfigResourceCondition} that checks if the
085         * {@code spring.hazelcast.config} configuration key is defined.
086         */
087        static class ConfigAvailableCondition extends HazelcastConfigResourceCondition {
088
089                ConfigAvailableCondition() {
090                        super("spring.hazelcast", "config");
091                }
092
093        }
094
095}