Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d1e141c525512525c21b6f92292ff43901bd0599 (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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/****************************************************************************
 * Copyright (c) 2004 Composent, Inc. and others.
 * 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:
 *    Composent, Inc. - initial API and implementation
 *****************************************************************************/

package org.eclipse.ecf.example.collab.share;

import java.io.IOException;
import java.io.Serializable;
import java.util.Hashtable;
import java.util.Random;

import org.eclipse.core.runtime.Assert;
import org.eclipse.ecf.core.events.IContainerConnectedEvent;
import org.eclipse.ecf.core.events.IContainerDisconnectedEvent;
import org.eclipse.ecf.core.identity.ID;
import org.eclipse.ecf.core.identity.IDFactory;
import org.eclipse.ecf.core.sharedobject.ISharedObject;
import org.eclipse.ecf.core.sharedobject.ISharedObjectConfig;
import org.eclipse.ecf.core.sharedobject.ISharedObjectContainerTransaction;
import org.eclipse.ecf.core.sharedobject.ISharedObjectContext;
import org.eclipse.ecf.core.sharedobject.ReplicaSharedObjectDescription;
import org.eclipse.ecf.core.sharedobject.SharedObjectInitException;
import org.eclipse.ecf.core.sharedobject.events.ISharedObjectActivatedEvent;
import org.eclipse.ecf.core.sharedobject.events.ISharedObjectCreateResponseEvent;
import org.eclipse.ecf.core.sharedobject.events.ISharedObjectDeactivatedEvent;
import org.eclipse.ecf.core.sharedobject.events.ISharedObjectMessageEvent;
import org.eclipse.ecf.core.sharedobject.events.RemoteSharedObjectEvent;
import org.eclipse.ecf.core.sharedobject.util.IQueueEnqueue;
import org.eclipse.ecf.core.sharedobject.util.QueueException;
import org.eclipse.ecf.core.util.Event;
import org.eclipse.ecf.internal.example.collab.ClientPlugin;

public class GenericSharedObject implements ISharedObject {
	protected static final String ARGS_PROPERTY_NAME = "args"; //$NON-NLS-1$

	protected static final class MsgMap {
		String meth;
		Object obj;

		MsgMap(Object o, String m) {
			obj = o;
			meth = m;
		}

		public Object getObject() {
			return obj;
		}
	}

	private static long replicateID = 0;
	protected ISharedObjectConfig config;
	protected SharedObjectMsg currentMsg;
	protected ID currentMsgFromContainerID;
	protected ID currentMsgFromObjID;
	protected Hashtable msgMap;
	protected Object msgMapLock = new Object();

	ID localContainerID;

	protected static long getNextReplicateID() {
		return replicateID++;
	}

	public void activated(ID[] ids) {
		if (isHost())
			replicate(null);
	}

	public void deactivated() {
	}

	public void destroyRemote(ID remoteID) throws IOException {
		getContext().sendDispose(remoteID);
	}

	public void destroySelf() {
		if (isHost()) {
			try {
				// Send destroy message to all known remotes
				destroyRemote(null);
			} catch (IOException e) {
				log("Exception sending destroy message to remotes", e); //$NON-NLS-1$
			}
		}
		destroySelfLocal();
	}

	public void destroySelfLocal() {
		getContext().getSharedObjectManager().removeSharedObject(
				getConfig().getSharedObjectID());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ecf.core.ISharedObject#dispose(org.eclipse.ecf.core.identity.ID)
	 */
	public void dispose(ID containerID) {
	}

	protected void execMsg(ID fromID, SharedObjectMsg msg) {
		try {
			MsgMap m = null;
			synchronized (msgMapLock) {
				m = (MsgMap) ((msgMap == null) ? null : (msgMap.get(msg
						.getMethodName())));
			}
			Object o = this;
			String methName = null;
			if (m != null) {
				if (m.obj != null) {
					o = m.obj;
				}
				if (m.meth != null) {
					methName = m.meth;
				}
			}
			if (methName != null) {
				msg = SharedObjectMsg.createMsg(msg.getClassName(), methName,
						msg.getArgs());
			}
			if (currentMsgFromObjID == null)
				currentMsgFromObjID = getID();
			currentMsgFromContainerID = fromID;
			currentMsg = msg;
			// Actually invoke msg on given object. Typically will be 'this'.
			execMsgInvoke(msg, currentMsgFromObjID, o);
			currentMsg = null;
			currentMsgFromContainerID = null;
		} catch (Throwable e) {
			msgException(this, msg, e);
		}
	}

	protected void execMsgInvoke(SharedObjectMsg msg, ID fromID, Object o)
			throws Exception {
		if (msg == null || fromID == null || o == null) return;
		try {
			msg.invoke(o);
		} catch (NoSuchMethodException e) {
			msg.invokeFrom(fromID, o);
		}
	}

	protected void forwardMsgHome(SharedObjectMsg msg) throws IOException {
		forwardMsgTo(config.getHomeContainerID(), msg);
	}

	protected void forwardMsgTo(ID toID, SharedObjectMsg msg)
			throws IOException {
		getContext().sendMessage(toID,
				new RemoteSharedObjectMsgEvent(getID(), toID, msg));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ecf.core.ISharedObject#getAdapter(java.lang.Class)
	 */
	public Object getAdapter(Class clazz) {
		if (clazz.equals(ISharedObjectContainerTransaction.class)
				&& (this instanceof ISharedObjectContainerTransaction)) {
			return this;
		}
		return null;
	}

	public ISharedObjectContext getContext() {
		return getConfig().getContext();
	}

	public ISharedObjectConfig getConfig() {
		return config;
	}

	protected ID getHomeContainerID() {
		return getConfig().getHomeContainerID();
	}

	public ID getID() {
		return getConfig().getSharedObjectID();
	}

	protected ReplicaSharedObjectDescription getReplicaDescription(ID receiver) {
		return new ReplicaSharedObjectDescription(getClass(), getID(),
				getHomeContainerID(), getConfig().getProperties(),
				getNextReplicateID());
	}

	protected void handleCreateResponse(ID fromID, Throwable t, Long identifier) {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ecf.core.ISharedObject#handleEvent(org.eclipse.ecf.core.util.Event)
	 */
	public void handleEvent(Event event) {
		if (event instanceof ISharedObjectActivatedEvent) {
			ISharedObjectActivatedEvent ae = (ISharedObjectActivatedEvent) event;
			ID myID = getID();
			if (myID == null)
				return;
			if (myID.equals(ae.getActivatedID())) {
				activated(getContext().getSharedObjectManager()
						.getSharedObjectIDs());
			} else {
				otherActivated(ae.getActivatedID());
			}
		} else if (event instanceof ISharedObjectDeactivatedEvent) {
			ISharedObjectDeactivatedEvent ae = (ISharedObjectDeactivatedEvent) event;
			ID myID = getID();
			if (myID == null)
				return;
			if (myID.equals(ae.getDeactivatedID())) {
				deactivated();
			} else {
				otherDeactivated(ae.getDeactivatedID());
			}
		} else if (event instanceof IContainerConnectedEvent) {
			memberAdded(((IContainerConnectedEvent) event).getTargetID());
		} else if (event instanceof IContainerDisconnectedEvent) {
			memberRemoved(((IContainerDisconnectedEvent) event).getTargetID());
		} else if (event instanceof ISharedObjectMessageEvent) {
			handleSharedObjectMessageEvent(((ISharedObjectMessageEvent) event));
		}
	}

	protected void handleSharedObjectMessageEvent(
			ISharedObjectMessageEvent event) {
		if (event instanceof RemoteSharedObjectEvent) {
			if (event instanceof ISharedObjectCreateResponseEvent) {
				handleCreateResponseMessageEvent((ISharedObjectCreateResponseEvent) event);
			} else if (event instanceof RemoteSharedObjectMsgEvent) {
				handleSelfSendMessageEvent((RemoteSharedObjectMsgEvent) event);
			} else {
				RemoteSharedObjectMsgEvent me = (RemoteSharedObjectMsgEvent) event
						.getData();
				SharedObjectMsg msg = me.getMsg();
				execMsg(me.getRemoteContainerID(), msg);
			}
		}
	}

	protected void handleSelfSendMessageEvent(RemoteSharedObjectMsgEvent event) {
		execMsg(event.getRemoteContainerID(), event.getMsg());
	}

	protected void handleCreateResponseMessageEvent(
			ISharedObjectCreateResponseEvent event) {
		handleCreateResponse(event.getRemoteContainerID(),
				event.getException(), new Long(event.getSequence()));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ecf.core.ISharedObject#handleEvents(org.eclipse.ecf.core.util.Event[])
	 */
	public void handleEvents(Event[] events) {
		for (int i = 0; i < events.length; i++) {
			handleEvent(events[i]);
		}
	}

	public void handleRemoteData(ID spaceID, Serializable msg) {
		SharedObjectMsg aMsg = (SharedObjectMsg) msg;
		if (isReplicaMsgAllowed(spaceID, aMsg) != null) {
			execMsg(spaceID, aMsg);
		} else {
			ignoreReplicaMsg(spaceID, aMsg);
		}
	}

	protected void ignoreReplicaMsg(ID fromID, SharedObjectMsg msg) {
		// Do nothing
	}

	protected void ignoreSharedObjectMsg(ID fromID, SharedObjectMsg aMsg) {
		// Do nothing
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ecf.core.ISharedObject#init(org.eclipse.ecf.core.ISharedObjectConfig)
	 */
	public void init(ISharedObjectConfig initData)
			throws SharedObjectInitException {
		this.config = initData;
		this.localContainerID = getContext().getLocalContainerID();
	}

	public boolean isHost() {
		ID homeContainerID = getHomeContainerID();
		if (homeContainerID == null)
			return false;
		else
			return (homeContainerID.equals(localContainerID));
	}

	protected Object isMsgAllowed(ID fromID, SharedObjectMsg aMsg) {
		return this;
	}

	protected Object isReplicaMsgAllowed(ID fromID, SharedObjectMsg aMsg) {
		return this;
	}

	public boolean isServer() {
		return getContext().isGroupManager();
	}

	public void memberAdded(ID member) {
		if (isHost())
			replicate(member);
	}

	public void memberRemoved(ID member) {
	}

	public void msgException(Object target, SharedObjectMsg aMsg, Throwable e) {
		if (e != null)
			e.printStackTrace(System.err);
	}

	public void otherActivated(ID member) {
	}

	public void otherDeactivated(ID member) {
	}

	public void registerProxy(Object object, String msg) {
		registerProxy(object, msg, null);
	}

	protected void registerProxy(Object target, String msg, String method) {
		Assert.isNotNull(msg);
		Assert.isNotNull(target);
		synchronized (msgMapLock) {
			// Create table lazily
			if (msgMap == null)
				msgMap = new Hashtable();
			else if (msgMap.containsKey(msg))
				throw new IllegalArgumentException(
						"registerProxy:  proxy already registered for " //$NON-NLS-1$
								+ method + " by " + target); //$NON-NLS-1$
			// Then put entry into table with msg as key
			msgMap.put(msg, new MsgMap(target, method));
		}
	}

	protected void replicate(ID remote) {
		try {
			// Get current group membership
			ID[] group = getContext().getGroupMemberIDs();
			if (group == null || group.length < 1) {
				// we're done
				return;
			}
			ReplicaSharedObjectDescription createInfo = getReplicaDescription(remote);
			if (createInfo != null)
				getContext().sendCreate(remote, createInfo);
			else
				return;
		} catch (IOException e) {
			log("Exception in replicate", e); //$NON-NLS-1$
		}
	}

	protected void sendSelf(SharedObjectMsg msg) {
		IQueueEnqueue queue = getContext().getQueue();
		try {
			queue.enqueue(new RemoteSharedObjectMsgEvent(getID(), getContext()
					.getLocalContainerID(), msg));
		} catch (QueueException e) {
			log("QueueException enqueing message to self", e); //$NON-NLS-1$
		}
	}

	public void sharedObjectMsg(ID fromID, SharedObjectMsg msg) {
		if (isMsgAllowed(fromID, msg) != null) {
			currentMsgFromObjID = fromID;
			execMsg(localContainerID, msg);
			currentMsgFromObjID = null;
		} else {
			ignoreSharedObjectMsg(fromID, msg);
		}
	}

	protected void trace(String msg) {
	}

	protected void log(String msg, Throwable t) {
		ClientPlugin.log(msg, t);
	}

	public ID createObject(ID target, ReplicaSharedObjectDescription desc)
			throws Exception {
		if (target == null) {
			if (desc.getID() == null) {
				desc.setID(IDFactory.getDefault().createStringID(
						getUniqueString()));
			}
			try {
				return getContext().getSharedObjectManager()
						.createSharedObject(desc);
			} catch (Exception e) {
				log("Exception creating replicated object", e); //$NON-NLS-1$
				throw e;
			}
		} else
			throw new Exception(
					"Cannot send object creation request direct to target"); //$NON-NLS-1$
	}

	public String getUniqueString() {
		return String.valueOf((new Random()).nextLong());
	}
}

Back to the top