Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f3e11f7294480c64b39b6ac189cd71036e8ab6d8 (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
/*******************************************************************************
 * Copyright (c) 2004 Peter Nehrer and Composent, Inc.
 * 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:
 *     Peter Nehrer - initial API and implementation
 *******************************************************************************/
package org.eclipse.ecf.internal.sdo;

import java.io.IOException;

import org.eclipse.ecf.core.ISharedObject;
import org.eclipse.ecf.core.ISharedObjectConfig;
import org.eclipse.ecf.core.ISharedObjectContext;
import org.eclipse.ecf.core.SharedObjectInitException;
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.ECFException;
import org.eclipse.ecf.core.util.Event;
import org.eclipse.ecf.sdo.IPublicationCallback;
import org.eclipse.ecf.sdo.ISharedDataGraph;
import org.eclipse.ecf.sdo.ISubscriptionCallback;
import org.eclipse.ecf.sdo.IUpdateConsumer;
import org.eclipse.ecf.sdo.IUpdateProvider;
import org.eclipse.ecf.sdo.SDOPlugin;

import commonj.sdo.ChangeSummary;
import commonj.sdo.DataGraph;

/**
 * @author pnehrer
 */
public class SharedDataGraph implements ISharedObject, ISharedDataGraph {

    public static final String TRACE_TAG = "SharedDataGraph";

    private final IUpdateConsumer updateConsumer;

    private final ISubscriptionCallback subscriptionCallback;

    private final IPublicationCallback publicationCallback;

    private final IUpdateProvider updateProvider;

    private ISharedObjectConfig config;

    private DataGraph dataGraph;

    private Version version;

    SharedDataGraph(DataGraph dataGraph, IUpdateProvider updateProvider,
            IUpdateConsumer updateConsumer,
            IPublicationCallback publicationCallback,
            ISubscriptionCallback subscriptionCallback) {
        if (updateProvider == null)
            throw new IllegalArgumentException("updateProvider");

        if (updateConsumer == null)
            throw new IllegalArgumentException("updateConsumer");

        this.dataGraph = dataGraph;
        this.updateProvider = updateProvider;
        this.updateConsumer = updateConsumer;
        this.publicationCallback = publicationCallback;
        this.subscriptionCallback = subscriptionCallback;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ecf.sdo.ISharedDataGraph#getID()
     */
    public synchronized ID getID() {
        return config == null ? null : config.getSharedObjectID();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ecf.sdo.ISharedDataGraph#getDataGraph()
     */
    public synchronized DataGraph getDataGraph() {
        return dataGraph;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ecf.sdo.ISharedDataGraph#dispose()
     */
    public synchronized void dispose() {
        if (config != null)
            config.getContext().getSharedObjectManager().removeSharedObject(
                    config.getSharedObjectID());
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ecf.sdo.ISharedDataGraph#commit()
     */
    public synchronized void commit() throws ECFException {
        if (config == null)
            throw new ECFException("Object is disconnected.");

        if (dataGraph == null)
            throw new ECFException("Not subscribed.");

        ChangeSummary changeSummary = dataGraph.getChangeSummary();
        if (changeSummary.getChangedDataObjects().isEmpty())
            return;

        changeSummary.endLogging();
        byte[] data = updateProvider.createUpdate(this);
        try {
            config.getContext().sendMessage(null,
                    new UpdateDataGraphMessage(version, data));
        } catch (IOException e) {
            throw new ECFException(e);
        }

        changeSummary.beginLogging();
        version = version.getNext(config.getContext().getLocalContainerID());
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ecf.core.ISharedObject#init(org.eclipse.ecf.core.ISharedObjectConfig)
     */
    public synchronized void init(ISharedObjectConfig initData)
            throws SharedObjectInitException {
        if (config == null)
            config = initData;
        else
            throw new SharedObjectInitException("Already initialized.");

        if (version == null)
            version = new Version(config.getSharedObjectID());

        if (dataGraph != null)
            dataGraph.getChangeSummary().beginLogging();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ecf.core.ISharedObject#handleEvent(org.eclipse.ecf.util.Event)
     */
    public void handleEvent(Event event) {
        if (event instanceof ISharedObjectActivatedEvent
                && ((ISharedObjectActivatedEvent) event).getActivatedID()
                        .equals(config.getSharedObjectID())) {
            synchronized (this) {
                if (dataGraph == null) {
                    try {
                        config.getContext().sendMessage(null,
                                new RequestDataGraphMessage());
                    } catch (IOException e) {
                        if (subscriptionCallback != null)
                            subscriptionCallback.subscriptionFailed(this, e);
                    }
                } else if (publicationCallback != null)
                    publicationCallback.dataGraphPublished(this);
            }
        } else if (event instanceof ISharedObjectDeactivatedEvent
                && ((ISharedObjectDeactivatedEvent) event).getDeactivatedID()
                        .equals(config.getSharedObjectID())) {
            synchronized (this) {
                if (dataGraph != null
                        && dataGraph.getChangeSummary().isLogging())
                    dataGraph.getChangeSummary().endLogging();
            }
        } else if (event instanceof ISharedObjectMessageEvent) {
            ISharedObjectMessageEvent e = (ISharedObjectMessageEvent) event;
            Object msg = e.getData();
            if (msg instanceof RequestDataGraphMessage)
                handleRequestDataGraphMessage(e.getRemoteContainerID());
            else if (msg instanceof ReceiveDataGraphMessage) {
                ReceiveDataGraphMessage m = (ReceiveDataGraphMessage) msg;
                handleReceiveDataGraphMessage(e.getRemoteContainerID(), m
                        .getVersion(), m.getData());
            } else if (msg instanceof UpdateDataGraphMessage) {
                UpdateDataGraphMessage m = (UpdateDataGraphMessage) msg;
                handleUpdateDataGraphMessage(e.getRemoteContainerID(), m
                        .getVersion(), m.getData());
            }
        }
    }

    private synchronized void handleRequestDataGraphMessage(ID containerID) {
        if (dataGraph == null)
            return;

        try {
            Object data = updateProvider.serializeDataGraph(dataGraph);
            config.getContext().sendMessage(containerID,
                    new ReceiveDataGraphMessage(version, data));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private synchronized void handleReceiveDataGraphMessage(ID containerID,
            Version v, Object data) {
        if (dataGraph == null) {
            try {
                dataGraph = updateProvider.deserializeDataGraph(data);
            } catch (IOException e) {
                // keep waiting; maybe we can successfully deserialize another
                // message...
                return;
            } catch (ClassNotFoundException e) {
                // keep waiting; maybe we can successfully deserialize another
                // message...
                return;
            }

            this.version = v;
            dataGraph.getChangeSummary().beginLogging();
            if (subscriptionCallback != null)
                subscriptionCallback.dataGraphSubscribed(this, containerID);
        }
    }

    private synchronized void handleUpdateDataGraphMessage(ID containerID,
            Version v, Object data) {
        if (dataGraph == null)
            return;

        if (!v.equals(this.version)) {
            if (SDOPlugin.isTracing(TRACE_TAG))
                SDOPlugin.getTraceLog().println(
                        "Version mismatch: current=" + this.version + "; new="
                                + v);

            updateConsumer.updateFailed(this, containerID, null);
            return;
        }

        try {
            updateProvider.applyUpdate(this, data);
        } catch (ECFException e) {
            updateConsumer.updateFailed(this, containerID, e);
            return;
        }

        if (updateConsumer.consumeUpdate(this, containerID))
            this.version = version.getNext(containerID);
    }

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

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ecf.core.ISharedObject#dispose(org.eclipse.ecf.identity.ID)
     */
    public synchronized void dispose(ID containerID) {
        if (config != null) {
            // TODO Do we even have a context now?
            ISharedObjectContext context = config.getContext();
            if (context != null
                    && context.getLocalContainerID().equals(containerID)) {
                config = null;
            }
        }
    }

    public String toString() {
        StringBuffer buf = new StringBuffer("SharedDataGraph[");
        buf.append("provider=").append(updateProvider).append(";");
        buf.append("consumer=").append(updateConsumer).append(";");
        buf.append("callback=").append(subscriptionCallback).append(";");
        buf.append("config=").append(config).append(";");
        buf.append("dataGraph=").append(dataGraph).append(";");
        buf.append("version=").append(version).append("]");
        return buf.toString();
    }

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

Back to the top