Skip to main content
summaryrefslogtreecommitdiffstats
blob: 0a782482dd442935b7f1d9eaea6718f11815eefe (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.cluster.hazelcast.internal;

import java.io.IOException;
import java.lang.reflect.Constructor;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import org.eclipse.osee.cluster.Cluster;
import org.eclipse.osee.cluster.ClusterService;
import org.eclipse.osee.cluster.ClusterServiceUtils;
import org.eclipse.osee.cluster.DistributedExecutorService;
import org.eclipse.osee.cluster.Transaction;
import org.eclipse.osee.cluster.TransactionWork;
import org.eclipse.osee.distributed.AtomicNumber;
import org.eclipse.osee.distributed.DistributedId;
import org.eclipse.osee.distributed.DistributedLock;
import org.eclipse.osee.distributed.DistributedMap;
import org.eclipse.osee.distributed.DistributedMultiMap;
import org.eclipse.osee.distributed.DistributedObject;
import org.eclipse.osee.distributed.InstanceManager;
import org.eclipse.osee.event.EventService;
import org.eclipse.osee.logger.Log;
import com.hazelcast.config.Config;
import com.hazelcast.config.UrlXmlConfig;
import com.hazelcast.config.XmlConfigBuilder;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IList;
import com.hazelcast.core.ILock;
import com.hazelcast.core.IMap;
import com.hazelcast.core.IQueue;
import com.hazelcast.core.ISet;
import com.hazelcast.core.IdGenerator;
import com.hazelcast.core.Instance;
import com.hazelcast.core.InstanceEvent;
import com.hazelcast.core.InstanceListener;
import com.hazelcast.core.LifecycleService;
import com.hazelcast.core.MultiMap;
import com.hazelcast.impl.GroupProperties;

/**
 * @author Roberto E. Escobar
 */
public class ClusterServiceImpl implements ClusterService, InstanceManager {

   private final Map<Object, DistributedObject> distributedObjects = new ConcurrentHashMap<Object, DistributedObject>();
   private final ProxyCleaner proxyCleaner = new ProxyCleaner();
   private Log logger;
   private EventService eventService;
   private HazelcastInstance instance;
   private Thread thread;
   private ClusterEventNotifier eventNotifier;
   private ClusterProxy clusterProxy;
   private DistributedExecutorService executor;

   public void setEventService(EventService eventService) {
      this.eventService = eventService;
   }

   private EventService getEventService() {
      return eventService;
   }

   public void setLogger(Log logger) {
      this.logger = logger;
   }

   private Log getLogger() {
      return logger;
   }

   private Config getConfiguration(Map<String, Object> properties) {
      Config config = null;

      String loggerClass = System.getProperty("hazelcast.logging.class");
      if (loggerClass == null) {
         String loggerType = System.getProperty("hazelcast.logging.type");
         if (loggerType == null) {
            System.setProperty("hazelcast.logging.type", "slf4j");
         }
      }
      String configPath = ClusterServiceUtils.getConfigurationURL(properties);
      if (configPath != null && configPath.length() > 0) {
         config = loadConfiguration(configPath);
      } else {
         config = new XmlConfigBuilder().build();
      }
      Properties props = config.getProperties();
      props.put(GroupProperties.PROP_VERSION_CHECK_ENABLED, "false");
      //      props.put(GroupProperties.PROP_REST_ENABLED, "true");
      props.put(GroupProperties.PROP_ENABLE_JMX, "false");
      props.put(GroupProperties.PROP_ENABLE_JMX_DETAILED, "false");
      return config;
   }

   private Config loadConfiguration(String configPath) {
      // InputStream stream = null;
      try {
         //         URL configURL = new URL(configPath);
         //         stream = new BufferedInputStream(configURL.openStream());
         //         
         //         XmlConfigBuilder builder = new XmlConfigBuilder(stream);
         return new UrlXmlConfig(configPath);
      } catch (IOException ex) {
         throw new RuntimeException(ex);
         //      } finally {
         //         if (stream != null) {
         //            try {
         //               stream.close();
         //            } catch (IOException ex) {
         //               // Do Nothing 
         //            }
         //         }
      }
   }

   public synchronized void start(final Map<String, Object> properties) {
      thread = new Thread("Register Pending Rest Services") {
         @Override
         public void run() {
            Config config = getConfiguration(properties);
            instance = Hazelcast.init(config);
            clusterProxy = new ClusterProxy(instance);
            executor = new DistributedExecutorServiceImpl(instance);

            String componentName = ClusterServiceUtils.getComponentName(properties);
            String contextName = ClusterServiceUtils.getContextName(properties);
            eventNotifier = new ClusterEventNotifier(componentName, contextName, getEventService());
            registerEventListeners();
            eventNotifier.notifyRegistration();
         }
      };
      thread.start();
   }

   public synchronized void stop(Map<String, Object> properties) {
      if (thread != null && thread.isAlive()) {
         thread.interrupt();
         thread = null;
      }
      distributedObjects.clear();
      if (instance != null) {
         deregisterEventListeners();
         LifecycleService service = instance.getLifecycleService();
         service.shutdown();
         instance = null;
      }
      eventNotifier.notifyDeRegistration();
      eventNotifier = null;
   }

   private void registerEventListeners() {
      instance.addInstanceListener(proxyCleaner);
      instance.addInstanceListener(eventNotifier);
      instance.getLifecycleService().addLifecycleListener(eventNotifier);
      instance.getCluster().addMembershipListener(eventNotifier);
   }

   private void deregisterEventListeners() {
      instance.removeInstanceListener(proxyCleaner);
      instance.removeInstanceListener(eventNotifier);
      instance.getLifecycleService().removeLifecycleListener(eventNotifier);
      instance.getCluster().removeMembershipListener(eventNotifier);
   }

   protected HazelcastInstance getHazelcastInstance() {
      return instance;
   }

   @Override
   public DistributedExecutorService getExecutor() {
      return executor;
   }

   @Override
   public Cluster getCluster() {
      return clusterProxy;
   }

   @Override
   public String getName() {
      return getHazelcastInstance().getName();
   }

   @Override
   public Transaction getTransaction() {
      // Create a new one every time
      return new TransactionProxy(getHazelcastInstance().getTransaction());
   }

   @Override
   public <T> Callable<T> createTxCallable(TransactionWork<T> work) {
      Transaction txn = getTransaction();
      return new CallableTransactionImpl<T>(getLogger(), txn, work);
   }

   @Override
   public AtomicNumber getAtomicNumber(String name) {
      return getProxyObject(AtomicNumberProxy.class, com.hazelcast.core.AtomicNumber.class,
         getHazelcastInstance().getAtomicNumber(name));
   }

   @SuppressWarnings({"unchecked"})
   @Override
   public <E> BlockingQueue<E> getQueue(String name) {
      return getProxyObject(DistributedBlockingQueueProxy.class, IQueue.class, getHazelcastInstance().getQueue(name));
   }

   @SuppressWarnings({"unchecked"})
   @Override
   public <E> Set<E> getSet(String name) {
      return getProxyObject(DistributedSetProxy.class, ISet.class, getHazelcastInstance().getSet(name));
   }

   @SuppressWarnings({"unchecked"})
   @Override
   public <E> List<E> getList(String name) {
      return getProxyObject(DistributedListProxy.class, IList.class, getHazelcastInstance().getList(name));
   }

   @SuppressWarnings({"unchecked"})
   @Override
   public <K, V> DistributedMap<K, V> getMap(String name) {
      return getProxyObject(DistributedMapProxy.class, IMap.class, getHazelcastInstance().getMap(name));
   }

   @SuppressWarnings({"unchecked"})
   @Override
   public <K, V> DistributedMultiMap<K, V> getMultiMap(String name) {
      return getProxyObject(DistributedMultiMapProxy.class, MultiMap.class, getHazelcastInstance().getMultiMap(name));
   }

   @Override
   public DistributedId getIdGenerator(String name) {
      return getProxyObject(DistributedIdProxy.class, IdGenerator.class, getHazelcastInstance().getIdGenerator(name));
   }

   @Override
   public DistributedLock getLock(Object key) {
      return getProxyObject(DistributedLockProxy.class, ILock.class, getHazelcastInstance().getLock(key));
   }

   @SuppressWarnings("unchecked")
   private <T extends DistributedObject> T getProxyObject(Class<T> proxyClass, Class<?> hClazz, Instance instance) {
      Object key = instance.getId();
      T proxy = (T) distributedObjects.get(key);
      if (proxy == null) {
         try {
            Constructor<T> constructor = proxyClass.getConstructor(hClazz);
            proxy = constructor.newInstance(instance);
            distributedObjects.put(key, proxy);
         } catch (Exception ex) {
            logger.error(ex, "Error creating proxy object");
         }
      }
      return proxy;
   }

   private final class ProxyCleaner implements InstanceListener {

      @Override
      public void instanceCreated(InstanceEvent event) {
         // Do nothing
      }

      @Override
      public void instanceDestroyed(InstanceEvent event) {
         Instance instance = event.getInstance();
         distributedObjects.remove(instance.getId());
      }
   }
}

Back to the top