1 11 package org.eclipse.team.internal.ccvs.core.util; 12 13 import java.util.ArrayList ; 14 import java.util.HashMap ; 15 import java.util.List ; 16 import java.util.Map ; 17 18 import org.eclipse.core.resources.IProject; 19 import org.eclipse.core.resources.ResourcesPlugin; 20 import org.eclipse.core.runtime.*; 21 import org.eclipse.core.runtime.preferences.IEclipsePreferences; 22 import org.eclipse.core.runtime.preferences.IEclipsePreferences.INodeChangeListener; 23 import org.eclipse.core.runtime.preferences.IEclipsePreferences.IPreferenceChangeListener; 24 import org.eclipse.core.runtime.preferences.IEclipsePreferences.NodeChangeEvent; 25 import org.eclipse.core.runtime.preferences.IEclipsePreferences.PreferenceChangeEvent; 26 import org.eclipse.team.core.RepositoryProvider; 27 import org.eclipse.team.internal.ccvs.core.*; 28 import org.eclipse.team.internal.ccvs.core.connection.CVSRepositoryLocation; 29 import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot; 30 import org.eclipse.team.internal.ccvs.core.syncinfo.FolderSyncInfo; 31 import org.osgi.service.prefs.BackingStoreException; 32 33 37 public class KnownRepositories implements INodeChangeListener, IPreferenceChangeListener { 38 39 private List repositoryListeners = new ArrayList (); 40 private Map repositories; 41 42 private static KnownRepositories instance; 43 44 public static synchronized KnownRepositories getInstance() { 45 if (instance == null) { 46 instance = new KnownRepositories(); 47 } 48 return instance; 49 } 50 51 56 private abstract class Notification implements ISafeRunnable { 57 private ICVSListener listener; 58 public void handleException(Throwable exception) { 59 } 61 public void run(ICVSListener listener) { 62 this.listener = listener; 63 Platform.run(this); 64 } 65 public void run() throws Exception { 66 notify(listener); 67 } 68 72 protected abstract void notify(ICVSListener listener); 73 } 74 75 78 public void addRepositoryListener(ICVSListener listener) { 79 synchronized(repositoryListeners) { 80 repositoryListeners.add(listener); 81 } 82 } 83 84 87 public void removeRepositoryListener(ICVSListener listener) { 88 synchronized(repositoryListeners) { 89 repositoryListeners.remove(listener); 90 } 91 } 92 93 97 public ICVSRepositoryLocation addRepository(final ICVSRepositoryLocation repository, boolean broadcast) { 98 CVSRepositoryLocation existingLocation; 99 synchronized (this) { 100 existingLocation = internalGetRepository(repository.getLocation(false)); 102 if (existingLocation == null) { 103 store((CVSRepositoryLocation)repository); 105 existingLocation = (CVSRepositoryLocation)repository; 106 } 107 } 108 if (broadcast) { 110 final CVSRepositoryLocation location = existingLocation; 111 ((CVSRepositoryLocation)repository).updateCache(); 112 fireNotification(new Notification() { 113 public void notify(ICVSListener listener) { 114 listener.repositoryAdded(location); 115 } 116 }); 117 } 118 return existingLocation; 119 } 120 121 126 public void disposeRepository(final ICVSRepositoryLocation repository) { 127 Object removed; 128 synchronized (this) { 129 ((CVSRepositoryLocation)repository).dispose(); 130 removed = getRepositoriesMap().remove(repository.getLocation(false)); 131 } 132 if (removed != null) { 133 fireNotification(new Notification() { 134 public void notify(ICVSListener listener) { 135 listener.repositoryRemoved(repository); 136 } 137 }); 138 } 139 } 140 141 145 public synchronized boolean isKnownRepository(String location) { 146 return internalGetRepository(location) != null; 147 } 148 149 152 public synchronized ICVSRepositoryLocation[] getRepositories() { 153 return (ICVSRepositoryLocation[])getRepositoriesMap().values().toArray(new ICVSRepositoryLocation[getRepositoriesMap().size()]); 154 } 155 156 178 public synchronized ICVSRepositoryLocation getRepository(String location) throws CVSException { 179 ICVSRepositoryLocation repository = internalGetRepository(location); 180 if (repository == null) { 181 repository = CVSRepositoryLocation.fromString(location); 182 } 183 return repository; 184 } 185 186 private CVSRepositoryLocation internalGetRepository(String location) { 187 return (CVSRepositoryLocation)getRepositoriesMap().get(location); 188 } 189 190 193 private void store(CVSRepositoryLocation location) { 194 getRepositoriesMap().put(location.getLocation(), location); 196 location.storePreferences(); 197 } 198 199 private Map getRepositoriesMap() { 200 if (repositories == null) { 201 repositories = new HashMap (); 203 IEclipsePreferences prefs = (IEclipsePreferences) CVSRepositoryLocation.getParentPreferences(); 204 prefs.addNodeChangeListener(this); 205 try { 206 String [] keys = prefs.childrenNames(); 207 for (int i = 0; i < keys.length; i++) { 208 String key = keys[i]; 209 try { 210 IEclipsePreferences node = (IEclipsePreferences) prefs.node(key); 211 node.addPreferenceChangeListener(this); 212 String location = node.get(CVSRepositoryLocation.PREF_LOCATION, null); 213 if (location != null) { 214 repositories.put(location, CVSRepositoryLocation.fromString(location)); 215 } else { 216 node.removeNode(); 217 prefs.flush(); 218 } 219 } catch (CVSException e) { 220 CVSProviderPlugin.log(e); 222 } 223 } 224 if (repositories.isEmpty()) { 225 getRepositoriesFromProjects(); 226 } 227 } catch (BackingStoreException e) { 228 CVSProviderPlugin.log(IStatus.ERROR, CVSMessages.KnownRepositories_0, e); 230 } catch (CVSException e) { 231 CVSProviderPlugin.log(e); 232 } 233 } 234 return repositories; 235 } 236 237 private void getRepositoriesFromProjects() throws CVSException { 238 IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects(); 241 for (int i = 0; i < projects.length; i++) { 242 RepositoryProvider provider = RepositoryProvider.getProvider(projects[i], CVSProviderPlugin.getTypeId()); 243 if (provider!=null) { 244 ICVSFolder folder = (ICVSFolder)CVSWorkspaceRoot.getCVSResourceFor(projects[i]); 245 FolderSyncInfo info = folder.getFolderSyncInfo(); 246 if (info != null) { 247 addRepository(getRepository(info.getRoot()), false); 248 } 249 } 250 } 251 } 252 253 private ICVSListener[] getListeners() { 254 synchronized(repositoryListeners) { 255 return (ICVSListener[]) repositoryListeners.toArray(new ICVSListener[repositoryListeners.size()]); 256 } 257 } 258 259 private void fireNotification(Notification notification) { 260 ICVSListener[] listeners = getListeners(); 262 for (int i = 0; i < listeners.length; i++) { 264 ICVSListener listener = listeners[i]; 265 notification.run(listener); 266 } 267 } 268 269 public void added(NodeChangeEvent event) { 270 ((IEclipsePreferences)event.getChild()).addPreferenceChangeListener(this); 271 } 272 273 public void removed(NodeChangeEvent event) { 274 } 277 278 public void preferenceChange(PreferenceChangeEvent event) { 279 if (CVSRepositoryLocation.PREF_LOCATION.equals(event.getKey())) { 280 String location = (String )event.getNewValue(); 281 if (location == null) { 282 ((IEclipsePreferences)event.getNode()).removePreferenceChangeListener(this); 283 } else { 284 try { 285 addRepository(CVSRepositoryLocation.fromString(location), true); 286 } catch (CVSException e) { 287 CVSProviderPlugin.log(e); 288 } 289 } 290 } 291 } 292 } 293 | Popular Tags |