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  package net.anyflow.lannister.cluster;
17  
18  import java.util.UUID;
19  import java.util.concurrent.locks.Lock;
20  import java.util.concurrent.locks.ReentrantLock;
21  
22  import com.hazelcast.core.ITopic;
23  import com.hazelcast.core.IdGenerator;
24  
25  import net.anyflow.lannister.Settings;
26  
27  public class ClusterDataFactory {
28  	private static final String ID = UUID.randomUUID().toString();
29  
30  	public static final ClusterDataFactory INSTANCE = new ClusterDataFactory();
31  
32  	private ClusterDataFactory() {
33  	}
34  
35  	public String currentId() {
36  		switch (Settings.INSTANCE.clusteringMode()) {
37  		case HAZELCAST:
38  			return Hazelcast.INSTANCE.currentId();
39  
40  		case IGNITE:
41  		case SINGLE:
42  			return ID;
43  
44  		default:
45  			return null;
46  		}
47  	}
48  
49  	public <K, V> Map<K, V> createMap(String name) {
50  		switch (Settings.INSTANCE.clusteringMode()) {
51  		case HAZELCAST:
52  			return new HazelcastMap<K, V>(name);
53  
54  		case IGNITE:
55  			return new IgniteMap<K, V>(name);
56  
57  		case SINGLE:
58  			return new NativeMap<K, V>(name);
59  
60  		default:
61  			return null;
62  		}
63  	}
64  
65  	public <K, V> Map<K, Set<V>> createSetValueMap(String name) {
66  		switch (Settings.INSTANCE.clusteringMode()) {
67  		case HAZELCAST:
68  			return new HazelcastSetValueMap<K, V>(name);
69  
70  		case IGNITE:
71  		case SINGLE:
72  			return new NativeSetValueMap<K, V>(name);
73  
74  		default:
75  			return null;
76  		}
77  	}
78  
79  	public <V> Set<V> createSet(String name) {
80  		switch (Settings.INSTANCE.clusteringMode()) {
81  		case HAZELCAST:
82  			return new HazelcastSet<V>(name);
83  
84  		case IGNITE:
85  		case SINGLE:
86  			return new NativeSet<V>();
87  
88  		default:
89  			return null;
90  		}
91  	}
92  
93  	public Lock createLock(String key) {
94  		switch (Settings.INSTANCE.clusteringMode()) {
95  		case HAZELCAST:
96  			return Hazelcast.INSTANCE.getLock(key);
97  
98  		case IGNITE:
99  		case SINGLE:
100 			return new ReentrantLock();
101 
102 		default:
103 			return null;
104 		}
105 	}
106 
107 	public <E> ITopic<E> createTopic(String name) {
108 		switch (Settings.INSTANCE.clusteringMode()) {
109 		case HAZELCAST:
110 			return Hazelcast.INSTANCE.getTopic(name);
111 
112 		case IGNITE:
113 		case SINGLE:
114 			return new SingleTopic<E>(name);
115 
116 		default:
117 			return null;
118 		}
119 	}
120 
121 	public IdGenerator createIdGenerator(String name) {
122 		switch (Settings.INSTANCE.clusteringMode()) {
123 		case HAZELCAST:
124 			return Hazelcast.INSTANCE.getIdGenerator(name);
125 
126 		case IGNITE:
127 		case SINGLE:
128 			return new SingleIdGenerator(name);
129 
130 		default:
131 			return null;
132 		}
133 	}
134 }