All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hazelcast.spring.cache.SpringHazelcastCachingProvider Maven / Gradle / Ivy

/*
 * Copyright (c) 2008-2016, Hazelcast, Inc. All Rights Reserved.
 *
 * 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 com.hazelcast.spring.cache;

import com.hazelcast.cache.HazelcastCachingProvider;
import com.hazelcast.cache.impl.HazelcastServerCachingProvider;
import com.hazelcast.client.cache.impl.HazelcastClientCachingProvider;
import com.hazelcast.client.impl.HazelcastClientProxy;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.util.ExceptionUtil;

import javax.cache.CacheManager;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Properties;

/**
 * Spring utility class for connecting {@link HazelcastCachingProvider} interface and Hazelcast instance
 */
public final class SpringHazelcastCachingProvider {

    private SpringHazelcastCachingProvider() { }

    /**
     * Creates a {@link CacheManager} on an existing HazelcastInstance.
     *
     * @param uriString Scope of {@link CacheManager}
     * @param instance Hazelcast instance that created {@link CacheManager} is connected.
     * @param props Extra properties to be passed to cache manager. If {@code props} contain hazelcast.instance.name
     *              it overrides {@code instance} parameter
     * @return
     */
    public static CacheManager getCacheManager(HazelcastInstance instance, String uriString, Properties props) {
        URI uri = null;
        if (uriString != null) {
            try {
                uri = new URI(uriString);
            } catch (URISyntaxException e) {
                ExceptionUtil.rethrow(e);
            }
        }
        if (instance instanceof HazelcastClientProxy) {
            return HazelcastClientCachingProvider.createCachingProvider(instance).getCacheManager(uri, null, props);
        } else {
            return HazelcastServerCachingProvider.createCachingProvider(instance).getCacheManager(uri, null, props);
        }
    }

    public static CacheManager getCacheManager(String uriString, Properties properties) {
        String instanceName = properties.getProperty(HazelcastCachingProvider.HAZELCAST_INSTANCE_NAME);
        if (instanceName == null) {
            throw new IllegalStateException("Either instance-ref' attribute or "
                    + HazelcastCachingProvider.HAZELCAST_INSTANCE_NAME + " property is required for creating cache manager");
        }
        return getCacheManager(Hazelcast.getHazelcastInstanceByName(instanceName), uriString, properties);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy