Skip to main content
summaryrefslogtreecommitdiffstats
blob: c4ee325946e57ba85dbceda5236e785394835cb2 (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*******************************************************************************
 * Copyright (c) 2000, 2003 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.team.internal.core;

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

import org.eclipse.core.resources.ISaveContext;
import org.eclipse.core.resources.ISaveParticipant;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.ISafeRunnable;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.QualifiedName;
import org.eclipse.team.core.TeamException;
import org.eclipse.team.core.subscribers.ITeamResourceChangeListener;
import org.eclipse.team.core.subscribers.TeamDelta;
import org.eclipse.team.core.subscribers.TeamSubscriber;
import org.eclipse.team.core.subscribers.TeamSubscriberFactory;

/**
 * This class provides the private implementation of <code>ISubscriberManager</code>.
 */
public class SubscriberManager implements ISaveParticipant {

	private static String SUBSCRIBER_EXTENSION = "subscriber"; //$NON-NLS-1$
	final static private String SAVECTX_SUBSCRIBERS = "subscribers";  //$NON-NLS-1$
	final static private String SAVECTX_SUBSCRIBER = "subscriber"; //$NON-NLS-1$
	final static private String SAVECTX_QUALIFIER = "qualifier"; //$NON-NLS-1$
	final static private String SAVECTX_LOCALNAME = "localName"; //$NON-NLS-1$
	
	private Map subscribers = new HashMap();
	private List listeners = new ArrayList(1);
	private Map factories = new HashMap();
	
	/* (non-Javadoc)
	 * @see org.eclipse.team.core.subscribers.ISubscriberManager#getSubscriber(org.eclipse.core.runtime.QualifiedName)
	 */
	public TeamSubscriber getSubscriber(QualifiedName id) {
		synchronized(subscribers) {
			TeamSubscriber s = (TeamSubscriber)subscribers.get(id);
			return s;
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.team.core.subscribers.ISubscriberManager#getSubscribers()
	 */
	public TeamSubscriber[] getSubscribers() {
		synchronized(subscribers) {
			return (TeamSubscriber[])subscribers.values().toArray(
				new TeamSubscriber[subscribers.size()]);
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.team.core.subscribers.ISubscriberManager#addTeamResourceChangeListener(org.eclipse.team.core.subscribers.ITeamResourceChangeListener)
	 */
	public void addTeamResourceChangeListener(ITeamResourceChangeListener listener) {
		synchronized(listener) {
			listeners.add(listener);
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.team.core.subscribers.ISubscriberManager#removeTeamResourceChangeListener(org.eclipse.team.core.subscribers.ITeamResourceChangeListener)
	 */
	public void removeTeamResourceChangeListener(ITeamResourceChangeListener listener) {
		synchronized(listener) {
			listeners.remove(listener);
		}
	}

	public void doneSaving(ISaveContext context) {
	}

	public void prepareToSave(ISaveContext context) throws CoreException {
	}

	public void rollback(ISaveContext context) {
	}

	public void saving(ISaveContext context) throws CoreException {
		// save subscribers during snapshot and at full save
		//saveSubscribers();
	}
	
	public void startup() {		
	}
	
	/*
	 * Fires a team resource change event to all registered listeners
	 * Only listeners registered at the time this method is called are notified.
	 */
	protected void fireTeamResourceChange(TeamDelta[] deltas) {
		ITeamResourceChangeListener[] allListeners;
		// Copy the listener list so we're not calling client code while synchronized
		synchronized(listeners) {
			allListeners = (ITeamResourceChangeListener[]) listeners.toArray(new ITeamResourceChangeListener[listeners.size()]);
		}
		fireTeamResourceChange(allListeners, deltas);
	}	
	
	public void fireTeamResourceChange(ITeamResourceChangeListener[] listeners, final TeamDelta[] deltas) {
		// Notify the listeners safely so all will receive notification
		for (int i = 0; i < listeners.length; i++) {
			final ITeamResourceChangeListener listener = listeners[i];
			Platform.run(new ISafeRunnable() {
				public void handleException(Throwable exception) {
					// don't log the exception....it is already being logged in Platform#run
				}
				public void run() throws Exception {
					listener.teamResourceChanged(deltas);

				}
			});
		}
	}

	synchronized void restoreSubscribers() {
//		try {
//			SaveContext root = SaveContextXMLWriter.readXMLPluginMetaFile(TeamPlugin.getPlugin(), "subscribers"); //$NON-NLS-1$
//			if(root != null && root.getName().equals(SAVECTX_SUBSCRIBERS)) {
//				SaveContext[] contexts = root.getChildren();
//				for (int i = 0; i < contexts.length; i++) {
//					SaveContext context = contexts[i];
//					if(context.getName().equals(SAVECTX_SUBSCRIBER)) {
//						String qualifier = context.getAttribute(SAVECTX_QUALIFIER);
//						String localName = context.getAttribute(SAVECTX_LOCALNAME);
//						TeamSubscriberFactory factory = create(qualifier);
//						if(factory == null) {
//							TeamPlugin.log(new TeamException(Policy.bind("TeamProvider.10", qualifier.toString()))); //$NON-NLS-1$
//						}
//						SaveContext[] children = context.getChildren();
//						if(children.length == 1) {			
//							TeamSubscriber s = factory.restoreSubscriber(new QualifiedName(qualifier, localName), children[0]);								
//							if(s != null) {
//								//registerSubscriber(s);
//							}
//						}
//					}
//				}
//			
//			}
//		} catch (TeamException e) {
//			TeamPlugin.log(e);
//		}
	}

	synchronized void saveSubscribers() {
		SaveContext root = new SaveContext();
		root.setName(SAVECTX_SUBSCRIBERS);
		List children = new ArrayList();
		try {
			for (Iterator it = subscribers.values().iterator(); it.hasNext();) {			
				TeamSubscriber subscriber = (TeamSubscriber) it.next();			
				String qualifier = subscriber.getId().getQualifier();
				TeamSubscriberFactory factory = create(qualifier);
				if(factory == null) {
					TeamPlugin.log(new TeamException(Policy.bind("TeamProvider.11", qualifier))); //$NON-NLS-1$
				}
				SaveContext child = factory.saveSubscriber(subscriber);
				if(child != null) { 
					SaveContext item = new SaveContext();				
					item.putChild(child);
					item.setName(SAVECTX_SUBSCRIBER);
					Map attributes = new HashMap();
					attributes.put(SAVECTX_QUALIFIER, subscriber.getId().getQualifier());
					attributes.put(SAVECTX_LOCALNAME, subscriber.getId().getLocalName());
					item.setAttributes(attributes);				
					children.add(item);
				}
			}
			root.setChildren((SaveContext[])children.toArray(new SaveContext[children.size()]));
			SaveContextXMLWriter.writeXMLPluginMetaFile(TeamPlugin.getPlugin(), "subscribers", root); //$NON-NLS-1$
		} catch (TeamException e) {
			TeamPlugin.log(e);
		}
	}
	
	private TeamSubscriberFactory create(String id) {
		TeamSubscriberFactory sFactory = (TeamSubscriberFactory)factories.get(id);
		if(sFactory != null) {
			return sFactory;
		}
		
		TeamPlugin plugin = TeamPlugin.getPlugin();
		if (plugin != null) {
			IExtensionPoint extension = plugin.getDescriptor().getExtensionPoint(SUBSCRIBER_EXTENSION);
			if (extension != null) {
				IExtension[] extensions =  extension.getExtensions();
				for (int i = 0; i < extensions.length; i++) {
					IConfigurationElement [] configElements = extensions[i].getConfigurationElements();
					for (int j = 0; j < configElements.length; j++) {
						try {
							//Its ok not to have a typeClass extension.  In this case, a default instance will be created.
							if(configElements[j].getAttribute("class") != null) { //$NON-NLS-1$
								sFactory = (TeamSubscriberFactory) configElements[j].createExecutableExtension("class"); //$NON-NLS-1$
							}
							factories.put(sFactory.getID(), sFactory);
							return sFactory;
							} catch (CoreException e) {
								TeamPlugin.log(e);
							} catch (ClassCastException e) {
								String className = configElements[j].getAttribute("class"); //$NON-NLS-1$
								TeamPlugin.log(IStatus.ERROR, Policy.bind("RepositoryProviderType.invalidClass", id.toString(), className), e); //$NON-NLS-1$
							}
						return null;
					}
				}
			}		
		}
		return null;
	}	
}

Back to the top