Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 34c0a8957a7f5f3d443f49bd55e00e20438fdcd8 (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
/**
 * Copyright (c) 2006 Ecliptical Software 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:
 *     Ecliptical Software Inc. - initial API and implementation
 */
package org.eclipse.ecf.pubsub.model.impl;

import java.util.Map;

import org.eclipse.core.runtime.PlatformObject;
import org.eclipse.ecf.core.IIdentifiable;
import org.eclipse.ecf.core.ISharedObject;
import org.eclipse.ecf.core.ISharedObjectConfig;
import org.eclipse.ecf.core.SharedObjectInitException;
import org.eclipse.ecf.core.events.IContainerConnectedEvent;
import org.eclipse.ecf.core.events.IContainerDisconnectedEvent;
import org.eclipse.ecf.core.events.ISharedObjectActivatedEvent;
import org.eclipse.ecf.core.events.ISharedObjectDeactivatedEvent;
import org.eclipse.ecf.core.events.ISharedObjectMessageEvent;
import org.eclipse.ecf.core.identity.ID;
import org.eclipse.ecf.core.util.Event;
import org.eclipse.ecf.pubsub.model.IModelUpdater;

public abstract class AgentBase extends PlatformObject implements ISharedObject, IIdentifiable {
	
	public static final Object INITIAL_DATA_KEY = new Integer(0);
	
	public static final Object MODEL_UPDATER_KEY = new Integer(1); 
	
	protected static final Object REQUESTOR_ID = new Integer(2); 
	
	protected ISharedObjectConfig config;
	
	protected Object data;
	
	protected String updaterID;
	
	protected IModelUpdater updater;

	public void init(ISharedObjectConfig config) throws SharedObjectInitException {
		Map props = config.getProperties();
		initializeData(props.get(INITIAL_DATA_KEY));
		updaterID = (String) props.get(MODEL_UPDATER_KEY);
		if (updaterID == null)
			throw new SharedObjectInitException("Model Updater is required.");
		
		initializeUpdater();
		this.config = config;
	}
	
	protected abstract void initializeData(Object data) throws SharedObjectInitException;
	
	protected void initializeUpdater() throws SharedObjectInitException {
	}
	
	public Object getData() {
		return data;
	}

	public void handleEvent(Event event) {
		if (event instanceof ISharedObjectActivatedEvent) {
			ISharedObjectActivatedEvent e = (ISharedObjectActivatedEvent) event;
			if (e.getActivatedID().equals(config.getSharedObjectID()))
				activated();
			else
				activated(e.getActivatedID());
		} else if (event instanceof ISharedObjectDeactivatedEvent) {
			ISharedObjectDeactivatedEvent e = (ISharedObjectDeactivatedEvent) event;
			if (e.getDeactivatedID().equals(config.getSharedObjectID()))
				deactivated();
			else
				deactivated(e.getDeactivatedID());
		} else if (event instanceof IContainerConnectedEvent) {
			IContainerConnectedEvent e = (IContainerConnectedEvent) event;
			if (e.getTargetID().equals(e.getLocalContainerID()))
				connected();
			else
				connected(e.getTargetID());
		} else if (event instanceof IContainerDisconnectedEvent) {
			IContainerDisconnectedEvent e = (IContainerDisconnectedEvent) event;
			if (e.getTargetID().equals(e.getLocalContainerID()))
				disconnected();
			else
				disconnected(e.getTargetID());
		} else if (event instanceof ISharedObjectMessageEvent) {
			ISharedObjectMessageEvent e = (ISharedObjectMessageEvent) event;
			received(e.getRemoteContainerID(), e.getData());
		}
	}
	
	protected boolean isConnected() {
		return config.getContext().getConnectedID() != null;
	}
	
	protected void activated(ID sharedObjectID) {
	}
	
	protected void activated() {
	}
	
	protected void deactivated(ID sharedObjectID) {
	}
	
	protected void deactivated() {
	}
	
	protected void connected(ID containerID) {
	}
	
	protected void connected() {
	}
	
	protected void disconnected(ID containerID) {
	}
	
	protected void disconnected() {
	}
	
	protected void received(ID containerID, Object data) { 
	}

	public void handleEvents(Event[] events) {
		for (int i = 0; i < events.length; ++i)
			handleEvent(events[i]);
	}
	
	public ID getID() {
		return config.getSharedObjectID();
	}
	
	public void dispose(ID containerID) {
		config = null;
		data = null;
		updater = null;
	}
}

Back to the top