001/*
002 * Copyright 2012-2017 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;
020import java.net.URL;
021
022import com.hazelcast.client.HazelcastClient;
023import com.hazelcast.client.config.ClientConfig;
024import com.hazelcast.client.config.XmlClientConfigBuilder;
025import com.hazelcast.core.HazelcastInstance;
026
027import org.springframework.core.io.Resource;
028import org.springframework.util.Assert;
029import org.springframework.util.StringUtils;
030
031/**
032 * Factory that can be used to create a client {@link HazelcastInstance}.
033 *
034 * @author Vedran Pavic
035 * @since 2.0.0
036 */
037public class HazelcastClientFactory {
038
039        private final ClientConfig clientConfig;
040
041        /**
042         * Create a {@link HazelcastClientFactory} for the specified configuration location.
043         * @param clientConfigLocation the location of the configuration file
044         * @throws IOException if the configuration location could not be read
045         */
046        public HazelcastClientFactory(Resource clientConfigLocation) throws IOException {
047                this.clientConfig = getClientConfig(clientConfigLocation);
048        }
049
050        /**
051         * Create a {@link HazelcastClientFactory} for the specified configuration.
052         * @param clientConfig the configuration
053         */
054        public HazelcastClientFactory(ClientConfig clientConfig) {
055                Assert.notNull(clientConfig, "ClientConfig must not be null");
056                this.clientConfig = clientConfig;
057        }
058
059        private ClientConfig getClientConfig(Resource clientConfigLocation)
060                        throws IOException {
061                URL configUrl = clientConfigLocation.getURL();
062                return new XmlClientConfigBuilder(configUrl).build();
063        }
064
065        /**
066         * Get the {@link HazelcastInstance}.
067         * @return the {@link HazelcastInstance}
068         */
069        public HazelcastInstance getHazelcastInstance() {
070                if (StringUtils.hasText(this.clientConfig.getInstanceName())) {
071                        return HazelcastClient
072                                        .getHazelcastClientByName(this.clientConfig.getInstanceName());
073                }
074                return HazelcastClient.newHazelcastClient(this.clientConfig);
075        }
076
077}