PageRenderTime 23ms CodeModel.GetById 10ms app.highlight 8ms RepoModel.GetById 2ms app.codeStats 0ms

/hazelcast-hibernate/src/main/java/com/hazelcast/hibernate/instance/CacheProviderHazelcastAccessor.java

https://bitbucket.org/gabral6_gmailcom/hazelcast
Java | 49 lines | 29 code | 5 blank | 15 comment | 3 complexity | b25e282800b8887a5b94bafeb10ea841 MD5 | raw file
 1/*
 2 * Copyright (c) 2008-2013, Hazelcast, Inc. All Rights Reserved.
 3 *
 4 * Licensed under the Apache License, Version 2.0 (the "License");
 5 * you may not use this file except in compliance with the License.
 6 * You may obtain a copy of the License at
 7 *
 8 * http://www.apache.org/licenses/LICENSE-2.0
 9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.hazelcast.hibernate.instance;
18
19import com.hazelcast.core.HazelcastInstance;
20import com.hazelcast.hibernate.provider.HazelcastCacheProvider;
21import org.hibernate.cache.CacheProvider;
22import org.hibernate.cfg.Settings;
23
24import java.lang.reflect.Method;
25import java.util.logging.Level;
26
27final class CacheProviderHazelcastAccessor extends HazelcastAccessor {
28
29    public HazelcastInstance getHazelcastInstance(Settings settings) {
30        Object providerObject = null;
31        try {
32            Method getCacheProviderMethod = Settings.class.getMethod(METHOD_GET_CACHE_PROVIDER);
33            providerObject = getCacheProviderMethod.invoke(settings);
34        } catch (Exception e) {
35            logger.log(Level.SEVERE, e.getMessage(), e);
36            return null;
37        }
38        if (providerObject == null) {
39            logger.log(Level.SEVERE, "Hibernate 2nd level cache has not been enabled!");
40            return null;
41        }
42        final CacheProvider provider = (CacheProvider) providerObject;
43        if (provider instanceof HazelcastCacheProvider) {
44            return ((HazelcastCacheProvider) provider).getHazelcastInstance();
45        }
46        logger.log(Level.WARNING, "Current 2nd level cache implementation is not HazelcastCacheProvider!");
47        return null;
48    }
49}