1 19 package org.netbeans.modules.versioning; 20 21 import org.netbeans.modules.versioning.spi.VersioningSystem; 22 import org.netbeans.modules.versioning.spi.VCSContext; 23 import org.netbeans.modules.versioning.spi.LocalHistory; 24 import org.netbeans.modules.versioning.util.Utils; 25 import org.netbeans.modules.masterfs.providers.InterceptionListener; 26 import org.openide.util.Lookup; 27 import org.openide.util.LookupListener; 28 import org.openide.util.LookupEvent; 29 30 import java.io.File ; 31 import java.util.*; 32 import java.beans.PropertyChangeListener ; 33 import java.beans.PropertyChangeEvent ; 34 35 40 public class VersioningManager implements PropertyChangeListener , LookupListener { 41 42 private static VersioningManager instance; 43 44 public static synchronized VersioningManager getInstance() { 45 if (instance == null) { 46 instance = new VersioningManager(); 47 instance.init(); 48 } 49 return instance; 50 } 51 52 54 private final FilesystemInterceptor filesystemInterceptor; 55 56 59 private final VersioningOutputManager outputManager = new VersioningOutputManager(); 60 61 64 private final Collection<VersioningSystem> versioningSystems = new ArrayList<VersioningSystem>(2); 65 66 69 private final Map<File , VersioningSystem> folderOwners = new WeakHashMap<File , VersioningSystem>(100); 70 71 74 private VersioningSystem localHistory; 75 76 79 private Map<File , Boolean > localHistoryFolders = new WeakHashMap<File , Boolean >(100); 80 81 private final VersioningSystem NULL_OWNER = new VersioningSystem() { 82 public String getDisplayName() { 83 return null; 84 } 85 }; 86 87 private VersioningManager() { 88 filesystemInterceptor = new FilesystemInterceptor(); 89 } 90 91 private void init() { 92 initVersioningSystems(); 93 filesystemInterceptor.init(this); 94 } 95 96 private void initVersioningSystems() { 97 Lookup.Result<VersioningSystem> result = Lookup.getDefault().lookup(new Lookup.Template<VersioningSystem>(VersioningSystem.class)); 98 refreshVersioningSystems(result.allInstances()); 99 result.addLookupListener(this); 100 } 101 102 107 private void refreshVersioningSystems(Collection<? extends VersioningSystem> systems) { 108 synchronized(versioningSystems) { 109 unloadVersioningSystems(); 110 loadVersioningSystems(systems); 111 } 112 } 113 114 private void loadVersioningSystems(Collection<? extends VersioningSystem> systems) { 115 assert versioningSystems.size() == 0; 116 assert localHistory == null; 117 versioningSystems.addAll(systems); 118 for (VersioningSystem system : versioningSystems) { 119 if (localHistory == null && system instanceof LocalHistory) { 120 localHistory = system; 121 } 122 system.addPropertyChangeListener(this); 123 } 124 } 125 126 private void unloadVersioningSystems() { 127 for (VersioningSystem system : versioningSystems) { 128 system.removePropertyChangeListener(this); 129 } 130 versioningSystems.clear(); 131 localHistory = null; 132 } 133 134 InterceptionListener getInterceptionListener() { 135 return filesystemInterceptor; 136 } 137 138 private synchronized void flushFileOwnerCache() { 139 folderOwners.clear(); 140 localHistoryFolders.clear(); 141 } 142 143 synchronized VersioningSystem[] getVersioningSystems() { 144 return versioningSystems.toArray(new VersioningSystem[versioningSystems.size()]); 145 } 146 147 153 VersioningSystem[] getOwners(VCSContext ctx) { 154 Set<File > files = ctx.getRootFiles(); 155 Set<VersioningSystem> owners = new HashSet<VersioningSystem>(); 156 for (File file : files) { 157 VersioningSystem vs = getOwner(file); 158 if (vs != null) { 159 owners.add(vs); 160 } 161 } 162 return (VersioningSystem[]) owners.toArray(new VersioningSystem[owners.size()]); 163 } 164 165 178 public synchronized VersioningSystem getOwner(File file) { 179 File folder = file; 180 if (file.isFile()) { 181 folder = file.getParentFile(); 182 if (folder == null) return null; 183 } 184 185 VersioningSystem owner = folderOwners.get(folder); 186 if (owner == NULL_OWNER) return null; 187 if (owner != null) return owner; 188 189 File closestParent = null; 190 for (VersioningSystem system : versioningSystems) { 191 if (system != localHistory) { File topmost = system.getTopmostManagedParent(folder); 193 if (topmost != null && (closestParent == null || Utils.isParentOrEqual(closestParent, topmost))) { 194 owner = system; 195 closestParent = topmost; 196 } 197 } 198 } 199 200 if (owner != null) { 201 folderOwners.put(folder, owner); 202 } else { 203 folderOwners.put(folder, NULL_OWNER); 204 } 205 return owner; 206 } 207 208 214 synchronized VersioningSystem getLocalHistory(File file) { 215 if (localHistory == null) return null; 216 File folder = file; 217 if (file.isFile()) { 218 folder = file.getParentFile(); 219 if (folder == null) return null; 220 } 221 222 Boolean isManagedByLocalHistory = localHistoryFolders.get(folder); 223 if (isManagedByLocalHistory != null) { 224 return isManagedByLocalHistory ? localHistory : null; 225 } 226 227 boolean isManaged = localHistory.getTopmostManagedParent(folder) != null; 228 if (isManaged) { 229 localHistoryFolders.put(folder, Boolean.TRUE); 230 return localHistory; 231 } else { 232 localHistoryFolders.put(folder, Boolean.FALSE); 233 return null; 234 } 235 } 236 237 public VersioningOutputManager getOutputManager() { 238 return outputManager; 239 } 240 241 public void resultChanged(LookupEvent ev) { 242 Lookup.Result<VersioningSystem> result = (Lookup.Result<VersioningSystem>) ev.getSource(); 243 refreshVersioningSystems(result.allInstances()); 244 } 245 246 249 public void propertyChange(PropertyChangeEvent evt) { 250 if (VersioningSystem.PROP_STATUS_CHANGED.equals(evt.getPropertyName())) { 251 Set<File > files = (Set<File >) evt.getNewValue(); 252 VersioningAnnotationProvider.instance.refreshAnnotations(files); 253 } else if (VersioningSystem.PROP_ANNOTATIONS_CHANGED.equals(evt.getPropertyName())) { 254 Set<File > files = (Set<File >) evt.getNewValue(); 255 VersioningAnnotationProvider.instance.refreshAnnotations(files); 256 } else if (VersioningSystem.PROP_VERSIONED_ROOTS.equals(evt.getPropertyName())) { 257 flushFileOwnerCache(); 258 } 259 } 260 } 261 | Popular Tags |