View Javadoc
1   /*
2    * Copyright 2016 The Lannister Project
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  
17  package net.anyflow.lannister.cluster;
18  
19  import java.io.IOException;
20  
21  import com.fasterxml.jackson.databind.JsonNode;
22  import com.hazelcast.config.Config;
23  import com.hazelcast.config.SerializerConfig;
24  import com.hazelcast.config.XmlConfigBuilder;
25  import com.hazelcast.core.HazelcastInstance;
26  import com.hazelcast.core.ILock;
27  import com.hazelcast.core.IMap;
28  import com.hazelcast.core.ISet;
29  import com.hazelcast.core.ITopic;
30  import com.hazelcast.core.IdGenerator;
31  
32  import net.anyflow.lannister.Application;
33  import net.anyflow.lannister.Settings;
34  import net.anyflow.lannister.serialization.JsonSerializer;
35  import net.anyflow.lannister.serialization.SerializableFactory;
36  
37  public class Hazelcast {
38  	private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(Hazelcast.class);
39  
40  	public static final Hazelcast INSTANCE = new Hazelcast();
41  	private static final String CONFIG_NAME = "lannister.cluster.xml";
42  
43  	private HazelcastInstance substance;
44  
45  	private Hazelcast() {
46  		if (Settings.INSTANCE.clusteringMode() != Mode.HAZELCAST) { return; }
47  
48  		substance = com.hazelcast.core.Hazelcast.newHazelcastInstance(createConfig());
49  	}
50  
51  	private Config createConfig() {
52  		Config config;
53  		try {
54  			config = new XmlConfigBuilder(Application.class.getClassLoader().getResource(CONFIG_NAME)).build();
55  		}
56  		catch (IOException e) {
57  			logger.error(e.getMessage(), e);
58  			throw new Error(e);
59  		}
60  
61  		config.getSerializationConfig().addDataSerializableFactory(SerializableFactory.ID, new SerializableFactory());
62  
63  		config.getSerializationConfig().getSerializerConfigs().add(new SerializerConfig().setTypeClass(JsonNode.class)
64  				.setImplementation(JsonSerializer.makePlain(JsonNode.class)));
65  
66  		return config;
67  	}
68  
69  	public void shutdown() {
70  		if (Settings.INSTANCE.clusteringMode() != Mode.HAZELCAST) { return; }
71  
72  		substance.shutdown();
73  	}
74  
75  	protected ILock getLock(String key) {
76  		return substance.getLock(key);
77  	}
78  
79  	protected <E> ITopic<E> getTopic(String name) {
80  		return substance.getTopic(name);
81  	}
82  
83  	protected IdGenerator getIdGenerator(String name) {
84  		return substance.getIdGenerator(name);
85  	}
86  
87  	protected <K, V> IMap<K, V> getMap(String name) {
88  		return substance.getMap(name);
89  	}
90  
91  	protected String currentId() {
92  		return substance.getLocalEndpoint().getUuid();
93  	}
94  
95  	public <V> ISet<V> getSet(String name) {
96  		return substance.getSet(name);
97  	}
98  }