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

https://bitbucket.org/gabral6_gmailcom/hazelcast · Java · 55 lines · 33 code · 7 blank · 15 comment · 4 complexity · c8233d15620c2cabd5a0798ce9a5f745 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.CacheEnvironment;
  19. import org.hibernate.cache.CacheException;
  20. import java.util.Properties;
  21. public final class HazelcastInstanceFactory {
  22. private final static String HZ_CLIENT_LOADER_CLASSNAME = "com.hazelcast.hibernate.instance.HazelcastClientLoader";
  23. private final static String HZ_INSTANCE_LOADER_CLASSNAME = "com.hazelcast.hibernate.instance.HazelcastInstanceLoader";
  24. public static HazelcastInstance createInstance(Properties props) throws CacheException {
  25. return createInstanceLoader(props).loadInstance();
  26. }
  27. public static IHazelcastInstanceLoader createInstanceLoader(Properties props) throws CacheException {
  28. boolean useNativeClient = false;
  29. if (props != null) {
  30. useNativeClient = CacheEnvironment.isNativeClient(props);
  31. }
  32. IHazelcastInstanceLoader loader = null;
  33. Class loaderClass = null;
  34. ClassLoader cl = HazelcastInstanceFactory.class.getClassLoader();
  35. try {
  36. if (useNativeClient) {
  37. loaderClass = cl.loadClass(HZ_CLIENT_LOADER_CLASSNAME);
  38. } else {
  39. loaderClass = cl.loadClass(HZ_INSTANCE_LOADER_CLASSNAME);
  40. }
  41. loader = (IHazelcastInstanceLoader) loaderClass.newInstance();
  42. } catch (Exception e) {
  43. throw new CacheException(e);
  44. }
  45. loader.configure(props);
  46. return loader;
  47. }
  48. }