diff options
9 files changed, 552 insertions, 540 deletions
diff --git a/examples/bundles/org.eclipse.ecf.example.sdo.editor/src/org/eclipse/ecf/example/sdo/editor/EditorPlugin.java b/examples/bundles/org.eclipse.ecf.example.sdo.editor/src/org/eclipse/ecf/example/sdo/editor/EditorPlugin.java index 2f1cfaed8..cd0cd2e00 100644 --- a/examples/bundles/org.eclipse.ecf.example.sdo.editor/src/org/eclipse/ecf/example/sdo/editor/EditorPlugin.java +++ b/examples/bundles/org.eclipse.ecf.example.sdo.editor/src/org/eclipse/ecf/example/sdo/editor/EditorPlugin.java @@ -12,6 +12,7 @@ package org.eclipse.ecf.example.sdo.editor; import java.util.MissingResourceException; import java.util.ResourceBundle; + import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.CoreException; @@ -22,14 +23,15 @@ import org.eclipse.ecf.core.identity.ID; import org.eclipse.ecf.core.identity.IDFactory; import org.eclipse.ecf.core.util.ECFException; import org.eclipse.ecf.example.collab.Client; +import org.eclipse.ecf.sdo.DataGraphSharingFactory; import org.eclipse.ecf.sdo.ISharedDataGraph; import org.eclipse.ecf.sdo.IUpdateConsumer; -import org.eclipse.ecf.sdo.SDOPlugin; import org.eclipse.ecf.sdo.WaitablePublicationCallback; import org.eclipse.ecf.sdo.WaitableSubscriptionCallback; import org.eclipse.ecf.sdo.emf.EMFUpdateProvider; import org.eclipse.ui.plugin.AbstractUIPlugin; import org.osgi.framework.BundleContext; + import commonj.sdo.DataGraph; /** @@ -117,12 +119,11 @@ public class EditorPlugin extends AbstractUIPlugin { .getWorkspace().getRoot().getProject(p.segment(0))); PublishedGraphTracker tracker = getTracker(container); - SDOPlugin.getDefault().setDebug(true); ID id = IDFactory.makeStringID(path); WaitableSubscriptionCallback mutex = new WaitableSubscriptionCallback(); - ISharedDataGraph result = SDOPlugin.getDefault().getDataGraphSharing( - container).subscribe(id, new EMFUpdateProvider(), consumer, - mutex); + ISharedDataGraph result = DataGraphSharingFactory.getDataGraphSharing( + container, "default").subscribe(id, new EMFUpdateProvider(), + consumer, mutex); ID containerID = null; try { containerID = mutex.waitForSubscription(5000); @@ -144,12 +145,11 @@ public class EditorPlugin extends AbstractUIPlugin { .getWorkspace().getRoot().getProject(p.segment(0))); PublishedGraphTracker tracker = getTracker(container); - SDOPlugin.getDefault().setDebug(true); ID id = IDFactory.makeStringID(path); WaitablePublicationCallback mutex = new WaitablePublicationCallback(); - ISharedDataGraph result = SDOPlugin.getDefault().getDataGraphSharing( - container).publish(dataGraph, id, new EMFUpdateProvider(), - consumer, mutex); + ISharedDataGraph result = DataGraphSharingFactory.getDataGraphSharing( + container, "default").publish(dataGraph, id, + new EMFUpdateProvider(), consumer, mutex); try { if (!mutex.waitForPublication(5000)) throw new ECFException("Publication timed out."); diff --git a/examples/bundles/org.eclipse.ecf.sdo/.options b/examples/bundles/org.eclipse.ecf.sdo/.options new file mode 100644 index 000000000..f460437c0 --- /dev/null +++ b/examples/bundles/org.eclipse.ecf.sdo/.options @@ -0,0 +1,4 @@ +org.eclipse.ecf.sdo/debug=true +org.eclipse.ecf.sdo/DataGraphSharingFactory=true +org.eclipse.ecf.sdo/SharedDataGraph=true +org.eclipse.ecf.sdo/EMFUpdateManager=true diff --git a/examples/bundles/org.eclipse.ecf.sdo/META-INF/MANIFEST.MF b/examples/bundles/org.eclipse.ecf.sdo/META-INF/MANIFEST.MF index 6b8436287..a61524ba5 100644 --- a/examples/bundles/org.eclipse.ecf.sdo/META-INF/MANIFEST.MF +++ b/examples/bundles/org.eclipse.ecf.sdo/META-INF/MANIFEST.MF @@ -3,7 +3,7 @@ Bundle-Name: ECF SDO Core Bundle-SymbolicName: org.eclipse.ecf.sdo Bundle-Version: 1.0.0 Bundle-ClassPath: ecf.sdo.jar -Bundle-Activator: org.eclipse.ecf.sdo.SDOPlugin +Bundle-Activator: org.eclipse.ecf.sdo.SDOPlugin$EclipsePlugin Bundle-Vendor: Eclipse.org Bundle-Localization: plugin Require-Bundle: org.eclipse.core.runtime, 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 3cb8412d0..d679269ba 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 @@ -37,8 +37,6 @@ public class DataGraphSharing implements private ISharedObjectConfig config; - private boolean debug; - /* * (non-Javadoc) * @@ -59,8 +57,6 @@ public class DataGraphSharing implements ISharedObjectManager mgr = config.getContext().getSharedObjectManager(); SharedDataGraph sdg = new SharedDataGraph(dataGraph, provider, consumer, callback, null); - sdg.setDebug(debug); - mgr.addSharedObject(id, sdg, null, null); return sdg; } @@ -84,8 +80,6 @@ public class DataGraphSharing implements ISharedObjectManager mgr = config.getContext().getSharedObjectManager(); SharedDataGraph sdg = new SharedDataGraph(null, provider, consumer, null, callback); - sdg.setDebug(debug); - mgr.addSharedObject(id, sdg, null, null); return sdg; } @@ -101,16 +95,6 @@ public class DataGraphSharing implements 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) * diff --git a/examples/bundles/org.eclipse.ecf.sdo/src/org/eclipse/ecf/internal/sdo/DataGraphSharingManager.java b/examples/bundles/org.eclipse.ecf.sdo/src/org/eclipse/ecf/internal/sdo/DataGraphSharingManager.java index b126aea28..0169ed609 100644 --- a/examples/bundles/org.eclipse.ecf.sdo/src/org/eclipse/ecf/internal/sdo/DataGraphSharingManager.java +++ b/examples/bundles/org.eclipse.ecf.sdo/src/org/eclipse/ecf/internal/sdo/DataGraphSharingManager.java @@ -35,7 +35,6 @@ public class DataGraphSharingManager implements IDataGraphSharingManager { DataGraphSharing result = (DataGraphSharing) mgr.getSharedObject(id); if (result == null) { result = new DataGraphSharing(); - // result.setDebug(debug); mgr.addSharedObject(id, result, null, null); } 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 2f3fa0baf..14f2b70d1 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 @@ -27,6 +27,7 @@ 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; @@ -34,266 +35,266 @@ import commonj.sdo.DataGraph; /** * @author pnehrer */ -public class SharedDataGraph implements ISharedObject, - ISharedDataGraph { - - 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; - - private boolean debug; - - 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; - } - - 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."); - - 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 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) { - ISharedObjectContext context = config.getContext(); - if (context != null && context.getLocalContainerID().equals(containerID)) { +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 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 (SDOPlugin.isTracing(TRACE_TAG)) + SDOPlugin.getTraceLog().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) { + // 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) + } + + 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) { diff --git a/examples/bundles/org.eclipse.ecf.sdo/src/org/eclipse/ecf/sdo/DataGraphSharingFactory.java b/examples/bundles/org.eclipse.ecf.sdo/src/org/eclipse/ecf/sdo/DataGraphSharingFactory.java index 8e809fe67..2f04edcaa 100644 --- a/examples/bundles/org.eclipse.ecf.sdo/src/org/eclipse/ecf/sdo/DataGraphSharingFactory.java +++ b/examples/bundles/org.eclipse.ecf.sdo/src/org/eclipse/ecf/sdo/DataGraphSharingFactory.java @@ -20,32 +20,43 @@ import org.eclipse.ecf.core.util.ECFException; */ public class DataGraphSharingFactory { - private static final Hashtable managers = new Hashtable(); - - private DataGraphSharingFactory() { - } - - public static final IDataGraphSharing getDataGraphSharing( - ISharedObjectContainer container, String name) throws ECFException { - - IDataGraphSharingManager instantiator = (IDataGraphSharingManager) managers - .get(name); - if (instantiator == null) - return null; - else - return instantiator.getInstance(container); - } - - public static void registerManager(String name, - IDataGraphSharingManager manager) { - managers.put(name, manager); - } - - public static void unregisterManager(String name) { - managers.remove(name); - } - - static void unregisterAllManagers() { - managers.clear(); - } + public static final String TRACE_TAG = "DataGraphSharingFactory"; + + private static final Hashtable managers = new Hashtable(); + + private DataGraphSharingFactory() { + } + + public static final IDataGraphSharing getDataGraphSharing( + ISharedObjectContainer container, String name) throws ECFException { + + IDataGraphSharingManager instantiator = (IDataGraphSharingManager) managers + .get(name); + if (instantiator == null) + return null; + else + return instantiator.getInstance(container); + } + + public static void registerManager(String name, + IDataGraphSharingManager manager) { + if (SDOPlugin.isTracing(TRACE_TAG)) + SDOPlugin.getTraceLog().println("registerManager: " + name); + + managers.put(name, manager); + } + + public static void unregisterManager(String name) { + if (SDOPlugin.isTracing(TRACE_TAG)) + SDOPlugin.getTraceLog().println("unregisterManager: " + name); + + managers.remove(name); + } + + static void unregisterAllManagers() { + if (SDOPlugin.isTracing(TRACE_TAG)) + SDOPlugin.getTraceLog().println("unregisterAllManagers"); + + managers.clear(); + } } diff --git a/examples/bundles/org.eclipse.ecf.sdo/src/org/eclipse/ecf/sdo/SDOPlugin.java b/examples/bundles/org.eclipse.ecf.sdo/src/org/eclipse/ecf/sdo/SDOPlugin.java index a1c497a33..487266de7 100644 --- a/examples/bundles/org.eclipse.ecf.sdo/src/org/eclipse/ecf/sdo/SDOPlugin.java +++ b/examples/bundles/org.eclipse.ecf.sdo/src/org/eclipse/ecf/sdo/SDOPlugin.java @@ -10,154 +10,176 @@ *******************************************************************************/ package org.eclipse.ecf.sdo; +import java.io.PrintStream; + +import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IConfigurationElement; import org.eclipse.core.runtime.IExtensionDelta; import org.eclipse.core.runtime.IExtensionRegistry; import org.eclipse.core.runtime.IRegistryChangeEvent; import org.eclipse.core.runtime.IRegistryChangeListener; +import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Platform; import org.eclipse.core.runtime.Plugin; -import org.eclipse.ecf.core.ISharedObjectContainer; -import org.eclipse.ecf.core.util.ECFException; +import org.eclipse.core.runtime.Status; import org.osgi.framework.BundleContext; /** - * (Temporary) entry point into the Shared Data Graph API. Serves as an - * {@link org.eclipse.ecf.sdo.IDataGraphSharing IDataGraphSharing}factory. - * * @author pnehrer - * @deprecated Use - * {@link org.eclipse.ecf.sdo.DataGraphSharingFactory DataGraphSharingFactory} - * instead. */ -public class SDOPlugin extends Plugin { +public class SDOPlugin { - private static final String MANAGER_EXTENSION_POINT = "manager"; + public static final String PLUGIN_ID = "org.eclipse.ecf.sdo"; - private static final String MANAGER_EXTENSION = "manager"; + private static final String TRACE_PREFIX = PLUGIN_ID + "/"; - private static final String ATTR_NAME = "name"; + private static EclipsePlugin plugin; - private static final String ATTR_CLASS = "class"; + private static boolean tracingEnabled = Boolean.getBoolean(TRACE_PREFIX + + "debug"); - // The shared instance. - private static SDOPlugin plugin; + private SDOPlugin() { + } - private IRegistryChangeListener registryChangeListener; + public static void log(Object entry) { + if (plugin == null) { + if (entry instanceof Throwable) + ((Throwable) entry).printStackTrace(); + else + System.err.println(entry); + } else { + plugin.log(entry); + } + } - private boolean debug; + public static boolean isTracing(String tag) { + if (tracingEnabled) { + return plugin == null ? Boolean.getBoolean(TRACE_PREFIX + tag) + : plugin.isTracing(tag); + } else + return false; + } - /** - * The constructor. - */ - public SDOPlugin() { - super(); - plugin = this; + public static PrintStream getTraceLog() { + return System.out; } - /** - * This method is called upon plug-in activation - */ - public void start(BundleContext context) throws Exception { - super.start(context); - registryChangeListener = new IRegistryChangeListener() { - public void registryChanged(IRegistryChangeEvent event) { - IExtensionDelta[] deltas = event.getExtensionDeltas(getBundle() - .getSymbolicName(), MANAGER_EXTENSION_POINT); - for (int i = 0; i < deltas.length; ++i) { - switch (deltas[i].getKind()) { - case IExtensionDelta.ADDED: - registerManagers(deltas[i].getExtension() - .getConfigurationElements()); - break; - - case IExtensionDelta.REMOVED: - IConfigurationElement[] elems = deltas[i] - .getExtension().getConfigurationElements(); - for (int j = 0; j < elems.length; ++j) { - if (!MANAGER_EXTENSION.equals(elems[j].getName())) - continue; - - String name = elems[j].getAttribute(ATTR_NAME); - if (name != null && name.length() > 0) - DataGraphSharingFactory.unregisterManager(name); - } + public static class EclipsePlugin extends Plugin { + + private static final String MANAGER_EXTENSION_POINT = "manager"; - break; + private static final String MANAGER_EXTENSION = "manager"; + + private static final String ATTR_NAME = "name"; + + private static final String ATTR_CLASS = "class"; + + private IRegistryChangeListener registryChangeListener; + + public EclipsePlugin() { + plugin = this; + tracingEnabled = Platform.inDebugMode(); + } + + /** + * This method is called upon plug-in activation + */ + public void start(BundleContext context) throws Exception { + super.start(context); + registryChangeListener = new IRegistryChangeListener() { + public void registryChanged(IRegistryChangeEvent event) { + IExtensionDelta[] deltas = event.getExtensionDeltas( + getBundle().getSymbolicName(), + MANAGER_EXTENSION_POINT); + for (int i = 0; i < deltas.length; ++i) { + switch (deltas[i].getKind()) { + case IExtensionDelta.ADDED: + registerManagers(deltas[i].getExtension() + .getConfigurationElements()); + break; + + case IExtensionDelta.REMOVED: + IConfigurationElement[] elems = deltas[i] + .getExtension().getConfigurationElements(); + for (int j = 0; j < elems.length; ++j) { + if (!MANAGER_EXTENSION.equals(elems[j] + .getName())) + continue; + + String name = elems[j].getAttribute(ATTR_NAME); + if (name != null && name.length() > 0) + DataGraphSharingFactory + .unregisterManager(name); + } + + break; + } } } - } - }; + }; - IExtensionRegistry reg = Platform.getExtensionRegistry(); - IConfigurationElement[] elems = reg.getConfigurationElementsFor( - getBundle().getSymbolicName(), MANAGER_EXTENSION_POINT); - registerManagers(elems); - } + IExtensionRegistry reg = Platform.getExtensionRegistry(); + IConfigurationElement[] elems = reg.getConfigurationElementsFor( + getBundle().getSymbolicName(), MANAGER_EXTENSION_POINT); + registerManagers(elems); + } - private void registerManagers(IConfigurationElement[] elems) { - for (int i = 0; i < elems.length; ++i) { - if (!MANAGER_EXTENSION.equals(elems[i].getName())) - continue; - - String name = elems[i].getAttribute(ATTR_NAME); - if (name == null || name.length() == 0) - continue; - - IDataGraphSharingManager mgr; - try { - mgr = (IDataGraphSharingManager) elems[i] - .createExecutableExtension(ATTR_CLASS); - } catch (Exception ex) { - continue; - } + private void registerManagers(IConfigurationElement[] elems) { + for (int i = 0; i < elems.length; ++i) { + if (!MANAGER_EXTENSION.equals(elems[i].getName())) + continue; + + String name = elems[i].getAttribute(ATTR_NAME); + if (name == null || name.length() == 0) + continue; + + IDataGraphSharingManager mgr; + try { + mgr = (IDataGraphSharingManager) elems[i] + .createExecutableExtension(ATTR_CLASS); + } catch (Exception ex) { + continue; + } - DataGraphSharingFactory.registerManager(name, mgr); + DataGraphSharingFactory.registerManager(name, mgr); + } } - } - /** - * This method is called when the plug-in is stopped - */ - public void stop(BundleContext context) throws Exception { - if (registryChangeListener != null) - Platform.getExtensionRegistry().removeRegistryChangeListener( - registryChangeListener); + /** + * This method is called when the plug-in is stopped + */ + public void stop(BundleContext context) throws Exception { + if (registryChangeListener != null) + Platform.getExtensionRegistry().removeRegistryChangeListener( + registryChangeListener); - DataGraphSharingFactory.unregisterAllManagers(); - super.stop(context); - } - - /** - * Returns the shared instance. - */ - public static SDOPlugin getDefault() { - return plugin; - } + DataGraphSharingFactory.unregisterAllManagers(); + super.stop(context); + } - /** - * @param container - * @return - * @throws ECFException - * @deprecated Use - * {@link DataGraphSharingFactory#getDataGraphSharing(ISharedObjectContainer, String) DataGraphSharingFactory.getDataGraphSharing(ISharedObjectContainer, String)} - * instead. - */ - public IDataGraphSharing getDataGraphSharing( - ISharedObjectContainer container) throws ECFException { - - return DataGraphSharingFactory - .getDataGraphSharing(container, "default"); - } + public void log(Object entry) { + IStatus status; + if (entry instanceof IStatus) + status = (IStatus) entry; + else if (entry instanceof CoreException) + status = ((CoreException) entry).getStatus(); + else if (entry instanceof Throwable) { + Throwable t = (Throwable) entry; + status = new Status(Status.ERROR, + getBundle().getSymbolicName(), 0, + t.getLocalizedMessage() == null ? "Unknown error." : t + .getLocalizedMessage(), t); + } else + status = new Status(Status.WARNING, getBundle() + .getSymbolicName(), 0, String.valueOf(entry), + new RuntimeException().fillInStackTrace()); + + getLog().log(status); + } - /** - * Sets the debug flag. - * - * @param debug - * @deprecated Use Eclipse plug-in tracing support instead. - */ - public void setDebug(boolean debug) { - this.debug = debug; + public boolean isTracing(String tag) { + return Boolean.TRUE.equals(Boolean.valueOf(Platform + .getDebugOption(TRACE_PREFIX + tag))); + } } - }
\ No newline at end of file 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 fcf3b2661..ac131c436 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 @@ -17,6 +17,7 @@ import java.io.IOException; import org.eclipse.ecf.core.util.ECFException; import org.eclipse.ecf.sdo.ISharedDataGraph; import org.eclipse.ecf.sdo.IUpdateProvider; +import org.eclipse.ecf.sdo.SDOPlugin; import org.eclipse.emf.ecore.resource.Resource; import org.eclipse.emf.ecore.sdo.EChangeSummary; import org.eclipse.emf.ecore.sdo.EDataGraph; @@ -31,116 +32,106 @@ 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, 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); - } + public static final String TRACE_TAG = "EMFUpdateProvider"; + + 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 (SDOPlugin.isTracing(TRACE_TAG)) { + SDOPlugin.getTraceLog().println("commit:"); + changes.eResource().save(SDOPlugin.getTraceLog(), 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 (SDOPlugin.isTracing(TRACE_TAG)) { + SDOPlugin.getTraceLog().println("processUpdate:"); + res.save(SDOPlugin.getTraceLog(), 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); + } } |