diff options
| author | pnehrer | 2005-01-31 16:21:10 +0000 |
|---|---|---|
| committer | pnehrer | 2005-01-31 16:21:10 +0000 |
| commit | cebe12eac3c6613e41ddbd9382c9b8e9b8f9b5cc (patch) | |
| tree | 72c35d0c64d988857db7f402aca1dae3a4f8cbdc | |
| parent | 8c996b06a1ebf617c38c60755e406dcb149e0ae6 (diff) | |
| download | org.eclipse.ecf-cebe12eac3c6613e41ddbd9382c9b8e9b8f9b5cc.tar.gz org.eclipse.ecf-cebe12eac3c6613e41ddbd9382c9b8e9b8f9b5cc.tar.xz org.eclipse.ecf-cebe12eac3c6613e41ddbd9382c9b8e9b8f9b5cc.zip | |
Improved failure handling.
13 files changed, 776 insertions, 741 deletions
diff --git a/examples/bundles/org.eclipse.ecf.sdo/src/org/eclipse/ecf/internal/sdo/DataGraphSharing.java b/examples/bundles/org.eclipse.ecf.sdo/src/org/eclipse/ecf/internal/sdo/DataGraphSharing.java index f54698110..fadf0cd58 100644 --- a/examples/bundles/org.eclipse.ecf.sdo/src/org/eclipse/ecf/internal/sdo/DataGraphSharing.java +++ b/examples/bundles/org.eclipse.ecf.sdo/src/org/eclipse/ecf/internal/sdo/DataGraphSharing.java @@ -10,22 +10,19 @@ *******************************************************************************/ package org.eclipse.ecf.internal.sdo; -import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.PlatformObject; -import org.eclipse.core.runtime.Status; import org.eclipse.ecf.core.ISharedObject; import org.eclipse.ecf.core.ISharedObjectConfig; import org.eclipse.ecf.core.ISharedObjectManager; -import org.eclipse.ecf.core.SharedObjectAddException; import org.eclipse.ecf.core.SharedObjectInitException; 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.IDataGraphSharing; 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.DataGraph; @@ -33,144 +30,127 @@ import commonj.sdo.DataGraph; * @author pnehrer */ public class DataGraphSharing extends PlatformObject implements - IDataGraphSharing, ISharedObject { - - public static final String DATA_GRAPH_SHARING_ID = DataGraphSharing.class - .getName(); - - private ISharedObjectConfig config; - - private boolean debug; - - /* - * (non-Javadoc) - * - * @see org.eclipse.ecf.sdo.IDataGraphSharing#publish(commonj.sdo.DataGraph, - * org.eclipse.ecf.core.identity.ID, - * org.eclipse.ecf.sdo.IUpdateProvider, - * org.eclipse.ecf.sdo.IUpdateConsumer) - */ - public synchronized ISharedDataGraph publish(DataGraph dataGraph, ID id, - IUpdateProvider provider, IUpdateConsumer consumer) - throws CoreException { - - if (config == null) - throw new CoreException(new Status(Status.ERROR, SDOPlugin - .getDefault().getBundle().getSymbolicName(), 0, - "Not initialized.", null)); - - // create local object - ISharedObjectManager mgr = config.getContext().getSharedObjectManager(); - SharedDataGraph sdg = new SharedDataGraph(dataGraph, provider, consumer); - sdg.setDebug(debug); - - try { - mgr.addSharedObject(id, sdg, null, null); - } catch (SharedObjectAddException e) { - throw new CoreException(new Status(Status.ERROR, SDOPlugin - .getDefault().getBundle().getSymbolicName(), 0, - "Could not publish data graph.", e)); - } - - return sdg; - } - - /* - * (non-Javadoc) - * - * @see org.eclipse.ecf.sdo.IDataGraphSharing#subscribe(org.eclipse.ecf.core.identity.ID, - * org.eclipse.ecf.sdo.ISubscriptionCallback, - * org.eclipse.ecf.sdo.IUpdateProvider, - * org.eclipse.ecf.sdo.IUpdateConsumer) - */ - public synchronized ISharedDataGraph subscribe(ID id, - ISubscriptionCallback callback, IUpdateProvider provider, - IUpdateConsumer consumer) throws CoreException { - - if (config == null) - throw new CoreException(new Status(Status.ERROR, SDOPlugin - .getDefault().getBundle().getSymbolicName(), 0, - "Not initialized.", null)); - - // create local object - ISharedObjectManager mgr = config.getContext().getSharedObjectManager(); - SharedDataGraph sdg = new SharedDataGraph(null, provider, consumer, - callback); - sdg.setDebug(debug); - - try { - mgr.addSharedObject(id, sdg, null, null); - } catch (SharedObjectAddException e) { - throw new CoreException(new Status(Status.ERROR, SDOPlugin - .getDefault().getBundle().getSymbolicName(), 0, - "Could not subscribe to data graph with id" + id + ".", e)); - } - - return sdg; - } - - /* - * (non-Javadoc) - * - * @see org.eclipse.ecf.sdo.IDataGraphSharing#dispose() - */ - public synchronized void dispose() { - if (config != null) - config.getContext().getSharedObjectManager().removeSharedObject( - config.getSharedObjectID()); - } - - /** - * Sets the debug flag. - * - * @param debug - * @deprecated Use Eclipse's plugin tracing support instead. - */ - public void setDebug(boolean debug) { - this.debug = debug; - } - - /* - * (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."); - } - - /* - * (non-Javadoc) - * - * @see org.eclipse.ecf.core.ISharedObject#handleEvent(org.eclipse.ecf.core.util.Event) - */ - public void handleEvent(Event event) { - } - - /* - * (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]); - } - - /* - * (non-Javadoc) - * - * @see org.eclipse.ecf.core.ISharedObject#dispose(org.eclipse.ecf.core.identity.ID) - */ - public synchronized void dispose(ID containerID) { - if (config != null - && config.getContext().getLocalContainerID() - .equals(containerID)) - config = null; - } + IDataGraphSharing, ISharedObject { + + public static final String DATA_GRAPH_SHARING_ID = DataGraphSharing.class + .getName(); + + private ISharedObjectConfig config; + + private boolean debug; + + /* + * (non-Javadoc) + * + * @see org.eclipse.ecf.sdo.IDataGraphSharing#publish(commonj.sdo.DataGraph, + * org.eclipse.ecf.core.identity.ID, + * org.eclipse.ecf.sdo.IUpdateProvider, + * org.eclipse.ecf.sdo.IUpdateConsumer) + */ + public synchronized ISharedDataGraph publish(DataGraph dataGraph, ID id, + IUpdateProvider provider, IUpdateConsumer consumer) + throws ECFException { + + if (config == null) + throw new ECFException("Not initialized."); + + // create local object + ISharedObjectManager mgr = config.getContext().getSharedObjectManager(); + SharedDataGraph sdg = new SharedDataGraph(dataGraph, provider, + consumer, null); + sdg.setDebug(debug); + + mgr.addSharedObject(id, sdg, null, null); + return sdg; + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.ecf.sdo.IDataGraphSharing#subscribe(org.eclipse.ecf.core.identity.ID, + * org.eclipse.ecf.sdo.ISubscriptionCallback, + * org.eclipse.ecf.sdo.IUpdateProvider, + * org.eclipse.ecf.sdo.IUpdateConsumer) + */ + public synchronized ISharedDataGraph subscribe(ID id, + ISubscriptionCallback callback, IUpdateProvider provider, + IUpdateConsumer consumer) throws ECFException { + + if (config == null) + throw new ECFException("Not initialized."); + + // create local object + ISharedObjectManager mgr = config.getContext().getSharedObjectManager(); + SharedDataGraph sdg = new SharedDataGraph(null, provider, consumer, + callback); + sdg.setDebug(debug); + + mgr.addSharedObject(id, sdg, null, null); + return sdg; + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.ecf.sdo.IDataGraphSharing#dispose() + */ + public synchronized void dispose() { + if (config != null) + config.getContext().getSharedObjectManager().removeSharedObject( + config.getSharedObjectID()); + } + + /** + * Sets the debug flag. + * + * @param debug + * @deprecated Use Eclipse's plugin tracing support instead. + */ + public void setDebug(boolean debug) { + this.debug = debug; + } + + /* + * (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."); + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.ecf.core.ISharedObject#handleEvent(org.eclipse.ecf.core.util.Event) + */ + public void handleEvent(Event event) { + } + + /* + * (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]); + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.ecf.core.ISharedObject#dispose(org.eclipse.ecf.core.identity.ID) + */ + public synchronized void dispose(ID containerID) { + if (config != null + && config.getContext().getLocalContainerID() + .equals(containerID)) + config = null; + } }
\ No newline at end of file diff --git a/examples/bundles/org.eclipse.ecf.sdo/src/org/eclipse/ecf/internal/sdo/ReceiveDataGraphMessage.java b/examples/bundles/org.eclipse.ecf.sdo/src/org/eclipse/ecf/internal/sdo/ReceiveDataGraphMessage.java index b586a06e9..22eebcab4 100644 --- a/examples/bundles/org.eclipse.ecf.sdo/src/org/eclipse/ecf/internal/sdo/ReceiveDataGraphMessage.java +++ b/examples/bundles/org.eclipse.ecf.sdo/src/org/eclipse/ecf/internal/sdo/ReceiveDataGraphMessage.java @@ -17,42 +17,44 @@ import java.io.Serializable; */ public class ReceiveDataGraphMessage implements Serializable { - private final Version version; - - private final byte[] data; - - public ReceiveDataGraphMessage(Version version, byte[] data) { - this.version = version; - this.data = data; - } - - public Version getVersion() { - return version; - } - - public byte[] getData() { - return data; - } - - public boolean equals(Object other) { - if (other instanceof ReceiveDataGraphMessage) { - ReceiveDataGraphMessage o = (ReceiveDataGraphMessage) other; - return version.equals(o.version) && data.equals(o.data); - } else - return false; - } - - public int hashCode() { - int c = 17; - c = 37 * c + version.hashCode(); - c = 37 * c + data.hashCode(); - return c; - } - - public String toString() { - StringBuffer buf = new StringBuffer("ReceiveDataGraphMessage["); - buf.append("version=").append(version).append(";"); - buf.append("data=").append(data).append("]"); - return buf.toString(); - } + private static final long serialVersionUID = 3617293441302606899L; + + private final Version version; + + private final Object data; + + public ReceiveDataGraphMessage(Version version, Object data) { + this.version = version; + this.data = data; + } + + public Version getVersion() { + return version; + } + + public Object getData() { + return data; + } + + public boolean equals(Object other) { + if (other instanceof ReceiveDataGraphMessage) { + ReceiveDataGraphMessage o = (ReceiveDataGraphMessage) other; + return version.equals(o.version) && data.equals(o.data); + } else + return false; + } + + public int hashCode() { + int c = 17; + c = 37 * c + version.hashCode(); + c = 37 * c + data.hashCode(); + return c; + } + + public String toString() { + StringBuffer buf = new StringBuffer("ReceiveDataGraphMessage["); + buf.append("version=").append(version).append(";"); + buf.append("data=").append(data).append("]"); + return buf.toString(); + } } diff --git a/examples/bundles/org.eclipse.ecf.sdo/src/org/eclipse/ecf/internal/sdo/RequestDataGraphMessage.java b/examples/bundles/org.eclipse.ecf.sdo/src/org/eclipse/ecf/internal/sdo/RequestDataGraphMessage.java index 6b2ce7e0c..80a43bdc1 100644 --- a/examples/bundles/org.eclipse.ecf.sdo/src/org/eclipse/ecf/internal/sdo/RequestDataGraphMessage.java +++ b/examples/bundles/org.eclipse.ecf.sdo/src/org/eclipse/ecf/internal/sdo/RequestDataGraphMessage.java @@ -17,4 +17,6 @@ import java.io.Serializable; */ public class RequestDataGraphMessage implements Serializable { + private static final long serialVersionUID = 3546646511992713267L; + } diff --git a/examples/bundles/org.eclipse.ecf.sdo/src/org/eclipse/ecf/internal/sdo/SharedDataGraph.java b/examples/bundles/org.eclipse.ecf.sdo/src/org/eclipse/ecf/internal/sdo/SharedDataGraph.java index 22f42ae08..6f50b5bd6 100644 --- a/examples/bundles/org.eclipse.ecf.sdo/src/org/eclipse/ecf/internal/sdo/SharedDataGraph.java +++ b/examples/bundles/org.eclipse.ecf.sdo/src/org/eclipse/ecf/internal/sdo/SharedDataGraph.java @@ -34,242 +34,253 @@ import commonj.sdo.DataGraph; * @author pnehrer */ public class SharedDataGraph extends PlatformObject implements ISharedObject, - ISharedDataGraph { - - private final IUpdateConsumer updateConsumer; - - private final ISubscriptionCallback subscriptionCallback; - - private final IUpdateProvider updateProvider; - - private ISharedObjectConfig config; - - private DataGraph dataGraph; - - private Version version; - - private boolean debug; - - SharedDataGraph(DataGraph dataGraph, IUpdateProvider updateProvider, - IUpdateConsumer updateConsumer, - ISubscriptionCallback subscriptionCallback) { - this.dataGraph = dataGraph; - this.updateProvider = updateProvider; - this.updateConsumer = updateConsumer; - this.subscriptionCallback = subscriptionCallback; - } - - SharedDataGraph(DataGraph dataGraph, IUpdateProvider updateProvider, - IUpdateConsumer updateConsumer) { - this(dataGraph, updateProvider, updateConsumer, null); - } - - void setDebug(boolean debug) { - this.debug = debug; - } - - /* - * (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."); - } - - /* - * (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) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } else { - if (version == null) - version = new Version(config.getSharedObjectID()); - - dataGraph.getChangeSummary().beginLogging(); - } - } - } 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 { - byte[] 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 version, byte[] data) { - if (dataGraph == null) { - try { - dataGraph = updateProvider.deserializeDataGraph(data); - } catch (ClassNotFoundException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - return; - } - - this.version = version; - dataGraph.getChangeSummary().beginLogging(); - if (subscriptionCallback != null) - subscriptionCallback.dataGraphSubscribed(this, containerID); - } - } - - private synchronized void handleUpdateDataGraphMessage(ID containerID, - Version version, byte[] data) { - - if (dataGraph == null) - return; - - if (!version.equals(this.version)) { - if (debug) - System.err.println("Version mismatch: current=" + this.version - + "; new=" + version); - - updateConsumer.updateFailed(this, containerID); - return; - } - - updateProvider.applyUpdate(this, data); - 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 - && config.getContext().getLocalContainerID() - .equals(containerID)) - config = null; - } - - public String toString() { - StringBuffer buf = new StringBuffer("SharedDataGraph["); - buf.append("config=").append(config).append(";"); - buf.append("dataGraph=").append(dataGraph).append(";"); - buf.append("version=").append(version).append(";"); - buf.append("debug=").append(debug).append("]"); - return buf.toString(); - } + ISharedDataGraph { + + private final IUpdateConsumer updateConsumer; + + private final ISubscriptionCallback subscriptionCallback; + + private final IUpdateProvider updateProvider; + + private ISharedObjectConfig config; + + private DataGraph dataGraph; + + private Version version; + + private boolean debug; + + SharedDataGraph(DataGraph dataGraph, IUpdateProvider updateProvider, + IUpdateConsumer updateConsumer, + 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.subscriptionCallback = subscriptionCallback; + } + + void setDebug(boolean debug) { + this.debug = debug; + } + + /* + * (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."); + } + + /* + * (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 (version == null) + version = new Version(config.getSharedObjectID()); + + dataGraph.getChangeSummary().beginLogging(); + } + } + } 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 version, 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 = version; + dataGraph.getChangeSummary().beginLogging(); + if (subscriptionCallback != null) + subscriptionCallback.dataGraphSubscribed(this, containerID); + } + } + + private synchronized void handleUpdateDataGraphMessage(ID containerID, + Version version, Object data) { + if (dataGraph == null) + return; + + if (!version.equals(this.version)) { + if (debug) + System.err.println("Version mismatch: current=" + this.version + + "; new=" + version); + + 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 + && config.getContext().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(); + } }
\ No newline at end of file diff --git a/examples/bundles/org.eclipse.ecf.sdo/src/org/eclipse/ecf/internal/sdo/UpdateDataGraphMessage.java b/examples/bundles/org.eclipse.ecf.sdo/src/org/eclipse/ecf/internal/sdo/UpdateDataGraphMessage.java index 02ac5f59d..37259c331 100644 --- a/examples/bundles/org.eclipse.ecf.sdo/src/org/eclipse/ecf/internal/sdo/UpdateDataGraphMessage.java +++ b/examples/bundles/org.eclipse.ecf.sdo/src/org/eclipse/ecf/internal/sdo/UpdateDataGraphMessage.java @@ -17,42 +17,44 @@ import java.io.Serializable; */ public class UpdateDataGraphMessage implements Serializable { - private final Version version; - - private final byte[] data; - - public UpdateDataGraphMessage(Version version, byte[] data) { - this.version = version; - this.data = data; - } - - public Version getVersion() { - return version; - } - - public byte[] getData() { - return data; - } - - public boolean equals(Object other) { - if (other instanceof UpdateDataGraphMessage) { - UpdateDataGraphMessage o = (UpdateDataGraphMessage) other; - return version.equals(o.version) && data.equals(o.data); - } else - return false; - } - - public int hashCode() { - int c = 17; - c = 37 * c + version.hashCode(); - c = 37 * c + data.hashCode(); - return c; - } - - public String toString() { - StringBuffer buf = new StringBuffer("UpdateDataGraphMessage["); - buf.append("version=").append(version).append(";"); - buf.append("data=").append(data).append("]"); - return buf.toString(); - } + private static final long serialVersionUID = 3256439205344260914L; + + private final Version version; + + private final Object data; + + public UpdateDataGraphMessage(Version version, Object data) { + this.version = version; + this.data = data; + } + + public Version getVersion() { + return version; + } + + public Object getData() { + return data; + } + + public boolean equals(Object other) { + if (other instanceof UpdateDataGraphMessage) { + UpdateDataGraphMessage o = (UpdateDataGraphMessage) other; + return version.equals(o.version) && data.equals(o.data); + } else + return false; + } + + public int hashCode() { + int c = 17; + c = 37 * c + version.hashCode(); + c = 37 * c + data.hashCode(); + return c; + } + + public String toString() { + StringBuffer buf = new StringBuffer("UpdateDataGraphMessage["); + buf.append("version=").append(version).append(";"); + buf.append("data=").append(data).append("]"); + return buf.toString(); + } } diff --git a/examples/bundles/org.eclipse.ecf.sdo/src/org/eclipse/ecf/internal/sdo/Version.java b/examples/bundles/org.eclipse.ecf.sdo/src/org/eclipse/ecf/internal/sdo/Version.java index 9cf10db99..7ada28faa 100644 --- a/examples/bundles/org.eclipse.ecf.sdo/src/org/eclipse/ecf/internal/sdo/Version.java +++ b/examples/bundles/org.eclipse.ecf.sdo/src/org/eclipse/ecf/internal/sdo/Version.java @@ -19,7 +19,9 @@ import org.eclipse.ecf.core.identity.ID; */ public class Version implements Serializable { - private final long sequence; + private static final long serialVersionUID = 3258415036413456951L; + + private final long sequence; private final ID containerID; diff --git a/examples/bundles/org.eclipse.ecf.sdo/src/org/eclipse/ecf/sdo/IDataGraphSharing.java b/examples/bundles/org.eclipse.ecf.sdo/src/org/eclipse/ecf/sdo/IDataGraphSharing.java index 82756f3ab..451fb7a52 100644 --- a/examples/bundles/org.eclipse.ecf.sdo/src/org/eclipse/ecf/sdo/IDataGraphSharing.java +++ b/examples/bundles/org.eclipse.ecf.sdo/src/org/eclipse/ecf/sdo/IDataGraphSharing.java @@ -10,8 +10,8 @@ *******************************************************************************/ package org.eclipse.ecf.sdo; -import org.eclipse.core.runtime.CoreException; import org.eclipse.ecf.core.identity.ID; +import org.eclipse.ecf.core.util.ECFException; import commonj.sdo.DataGraph; @@ -24,48 +24,48 @@ import commonj.sdo.DataGraph; */ public interface IDataGraphSharing { - /** - * Publishes the given data graph under the given id. - * - * @param dataGraph - * local data graph instance to share - * @param id - * identifier under which to share this data graph - * @param provider - * update provider compatible with the given data graph's - * implementation - * @param consumer - * application-specific update consumer - * @return shared data graph - * @throws CoreException - */ - ISharedDataGraph publish(DataGraph dataGraph, ID id, - IUpdateProvider provider, IUpdateConsumer consumer) - throws CoreException; + /** + * Publishes the given data graph under the given id. + * + * @param dataGraph + * local data graph instance to share + * @param id + * identifier under which to share this data graph + * @param provider + * update provider compatible with the given data graph's + * implementation + * @param consumer + * application-specific update consumer + * @return shared data graph + * @throws ECFException + */ + ISharedDataGraph publish(DataGraph dataGraph, ID id, + IUpdateProvider provider, IUpdateConsumer consumer) + throws ECFException; - /** - * Subscribes to a data graph with the given id. - * - * @param id - * identifier of a previously-published data graph - * @param callback - * callback used to notify the caller when the subscription - * completes - * @param provider - * update provider compatible with the given data graph's - * implementation - * @param consumer - * application-specific update consumer - * @return shared data graph - * @throws CoreException - */ - ISharedDataGraph subscribe(ID id, ISubscriptionCallback callback, - IUpdateProvider provider, IUpdateConsumer consumer) - throws CoreException; + /** + * Subscribes to a data graph with the given id. + * + * @param id + * identifier of a previously-published data graph + * @param callback + * optional callback used to notify the caller about subscription + * status + * @param provider + * update provider compatible with the given data graph's + * implementation + * @param consumer + * application-specific update consumer + * @return shared data graph + * @throws ECFException + */ + ISharedDataGraph subscribe(ID id, ISubscriptionCallback callback, + IUpdateProvider provider, IUpdateConsumer consumer) + throws ECFException; - /** - * Disposes this instance, after which it will be no longer possible to - * publish or subscribe. - */ - void dispose(); + /** + * Disposes this instance, after which it will be no longer possible to + * publish or subscribe. + */ + void dispose(); } diff --git a/examples/bundles/org.eclipse.ecf.sdo/src/org/eclipse/ecf/sdo/ISubscriptionCallback.java b/examples/bundles/org.eclipse.ecf.sdo/src/org/eclipse/ecf/sdo/ISubscriptionCallback.java index f1149c797..856984777 100644 --- a/examples/bundles/org.eclipse.ecf.sdo/src/org/eclipse/ecf/sdo/ISubscriptionCallback.java +++ b/examples/bundles/org.eclipse.ecf.sdo/src/org/eclipse/ecf/sdo/ISubscriptionCallback.java @@ -14,20 +14,30 @@ import org.eclipse.ecf.core.identity.ID; /** * Interface used by service implementations to notify subscribing applications - * when the initial copy of the data graph has been obtained. + * of the subscription status. * * @author pnehrer */ public interface ISubscriptionCallback { - /** - * Notifies the implementor that the given graph has been successfully - * subscribed to. - * - * @param graph - * shared data graph that has been subscribed - * @param containerID - * id of the container that originated the initial copy - */ - void dataGraphSubscribed(ISharedDataGraph graph, ID containerID); + /** + * Notifies the implementor that the given graph has been successfully + * subscribed to. + * + * @param graph + * shared data graph that has been subscribed + * @param containerID + * id of the container that originated the initial copy + */ + void dataGraphSubscribed(ISharedDataGraph graph, ID containerID); + + /** + * Notifies the implementor that the subscription failed. + * + * @param graph + * shared data graph whose subscription failed + * @param cause + * exception that is the cause of the failure + */ + void subscriptionFailed(ISharedDataGraph graph, Throwable cause); } diff --git a/examples/bundles/org.eclipse.ecf.sdo/src/org/eclipse/ecf/sdo/IUpdateConsumer.java b/examples/bundles/org.eclipse.ecf.sdo/src/org/eclipse/ecf/sdo/IUpdateConsumer.java index 2cf0c1279..2b53dfd0b 100644 --- a/examples/bundles/org.eclipse.ecf.sdo/src/org/eclipse/ecf/sdo/IUpdateConsumer.java +++ b/examples/bundles/org.eclipse.ecf.sdo/src/org/eclipse/ecf/sdo/IUpdateConsumer.java @@ -20,28 +20,31 @@ import org.eclipse.ecf.core.identity.ID; */ public interface IUpdateConsumer { - /** - * Consumes the remote update. The implementor is expected to leave behind - * an empty Change Summary. - * - * @param graph - * shared data graph whose remote changes to consume - * @param containerID - * id of the remote container that made the change - * @return <code>true</code> if the update has been consumed, - * <code>false</code> otherwise (the update will be rejected as a - * result) - */ - boolean consumeUpdate(ISharedDataGraph graph, ID containerID); + /** + * Gives implementor the opportunity to consume the remote update. The + * implementor is expected to leave behind an empty Change Summary (with + * logging turned on). + * + * @param graph + * shared data graph whose remote changes to consume + * @param containerID + * id of the remote container that made the change + * @return <code>true</code> if the update has been consumed, + * <code>false</code> otherwise (the update will be rejected as a + * result) + */ + boolean consumeUpdate(ISharedDataGraph graph, ID containerID); - /** - * Notifies the implementor that a remote update has been received, but was - * not successfully applied (i.e., cannot be consumed). - * - * @param graph - * shared data graph whose update failed - * @param containerID - * id of the container that sent the update - */ - void updateFailed(ISharedDataGraph graph, ID containerID); + /** + * Notifies the implementor that a remote update has been received, but was + * not successfully applied (i.e., cannot be consumed). + * + * @param graph + * shared data graph whose update failed + * @param containerID + * id of the container that sent the update + * @param cause + * optional exception that caused the failure + */ + void updateFailed(ISharedDataGraph graph, ID containerID, Throwable cause); } diff --git a/examples/bundles/org.eclipse.ecf.sdo/src/org/eclipse/ecf/sdo/IUpdateProvider.java b/examples/bundles/org.eclipse.ecf.sdo/src/org/eclipse/ecf/sdo/IUpdateProvider.java index 7aceb9150..e53dfbf1a 100644 --- a/examples/bundles/org.eclipse.ecf.sdo/src/org/eclipse/ecf/sdo/IUpdateProvider.java +++ b/examples/bundles/org.eclipse.ecf.sdo/src/org/eclipse/ecf/sdo/IUpdateProvider.java @@ -25,7 +25,7 @@ import commonj.sdo.DataGraph; * update to the local data graph. * </p> * <p> - * Until serialization-related issues witin ECF are resolved, the service also + * Until serialization-related issues within ECF are resolved, the service also * needs to delegate data graph (de)serialization during subscription. * </p> * @@ -33,46 +33,49 @@ import commonj.sdo.DataGraph; */ public interface IUpdateProvider { - /** - * Creates an update from the given data graph, which will be forwarded to - * other group members. The implementor may use the graph's Change Summary - * to find out what changed. - * - * @param graph - * shared data graph from whose changes to create the update - * @return serialized update data - * @throws ECFException - */ - byte[] createUpdate(ISharedDataGraph graph) throws ECFException; + /** + * Creates an update from the given data graph, which will be forwarded to + * other group members. The implementor may use the graph's Change Summary + * to find out what changed. + * + * @param graph + * shared data graph from whose changes to create the update + * @return serialized update data + * @throws ECFException when an update cannot be created + */ + byte[] createUpdate(ISharedDataGraph graph) throws ECFException; - /** - * Applies the remote update to the given data graph. The implementor is - * expected to create a Change Summary that reflects the received changes. - * - * @param graph - * local data graph to which to apply the update - * @param data - * update data received from a remote group member - */ - void applyUpdate(ISharedDataGraph graph, byte[] data); + /** + * Applies the remote update to the given data graph. The implementor is + * expected to create a Change Summary that reflects the received changes. + * + * @param graph + * local data graph to which to apply the update + * @param data + * update data received from a remote group member + * @throws ECFException when this update cannot be applied + */ + void applyUpdate(ISharedDataGraph graph, Object data) throws ECFException; - /** - * Serializes the given data graph. - * - * @param graph - * data graph instance to serialize - * @return serialized data graph - * @throws IOException - */ - byte[] serializeDataGraph(DataGraph graph) throws IOException; + /** + * Serializes the given data graph. + * + * @param graph + * data graph instance to serialize + * @return serialized data graph + * @throws IOException + */ + Object serializeDataGraph(DataGraph graph) throws IOException; - /** - * Deserializes the given data graph. - * - * @param data - * serialized data graph - * @return deserialized instance of data graph - * @throws ClassNotFoundException - */ - DataGraph deserializeDataGraph(byte[] data) throws ClassNotFoundException; + /** + * Deserializes the given data graph. + * + * @param data + * serialized data graph + * @return deserialized instance of data graph + * @throws IOException + * @throws ClassNotFoundException + */ + DataGraph deserializeDataGraph(Object data) throws IOException, + ClassNotFoundException; } diff --git a/examples/bundles/org.eclipse.ecf.sdo/src/org/eclipse/ecf/sdo/SubscriptionBlocker.java b/examples/bundles/org.eclipse.ecf.sdo/src/org/eclipse/ecf/sdo/SubscriptionBlocker.java deleted file mode 100644 index bdfad8415..000000000 --- a/examples/bundles/org.eclipse.ecf.sdo/src/org/eclipse/ecf/sdo/SubscriptionBlocker.java +++ /dev/null @@ -1,52 +0,0 @@ -/******************************************************************************* - * 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.sdo; - -import org.eclipse.ecf.core.identity.ID; - -/** - * Convenience callback implementation that can be used to block the calling - * thread until the data graph is obtained. - * - * @author pnehrer - */ -public class SubscriptionBlocker implements ISubscriptionCallback { - - private boolean subscribed; - - /* - * (non-Javadoc) - * - * @see org.eclipse.ecf.sdo.ISubscriptionCallback#dataGraphObtained(org.eclipse.ecf.sdo.ISharedDataGraph, - * org.eclipse.ecf.core.identity.ID) - */ - public synchronized void dataGraphSubscribed(ISharedDataGraph graph, - ID containerID) { - subscribed = true; - notifyAll(); - } - - /** - * Blocks the calling thread until the data graph is obtained. - * - * @param timeout - * period, in milliseconds, to wait for subscription - * @return <code>true</code> if the data graph has been obtained - * @throws InterruptedException - */ - public synchronized boolean waitForSubscription(long timeout) - throws InterruptedException { - if (!subscribed) - wait(timeout); - - return subscribed; - } -} diff --git a/examples/bundles/org.eclipse.ecf.sdo/src/org/eclipse/ecf/sdo/WaitableSubscriptionCallback.java b/examples/bundles/org.eclipse.ecf.sdo/src/org/eclipse/ecf/sdo/WaitableSubscriptionCallback.java new file mode 100644 index 000000000..c52207ebd --- /dev/null +++ b/examples/bundles/org.eclipse.ecf.sdo/src/org/eclipse/ecf/sdo/WaitableSubscriptionCallback.java @@ -0,0 +1,73 @@ +/******************************************************************************* + * 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.sdo; + +import org.eclipse.ecf.core.identity.ID; +import org.eclipse.ecf.core.util.ECFException; + +/** + * Convenience callback implementation that can be used to block the calling + * thread until the data graph is obtained. + * + * @author pnehrer + */ +public class WaitableSubscriptionCallback implements ISubscriptionCallback { + + private ID containerID; + + private Throwable cause; + + /* + * (non-Javadoc) + * + * @see org.eclipse.ecf.sdo.ISubscriptionCallback#dataGraphObtained(org.eclipse.ecf.sdo.ISharedDataGraph, + * org.eclipse.ecf.core.identity.ID) + */ + public synchronized void dataGraphSubscribed(ISharedDataGraph graph, + ID containerID) { + this.containerID = containerID; + notifyAll(); + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.ecf.sdo.ISubscriptionCallback#subscriptionFailed(org.eclipse.ecf.sdo.ISharedDataGraph, + * java.lang.Throwable) + */ + public synchronized void subscriptionFailed(ISharedDataGraph graph, + Throwable cause) { + this.cause = cause; + notifyAll(); + } + + /** + * Blocks the calling thread until the data graph is obtained. + * + * @param timeout + * period, in milliseconds, to wait for subscription + * @return id of the container from which the data graph originated + * @throws InterruptedException + * if interrupted while waiting for notification + * @throws ECFException + * if subscription failed + */ + public synchronized ID waitForSubscription(long timeout) + throws InterruptedException, ECFException { + if (containerID == null && cause == null) + wait(timeout); + + if (cause != null) + throw new ECFException(cause); + + return containerID; + } +} diff --git a/examples/bundles/org.eclipse.ecf.sdo/src/org/eclipse/ecf/sdo/emf/EMFUpdateProvider.java b/examples/bundles/org.eclipse.ecf.sdo/src/org/eclipse/ecf/sdo/emf/EMFUpdateProvider.java index 94a8e4ef7..fcf3b2661 100644 --- a/examples/bundles/org.eclipse.ecf.sdo/src/org/eclipse/ecf/sdo/emf/EMFUpdateProvider.java +++ b/examples/bundles/org.eclipse.ecf.sdo/src/org/eclipse/ecf/sdo/emf/EMFUpdateProvider.java @@ -31,117 +31,116 @@ import commonj.sdo.DataGraph; */ public class EMFUpdateProvider implements IUpdateProvider { - private boolean debug; - - /** - * Sets the debug flag. - * - * @param debug - * @deprecated Use Eclipse's plugin tracing support instead. - */ - public void setDebug(boolean debug) { - this.debug = debug; - } - - private EDataGraph clone(EDataGraph source) throws IOException { - ByteArrayOutputStream buf = new ByteArrayOutputStream(); - source.getDataGraphResource().save(buf, null); - return SDOUtil.loadDataGraph( - new ByteArrayInputStream(buf.toByteArray()), null); - } - - /* - * (non-Javadoc) - * - * @see org.eclipse.ecf.sdo.IUpdateProvider#createUpdate(org.eclipse.ecf.sdo.ISharedDataGraph) - */ - public byte[] createUpdate(ISharedDataGraph graph) throws ECFException { - EDataGraph clone; - try { - clone = clone((EDataGraph) graph.getDataGraph()); - } catch (IOException e) { - throw new ECFException(e); - } - - EChangeSummary changes = (EChangeSummary) clone.getChangeSummary(); - changes.applyAndReverse(); - - ByteArrayOutputStream buf = new ByteArrayOutputStream(); - try { - changes.eResource().save(buf, null); - if (debug) { - System.out.println("commit:"); - changes.eResource().save(System.out, null); - } - } catch (IOException e) { - throw new ECFException(e); - } - - return buf.toByteArray(); - } - - /* - * (non-Javadoc) - * - * @see org.eclipse.ecf.sdo.IUpdateProvider#applyUpdate(org.eclipse.ecf.sdo.ISharedDataGraph, - * byte[]) - */ - public void applyUpdate(ISharedDataGraph graph, byte[] data) { - EDataGraph dataGraph = (EDataGraph) graph.getDataGraph(); - EChangeSummary changeSummary = (EChangeSummary) dataGraph - .getChangeSummary(); - changeSummary.endLogging(); - changeSummary.apply(); - - Resource res = changeSummary.eResource(); - res.unload(); - - // apply changes from the event - try { - res.load(new ByteArrayInputStream(data), null); - if (debug) { - System.out.println("processUpdate:"); - res.save(System.out, null); - } - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - changeSummary = (EChangeSummary) res.getContents().get(0); - dataGraph.setEChangeSummary(changeSummary); - changeSummary.applyAndReverse(); - changeSummary.resumeLogging(); - } - - /* - * (non-Javadoc) - * - * @see org.eclipse.ecf.sdo.IUpdateProvider#serializeDataGraph(commonj.sdo.DataGraph) - */ - public byte[] serializeDataGraph(DataGraph dataGraph) throws IOException { - EDataGraph clone = clone((EDataGraph) dataGraph); - EChangeSummary changeSummary = clone.getEChangeSummary(); - if (changeSummary != null) - changeSummary.apply(); - - ByteArrayOutputStream buf = new ByteArrayOutputStream(); - clone.getDataGraphResource().save(buf, null); - return buf.toByteArray(); - } - - /* - * (non-Javadoc) - * - * @see org.eclipse.ecf.sdo.IUpdateProvider#deserializeDataGraph(byte[]) - */ - public DataGraph deserializeDataGraph(byte[] data) - throws ClassNotFoundException { - try { - return SDOUtil.loadDataGraph(new ByteArrayInputStream(data), null); - } catch (IOException e) { - throw new ClassNotFoundException( - "Could not deserialize data graph.", e); - } - } + private boolean debug; + + /** + * Sets the debug flag. + * + * @param debug + * @deprecated Use Eclipse's plugin tracing support instead. + */ + public void setDebug(boolean debug) { + this.debug = debug; + } + + private EDataGraph clone(EDataGraph source) throws IOException { + ByteArrayOutputStream buf = new ByteArrayOutputStream(); + source.getDataGraphResource().save(buf, null); + return SDOUtil.loadDataGraph( + new ByteArrayInputStream(buf.toByteArray()), null); + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.ecf.sdo.IUpdateProvider#createUpdate(org.eclipse.ecf.sdo.ISharedDataGraph) + */ + public byte[] createUpdate(ISharedDataGraph graph) throws ECFException { + EDataGraph clone; + try { + clone = clone((EDataGraph) graph.getDataGraph()); + } catch (IOException e) { + throw new ECFException(e); + } + + EChangeSummary changes = (EChangeSummary) clone.getChangeSummary(); + changes.applyAndReverse(); + + ByteArrayOutputStream buf = new ByteArrayOutputStream(); + try { + changes.eResource().save(buf, null); + if (debug) { + System.out.println("commit:"); + changes.eResource().save(System.out, null); + } + } catch (IOException e) { + throw new ECFException(e); + } + + return buf.toByteArray(); + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.ecf.sdo.IUpdateProvider#applyUpdate(org.eclipse.ecf.sdo.ISharedDataGraph, + * byte[]) + */ + public void applyUpdate(ISharedDataGraph graph, Object data) + throws ECFException { + EDataGraph dataGraph = (EDataGraph) graph.getDataGraph(); + EChangeSummary changeSummary = (EChangeSummary) dataGraph + .getChangeSummary(); + changeSummary.endLogging(); + // throw away any local changes + changeSummary.apply(); + + Resource res = changeSummary.eResource(); + res.unload(); + + // apply changes from the event + try { + res.load(new ByteArrayInputStream((byte[]) data), null); + if (debug) { + System.out.println("processUpdate:"); + res.save(System.out, null); + } + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + changeSummary = (EChangeSummary) res.getContents().get(0); + dataGraph.setEChangeSummary(changeSummary); + // leave a change summary showing what has changed + changeSummary.applyAndReverse(); + changeSummary.resumeLogging(); + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.ecf.sdo.IUpdateProvider#serializeDataGraph(commonj.sdo.DataGraph) + */ + public Object serializeDataGraph(DataGraph dataGraph) throws IOException { + EDataGraph clone = clone((EDataGraph) dataGraph); + EChangeSummary changeSummary = clone.getEChangeSummary(); + if (changeSummary != null) + changeSummary.apply(); + + ByteArrayOutputStream buf = new ByteArrayOutputStream(); + clone.getDataGraphResource().save(buf, null); + return buf.toByteArray(); + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.ecf.sdo.IUpdateProvider#deserializeDataGraph(Object) + */ + public DataGraph deserializeDataGraph(Object data) throws IOException, + ClassNotFoundException { + return SDOUtil.loadDataGraph(new ByteArrayInputStream((byte[]) data), + null); + } } |
