/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. package com.hazelcast.hibernate.instance;
  17. import com.hazelcast.core.HazelcastInstance;
  18. import com.hazelcast.hibernate.provider.HazelcastCacheProvider;
  19. import org.hibernate.cache.CacheProvider;
  20. import org.hibernate.cfg.Settings;
  21. import java.lang.reflect.Method;
  22. import java.util.logging.Level;
  23. final class CacheProviderHazelcastAccessor extends HazelcastAccessor {
  24. public HazelcastInstance getHazelcastInstance(Settings settings) {
  25. Object providerObject = null;
  26. try {
  27. Method getCacheProviderMethod = Settings.class.getMethod(METHOD_GET_CACHE_PROVIDER);
  28. providerObject = getCacheProviderMethod.invoke(settings);
  29. } catch (Exception e) {
  30. logger.log(Level.SEVERE, e.getMessage(), e);
  31. return null;
  32. }
  33. if (providerObject == null) {
  34. logger.log(Level.SEVERE, "Hibernate 2nd level cache has not been enabled!");
  35. return null;
  36. }
  37. final CacheProvider provider = (CacheProvider) providerObject;
  38. if (provider instanceof HazelcastCacheProvider) {
  39. return ((HazelcastCacheProvider) provider).getHazelcastInstance();
  40. }
  41. logger.log(Level.WARNING, "Current 2nd level cache implementation is not HazelcastCacheProvider!");
  42. return null;
  43. }
  44. }