summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEike Stepper2007-10-08 02:31:50 (EDT)
committerEike Stepper2007-10-08 02:31:50 (EDT)
commitb9e31e4f77d5998be79fe0a775e6d928fca8b4c6 (patch)
treee7ef6646c41ffcf3d624d94dd03f0bae52570a1e
parent3e872f7c6776a8539d0681299c7c43beecd9e305 (diff)
downloadcdo-b9e31e4f77d5998be79fe0a775e6d928fca8b4c6.zip
cdo-b9e31e4f77d5998be79fe0a775e6d928fca8b4c6.tar.gz
cdo-b9e31e4f77d5998be79fe0a775e6d928fca8b4c6.tar.bz2
[205668] Provide buddies example
https://bugs.eclipse.org/bugs/show_bug.cgi?id=205668
-rw-r--r--plugins/org.eclipse.net4j.buddies.server/src/org/eclipse/net4j/buddies/internal/server/Buddy.java37
-rw-r--r--plugins/org.eclipse.net4j.buddies.server/src/org/eclipse/net4j/buddies/internal/server/BuddyAdmin.java5
-rw-r--r--plugins/org.eclipse.net4j.buddies.server/src/org/eclipse/net4j/buddies/internal/server/BuddySession.java16
-rw-r--r--plugins/org.eclipse.net4j.buddies.server/src/org/eclipse/net4j/buddies/internal/server/protocol/OpenSessionIndication.java2
-rw-r--r--plugins/org.eclipse.net4j.buddies.server/src/org/eclipse/net4j/buddies/server/IBuddySession.java4
-rw-r--r--plugins/org.eclipse.net4j.buddies/src/org/eclipse/net4j/buddies/IBuddySession.java10
-rw-r--r--plugins/org.eclipse.net4j.buddies/src/org/eclipse/net4j/internal/buddies/Buddy.java61
-rw-r--r--plugins/org.eclipse.net4j.buddies/src/org/eclipse/net4j/internal/buddies/BuddySession.java70
-rw-r--r--plugins/org.eclipse.net4j.buddies/src/org/eclipse/net4j/internal/buddies/Self.java45
-rw-r--r--plugins/org.eclipse.net4j.buddies/src/org/eclipse/net4j/internal/buddies/protocol/OpenSessionRequest.java11
10 files changed, 212 insertions, 49 deletions
diff --git a/plugins/org.eclipse.net4j.buddies.server/src/org/eclipse/net4j/buddies/internal/server/Buddy.java b/plugins/org.eclipse.net4j.buddies.server/src/org/eclipse/net4j/buddies/internal/server/Buddy.java
new file mode 100644
index 0000000..128be57
--- /dev/null
+++ b/plugins/org.eclipse.net4j.buddies.server/src/org/eclipse/net4j/buddies/internal/server/Buddy.java
@@ -0,0 +1,37 @@
+/***************************************************************************
+ * Copyright (c) 2004 - 2007 Eike Stepper, Germany.
+ * 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.server;
+
+import org.eclipse.net4j.buddies.protocol.IBuddy;
+import org.eclipse.net4j.buddies.protocol.IBuddyAccount;
+
+/**
+ * @author Eike Stepper
+ */
+public class Buddy implements IBuddy
+{
+ private IBuddyAccount account;
+
+ public Buddy(IBuddyAccount account)
+ {
+ this.account = account;
+ }
+
+ public String getUserID()
+ {
+ return account.getUserID();
+ }
+
+ public IBuddyAccount getAccount()
+ {
+ return account;
+ }
+}
diff --git a/plugins/org.eclipse.net4j.buddies.server/src/org/eclipse/net4j/buddies/internal/server/BuddyAdmin.java b/plugins/org.eclipse.net4j.buddies.server/src/org/eclipse/net4j/buddies/internal/server/BuddyAdmin.java
index 32420ed..2aa4357 100644
--- a/plugins/org.eclipse.net4j.buddies.server/src/org/eclipse/net4j/buddies/internal/server/BuddyAdmin.java
+++ b/plugins/org.eclipse.net4j.buddies.server/src/org/eclipse/net4j/buddies/internal/server/BuddyAdmin.java
@@ -73,7 +73,8 @@ public class BuddyAdmin extends Lifecycle implements IBuddyAdmin, IListener
accounts.put(userID, account);
}
- BuddySession session = new BuddySession(channel, account);
+ Buddy buddy = new Buddy(account);
+ BuddySession session = new BuddySession(channel, buddy);
sessions.put(userID, session);
session.addListener(this);
return session;
@@ -87,7 +88,7 @@ public class BuddyAdmin extends Lifecycle implements IBuddyAdmin, IListener
{
if (((ILifecycleEvent)event).getKind() == ILifecycleEvent.Kind.DEACTIVATED)
{
- String userID = ((IBuddySession)event.getSource()).getAccount().getUserID();
+ String userID = ((IBuddySession)event.getSource()).getBuddy().getUserID();
synchronized (this)
{
if (sessions.remove(userID) != null)
diff --git a/plugins/org.eclipse.net4j.buddies.server/src/org/eclipse/net4j/buddies/internal/server/BuddySession.java b/plugins/org.eclipse.net4j.buddies.server/src/org/eclipse/net4j/buddies/internal/server/BuddySession.java
index 6a23373..8a2867f 100644
--- a/plugins/org.eclipse.net4j.buddies.server/src/org/eclipse/net4j/buddies/internal/server/BuddySession.java
+++ b/plugins/org.eclipse.net4j.buddies.server/src/org/eclipse/net4j/buddies/internal/server/BuddySession.java
@@ -11,7 +11,7 @@
package org.eclipse.net4j.buddies.internal.server;
import org.eclipse.net4j.IChannel;
-import org.eclipse.net4j.buddies.protocol.IBuddyAccount;
+import org.eclipse.net4j.buddies.protocol.IBuddy;
import org.eclipse.net4j.buddies.server.IBuddySession;
import org.eclipse.net4j.internal.util.lifecycle.Lifecycle;
import org.eclipse.net4j.util.event.IEvent;
@@ -26,12 +26,12 @@ public class BuddySession extends Lifecycle implements IBuddySession, IListener
{
private IChannel channel;
- private IBuddyAccount account;
+ private IBuddy buddy;
- public BuddySession(IChannel channel, IBuddyAccount account)
+ public BuddySession(IChannel channel, IBuddy buddy)
{
this.channel = channel;
- this.account = account;
+ this.buddy = buddy;
LifecycleUtil.activate(this);
}
@@ -40,9 +40,9 @@ public class BuddySession extends Lifecycle implements IBuddySession, IListener
return channel;
}
- public IBuddyAccount getAccount()
+ public IBuddy getBuddy()
{
- return account;
+ return buddy;
}
public void close()
@@ -70,13 +70,13 @@ public class BuddySession extends Lifecycle implements IBuddySession, IListener
{
super.doActivate();
channel.addListener(this);
- account.touch();
+ buddy.getAccount().touch();
}
@Override
protected void doDeactivate() throws Exception
{
- account.touch();
+ buddy.getAccount().touch();
channel.removeListener(this);
super.doDeactivate();
}
diff --git a/plugins/org.eclipse.net4j.buddies.server/src/org/eclipse/net4j/buddies/internal/server/protocol/OpenSessionIndication.java b/plugins/org.eclipse.net4j.buddies.server/src/org/eclipse/net4j/buddies/internal/server/protocol/OpenSessionIndication.java
index f7e33ab..8f5d5e4 100644
--- a/plugins/org.eclipse.net4j.buddies.server/src/org/eclipse/net4j/buddies/internal/server/protocol/OpenSessionIndication.java
+++ b/plugins/org.eclipse.net4j.buddies.server/src/org/eclipse/net4j/buddies/internal/server/protocol/OpenSessionIndication.java
@@ -59,7 +59,7 @@ public class OpenSessionIndication extends IndicationWithResponse
if (session != null)
{
getProtocol().setInfraStructure(session);
- account = session.getAccount();
+ account = session.getBuddy().getAccount();
}
else
{
diff --git a/plugins/org.eclipse.net4j.buddies.server/src/org/eclipse/net4j/buddies/server/IBuddySession.java b/plugins/org.eclipse.net4j.buddies.server/src/org/eclipse/net4j/buddies/server/IBuddySession.java
index 19855d2..f2c38ce 100644
--- a/plugins/org.eclipse.net4j.buddies.server/src/org/eclipse/net4j/buddies/server/IBuddySession.java
+++ b/plugins/org.eclipse.net4j.buddies.server/src/org/eclipse/net4j/buddies/server/IBuddySession.java
@@ -11,7 +11,7 @@
package org.eclipse.net4j.buddies.server;
import org.eclipse.net4j.IChannel;
-import org.eclipse.net4j.buddies.protocol.IBuddyAccount;
+import org.eclipse.net4j.buddies.protocol.IBuddy;
/**
* @author Eike Stepper
@@ -20,7 +20,7 @@ public interface IBuddySession
{
public IChannel getChannel();
- public IBuddyAccount getAccount();
+ public IBuddy getBuddy();
public void close();
}
diff --git a/plugins/org.eclipse.net4j.buddies/src/org/eclipse/net4j/buddies/IBuddySession.java b/plugins/org.eclipse.net4j.buddies/src/org/eclipse/net4j/buddies/IBuddySession.java
index 38dd038..88ff356 100644
--- a/plugins/org.eclipse.net4j.buddies/src/org/eclipse/net4j/buddies/IBuddySession.java
+++ b/plugins/org.eclipse.net4j.buddies/src/org/eclipse/net4j/buddies/IBuddySession.java
@@ -11,21 +11,21 @@
package org.eclipse.net4j.buddies;
import org.eclipse.net4j.IChannel;
-import org.eclipse.net4j.buddies.protocol.IBuddyAccount;
+import org.eclipse.net4j.buddies.protocol.IBuddy;
import org.eclipse.net4j.util.container.IContainer;
-import java.util.Set;
+import java.util.Map;
/**
* @author Eike Stepper
*/
-public interface IBuddySession extends IContainer<String>
+public interface IBuddySession extends IContainer<IBuddy>
{
public IChannel getChannel();
- public IBuddyAccount getAccount();
+ public IBuddy getSelf();
- public Set<String> getBuddies();
+ public Map<String, IBuddy> getBuddies();
public void close();
}
diff --git a/plugins/org.eclipse.net4j.buddies/src/org/eclipse/net4j/internal/buddies/Buddy.java b/plugins/org.eclipse.net4j.buddies/src/org/eclipse/net4j/internal/buddies/Buddy.java
new file mode 100644
index 0000000..e2959bd
--- /dev/null
+++ b/plugins/org.eclipse.net4j.buddies/src/org/eclipse/net4j/internal/buddies/Buddy.java
@@ -0,0 +1,61 @@
+/***************************************************************************
+ * Copyright (c) 2004 - 2007 Eike Stepper, Germany.
+ * 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.internal.buddies;
+
+import org.eclipse.net4j.buddies.protocol.IBuddy;
+import org.eclipse.net4j.buddies.protocol.IBuddyAccount;
+
+/**
+ * @author Eike Stepper
+ */
+public class Buddy implements IBuddy
+{
+ private BuddySession session;
+
+ private String userID;
+
+ private IBuddyAccount account;
+
+ public Buddy(BuddySession session, String userID)
+ {
+ this.session = session;
+ this.userID = userID;
+ }
+
+ protected Buddy(BuddySession session, IBuddyAccount account)
+ {
+ }
+
+ public BuddySession getSession()
+ {
+ return session;
+ }
+
+ public String getUserID()
+ {
+ return userID;
+ }
+
+ public IBuddyAccount getAccount()
+ {
+ if (account == null)
+ {
+ account = loadAccount(userID);
+ }
+
+ return account;
+ }
+
+ protected IBuddyAccount loadAccount(String userID)
+ {
+ return null;
+ }
+}
diff --git a/plugins/org.eclipse.net4j.buddies/src/org/eclipse/net4j/internal/buddies/BuddySession.java b/plugins/org.eclipse.net4j.buddies/src/org/eclipse/net4j/internal/buddies/BuddySession.java
index f2e135e..9a3c718 100644
--- a/plugins/org.eclipse.net4j.buddies/src/org/eclipse/net4j/internal/buddies/BuddySession.java
+++ b/plugins/org.eclipse.net4j.buddies/src/org/eclipse/net4j/internal/buddies/BuddySession.java
@@ -12,6 +12,7 @@ package org.eclipse.net4j.internal.buddies;
import org.eclipse.net4j.IChannel;
import org.eclipse.net4j.buddies.IBuddySession;
+import org.eclipse.net4j.buddies.protocol.IBuddy;
import org.eclipse.net4j.buddies.protocol.IBuddyAccount;
import org.eclipse.net4j.internal.util.container.SingleDeltaContainerEvent;
import org.eclipse.net4j.internal.util.lifecycle.Lifecycle;
@@ -19,11 +20,10 @@ import org.eclipse.net4j.util.container.IContainerDelta;
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.LifecycleUtil;
import java.util.Collections;
-import java.util.HashSet;
-import java.util.Set;
+import java.util.HashMap;
+import java.util.Map;
/**
* @author Eike Stepper
@@ -32,20 +32,13 @@ public class BuddySession extends Lifecycle implements IBuddySession, IListener
{
private IChannel channel;
- private IBuddyAccount account;
+ private IBuddy self;
- private Set<String> buddies = new HashSet<String>();
+ private Map<String, IBuddy> buddies = new HashMap<String, IBuddy>();
- public BuddySession(IChannel channel, IBuddyAccount account, String[] buddies)
+ public BuddySession(IChannel channel)
{
this.channel = channel;
- this.account = account;
- for (String buddy : buddies)
- {
- this.buddies.add(buddy);
- }
-
- LifecycleUtil.activate(this);
}
public IChannel getChannel()
@@ -53,24 +46,44 @@ public class BuddySession extends Lifecycle implements IBuddySession, IListener
return channel;
}
- public IBuddyAccount getAccount()
+ public IBuddy getSelf()
+ {
+ return self;
+ }
+
+ public void setSelf(IBuddyAccount account)
{
- return account;
+ this.self = new Buddy(this, account);
}
- public Set<String> getBuddies()
+ public IBuddy addBuddy(String userID)
{
+ Buddy buddy = null;
synchronized (buddies)
{
- return Collections.unmodifiableSet(buddies);
+ if (!buddies.containsKey(userID))
+ {
+ buddy = new Buddy(this, userID);
+ buddies.put(userID, buddy);
+ }
}
+
+ return buddy;
}
- public String[] getElements()
+ public Map<String, IBuddy> getBuddies()
{
synchronized (buddies)
{
- return buddies.toArray(new String[buddies.size()]);
+ return Collections.unmodifiableMap(buddies);
+ }
+ }
+
+ public IBuddy[] getElements()
+ {
+ synchronized (buddies)
+ {
+ return buddies.values().toArray(new IBuddy[buddies.size()]);
}
}
@@ -102,24 +115,27 @@ public class BuddySession extends Lifecycle implements IBuddySession, IListener
}
}
- public void buddyAdded(String buddy)
+ public void buddyAdded(String userID)
{
- synchronized (buddies)
+ IBuddy buddy = addBuddy(userID);
+ if (buddy != null)
{
- buddies.add(buddy);
+ fireEvent(new SingleDeltaContainerEvent<IBuddy>(this, buddy, IContainerDelta.Kind.ADDED));
}
-
- fireEvent(new SingleDeltaContainerEvent<String>(this, buddy, IContainerDelta.Kind.ADDED));
}
- public void buddyRemoved(String buddy)
+ public void buddyRemoved(String userID)
{
+ IBuddy buddy;
synchronized (buddies)
{
- buddies.remove(buddy);
+ buddy = buddies.remove(userID);
}
- fireEvent(new SingleDeltaContainerEvent<String>(this, buddy, IContainerDelta.Kind.REMOVED));
+ if (buddy != null)
+ {
+ fireEvent(new SingleDeltaContainerEvent<IBuddy>(this, buddy, IContainerDelta.Kind.REMOVED));
+ }
}
@Override
diff --git a/plugins/org.eclipse.net4j.buddies/src/org/eclipse/net4j/internal/buddies/Self.java b/plugins/org.eclipse.net4j.buddies/src/org/eclipse/net4j/internal/buddies/Self.java
new file mode 100644
index 0000000..76288e2
--- /dev/null
+++ b/plugins/org.eclipse.net4j.buddies/src/org/eclipse/net4j/internal/buddies/Self.java
@@ -0,0 +1,45 @@
+/***************************************************************************
+ * Copyright (c) 2004 - 2007 Eike Stepper, Germany.
+ * 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.internal.buddies;
+
+import org.eclipse.net4j.buddies.protocol.IBuddy;
+import org.eclipse.net4j.buddies.protocol.IBuddyAccount;
+
+/**
+ * @author Eike Stepper
+ */
+public class Self implements IBuddy
+{
+ private BuddySession session;
+
+ private IBuddyAccount account;
+
+ protected Self(BuddySession session, IBuddyAccount account)
+ {
+ this.session = session;
+ this.account = account;
+ }
+
+ public BuddySession getSession()
+ {
+ return session;
+ }
+
+ public String getUserID()
+ {
+ return account.getUserID();
+ }
+
+ public IBuddyAccount getAccount()
+ {
+ return account;
+ }
+}
diff --git a/plugins/org.eclipse.net4j.buddies/src/org/eclipse/net4j/internal/buddies/protocol/OpenSessionRequest.java b/plugins/org.eclipse.net4j.buddies/src/org/eclipse/net4j/internal/buddies/protocol/OpenSessionRequest.java
index 68f6f92..980a102 100644
--- a/plugins/org.eclipse.net4j.buddies/src/org/eclipse/net4j/internal/buddies/protocol/OpenSessionRequest.java
+++ b/plugins/org.eclipse.net4j.buddies/src/org/eclipse/net4j/internal/buddies/protocol/OpenSessionRequest.java
@@ -19,6 +19,7 @@ import org.eclipse.net4j.signal.RequestWithConfirmation;
import org.eclipse.net4j.util.WrappedException;
import org.eclipse.net4j.util.io.ExtendedDataInputStream;
import org.eclipse.net4j.util.io.ExtendedDataOutputStream;
+import org.eclipse.net4j.util.lifecycle.LifecycleUtil;
import java.io.IOException;
import java.io.ObjectInputStream;
@@ -61,11 +62,13 @@ public class OpenSessionRequest extends RequestWithConfirmation<IBuddySession>
return null;
}
+ BuddySession session = new BuddySession(getProtocol().getChannel());
+ getProtocol().setInfraStructure(session);
+
int size = in.readInt();
- String[] buddies = new String[size];
for (int i = 0; i < size; i++)
{
- buddies[i] = in.readString();
+ session.addBuddy(in.readString());
}
IBuddyAccount account;
@@ -83,8 +86,8 @@ public class OpenSessionRequest extends RequestWithConfirmation<IBuddySession>
throw WrappedException.wrap(ex);
}
- BuddySession session = new BuddySession(getProtocol().getChannel(), account, buddies);
- getProtocol().setInfraStructure(session);
+ session.setSelf(account);
+ LifecycleUtil.activate(session);
return session;
}
}