diff options
author | pnehrer | 2006-07-12 19:51:49 +0000 |
---|---|---|
committer | pnehrer | 2006-07-12 19:51:49 +0000 |
commit | d2b9e1602656f0dcd6601ccb41c799bad551a7de (patch) | |
tree | 33cfb811c54e8c5092dcd5f1692c132ff2f77c28 /examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf | |
parent | de31eefad8254c33dab3a62b7c3e1e316332c0ad (diff) | |
download | org.eclipse.ecf-d2b9e1602656f0dcd6601ccb41c799bad551a7de.tar.gz org.eclipse.ecf-d2b9e1602656f0dcd6601ccb41c799bad551a7de.tar.xz org.eclipse.ecf-d2b9e1602656f0dcd6601ccb41c799bad551a7de.zip |
Fixed serialization issues.
Refactored to work with Collab Example.
Diffstat (limited to 'examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf')
16 files changed, 252 insertions, 204 deletions
diff --git a/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/example/pubsub/AppendableList.java b/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/example/pubsub/AppendableList.java index b164487c7..4874fc7e1 100644 --- a/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/example/pubsub/AppendableList.java +++ b/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/example/pubsub/AppendableList.java @@ -46,7 +46,7 @@ public class AppendableList implements Serializable { public void handleException(Throwable exception) { // TODO Auto-generated method stub - + exception.printStackTrace(); } }); } @@ -69,7 +69,7 @@ public class AppendableList implements Serializable { return result; } - public String toString() { + public synchronized String toString() { return values.toString(); } } diff --git a/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/example/pubsub/PubSubView.java b/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/example/pubsub/PubSubView.java index d4e9f9ab2..ebcb5d4f0 100644 --- a/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/example/pubsub/PubSubView.java +++ b/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/example/pubsub/PubSubView.java @@ -13,12 +13,12 @@ package org.eclipse.ecf.example.pubsub; import java.io.IOException; import java.util.Vector; +import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.ecf.core.ISharedObjectContainer; -import org.eclipse.ecf.core.SharedObjectContainerFactory; import org.eclipse.ecf.core.SharedObjectCreateException; import org.eclipse.ecf.core.identity.IDFactory; import org.eclipse.ecf.core.identity.IDInstantiationException; -import org.eclipse.ecf.core.util.ECFException; +import org.eclipse.ecf.example.collab.CollabClient; import org.eclipse.ecf.pubsub.IPublishedServiceDirectory; import org.eclipse.ecf.pubsub.IPublishedServiceDirectoryListener; import org.eclipse.ecf.pubsub.IPublishedServiceRequestor; @@ -28,6 +28,7 @@ import org.eclipse.ecf.pubsub.PublishedServiceDescriptor; import org.eclipse.ecf.pubsub.PublishedServiceDirectoryChangeEvent; import org.eclipse.ecf.pubsub.model.IMasterModel; import org.eclipse.ecf.pubsub.model.SharedModelFactory; +import org.eclipse.ecf.pubsub.model.impl.LocalAgent; import org.eclipse.jface.action.Action; import org.eclipse.jface.action.IMenuManager; import org.eclipse.jface.action.MenuManager; @@ -90,8 +91,7 @@ public class PubSubView extends ViewPart { // TODO Auto-generated catch block e.printStackTrace(); } catch (IDInstantiationException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + throw new RuntimeException(e); } } }; @@ -99,19 +99,18 @@ public class PubSubView extends ViewPart { shareSomethingAction.setEnabled(false); IMenuManager mgr = site.getActionBars().getMenuManager(); - mgr.add(new Action("Connect") { + mgr.add(new Action("Start") { public void run() { - try { - container = SharedObjectContainerFactory.getDefault().createSharedObjectContainer("ecf.generic.client"); - container.connect(IDFactory.getDefault().createStringID("ecftcp://localhost:3282/server"), null); - IPublishedServiceDirectory directory = (IPublishedServiceDirectory) container.getAdapter(IPublishedServiceDirectory.class); - viewer.setInput(directory); - setEnabled(false); - shareSomethingAction.setEnabled(true); - } catch (ECFException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + container = CollabClient.getContainer(ResourcesPlugin.getWorkspace().getRoot()); + if (container == null) { + MessageDialog.openError(getSite().getShell(), "Error", "Collaboration environment not found."); + return; } + + IPublishedServiceDirectory directory = (IPublishedServiceDirectory) container.getAdapter(IPublishedServiceDirectory.class); + viewer.setInput(directory); + setEnabled(false); + shareSomethingAction.setEnabled(true); } }); @@ -142,12 +141,15 @@ public class PubSubView extends ViewPart { dlg.open(); String value = dlg.getValue(); if (value != null) { - IMasterModel list = (IMasterModel) getStructuredSelection().getFirstElement(); - try { - list.update(value); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + LocalAgent model = (LocalAgent) getStructuredSelection().getFirstElement(); + AppendableList list = (AppendableList) model.getData(); + if (list.add(value)) { + try { + model.update(value); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } } } } @@ -259,7 +261,7 @@ public class PubSubView extends ViewPart { }); } - public void subscriptionFailed(final Throwable t) { + public void requestFailed(final Throwable t) { Display.getDefault().asyncExec(new Runnable() { public void run() { MessageDialog.openError(getSite().getShell(), "Subscription Error", t.getLocalizedMessage()); diff --git a/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/example/pubsub/SerializationUtil.java b/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/example/pubsub/SerializationUtil.java new file mode 100644 index 000000000..dfd41e837 --- /dev/null +++ b/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/example/pubsub/SerializationUtil.java @@ -0,0 +1,39 @@ +/** + * Copyright (c) 2006 Ecliptical Software Inc. and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Ecliptical Software Inc. - initial API and implementation + */ +package org.eclipse.ecf.example.pubsub; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; + +public class SerializationUtil { + + private SerializationUtil() { + super(); + } + + public static byte[] serialize(Object object) throws IOException { + ByteArrayOutputStream buf = new ByteArrayOutputStream(); + ObjectOutputStream out = new ObjectOutputStream(buf); + out.writeObject(object); + out.close(); + return buf.toByteArray(); + } + + public static Object deserialize(byte[] buf) throws IOException, ClassNotFoundException { + ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(buf)); + Object object = in.readObject(); + in.close(); + return object; + } +} diff --git a/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/pubsub/IPublishedService.java b/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/pubsub/IPublishedService.java index 3c24eae9d..22dd1f324 100644 --- a/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/pubsub/IPublishedService.java +++ b/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/pubsub/IPublishedService.java @@ -21,5 +21,7 @@ public interface IPublishedService extends IIdentifiable { void subscribe(ID containerID); + // TODO this is a bit unsafe -- does not prevent duplicate unsubscription + // -- perhaps we should create a token (ID) in subscribe() and require it here? void unsubscribe(ID containerID); } diff --git a/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/pubsub/IPublishedServiceDirectory.java b/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/pubsub/IPublishedServiceDirectory.java index b9d68a723..750d4c27f 100644 --- a/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/pubsub/IPublishedServiceDirectory.java +++ b/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/pubsub/IPublishedServiceDirectory.java @@ -12,6 +12,8 @@ package org.eclipse.ecf.pubsub; public interface IPublishedServiceDirectory { + // TODO initial state currently delivered as an ADD event during listener registration + // -- should there be a more explicit initial state delivery? void addReplicatedServiceListener(IPublishedServiceDirectoryListener listener); void removeReplicatedServiceListener(IPublishedServiceDirectoryListener listener); diff --git a/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/pubsub/ISubscriptionCallback.java b/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/pubsub/ISubscriptionCallback.java index 7cedc6a35..54d43e2de 100644 --- a/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/pubsub/ISubscriptionCallback.java +++ b/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/pubsub/ISubscriptionCallback.java @@ -14,5 +14,5 @@ public interface ISubscriptionCallback { void subscribed(ISubscription subscription); - void subscriptionFailed(Throwable t); + void requestFailed(Throwable t); } diff --git a/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/pubsub/impl/DiscoveryAgent.java b/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/pubsub/impl/DiscoveryAgent.java index 1b642d106..b9a48c9d9 100644 --- a/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/pubsub/impl/DiscoveryAgent.java +++ b/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/pubsub/impl/DiscoveryAgent.java @@ -17,12 +17,9 @@ import java.util.Map; import org.eclipse.core.runtime.PlatformObject; import org.eclipse.ecf.core.ISharedObject; import org.eclipse.ecf.core.ISharedObjectConfig; -import org.eclipse.ecf.core.ISharedObjectConnector; import org.eclipse.ecf.core.ISharedObjectContext; import org.eclipse.ecf.core.ISharedObjectManager; import org.eclipse.ecf.core.ReplicaSharedObjectDescription; -import org.eclipse.ecf.core.SharedObjectConnectException; -import org.eclipse.ecf.core.SharedObjectDisconnectException; import org.eclipse.ecf.core.SharedObjectInitException; import org.eclipse.ecf.core.events.IContainerConnectedEvent; import org.eclipse.ecf.core.events.IContainerDisconnectedEvent; @@ -30,18 +27,26 @@ 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.identity.IDFactory; -import org.eclipse.ecf.core.identity.IDInstantiationException; import org.eclipse.ecf.core.util.Event; -import org.eclipse.ecf.core.util.QueueException; +import org.eclipse.ecf.example.pubsub.SerializationUtil; import org.eclipse.ecf.pubsub.IPublishedService; import org.eclipse.ecf.pubsub.PublishedServiceDescriptor; public class DiscoveryAgent extends PlatformObject implements ISharedObject { + + protected static final Object DIRECTORY_KEY = new Integer(0); protected ISharedObjectConfig config; + + protected PublishedServiceDirectory directory; public void init(ISharedObjectConfig config) throws SharedObjectInitException { + if (config.getContext().getLocalContainerID().equals(config.getHomeContainerID())) { + directory = (PublishedServiceDirectory) config.getProperties().get(DIRECTORY_KEY); + if (directory == null) + throw new SharedObjectInitException("Directory is required."); + } + this.config = config; } @@ -93,7 +98,7 @@ public class DiscoveryAgent extends PlatformObject implements ISharedObject { Map props = svc.getProperties(); PublishedServiceDescriptor desc = new PublishedServiceDescriptor(ctx.getLocalContainerID(), sharedObjectID, props); try { - ctx.sendMessage(config.getHomeContainerID(), new DiscoveryMessage(DiscoveryMessage.ADDED, desc)); + ctx.sendMessage(config.getHomeContainerID(), SerializationUtil.serialize(new DiscoveryMessage(DiscoveryMessage.ADDED, desc))); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); @@ -117,7 +122,7 @@ public class DiscoveryAgent extends PlatformObject implements ISharedObject { Map props = svc.getProperties(); PublishedServiceDescriptor desc = new PublishedServiceDescriptor(ctx.getLocalContainerID(), sharedObjectID, props); try { - ctx.sendMessage(config.getHomeContainerID(), new DiscoveryMessage(DiscoveryMessage.REMOVED, desc)); + ctx.sendMessage(config.getHomeContainerID(), SerializationUtil.serialize(new DiscoveryMessage(DiscoveryMessage.REMOVED, desc))); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); @@ -174,7 +179,7 @@ public class DiscoveryAgent extends PlatformObject implements ISharedObject { PublishedServiceDescriptor[] descriptors = new PublishedServiceDescriptor[published.size()]; published.toArray(descriptors); try { - ctx.sendMessage(config.getHomeContainerID(), new DiscoveryMessage(DiscoveryMessage.ADDED, descriptors)); + ctx.sendMessage(config.getHomeContainerID(), SerializationUtil.serialize(new DiscoveryMessage(DiscoveryMessage.ADDED, descriptors))); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); @@ -192,28 +197,26 @@ public class DiscoveryAgent extends PlatformObject implements ISharedObject { } protected void received(ISharedObjectMessageEvent event) { - if (!(event.getData() instanceof DiscoveryMessage)) + Object data = event.getData(); + if (!(data instanceof byte[])) return; try { - ID directoryID = IDFactory.getDefault().createStringID(PublishedServiceDirectory.SHARED_OBJECT_ID); - ISharedObjectManager mgr = config.getContext().getSharedObjectManager(); - ISharedObjectConnector conn = mgr.connectSharedObjects(config.getSharedObjectID(), new ID[] { directoryID }); - conn.enqueue(event); - mgr.disconnectSharedObjects(conn); - } catch (IDInstantiationException e) { + data = SerializationUtil.deserialize((byte[]) data); + } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); - } catch (SharedObjectConnectException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (QueueException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (SharedObjectDisconnectException e) { + return; + } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); + return; } + + if (!(data instanceof DiscoveryMessage)) + return; + + directory.handleDiscovery(event.getRemoteContainerID(), (DiscoveryMessage) data); } public void handleEvents(Event[] events) { @@ -223,5 +226,6 @@ public class DiscoveryAgent extends PlatformObject implements ISharedObject { public void dispose(ID containerID) { config = null; + directory = null; } } diff --git a/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/pubsub/impl/PubSubAdapterFactory.java b/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/pubsub/impl/PubSubAdapterFactory.java index 88b965b88..e41042f69 100644 --- a/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/pubsub/impl/PubSubAdapterFactory.java +++ b/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/pubsub/impl/PubSubAdapterFactory.java @@ -43,9 +43,7 @@ public class PubSubAdapterFactory implements IAdapterFactory { try { directoryID = IDFactory.getDefault().createStringID(PublishedServiceDirectory.SHARED_OBJECT_ID); } catch (IDInstantiationException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - return null; + throw new RuntimeException(e); } final ISharedObjectManager mgr = container.getSharedObjectManager(); diff --git a/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/pubsub/impl/PublishedServiceDirectory.java b/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/pubsub/impl/PublishedServiceDirectory.java index ed28700ab..7838a46e9 100644 --- a/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/pubsub/impl/PublishedServiceDirectory.java +++ b/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/pubsub/impl/PublishedServiceDirectory.java @@ -28,7 +28,6 @@ import org.eclipse.ecf.core.SharedObjectInitException; import org.eclipse.ecf.core.events.IContainerDisconnectedEvent; import org.eclipse.ecf.core.events.ISharedObjectActivatedEvent; import org.eclipse.ecf.core.events.ISharedObjectDeactivatedEvent; -import org.eclipse.ecf.core.events.ISharedObjectMessageEvent; import org.eclipse.ecf.core.identity.ID; import org.eclipse.ecf.core.identity.IDFactory; import org.eclipse.ecf.core.util.ECFException; @@ -63,7 +62,7 @@ public class PublishedServiceDirectory extends PlatformObject implements IShared public void handleException(Throwable exception) { // TODO Auto-generated method stub - + exception.printStackTrace(); } }); } @@ -84,11 +83,34 @@ public class PublishedServiceDirectory extends PlatformObject implements IShared public void handleException(Throwable exception) { // TODO Auto-generated method stub - + exception.printStackTrace(); } }); } } + + void handleDiscovery(ID containerID, DiscoveryMessage msg) { + PublishedServiceDescriptor[] descriptors = msg.getDescriptors(); + + synchronized (this) { + Collection values = (Collection) services.get(containerID); + if (values == null) { + values = new HashSet(); + services.put(containerID, values); + } + + if (msg.getKind() == DiscoveryMessage.ADDED) { + values.addAll(Arrays.asList(descriptors)); + } else { + values.removeAll(Arrays.asList(descriptors)); + if (values.isEmpty()) + services.remove(containerID); + } + + int kind = msg.getKind() == DiscoveryMessage.ADDED ? PublishedServiceDirectoryChangeEvent.ADDED : PublishedServiceDirectoryChangeEvent.REMOVED; + fireServiceChangedEvent(new PublishedServiceDirectoryChangeEvent(this, kind, descriptors)); + } + } public void init(ISharedObjectConfig config) throws SharedObjectInitException { this.config = config; @@ -99,8 +121,6 @@ public class PublishedServiceDirectory extends PlatformObject implements IShared activated(((ISharedObjectActivatedEvent) event).getActivatedID()); else if (event instanceof ISharedObjectDeactivatedEvent) deactivated(((ISharedObjectDeactivatedEvent) event).getDeactivatedID()); - else if (event instanceof ISharedObjectMessageEvent) - received((ISharedObjectMessageEvent) event); else if (event instanceof IContainerDisconnectedEvent) disconnected((IContainerDisconnectedEvent) event); } @@ -111,7 +131,7 @@ public class PublishedServiceDirectory extends PlatformObject implements IShared if (discoveryAgentID == null) { try { discoveryAgentID = IDFactory.getDefault().createGUID(); - mgr.createSharedObject(new SharedObjectDescription(DiscoveryAgent.class, discoveryAgentID, null)); + mgr.createSharedObject(createDiscoveryAgentDescription()); } catch (ECFException e) { // TODO Log me! e.printStackTrace(); @@ -142,35 +162,6 @@ public class PublishedServiceDirectory extends PlatformObject implements IShared } } } - - protected void received(ISharedObjectMessageEvent event) { - Object data = event.getData(); - if (!(data instanceof DiscoveryMessage)) - return; - - DiscoveryMessage msg = (DiscoveryMessage) event.getData(); - PublishedServiceDescriptor[] descriptors = msg.getDescriptors(); - - synchronized (this) { - ID containerID = event.getRemoteContainerID(); - Collection values = (Collection) services.get(containerID); - if (values == null) { - values = new HashSet(); - services.put(containerID, values); - } - - if (msg.getKind() == DiscoveryMessage.ADDED) { - values.addAll(Arrays.asList(descriptors)); - } else { - values.removeAll(Arrays.asList(descriptors)); - if (values.isEmpty()) - services.remove(containerID); - } - - int kind = msg.getKind() == DiscoveryMessage.ADDED ? PublishedServiceDirectoryChangeEvent.ADDED : PublishedServiceDirectoryChangeEvent.REMOVED; - fireServiceChangedEvent(new PublishedServiceDirectoryChangeEvent(this, kind, descriptors)); - } - } public void handleEvents(Event[] events) { for (int i = 0; i < events.length; ++i) @@ -186,4 +177,10 @@ public class PublishedServiceDirectory extends PlatformObject implements IShared config = null; } + + protected SharedObjectDescription createDiscoveryAgentDescription() { + HashMap props = new HashMap(1); + props.put(DiscoveryAgent.DIRECTORY_KEY, this); + return new SharedObjectDescription(DiscoveryAgent.class, discoveryAgentID, props); + } } diff --git a/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/pubsub/impl/ReplicatedServiceDiscoveryEvent.java b/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/pubsub/impl/ReplicatedServiceDiscoveryEvent.java deleted file mode 100644 index ad454981f..000000000 --- a/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/pubsub/impl/ReplicatedServiceDiscoveryEvent.java +++ /dev/null @@ -1,71 +0,0 @@ -/** - * Copyright (c) 2006 Ecliptical Software Inc. and others. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * Ecliptical Software Inc. - initial API and implementation - */ -package org.eclipse.ecf.pubsub.impl; - -import java.util.Arrays; - -import org.eclipse.ecf.core.identity.ID; -import org.eclipse.ecf.core.util.Event; - -public class ReplicatedServiceDiscoveryEvent implements Event { - - private static final long serialVersionUID = 1848459358595071814L; - - public static final int ADDED = 0; - - public static final int REMOVED = 1; - - private final int kind; - - private final ID containerID; - - private final ID[] sharedObjectIDs; - - public ReplicatedServiceDiscoveryEvent(int kind, ID containerID, ID[] sharedObjectIDs) { - this.kind = kind; - this.containerID = containerID; - this.sharedObjectIDs = sharedObjectIDs; - } - - public int getKind() { - return kind; - } - - public ID getContainerID() { - return containerID; - } - - public ID[] getSharedObjectIDs() { - return sharedObjectIDs; - } - - public int hashCode() { - int c = 17; - c = 37 * c + kind; - c = 37 * c + containerID.hashCode(); - c = 37 * c + sharedObjectIDs[0].hashCode(); - return c; - } - - public boolean equals(Object obj) { - if (this == obj) - return true; - - if (obj == null) - return false; - - if (getClass() != obj.getClass()) - return false; - - ReplicatedServiceDiscoveryEvent other = (ReplicatedServiceDiscoveryEvent) obj; - return kind == other.kind && containerID.equals(other.containerID) && Arrays.equals(sharedObjectIDs, other.sharedObjectIDs); - } -} diff --git a/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/pubsub/impl/ServiceRequestor.java b/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/pubsub/impl/ServiceRequestor.java index de0502ba0..18b01b4bd 100644 --- a/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/pubsub/impl/ServiceRequestor.java +++ b/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/pubsub/impl/ServiceRequestor.java @@ -51,7 +51,6 @@ public class ServiceRequestor implements IPublishedServiceRequestor { try { id = IDFactory.getDefault().createGUID(); } catch (IDInstantiationException e) { - // TODO handle this! throw new RuntimeException(e); } diff --git a/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/pubsub/impl/SubscriptionAgent.java b/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/pubsub/impl/SubscriptionAgent.java index 7e7902aa5..ce29aa2b0 100644 --- a/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/pubsub/impl/SubscriptionAgent.java +++ b/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/pubsub/impl/SubscriptionAgent.java @@ -52,10 +52,9 @@ public class SubscriptionAgent extends PlatformObject implements ISharedObject { protected boolean disposed; public void init(ISharedObjectConfig config) throws SharedObjectInitException { - this.config = config; Map props = config.getProperties(); - if (isPrimary()) { + if (config.getContext().getLocalContainerID().equals(config.getHomeContainerID())) { containerID = (ID) props.get(CONTAINER_ID_KEY); if (containerID == null) throw new SharedObjectInitException("containerID is required"); @@ -68,6 +67,8 @@ public class SubscriptionAgent extends PlatformObject implements ISharedObject { sharedObjectID = (ID) props.get(SHARED_OBJECT_ID_KEY); if (sharedObjectID == null) throw new SharedObjectInitException("sharedObjectID is required"); + + this.config = config; } public void handleEvent(Event event) { @@ -102,7 +103,7 @@ public class SubscriptionAgent extends PlatformObject implements ISharedObject { ctx.sendCreate(containerID, createReplicaDescription()); // TODO set timer to time out if no response received within some bound } catch (IOException e) { - callback.subscriptionFailed(e); + callback.requestFailed(e); ctx.getSharedObjectManager().removeSharedObject(config.getSharedObjectID()); } @@ -163,7 +164,7 @@ public class SubscriptionAgent extends PlatformObject implements ISharedObject { protected void received(ISharedObjectCreateResponseEvent e) { if (e.getRemoteContainerID().equals(containerID) && e.getSenderSharedObjectID().equals(config.getSharedObjectID())) - callback.subscriptionFailed(e.getException()); + callback.requestFailed(e.getException()); } protected ReplicaSharedObjectDescription createReplicaDescription() { diff --git a/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/pubsub/model/IMasterModel.java b/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/pubsub/model/IMasterModel.java index 54cca81bf..97bb899f7 100644 --- a/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/pubsub/model/IMasterModel.java +++ b/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/pubsub/model/IMasterModel.java @@ -12,9 +12,7 @@ package org.eclipse.ecf.pubsub.model; import java.io.IOException; -import org.eclipse.ecf.pubsub.IPublishedService; - -public interface IMasterModel extends IPublishedService { +public interface IMasterModel { void update(Object data) throws IOException; } diff --git a/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/pubsub/model/impl/AgentBase.java b/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/pubsub/model/impl/AgentBase.java index 85d86e319..ea9378cb5 100644 --- a/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/pubsub/model/impl/AgentBase.java +++ b/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/pubsub/model/impl/AgentBase.java @@ -12,10 +12,6 @@ package org.eclipse.ecf.pubsub.model.impl; import java.util.Map; -import org.eclipse.core.runtime.CoreException; -import org.eclipse.core.runtime.IConfigurationElement; -import org.eclipse.core.runtime.IExtensionRegistry; -import org.eclipse.core.runtime.Platform; import org.eclipse.core.runtime.PlatformObject; import org.eclipse.ecf.core.IIdentifiable; import org.eclipse.ecf.core.ISharedObject; @@ -45,34 +41,23 @@ public abstract class AgentBase extends PlatformObject implements ISharedObject, protected IModelUpdater updater; public void init(ISharedObjectConfig config) throws SharedObjectInitException { - this.config = config; Map props = config.getProperties(); - data = props.get(INITIAL_DATA_KEY); + initializeData(props.get(INITIAL_DATA_KEY)); updaterID = (String) props.get(MODEL_UPDATER_KEY); if (updaterID == null) throw new SharedObjectInitException("Model Updater is required."); - IExtensionRegistry registry = Platform.getExtensionRegistry(); - if (registry == null) - throw new SharedObjectInitException("No Platform Extension Registry."); - - IConfigurationElement[] elements = registry.getConfigurationElementsFor("org.eclipse.ecf.example.pubsub.modelUpdater"); - for (int i = 0; i < elements.length; ++i) { - if (updaterID.equals(elements[i].getAttribute("id"))) { - try { - updater = (IModelUpdater) elements[i].createExecutableExtension("class"); - } catch (CoreException e) { - throw new SharedObjectInitException(e); - } catch (ClassCastException e) { - throw new SharedObjectInitException(e); - } - - break; - } - } - - if (updater == null) - throw new SharedObjectInitException("Could not find specified Model Updater."); + initializeUpdater(); + this.config = config; + } + + protected abstract void initializeData(Object data) throws SharedObjectInitException; + + protected void initializeUpdater() throws SharedObjectInitException { + } + + public Object getData() { + return data; } public void handleEvent(Event event) { @@ -136,10 +121,6 @@ public abstract class AgentBase extends PlatformObject implements ISharedObject, protected void received(ID containerID, Object data) { } - - protected void apply(Object data) { - updater.update(this.data, data); - } public void handleEvents(Event[] events) { for (int i = 0; i < events.length; ++i) diff --git a/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/pubsub/model/impl/LocalAgent.java b/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/pubsub/model/impl/LocalAgent.java index 252975c38..148f00686 100644 --- a/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/pubsub/model/impl/LocalAgent.java +++ b/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/pubsub/model/impl/LocalAgent.java @@ -17,14 +17,24 @@ import java.util.Map; import org.eclipse.ecf.core.ISharedObjectContext; import org.eclipse.ecf.core.ReplicaSharedObjectDescription; +import org.eclipse.ecf.core.SharedObjectInitException; import org.eclipse.ecf.core.identity.ID; +import org.eclipse.ecf.example.pubsub.SerializationUtil; +import org.eclipse.ecf.pubsub.IPublishedService; import org.eclipse.ecf.pubsub.model.IMasterModel; -public class LocalAgent extends AgentBase implements IMasterModel { +public class LocalAgent extends AgentBase implements IPublishedService, IMasterModel { + + protected Map subscriptions; + + private final Object subscriptionMutex = new Object(); + + protected void initializeData(Object data) throws SharedObjectInitException { + this.data = data; + } public synchronized void update(Object data) throws IOException { - apply(data); - config.getContext().sendMessage(null, data); + config.getContext().sendMessage(null, SerializationUtil.serialize(data)); } public Map getProperties() { @@ -32,6 +42,18 @@ public class LocalAgent extends AgentBase implements IMasterModel { } public void subscribe(ID containerID) { + synchronized (subscriptionMutex) { + if (subscriptions == null) + subscriptions = new HashMap(); + + Integer refCount = (Integer) subscriptions.get(containerID); + if (refCount == null) + refCount = new Integer(0); + + refCount = new Integer(refCount.intValue() + 1); + subscriptions.put(containerID, refCount); + } + ISharedObjectContext ctx = config.getContext(); try { ctx.sendCreate(containerID, createRemoteAgentDescription()); @@ -42,8 +64,29 @@ public class LocalAgent extends AgentBase implements IMasterModel { } public void unsubscribe(ID containerID) { - // TODO Auto-generated method stub + boolean disposeReplica = false; + synchronized (subscriptionMutex) { + if (subscriptions != null) { + Integer refCount = (Integer) subscriptions.get(containerID); + if (refCount != null) { + refCount = new Integer(refCount.intValue() - 1); + if (refCount.intValue() <= 0) { + subscriptions.remove(containerID); + disposeReplica = true; + } else { + subscriptions.put(containerID, refCount); + } + } + } + } + if (disposeReplica) + try { + config.getContext().sendDispose(containerID); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } } protected void deactivated() { @@ -58,7 +101,13 @@ public class LocalAgent extends AgentBase implements IMasterModel { protected ReplicaSharedObjectDescription createRemoteAgentDescription() { Map props = new HashMap(2); - props.put(INITIAL_DATA_KEY, data); + try { + props.put(INITIAL_DATA_KEY, SerializationUtil.serialize(data)); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + props.put(MODEL_UPDATER_KEY, updaterID); return new ReplicaSharedObjectDescription(RemoteAgent.class, config.getSharedObjectID(), config.getHomeContainerID(), props); } diff --git a/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/pubsub/model/impl/RemoteAgent.java b/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/pubsub/model/impl/RemoteAgent.java index 076c42452..40f0f20b0 100644 --- a/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/pubsub/model/impl/RemoteAgent.java +++ b/examples/bundles/org.eclipse.ecf.example.pubsub/src/org/eclipse/ecf/pubsub/model/impl/RemoteAgent.java @@ -10,13 +10,52 @@ */ package org.eclipse.ecf.pubsub.model.impl; +import java.io.IOException; + +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IConfigurationElement; +import org.eclipse.core.runtime.IExtensionRegistry; +import org.eclipse.core.runtime.Platform; +import org.eclipse.ecf.core.SharedObjectInitException; import org.eclipse.ecf.core.identity.ID; +import org.eclipse.ecf.example.pubsub.SerializationUtil; +import org.eclipse.ecf.pubsub.model.IModelUpdater; import org.eclipse.ecf.pubsub.model.IReplicaModel; public class RemoteAgent extends AgentBase implements IReplicaModel { - public Object getData() { - return data; + protected void initializeData(Object data) throws SharedObjectInitException { + try { + this.data = SerializationUtil.deserialize((byte[]) data); + } catch (IOException e) { + throw new SharedObjectInitException(e); + } catch (ClassNotFoundException e) { + throw new SharedObjectInitException(e); + } + } + + protected void initializeUpdater() throws SharedObjectInitException { + IExtensionRegistry registry = Platform.getExtensionRegistry(); + if (registry == null) + throw new SharedObjectInitException("No Platform Extension Registry."); + + IConfigurationElement[] elements = registry.getConfigurationElementsFor("org.eclipse.ecf.example.pubsub.modelUpdater"); + for (int i = 0; i < elements.length; ++i) { + if (updaterID.equals(elements[i].getAttribute("id"))) { + try { + updater = (IModelUpdater) elements[i].createExecutableExtension("class"); + } catch (CoreException e) { + throw new SharedObjectInitException(e); + } catch (ClassCastException e) { + throw new SharedObjectInitException(e); + } + + break; + } + } + + if (updater == null) + throw new SharedObjectInitException("Could not find specified Model Updater."); } protected void disconnected() { @@ -29,6 +68,14 @@ public class RemoteAgent extends AgentBase implements IReplicaModel { } protected void received(ID containerID, Object data) { - apply(data); + try { + updater.update(this.data, SerializationUtil.deserialize((byte[]) data)); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (ClassNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } } } |