diff options
Diffstat (limited to 'plugins/org.eclipse.net4j.examples.installer/examples/org.eclipse.net4j.buddies.common/src/org/eclipse/net4j/buddies/internal/common')
18 files changed, 1881 insertions, 0 deletions
diff --git a/plugins/org.eclipse.net4j.examples.installer/examples/org.eclipse.net4j.buddies.common/src/org/eclipse/net4j/buddies/internal/common/Account.java b/plugins/org.eclipse.net4j.examples.installer/examples/org.eclipse.net4j.buddies.common/src/org/eclipse/net4j/buddies/internal/common/Account.java new file mode 100644 index 0000000000..52e914d2bc --- /dev/null +++ b/plugins/org.eclipse.net4j.examples.installer/examples/org.eclipse.net4j.buddies.common/src/org/eclipse/net4j/buddies/internal/common/Account.java @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) 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: + * Eike Stepper - initial API and implementation + */ +package org.eclipse.net4j.buddies.internal.common; + +import org.eclipse.net4j.buddies.common.IAccount; +import org.eclipse.net4j.util.ObjectUtil; + +import java.io.Serializable; +import java.util.HashMap; +import java.util.Map; + +/** + * @author Eike Stepper + */ +public class Account implements IAccount, Serializable +{ + private static final long serialVersionUID = 1L; + + private String userID; + + private transient String password; + + private Map<String, String> properties = new HashMap<String, String>(); + + private long timeStamp; + + protected Account() + { + } + + public Account(String userID, String password) + { + this.userID = userID; + this.password = password; + } + + public String getUserID() + { + return userID; + } + + public String getPassword() + { + return password; + } + + public void setPassword(String password) + { + this.password = password; + } + + public boolean authenticate(String password) + { + return ObjectUtil.equals(password, this.password); + } + + public Map<String, String> getProperties() + { + return properties; + } + + public void touch() + { + timeStamp = System.currentTimeMillis(); + } + + public long getTimeStamp() + { + return timeStamp; + } +} diff --git a/plugins/org.eclipse.net4j.examples.installer/examples/org.eclipse.net4j.buddies.common/src/org/eclipse/net4j/buddies/internal/common/Buddy.java b/plugins/org.eclipse.net4j.examples.installer/examples/org.eclipse.net4j.buddies.common/src/org/eclipse/net4j/buddies/internal/common/Buddy.java new file mode 100644 index 0000000000..6e1acd620c --- /dev/null +++ b/plugins/org.eclipse.net4j.examples.installer/examples/org.eclipse.net4j.buddies.common/src/org/eclipse/net4j/buddies/internal/common/Buddy.java @@ -0,0 +1,226 @@ +/* + * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) 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: + * Eike Stepper - initial API and implementation + */ +package org.eclipse.net4j.buddies.internal.common; + +import org.eclipse.net4j.buddies.common.IBuddy; +import org.eclipse.net4j.buddies.common.IBuddyStateEvent; +import org.eclipse.net4j.buddies.common.ICollaboration; +import org.eclipse.net4j.buddies.common.IMembership; +import org.eclipse.net4j.buddies.common.ISession; +import org.eclipse.net4j.util.ObjectUtil; +import org.eclipse.net4j.util.event.Event; +import org.eclipse.net4j.util.event.IEvent; +import org.eclipse.net4j.util.event.IListener; + +import org.eclipse.core.runtime.Platform; +import org.eclipse.core.runtime.PlatformObject; + +import java.text.MessageFormat; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +/** + * @author Eike Stepper + */ +public abstract class Buddy extends MembershipContainer implements IBuddy +{ + private ISession session; + + private State state = State.AVAILABLE; + + private Set<String> facilityTypes; + + public Buddy(ISession session, Set<String> facilityTypes) + { + this.session = session; + this.facilityTypes = facilityTypes == null ? null : Collections.unmodifiableSet(facilityTypes); + } + + public ISession getSession() + { + return session; + } + + public void setSession(ISession session) + { + this.session = session; + } + + public State getState() + { + return state; + } + + public void setState(State state) + { + if (this.state != state) + { + IEvent event = new BuddyStateEvent(this.state, state); + this.state = state; + IListener[] listeners = getListeners(); + if (listeners != null) + { + fireEvent(event, listeners); + } + } + } + + public Set<String> getFacilityTypes() + { + if (facilityTypes == null) + { + facilityTypes = Collections.unmodifiableSet(loadFacilityTypes()); + } + + return facilityTypes; + } + + public IMembership getMembership(Collaboration collaboration) + { + return getMembership(this, collaboration); + } + + public IMembership removeMembership(Collaboration collaboration) + { + return removeMembership(this, collaboration); + } + + public ICollaboration getCollaboration(long collaborationID) + { + for (IMembership membership : getMemberships()) + { + ICollaboration collaboration = membership.getCollaboration(); + if (collaboration.getID() == collaborationID) + { + return collaboration; + } + } + + return null; + } + + public ICollaboration[] getCollaborations() + { + List<ICollaboration> collaborations = new ArrayList<ICollaboration>(); + for (IMembership membership : getMemberships()) + { + ICollaboration collaboration = membership.getCollaboration(); + collaborations.add(collaboration); + } + + return collaborations.toArray(new ICollaboration[collaborations.size()]); + } + + public IMembership initiate() + { + return initiate((IBuddy)null); + } + + public IMembership initiate(IBuddy buddy) + { + HashSet<IBuddy> buddies = new HashSet<IBuddy>(); + if (buddy != null) + { + buddies.add(buddy); + } + + IMembership[] memberships = initiate(buddies); + return memberships.length == 0 ? null : memberships[0]; + } + + /** + * @see PlatformObject#getAdapter(Class) + */ + @SuppressWarnings("rawtypes") + public Object getAdapter(Class adapter) + { + return Platform.getAdapterManager().getAdapter(this, adapter); + } + + @Override + public boolean equals(Object obj) + { + if (obj == this) + { + return true; + } + + if (obj instanceof IBuddy) + { + IBuddy buddy = (IBuddy)obj; + return ObjectUtil.equals(getUserID(), buddy.getUserID()); + } + + return false; + } + + @Override + public int hashCode() + { + return ObjectUtil.hashCode(getUserID()); + } + + @Override + public String toString() + { + return MessageFormat.format("{0}[{1}]", getClass().getSimpleName(), getUserID()); //$NON-NLS-1$ + } + + protected Set<String> loadFacilityTypes() + { + throw new UnsupportedOperationException(); + } + + /** + * @author Eike Stepper + */ + private final class BuddyStateEvent extends Event implements IBuddyStateEvent + { + private static final long serialVersionUID = 1L; + + private State oldState; + + private State newState; + + public BuddyStateEvent(State oldState, State newState) + { + super(Buddy.this); + this.oldState = oldState; + this.newState = newState; + } + + @Override + public IBuddy getSource() + { + return (IBuddy)super.getSource(); + } + + public State getOldState() + { + return oldState; + } + + public State getNewState() + { + return newState; + } + + @Override + public String toString() + { + return MessageFormat.format("BuddyStateEvent[source={0}, oldState={1}, newState={2}]", getSource(), //$NON-NLS-1$ + getOldState(), getNewState()); + } + } +} diff --git a/plugins/org.eclipse.net4j.examples.installer/examples/org.eclipse.net4j.buddies.common/src/org/eclipse/net4j/buddies/internal/common/BuddyContainer.java b/plugins/org.eclipse.net4j.examples.installer/examples/org.eclipse.net4j.buddies.common/src/org/eclipse/net4j/buddies/internal/common/BuddyContainer.java new file mode 100644 index 0000000000..063404dca4 --- /dev/null +++ b/plugins/org.eclipse.net4j.examples.installer/examples/org.eclipse.net4j.buddies.common/src/org/eclipse/net4j/buddies/internal/common/BuddyContainer.java @@ -0,0 +1,152 @@ +/* + * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) 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: + * Eike Stepper - initial API and implementation + */ +package org.eclipse.net4j.buddies.internal.common; + +import org.eclipse.net4j.buddies.common.IBuddy; +import org.eclipse.net4j.buddies.common.IBuddyContainer; +import org.eclipse.net4j.util.container.IContainerDelta; +import org.eclipse.net4j.util.container.SingleDeltaContainerEvent; +import org.eclipse.net4j.util.event.IEvent; +import org.eclipse.net4j.util.event.IListener; +import org.eclipse.net4j.util.lifecycle.ILifecycleEvent; +import org.eclipse.net4j.util.lifecycle.Lifecycle; +import org.eclipse.net4j.util.lifecycle.LifecycleEvent; + +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +/** + * @author Eike Stepper + */ +public class BuddyContainer extends Lifecycle implements IBuddyContainer, IListener +{ + private Map<String, IBuddy> buddies = new HashMap<String, IBuddy>(); + + public BuddyContainer(Collection<IBuddy> buddies) + { + if (buddies != null) + { + for (IBuddy buddy : buddies) + { + this.buddies.put(buddy.getUserID(), buddy); + buddy.addListener(this); + } + } + } + + public BuddyContainer() + { + } + + public boolean addBuddy(IBuddy buddy) + { + String userID = buddy.getUserID(); + synchronized (buddies) + { + if (!buddies.containsKey(userID)) + { + buddies.put(userID, buddy); + IListener[] listeners = getListeners(); + if (listeners != null) + { + fireEvent(new SingleDeltaContainerEvent<IBuddy>(this, buddy, IContainerDelta.Kind.ADDED), listeners); + } + + buddy.addListener(this); + return true; + } + } + + return false; + } + + public IBuddy removeBuddy(String userID) + { + IBuddy buddy; + synchronized (buddies) + { + buddy = buddies.remove(userID); + } + + if (buddy != null) + { + buddy.removeListener(this); + IListener[] listeners = getListeners(); + if (listeners != null) + { + fireEvent(new SingleDeltaContainerEvent<IBuddy>(this, buddy, IContainerDelta.Kind.REMOVED), listeners); + } + } + + return buddy; + } + + public IBuddy[] getBuddies() + { + synchronized (buddies) + { + return buddies.values().toArray(new IBuddy[buddies.size()]); + } + } + + public IBuddy getBuddy(String userID) + { + synchronized (buddies) + { + return buddies.get(userID); + } + } + + public IBuddy[] getElements() + { + return getBuddies(); + } + + public boolean isEmpty() + { + synchronized (buddies) + { + return buddies.isEmpty(); + } + } + + public void notifyEvent(IEvent event) + { + if (event.getSource() instanceof IBuddy) + { + notifyBuddyEvent(event); + if (event instanceof LifecycleEvent) + { + LifecycleEvent e = (LifecycleEvent)event; + if (e.getKind() == ILifecycleEvent.Kind.DEACTIVATED) + { + removeBuddy(((IBuddy)e.getSource()).getUserID()); + } + } + } + } + + protected void notifyBuddyEvent(IEvent event) + { + } + + @Override + protected void doDeactivate() throws Exception + { + for (IBuddy buddy : getBuddies()) + { + buddy.removeListener(this); + } + + super.doDeactivate(); + } +} diff --git a/plugins/org.eclipse.net4j.examples.installer/examples/org.eclipse.net4j.buddies.common/src/org/eclipse/net4j/buddies/internal/common/Collaboration.java b/plugins/org.eclipse.net4j.examples.installer/examples/org.eclipse.net4j.buddies.common/src/org/eclipse/net4j/buddies/internal/common/Collaboration.java new file mode 100644 index 0000000000..040ed3ecb0 --- /dev/null +++ b/plugins/org.eclipse.net4j.examples.installer/examples/org.eclipse.net4j.buddies.common/src/org/eclipse/net4j/buddies/internal/common/Collaboration.java @@ -0,0 +1,297 @@ +/* + * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) 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: + * Eike Stepper - initial API and implementation + */ +package org.eclipse.net4j.buddies.internal.common; + +import org.eclipse.net4j.buddies.common.IBuddy; +import org.eclipse.net4j.buddies.common.ICollaboration; +import org.eclipse.net4j.buddies.common.IFacility; +import org.eclipse.net4j.buddies.common.IFacilityInstalledEvent; +import org.eclipse.net4j.buddies.common.IMembership; +import org.eclipse.net4j.buddies.common.IMessage; +import org.eclipse.net4j.buddies.internal.common.bundle.OM; +import org.eclipse.net4j.buddies.internal.common.protocol.MessageNotification; +import org.eclipse.net4j.signal.SignalProtocol; +import org.eclipse.net4j.util.ObjectUtil; +import org.eclipse.net4j.util.event.Event; +import org.eclipse.net4j.util.event.IEvent; +import org.eclipse.net4j.util.event.IListener; +import org.eclipse.net4j.util.lifecycle.LifecycleUtil; + +import org.eclipse.core.runtime.Platform; +import org.eclipse.core.runtime.PlatformObject; + +import java.text.MessageFormat; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; + +/** + * @author Eike Stepper + */ +public class Collaboration extends MembershipContainer implements ICollaboration +{ + private long id; + + private String title; + + private String description; + + private Visibility visibility = Visibility.PRIVATE; + + private ConcurrentMap<String, IFacility> facilities = new ConcurrentHashMap<String, IFacility>(); + + public Collaboration(long id) + { + this.id = id; + } + + public long getID() + { + return id; + } + + public String getTitle() + { + return title == null ? String.valueOf(id) : title; + } + + public String getDescription() + { + return description; + } + + public Visibility getVisibility() + { + return visibility; + } + + public boolean isPublic() + { + return visibility == Visibility.PUBLIC; + } + + public void setPublic(String title, String description) + { + visibility = Visibility.PUBLIC; + this.title = title; + this.description = description; + } + + public void setPrivate() + { + visibility = Visibility.PRIVATE; + title = null; + description = null; + } + + public IMembership getMembership(IBuddy buddy) + { + return getMembership(buddy, this); + } + + public IMembership removeMembership(IBuddy buddy) + { + return removeMembership(buddy, this); + } + + public IBuddy getBuddy(String userID) + { + for (IMembership membership : getMemberships()) + { + IBuddy buddy = membership.getBuddy(); + if (ObjectUtil.equals(buddy.getUserID(), userID)) + { + return buddy; + } + } + + return null; + } + + public IBuddy[] getBuddies() + { + List<IBuddy> buddies = new ArrayList<IBuddy>(); + for (IMembership membership : getMemberships()) + { + IBuddy buddy = membership.getBuddy(); + buddies.add(buddy); + } + + return buddies.toArray(new IBuddy[buddies.size()]); + } + + public String[] getFacilityTypes() + { + return facilities.keySet().toArray(new String[facilities.size()]); + } + + public IFacility[] getFacilities() + { + return facilities.values().toArray(new IFacility[facilities.size()]); + } + + public IFacility getFacility(String type) + { + return facilities.get(type); + } + + public boolean addFacility(IFacility facility, boolean remote) + { + String type = facility.getType(); + if (!facilities.containsKey(type)) + { + facilities.put(type, facility); + IListener[] listeners = getListeners(); + if (listeners != null) + { + fireEvent(new FacilityInstalledEvent(this, facility, remote), listeners); + } + + facility.addListener(this); + return true; + } + + return false; + } + + public void sendMessage(long collaborationID, String facilityType, IMessage message) + { + IMembership[] elements = getElements(); + for (IMembership membership : elements) + { + IBuddy receiver = membership.getBuddy(); + if (!ObjectUtil.equals(receiver.getUserID(), message.getSenderID())) + { + try + { + SignalProtocol<?> protocol = (SignalProtocol<?>)receiver.getSession().getProtocol(); + new MessageNotification(protocol, collaborationID, facilityType, message).sendAsync(); + } + catch (Exception ex) + { + OM.LOG.error(ex); + } + } + } + } + + /** + * @see PlatformObject#getAdapter(Class) + */ + @SuppressWarnings("rawtypes") + public Object getAdapter(Class adapter) + { + return Platform.getAdapterManager().getAdapter(this, adapter); + } + + @Override + public boolean equals(Object obj) + { + if (obj == this) + { + return true; + } + + if (obj instanceof ICollaboration) + { + ICollaboration collaboration = (ICollaboration)obj; + return getID() == collaboration.getID(); + } + + return false; + } + + @Override + public int hashCode() + { + return ObjectUtil.hashCode(id); + } + + @Override + public String toString() + { + return MessageFormat.format("{0}[{1}]", getClass().getSimpleName(), getTitle()); //$NON-NLS-1$ + } + + @Override + public void notifyEvent(IEvent event) + { + super.notifyEvent(event); + if (event.getSource() instanceof IFacility) + { + notifyFacilityEvent(event); + } + } + + protected void notifyFacilityEvent(IEvent event) + { + } + + @Override + protected void doDeactivate() throws Exception + { + for (IFacility facility : getFacilities()) + { + facility.removeListener(this); + LifecycleUtil.deactivate(facility); + } + + for (IMembership membership : getMemberships()) + { + LifecycleUtil.deactivate(membership); + } + + super.doDeactivate(); + } + + /** + * @author Eike Stepper + */ + private static final class FacilityInstalledEvent extends Event implements IFacilityInstalledEvent + { + private static final long serialVersionUID = 1L; + + private IFacility facility; + + private boolean remote; + + public FacilityInstalledEvent(ICollaboration source, IFacility facility, boolean remote) + { + super(source); + this.facility = facility; + this.remote = remote; + } + + @Override + public ICollaboration getSource() + { + return (ICollaboration)super.getSource(); + } + + public IFacility getFacility() + { + return facility; + } + + public boolean fromRemote() + { + return remote; + } + + @Override + public String toString() + { + return MessageFormat.format("FacilityInstalledEvent[source={0}, facility={1}, remote={2}]", getSource(), //$NON-NLS-1$ + facility, remote); + } + } +} diff --git a/plugins/org.eclipse.net4j.examples.installer/examples/org.eclipse.net4j.buddies.common/src/org/eclipse/net4j/buddies/internal/common/CollaborationContainer.java b/plugins/org.eclipse.net4j.examples.installer/examples/org.eclipse.net4j.buddies.common/src/org/eclipse/net4j/buddies/internal/common/CollaborationContainer.java new file mode 100644 index 0000000000..4dda2ae001 --- /dev/null +++ b/plugins/org.eclipse.net4j.examples.installer/examples/org.eclipse.net4j.buddies.common/src/org/eclipse/net4j/buddies/internal/common/CollaborationContainer.java @@ -0,0 +1,152 @@ +/* + * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) 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: + * Eike Stepper - initial API and implementation + */ +package org.eclipse.net4j.buddies.internal.common; + +import org.eclipse.net4j.buddies.common.ICollaboration; +import org.eclipse.net4j.buddies.common.ICollaborationContainer; +import org.eclipse.net4j.util.container.IContainerDelta; +import org.eclipse.net4j.util.container.SingleDeltaContainerEvent; +import org.eclipse.net4j.util.event.IEvent; +import org.eclipse.net4j.util.event.IListener; +import org.eclipse.net4j.util.lifecycle.ILifecycleEvent; +import org.eclipse.net4j.util.lifecycle.Lifecycle; +import org.eclipse.net4j.util.lifecycle.LifecycleEvent; + +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +/** + * @author Eike Stepper + */ +public class CollaborationContainer extends Lifecycle implements ICollaborationContainer, IListener +{ + private Map<Long, ICollaboration> collaborations = new HashMap<Long, ICollaboration>(); + + public CollaborationContainer(Collection<ICollaboration> collaborations) + { + if (collaborations != null) + { + for (ICollaboration collaboration : collaborations) + { + this.collaborations.put(collaboration.getID(), collaboration); + collaboration.addListener(this); + } + } + } + + public CollaborationContainer() + { + } + + public void addCollaboration(ICollaboration collaboration) + { + long id = collaboration.getID(); + synchronized (collaborations) + { + if (!collaborations.containsKey(id)) + { + collaborations.put(id, collaboration); + } + } + + IListener[] listeners = getListeners(); + if (listeners != null) + { + fireEvent(new SingleDeltaContainerEvent<ICollaboration>(this, collaboration, IContainerDelta.Kind.ADDED), + listeners); + } + + collaboration.addListener(this); + } + + public ICollaboration removeCollaboration(long id) + { + ICollaboration collaboration; + synchronized (collaborations) + { + collaboration = collaborations.remove(id); + } + + if (collaboration != null) + { + collaboration.removeListener(this); + IListener[] listeners = getListeners(); + if (listeners != null) + { + fireEvent(new SingleDeltaContainerEvent<ICollaboration>(this, collaboration, IContainerDelta.Kind.REMOVED), + listeners); + } + } + + return collaboration; + } + + public ICollaboration[] getCollaborations() + { + synchronized (collaborations) + { + return collaborations.values().toArray(new ICollaboration[collaborations.size()]); + } + } + + public ICollaboration getCollaboration(long id) + { + synchronized (collaborations) + { + return collaborations.get(id); + } + } + + public ICollaboration[] getElements() + { + return getCollaborations(); + } + + public boolean isEmpty() + { + synchronized (collaborations) + { + return collaborations.isEmpty(); + } + } + + public void notifyEvent(IEvent event) + { + if (event.getSource() instanceof ICollaboration) + { + notifyCollaborationEvent(event); + if (event instanceof LifecycleEvent) + { + LifecycleEvent e = (LifecycleEvent)event; + if (e.getKind() == ILifecycleEvent.Kind.DEACTIVATED) + { + removeCollaboration(((ICollaboration)e.getSource()).getID()); + } + } + } + } + + protected void notifyCollaborationEvent(IEvent event) + { + } + + @Override + protected void doDeactivate() throws Exception + { + for (ICollaboration collaboration : getCollaborations()) + { + collaboration.removeListener(this); + } + + super.doDeactivate(); + } +} diff --git a/plugins/org.eclipse.net4j.examples.installer/examples/org.eclipse.net4j.buddies.common/src/org/eclipse/net4j/buddies/internal/common/Membership.java b/plugins/org.eclipse.net4j.examples.installer/examples/org.eclipse.net4j.buddies.common/src/org/eclipse/net4j/buddies/internal/common/Membership.java new file mode 100644 index 0000000000..e6315db3a4 --- /dev/null +++ b/plugins/org.eclipse.net4j.examples.installer/examples/org.eclipse.net4j.buddies.common/src/org/eclipse/net4j/buddies/internal/common/Membership.java @@ -0,0 +1,118 @@ +/* + * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) 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: + * Eike Stepper - initial API and implementation + */ +package org.eclipse.net4j.buddies.internal.common; + +import org.eclipse.net4j.buddies.common.IBuddy; +import org.eclipse.net4j.buddies.common.ICollaboration; +import org.eclipse.net4j.buddies.common.IMembership; +import org.eclipse.net4j.buddies.common.IMembershipKey; +import org.eclipse.net4j.util.ObjectUtil; +import org.eclipse.net4j.util.lifecycle.Lifecycle; + +import org.eclipse.core.runtime.Platform; +import org.eclipse.core.runtime.PlatformObject; + +/** + * @author Eike Stepper + */ +public class Membership extends Lifecycle implements IMembership +{ + private MembershipKey key; + + private long startTime; + + private transient Object[] elements; + + private Membership(IBuddy buddy, ICollaboration collaboration) + { + key = new MembershipKey(buddy, collaboration); + elements = new Object[] { buddy, collaboration }; + startTime = System.currentTimeMillis(); + activate(); + } + + public IBuddy getBuddy() + { + return key.getBuddy(); + } + + public ICollaboration getCollaboration() + { + return key.getCollaboration(); + } + + public long getStartTime() + { + return startTime; + } + + public Object[] getElements() + { + return elements; + } + + public boolean isEmpty() + { + return false; + } + + public MembershipKey getKey() + { + return key; + } + + /** + * @see PlatformObject#getAdapter(Class) + */ + @SuppressWarnings("rawtypes") + public Object getAdapter(Class adapter) + { + return Platform.getAdapterManager().getAdapter(this, adapter); + } + + @Override + public boolean equals(Object obj) + { + if (obj == this) + { + return true; + } + + if (obj instanceof IMembershipKey) + { + IMembershipKey key = (IMembershipKey)obj; + return ObjectUtil.equals(getBuddy(), key.getBuddy()) + && ObjectUtil.equals(getCollaboration(), key.getCollaboration()); + } + + return false; + } + + @Override + public int hashCode() + { + return key.hashCode(); + } + + @Override + public String toString() + { + return key.toString(); + } + + public static IMembership create(IBuddy buddy, ICollaboration collaboration) + { + Membership membership = new Membership(buddy, collaboration); + ((Buddy)buddy).addMembership(membership); + ((Collaboration)collaboration).addMembership(membership); + return membership; + } +} diff --git a/plugins/org.eclipse.net4j.examples.installer/examples/org.eclipse.net4j.buddies.common/src/org/eclipse/net4j/buddies/internal/common/MembershipContainer.java b/plugins/org.eclipse.net4j.examples.installer/examples/org.eclipse.net4j.buddies.common/src/org/eclipse/net4j/buddies/internal/common/MembershipContainer.java new file mode 100644 index 0000000000..da520870b6 --- /dev/null +++ b/plugins/org.eclipse.net4j.examples.installer/examples/org.eclipse.net4j.buddies.common/src/org/eclipse/net4j/buddies/internal/common/MembershipContainer.java @@ -0,0 +1,130 @@ +/* + * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) 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: + * Eike Stepper - initial API and implementation + */ +package org.eclipse.net4j.buddies.internal.common; + +import org.eclipse.net4j.buddies.common.IBuddy; +import org.eclipse.net4j.buddies.common.ICollaboration; +import org.eclipse.net4j.buddies.common.IMembership; +import org.eclipse.net4j.buddies.common.IMembershipContainer; +import org.eclipse.net4j.buddies.common.IMembershipKey; +import org.eclipse.net4j.util.container.IContainerDelta; +import org.eclipse.net4j.util.container.SingleDeltaContainerEvent; +import org.eclipse.net4j.util.event.IEvent; +import org.eclipse.net4j.util.event.IListener; +import org.eclipse.net4j.util.lifecycle.ILifecycleEvent; +import org.eclipse.net4j.util.lifecycle.Lifecycle; + +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; + +/** + * @author Eike Stepper + */ +public class MembershipContainer extends Lifecycle implements IMembershipContainer, IListener +{ + private ConcurrentMap<IMembershipKey, IMembership> memberships = new ConcurrentHashMap<IMembershipKey, IMembership>(); + + public MembershipContainer() + { + } + + public void addMembership(IMembership membership) + { + if (memberships.putIfAbsent(membership, membership) == null) + { + IListener[] listeners = getListeners(); + if (listeners != null) + { + fireEvent(new SingleDeltaContainerEvent<IMembership>(this, membership, IContainerDelta.Kind.ADDED), listeners); + } + + membership.addListener(this); + } + } + + public IMembership removeMembership(IBuddy buddy, ICollaboration collaboration) + { + return removeMembership(new MembershipKey(buddy, collaboration)); + } + + public IMembership removeMembership(IMembershipKey key) + { + // for (IMembership membership : memberships.values()) + // { + // + // } + // + IMembership membership = memberships.remove(key); + if (membership != null) + { + membership.removeListener(this); + IListener[] listeners = getListeners(); + if (listeners != null) + { + fireEvent(new SingleDeltaContainerEvent<IMembership>(this, membership, IContainerDelta.Kind.REMOVED), listeners); + } + } + + return membership; + } + + public IMembership[] getMemberships() + { + return memberships.values().toArray(new IMembership[memberships.size()]); + } + + public IMembership getMembership(IBuddy buddy, ICollaboration collaboration) + { + return memberships.get(new MembershipKey(buddy, collaboration)); + } + + public IMembership[] getElements() + { + return getMemberships(); + } + + public boolean isEmpty() + { + return memberships.isEmpty(); + } + + public void notifyEvent(IEvent event) + { + if (event.getSource() instanceof IMembership) + { + IMembership membership = (IMembership)event.getSource(); + notifyMembershipEvent(event); + if (event instanceof ILifecycleEvent) + { + ILifecycleEvent e = (ILifecycleEvent)event; + if (e.getKind() == ILifecycleEvent.Kind.DEACTIVATED) + { + removeMembership(membership); + } + } + } + } + + protected void notifyMembershipEvent(IEvent event) + { + } + + @Override + protected void doDeactivate() throws Exception + { + for (IMembership membership : getMemberships()) + { + membership.removeListener(this); + } + + super.doDeactivate(); + } +} diff --git a/plugins/org.eclipse.net4j.examples.installer/examples/org.eclipse.net4j.buddies.common/src/org/eclipse/net4j/buddies/internal/common/MembershipKey.java b/plugins/org.eclipse.net4j.examples.installer/examples/org.eclipse.net4j.buddies.common/src/org/eclipse/net4j/buddies/internal/common/MembershipKey.java new file mode 100644 index 0000000000..eb2119f2d8 --- /dev/null +++ b/plugins/org.eclipse.net4j.examples.installer/examples/org.eclipse.net4j.buddies.common/src/org/eclipse/net4j/buddies/internal/common/MembershipKey.java @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) 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: + * Eike Stepper - initial API and implementation + */ +package org.eclipse.net4j.buddies.internal.common; + +import org.eclipse.net4j.buddies.common.IBuddy; +import org.eclipse.net4j.buddies.common.ICollaboration; +import org.eclipse.net4j.buddies.common.IMembershipKey; +import org.eclipse.net4j.util.ObjectUtil; + +/** + * @author Eike Stepper + */ +public class MembershipKey implements IMembershipKey +{ + private IBuddy buddy; + + private ICollaboration collaboration; + + public MembershipKey(IBuddy buddy, ICollaboration collaboration) + { + this.buddy = buddy; + this.collaboration = collaboration; + } + + public IBuddy getBuddy() + { + return buddy; + } + + public ICollaboration getCollaboration() + { + return collaboration; + } + + @Override + public boolean equals(Object obj) + { + if (obj == this) + { + return true; + } + + if (obj instanceof IMembershipKey) + { + IMembershipKey key = (IMembershipKey)obj; + return ObjectUtil.equals(getBuddy(), key.getBuddy()) + && ObjectUtil.equals(getCollaboration(), key.getCollaboration()); + } + + return false; + } + + @Override + public int hashCode() + { + return ObjectUtil.hashCode(buddy) ^ ObjectUtil.hashCode(collaboration); + } + + @Override + public String toString() + { + return buddy + "(" + collaboration + ")"; //$NON-NLS-1$ //$NON-NLS-2$ + } +} diff --git a/plugins/org.eclipse.net4j.examples.installer/examples/org.eclipse.net4j.buddies.common/src/org/eclipse/net4j/buddies/internal/common/MessageEvent.java b/plugins/org.eclipse.net4j.examples.installer/examples/org.eclipse.net4j.buddies.common/src/org/eclipse/net4j/buddies/internal/common/MessageEvent.java new file mode 100644 index 0000000000..7828824234 --- /dev/null +++ b/plugins/org.eclipse.net4j.examples.installer/examples/org.eclipse.net4j.buddies.common/src/org/eclipse/net4j/buddies/internal/common/MessageEvent.java @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) 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: + * Eike Stepper - initial API and implementation + */ +package org.eclipse.net4j.buddies.internal.common; + +import org.eclipse.net4j.buddies.common.IMessage; +import org.eclipse.net4j.buddies.common.IMessageEvent; +import org.eclipse.net4j.util.event.Event; +import org.eclipse.net4j.util.event.INotifier; + +/** + * @author Eike Stepper + */ +public final class MessageEvent extends Event implements IMessageEvent +{ + private static final long serialVersionUID = 1L; + + private IMessage message; + + public MessageEvent(INotifier notifier, IMessage message) + { + super(notifier); + this.message = message; + } + + public IMessage getMessage() + { + return message; + } +} diff --git a/plugins/org.eclipse.net4j.examples.installer/examples/org.eclipse.net4j.buddies.common/src/org/eclipse/net4j/buddies/internal/common/bundle/OM.java b/plugins/org.eclipse.net4j.examples.installer/examples/org.eclipse.net4j.buddies.common/src/org/eclipse/net4j/buddies/internal/common/bundle/OM.java new file mode 100644 index 0000000000..148e5e94fc --- /dev/null +++ b/plugins/org.eclipse.net4j.examples.installer/examples/org.eclipse.net4j.buddies.common/src/org/eclipse/net4j/buddies/internal/common/bundle/OM.java @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) 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: + * Eike Stepper - initial API and implementation + */ +package org.eclipse.net4j.buddies.internal.common.bundle; + +import org.eclipse.net4j.util.om.OMBundle; +import org.eclipse.net4j.util.om.OMPlatform; +import org.eclipse.net4j.util.om.OSGiActivator; +import org.eclipse.net4j.util.om.log.OMLogger; +import org.eclipse.net4j.util.om.trace.OMTracer; + +/** + * The <em>Operations & Maintenance</em> class of this bundle. + * + * @author Eike Stepper + */ +public abstract class OM +{ + public static final String BUNDLE_ID = "org.eclipse.net4j.buddies.common"; //$NON-NLS-1$ + + public static final OMBundle BUNDLE = OMPlatform.INSTANCE.bundle(BUNDLE_ID, OM.class); + + public static final OMTracer DEBUG = BUNDLE.tracer("debug"); //$NON-NLS-1$ + + public static final OMTracer DEBUG_PROTOCOL = DEBUG.tracer("protocol"); //$NON-NLS-1$ + + public static final OMTracer DEBUG_MODEL = DEBUG.tracer("model"); //$NON-NLS-1$ + + public static final OMTracer DEBUG_REVISION = DEBUG.tracer("revision"); //$NON-NLS-1$ + + public static final OMLogger LOG = BUNDLE.logger(); + + /** + * @author Eike Stepper + */ + public static final class Activator extends OSGiActivator + { + public Activator() + { + super(BUNDLE); + } + } +} diff --git a/plugins/org.eclipse.net4j.examples.installer/examples/org.eclipse.net4j.buddies.common/src/org/eclipse/net4j/buddies/internal/common/protocol/BuddyStateIndication.java b/plugins/org.eclipse.net4j.examples.installer/examples/org.eclipse.net4j.buddies.common/src/org/eclipse/net4j/buddies/internal/common/protocol/BuddyStateIndication.java new file mode 100644 index 0000000000..b1fc427a94 --- /dev/null +++ b/plugins/org.eclipse.net4j.examples.installer/examples/org.eclipse.net4j.buddies.common/src/org/eclipse/net4j/buddies/internal/common/protocol/BuddyStateIndication.java @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) 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: + * Eike Stepper - initial API and implementation + */ +package org.eclipse.net4j.buddies.internal.common.protocol; + +import org.eclipse.net4j.buddies.common.IBuddy.State; +import org.eclipse.net4j.signal.Indication; +import org.eclipse.net4j.signal.SignalProtocol; +import org.eclipse.net4j.util.io.ExtendedDataInputStream; + +/** + * @author Eike Stepper + */ +public abstract class BuddyStateIndication extends Indication +{ + public BuddyStateIndication(SignalProtocol<?> protocol) + { + super(protocol, ProtocolConstants.SIGNAL_BUDDY_STATE); + } + + @Override + protected void indicating(ExtendedDataInputStream in) throws Exception + { + String userID = in.readString(); + State state = ProtocolUtil.readState(in); + stateChanged(userID, state); + } + + protected abstract void stateChanged(String userID, State state); +} diff --git a/plugins/org.eclipse.net4j.examples.installer/examples/org.eclipse.net4j.buddies.common/src/org/eclipse/net4j/buddies/internal/common/protocol/BuddyStateNotification.java b/plugins/org.eclipse.net4j.examples.installer/examples/org.eclipse.net4j.buddies.common/src/org/eclipse/net4j/buddies/internal/common/protocol/BuddyStateNotification.java new file mode 100644 index 0000000000..a9c6f85e74 --- /dev/null +++ b/plugins/org.eclipse.net4j.examples.installer/examples/org.eclipse.net4j.buddies.common/src/org/eclipse/net4j/buddies/internal/common/protocol/BuddyStateNotification.java @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) 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: + * Eike Stepper - initial API and implementation + */ +package org.eclipse.net4j.buddies.internal.common.protocol; + +import org.eclipse.net4j.buddies.common.IBuddy.State; +import org.eclipse.net4j.signal.Request; +import org.eclipse.net4j.signal.SignalProtocol; +import org.eclipse.net4j.util.io.ExtendedDataOutputStream; + +/** + * @author Eike Stepper + */ +public class BuddyStateNotification extends Request +{ + private String userID; + + private State state; + + public BuddyStateNotification(SignalProtocol<?> protocol, String userID, State state) + { + super(protocol, ProtocolConstants.SIGNAL_BUDDY_STATE); + this.userID = userID; + this.state = state; + } + + @Override + protected void requesting(ExtendedDataOutputStream out) throws Exception + { + out.writeString(userID); + ProtocolUtil.writeState(out, state); + } +} diff --git a/plugins/org.eclipse.net4j.examples.installer/examples/org.eclipse.net4j.buddies.common/src/org/eclipse/net4j/buddies/internal/common/protocol/CollaborationLeftIndication.java b/plugins/org.eclipse.net4j.examples.installer/examples/org.eclipse.net4j.buddies.common/src/org/eclipse/net4j/buddies/internal/common/protocol/CollaborationLeftIndication.java new file mode 100644 index 0000000000..0e6a32d83c --- /dev/null +++ b/plugins/org.eclipse.net4j.examples.installer/examples/org.eclipse.net4j.buddies.common/src/org/eclipse/net4j/buddies/internal/common/protocol/CollaborationLeftIndication.java @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) 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: + * Eike Stepper - initial API and implementation + */ +package org.eclipse.net4j.buddies.internal.common.protocol; + +import org.eclipse.net4j.buddies.common.IBuddyProvider; +import org.eclipse.net4j.buddies.common.ICollaborationProvider; +import org.eclipse.net4j.buddies.internal.common.Buddy; +import org.eclipse.net4j.buddies.internal.common.Collaboration; +import org.eclipse.net4j.signal.Indication; +import org.eclipse.net4j.signal.SignalProtocol; +import org.eclipse.net4j.util.io.ExtendedDataInputStream; + +/** + * @author Eike Stepper + */ +public class CollaborationLeftIndication extends Indication +{ + private IBuddyProvider buddyProvider; + + private ICollaborationProvider collaborationProvider; + + public CollaborationLeftIndication(SignalProtocol<?> protocol, IBuddyProvider buddyProvider, + ICollaborationProvider collaborationProvider) + { + super(protocol, ProtocolConstants.SIGNAL_COLLABORATION_LEFT); + this.buddyProvider = buddyProvider; + this.collaborationProvider = collaborationProvider; + } + + @Override + protected void indicating(ExtendedDataInputStream in) throws Exception + { + long collaborationID = in.readLong(); + String userID = in.readString(); + + Collaboration collaboration = getCollaboration(collaborationID); + if (collaboration != null) + { + Buddy buddy = getBuddy(userID); + if (buddy != null) + { + collaborationLeft(buddy, collaboration); + } + } + } + + protected void collaborationLeft(Buddy buddy, Collaboration collaboration) + { + collaboration.removeMembership(buddy); + buddy.removeMembership(collaboration); + } + + protected Collaboration getCollaboration(long collaborationID) + { + return (Collaboration)collaborationProvider.getCollaboration(collaborationID); + } + + protected Buddy getBuddy(String userID) + { + return (Buddy)buddyProvider.getBuddy(userID); + } +} diff --git a/plugins/org.eclipse.net4j.examples.installer/examples/org.eclipse.net4j.buddies.common/src/org/eclipse/net4j/buddies/internal/common/protocol/CollaborationLeftNotification.java b/plugins/org.eclipse.net4j.examples.installer/examples/org.eclipse.net4j.buddies.common/src/org/eclipse/net4j/buddies/internal/common/protocol/CollaborationLeftNotification.java new file mode 100644 index 0000000000..4415b7a8e3 --- /dev/null +++ b/plugins/org.eclipse.net4j.examples.installer/examples/org.eclipse.net4j.buddies.common/src/org/eclipse/net4j/buddies/internal/common/protocol/CollaborationLeftNotification.java @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) 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: + * Eike Stepper - initial API and implementation + */ +package org.eclipse.net4j.buddies.internal.common.protocol; + +import org.eclipse.net4j.signal.Request; +import org.eclipse.net4j.signal.SignalProtocol; +import org.eclipse.net4j.util.io.ExtendedDataOutputStream; + +/** + * @author Eike Stepper + */ +public class CollaborationLeftNotification extends Request +{ + private long collaborationID; + + private String userID; + + public CollaborationLeftNotification(SignalProtocol<?> protocol, long collaborationID, String userID) + { + super(protocol, ProtocolConstants.SIGNAL_COLLABORATION_LEFT); + this.collaborationID = collaborationID; + this.userID = userID; + } + + @Override + protected void requesting(ExtendedDataOutputStream out) throws Exception + { + out.writeLong(collaborationID); + out.writeString(userID); + } +} diff --git a/plugins/org.eclipse.net4j.examples.installer/examples/org.eclipse.net4j.buddies.common/src/org/eclipse/net4j/buddies/internal/common/protocol/MessageIndication.java b/plugins/org.eclipse.net4j.examples.installer/examples/org.eclipse.net4j.buddies.common/src/org/eclipse/net4j/buddies/internal/common/protocol/MessageIndication.java new file mode 100644 index 0000000000..e968f371fa --- /dev/null +++ b/plugins/org.eclipse.net4j.examples.installer/examples/org.eclipse.net4j.buddies.common/src/org/eclipse/net4j/buddies/internal/common/protocol/MessageIndication.java @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) 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: + * Eike Stepper - initial API and implementation + */ +package org.eclipse.net4j.buddies.internal.common.protocol; + +import org.eclipse.net4j.buddies.common.ICollaboration; +import org.eclipse.net4j.buddies.common.ICollaborationProvider; +import org.eclipse.net4j.buddies.common.IMessage; +import org.eclipse.net4j.buddies.spi.common.Facility; +import org.eclipse.net4j.signal.Indication; +import org.eclipse.net4j.signal.SignalProtocol; +import org.eclipse.net4j.util.io.ExtendedDataInputStream; + +/** + * @author Eike Stepper + */ +public class MessageIndication extends Indication +{ + private ICollaborationProvider collaborationProvider; + + public MessageIndication(SignalProtocol<?> protocol, ICollaborationProvider collaborationProvider) + { + super(protocol, ProtocolConstants.SIGNAL_MESSAGE); + this.collaborationProvider = collaborationProvider; + } + + @Override + protected void indicating(ExtendedDataInputStream in) throws Exception + { + long collaborationID = in.readLong(); + String facilityType = in.readString(); + Facility facility = getFacility(collaborationID, facilityType); + if (facility != null) + { + IMessage message = ProtocolUtil.readMessage(in, facility.getClass().getClassLoader()); + facility.handleMessage(message); + } + } + + private Facility getFacility(long collaborationID, String facilityType) + { + ICollaboration collaboration = collaborationProvider.getCollaboration(collaborationID); + if (collaboration == null) + { + return null; + } + + return (Facility)collaboration.getFacility(facilityType); + } +} diff --git a/plugins/org.eclipse.net4j.examples.installer/examples/org.eclipse.net4j.buddies.common/src/org/eclipse/net4j/buddies/internal/common/protocol/MessageNotification.java b/plugins/org.eclipse.net4j.examples.installer/examples/org.eclipse.net4j.buddies.common/src/org/eclipse/net4j/buddies/internal/common/protocol/MessageNotification.java new file mode 100644 index 0000000000..19aa61a095 --- /dev/null +++ b/plugins/org.eclipse.net4j.examples.installer/examples/org.eclipse.net4j.buddies.common/src/org/eclipse/net4j/buddies/internal/common/protocol/MessageNotification.java @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) 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: + * Eike Stepper - initial API and implementation + */ +package org.eclipse.net4j.buddies.internal.common.protocol; + +import org.eclipse.net4j.buddies.common.IMessage; +import org.eclipse.net4j.signal.Request; +import org.eclipse.net4j.signal.SignalProtocol; +import org.eclipse.net4j.util.io.ExtendedDataOutputStream; + +/** + * @author Eike Stepper + */ +public class MessageNotification extends Request +{ + private long collaborationID; + + private String facilityType; + + private IMessage message; + + public MessageNotification(SignalProtocol<?> protocol, long collaborationID, String facilityType, IMessage message) + { + super(protocol, ProtocolConstants.SIGNAL_MESSAGE); + this.collaborationID = collaborationID; + this.facilityType = facilityType; + this.message = message; + } + + @Override + protected void requesting(ExtendedDataOutputStream out) throws Exception + { + out.writeLong(collaborationID); + out.writeString(facilityType); + ProtocolUtil.writeMessage(out, message); + } +} diff --git a/plugins/org.eclipse.net4j.examples.installer/examples/org.eclipse.net4j.buddies.common/src/org/eclipse/net4j/buddies/internal/common/protocol/ProtocolConstants.java b/plugins/org.eclipse.net4j.examples.installer/examples/org.eclipse.net4j.buddies.common/src/org/eclipse/net4j/buddies/internal/common/protocol/ProtocolConstants.java new file mode 100644 index 0000000000..8b1827f4d6 --- /dev/null +++ b/plugins/org.eclipse.net4j.examples.installer/examples/org.eclipse.net4j.buddies.common/src/org/eclipse/net4j/buddies/internal/common/protocol/ProtocolConstants.java @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) 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: + * Eike Stepper - initial API and implementation + */ +package org.eclipse.net4j.buddies.internal.common.protocol; + +/** + * @author Eike Stepper + */ +public interface ProtocolConstants +{ + public static final String PROTOCOL_NAME = "buddies"; //$NON-NLS-1$ + + public static final short SIGNAL_OPEN_SESSION = 1; + + public static final short SIGNAL_LOAD_ACCOUNT = 2; + + public static final short SIGNAL_BUDDY_ADDED = 3; + + public static final short SIGNAL_BUDDY_REMOVED = 4; + + public static final short SIGNAL_BUDDY_STATE = 5; + + public static final short SIGNAL_INITIATE_COLLABORATION = 6; + + public static final short SIGNAL_COLLABORATION_INITIATED = 7; + + public static final short SIGNAL_COLLABORATION_LEFT = 8; + + public static final short SIGNAL_INVITE_BUDDIES = 9; + + public static final short SIGNAL_BUDDIES_INVITED = 10; + + public static final short SIGNAL_INSTALL_FACILITY = 11; + + public static final short SIGNAL_FACILITY_INSTALLED = 12; + + public static final short SIGNAL_MESSAGE = 13; + + public static final long TIMEOUT = 5000L; + + public static final byte STATE_AVAILABLE = 1; + + public static final byte STATE_LONESOME = 2; + + public static final byte STATE_AWAY = 3; + + public static final byte STATE_DO_NOT_DISTURB = 4; +} diff --git a/plugins/org.eclipse.net4j.examples.installer/examples/org.eclipse.net4j.buddies.common/src/org/eclipse/net4j/buddies/internal/common/protocol/ProtocolUtil.java b/plugins/org.eclipse.net4j.examples.installer/examples/org.eclipse.net4j.buddies.common/src/org/eclipse/net4j/buddies/internal/common/protocol/ProtocolUtil.java new file mode 100644 index 0000000000..61b875f995 --- /dev/null +++ b/plugins/org.eclipse.net4j.examples.installer/examples/org.eclipse.net4j.buddies.common/src/org/eclipse/net4j/buddies/internal/common/protocol/ProtocolUtil.java @@ -0,0 +1,226 @@ +/* + * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) 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: + * Eike Stepper - initial API and implementation + */ +package org.eclipse.net4j.buddies.internal.common.protocol; + +import org.eclipse.net4j.buddies.common.IAccount; +import org.eclipse.net4j.buddies.common.IBuddy; +import org.eclipse.net4j.buddies.common.IBuddyContainer; +import org.eclipse.net4j.buddies.common.IMessage; +import org.eclipse.net4j.util.WrappedException; +import org.eclipse.net4j.util.io.ExtendedDataInputStream; +import org.eclipse.net4j.util.io.ExtendedDataOutputStream; + +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.ObjectStreamClass; +import java.util.Collection; +import java.util.HashSet; +import java.util.Set; + +/** + * @author Eike Stepper + */ +public final class ProtocolUtil +{ + private ProtocolUtil() + { + } + + public static void writeBuddies(ExtendedDataOutputStream out, Collection<IBuddy> buddies) throws IOException + { + if (buddies == null) + { + out.writeInt(0); + } + else + { + out.writeInt(buddies.size()); + for (IBuddy buddy : buddies) + { + out.writeString(buddy.getUserID()); + } + } + } + + public static Set<IBuddy> readBuddies(ExtendedDataInputStream in, IBuddyContainer buddyContainer) throws IOException + { + int size = in.readInt(); + Set<IBuddy> buddies = new HashSet<IBuddy>(); + for (int i = 0; i < size; i++) + { + String userID = in.readString(); + IBuddy buddy = buddyContainer.getBuddy(userID); + if (buddy != null) + { + buddies.add(buddy); + } + } + + return buddies; + } + + public static String[] readUserIDs(ExtendedDataInputStream in) throws IOException + { + int size = in.readInt(); + String[] userIDs = new String[size]; + for (int i = 0; i < size; i++) + { + userIDs[i] = in.readString(); + } + + return userIDs; + } + + public static void writeAccount(ExtendedDataOutputStream out, IAccount account) throws IOException + { + if (account != null) + { + out.writeBoolean(true); + ObjectOutputStream oos = new ObjectOutputStream(out); + oos.writeObject(account); + } + else + { + out.writeBoolean(false); + } + } + + public static IAccount readAccount(ExtendedDataInputStream in) throws IOException + { + boolean exists = in.readBoolean(); + if (!exists) + { + return null; + } + + try + { + ObjectInputStream ois = new ObjectInputStream(in); + return (IAccount)ois.readObject(); + } + catch (IOException ex) + { + throw ex; + } + catch (Exception ex) + { + throw WrappedException.wrap(ex); + } + } + + public static void writeState(ExtendedDataOutputStream out, IBuddy.State state) throws IOException + { + switch (state) + { + case AVAILABLE: + out.writeByte(ProtocolConstants.STATE_AVAILABLE); + break; + + case LONESOME: + out.writeByte(ProtocolConstants.STATE_LONESOME); + break; + + case AWAY: + out.writeByte(ProtocolConstants.STATE_AWAY); + break; + + case DO_NOT_DISTURB: + out.writeByte(ProtocolConstants.STATE_DO_NOT_DISTURB); + break; + + default: + throw new IllegalArgumentException("Illegal state: " + state); //$NON-NLS-1$ + } + } + + public static IBuddy.State readState(ExtendedDataInputStream in) throws IOException + { + byte state = in.readByte(); + switch (state) + { + case ProtocolConstants.STATE_AVAILABLE: + return IBuddy.State.AVAILABLE; + + case ProtocolConstants.STATE_LONESOME: + return IBuddy.State.LONESOME; + + case ProtocolConstants.STATE_AWAY: + return IBuddy.State.AWAY; + + case ProtocolConstants.STATE_DO_NOT_DISTURB: + return IBuddy.State.DO_NOT_DISTURB; + + default: + throw new IllegalArgumentException("Illegal state: " + state); //$NON-NLS-1$ + } + } + + public static void writeFacilityTypes(ExtendedDataOutputStream out, String[] facilityTypes) throws IOException + { + if (facilityTypes == null) + { + out.writeInt(0); + } + else + { + out.writeInt(facilityTypes.length); + for (String facilityType : facilityTypes) + { + out.writeString(facilityType); + } + } + } + + public static String[] readFacilityTypes(ExtendedDataInputStream in) throws IOException + { + int size = in.readInt(); + String[] facilityTypes = new String[size]; + for (int i = 0; i < size; i++) + { + facilityTypes[i] = in.readString(); + } + + return facilityTypes; + } + + public static void writeMessage(ExtendedDataOutputStream out, IMessage message) throws IOException + { + ObjectOutputStream oos = new ObjectOutputStream(out); + oos.writeObject(message); + } + + public static IMessage readMessage(ExtendedDataInputStream in, final ClassLoader classLoader) throws IOException + { + try + { + ObjectInputStream ois = new ObjectInputStream(in) + { + @Override + protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException + { + String className = desc.getName(); + return classLoader.loadClass(className); + } + }; + + return (IMessage)ois.readObject(); + } + catch (IOException ex) + { + throw ex; + } + catch (Exception ex) + { + throw WrappedException.wrap(ex); + } + } +} |