Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: eaf298dc037beff5c46341d6c3151173a9707417 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/****************************************************************************
 * Copyright (c) 2004 Composent, 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:
 *    Composent, Inc. - initial API and implementation
 *****************************************************************************/

package org.eclipse.ecf.presence.roster;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.ecf.core.identity.ID;
import org.eclipse.ecf.presence.IPresence;
import org.eclipse.ecf.presence.IPresenceSender;

public abstract class AbstractRosterManager implements IRosterManager {

	protected IRoster roster;

	private List rosterSubscriptionListeners = new ArrayList();
	private List rosterUpdateListeners = new ArrayList();

	public AbstractRosterManager() {

	}

	public AbstractRosterManager(IRoster roster) {
		this.roster = roster;
	}

	public synchronized void addRosterSubscriptionListener(
			IRosterSubscriptionListener listener) {
		if (listener != null) {
			synchronized (rosterSubscriptionListeners) {
				rosterSubscriptionListeners.add(listener);
			}
		}
	}

	public synchronized void addRosterListener(
			IRosterListener listener) {
		if (listener != null) {
			synchronized (rosterUpdateListeners) {
				rosterUpdateListeners.add(listener);
			}
		}
	}

	protected void fireRosterUpdate(IRosterItem changedItem) {
		List toNotify = null;
		synchronized (rosterUpdateListeners) {
			toNotify = new ArrayList(rosterUpdateListeners);
		}
		for (Iterator i = toNotify.iterator(); i.hasNext();)
			((IRosterListener) i.next()).handleRosterUpdate(roster,
					changedItem);
	}

	protected void fireRosterAdd(IRosterEntry entry) {
		List toNotify = null;
		synchronized (rosterUpdateListeners) {
			toNotify = new ArrayList(rosterUpdateListeners);
		}
		for (Iterator i = toNotify.iterator(); i.hasNext();)
			((IRosterListener) i.next()).handleRosterEntryAdd(entry);
		
	}
	
	protected void fireRosterRemove(IRosterEntry entry) {
		List toNotify = null;
		synchronized (rosterUpdateListeners) {
			toNotify = new ArrayList(rosterUpdateListeners);
		}
		for (Iterator i = toNotify.iterator(); i.hasNext();)
			((IRosterListener) i.next()).handleRosterEntryRemove(entry);
		
	}
	
	protected void fireSubscriptionListener(ID fromID, IPresence.Type presencetype) {
		List toNotify = null;
		synchronized (rosterSubscriptionListeners) {
			toNotify = new ArrayList(rosterSubscriptionListeners);
		}
		for (Iterator i = toNotify.iterator(); i
				.hasNext();) {
			IRosterSubscriptionListener l = (IRosterSubscriptionListener) i
					.next();
			if (presencetype.equals(IPresence.Type.SUBSCRIBE)) {
				l.handleSubscribeRequest(fromID);
			} else if (presencetype.equals(IPresence.Type.SUBSCRIBED)) {
				l.handleSubscribed(fromID);
			} else if (presencetype.equals(
					IPresence.Type.UNSUBSCRIBED)) {
				l.handleUnsubscribed(fromID);
			}
		}
	}

	public abstract IPresenceSender getPresenceSender();

	public IRoster getRoster() {
		return roster;
	}

	public abstract IRosterSubscriptionSender getRosterSubscriptionSender();

	public synchronized void removeRosterSubscriptionListener(
			IRosterSubscriptionListener listener) {
		if (listener != null) {
			synchronized (rosterSubscriptionListeners) {
				rosterSubscriptionListeners.remove(listener);				
			}
		}
	}

	public synchronized void removeRosterListener(
			IRosterListener listener) {
		if (listener != null) {
			synchronized (rosterUpdateListeners) {
				rosterUpdateListeners.remove(listener);				
			}
		}
	}

	public Object getAdapter(Class adapter) {
		return null;
	}
	
	public void disconnect() {
		roster.getItems().clear();
		fireRosterUpdate(roster);
		synchronized (rosterUpdateListeners) {
			rosterUpdateListeners.clear();
		}
		synchronized (rosterSubscriptionListeners) {
			rosterSubscriptionListeners.clear();
		}
	}
}

Back to the top