PageRenderTime 27ms CodeModel.GetById 19ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

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