1 11 package org.eclipse.core.internal.properties; 12 13 import java.util.*; 14 import org.eclipse.core.internal.events.ILifecycleListener; 15 import org.eclipse.core.internal.events.LifecycleEvent; 16 import org.eclipse.core.internal.resources.*; 17 import org.eclipse.core.resources.IResource; 18 import org.eclipse.core.resources.IResourceStatus; 19 import org.eclipse.core.runtime.*; 20 import org.eclipse.osgi.util.NLS; 21 22 25 public class PropertyManager implements IManager, ILifecycleListener, IPropertyManager { 26 protected Workspace workspace; 27 28 public PropertyManager(Workspace workspace) { 29 this.workspace = workspace; 30 } 31 32 public void closePropertyStore(IResource target) throws CoreException { 33 PropertyStore store = getPropertyStoreOrNull(target); 34 if (store == null) 35 return; 36 synchronized (store) { 37 store.shutdown(null); 38 setPropertyStore(target, null); 39 } 40 } 41 42 46 public void copy(IResource source, IResource destination, int depth) throws CoreException { 47 PropertyStore sourceStore = getPropertyStore(source); 49 PropertyStore destinationStore = getPropertyStore(destination); 50 synchronized (sourceStore) { 51 assertRunning(source, sourceStore); 52 synchronized (destinationStore) { 53 assertRunning(destination, destinationStore); 54 copyProperties(source, destination, depth); 55 sourceStore.commit(); 56 destinationStore.commit(); 57 } 58 } 59 } 60 61 64 private void assertRunning(IResource target, PropertyStore store) throws CoreException { 65 if (!store.isRunning()) { 66 String message = NLS.bind(CompatibilityMessages.resources_mustExist, target.getFullPath()); 69 throw new ResourceException(IResourceStatus.RESOURCE_NOT_FOUND, target.getFullPath(), message, null); 70 } 71 } 72 73 private void copyProperties(IResource source, IResource destination, int depth) throws CoreException { 74 PropertyStore sourceStore = getPropertyStore(source); 75 PropertyStore destStore = getPropertyStore(destination); 76 ResourceName sourceName = getPropertyKey(source); 77 ResourceName destName = getPropertyKey(destination); 78 QueryResults results = sourceStore.getAll(sourceName, depth); 79 for (Enumeration resources = results.getResourceNames(); resources.hasMoreElements();) { 80 ResourceName resourceName = (ResourceName) resources.nextElement(); 81 List properties = results.getResults(resourceName); 82 if (properties.isEmpty()) 83 continue; 84 StoredProperty[] propsArray = new StoredProperty[properties.size()]; 85 propsArray = (StoredProperty[]) properties.toArray(propsArray); 86 int segmentsToDrop = source.getProjectRelativePath().matchingFirstSegments(resourceName.getPath()); 87 IPath path = destName.getPath().append(resourceName.getPath().removeFirstSegments(segmentsToDrop)); 88 resourceName = new ResourceName(resourceName.getQualifier(), path); 89 destStore.set(resourceName, propsArray, IResource.DEPTH_ZERO, PropertyStore.SET_UPDATE); 90 } 91 } 92 93 public void deleteProperties(IResource target, int depth) throws CoreException { 94 switch (target.getType()) { 95 case IResource.FILE : 96 case IResource.FOLDER : 97 PropertyStore store = getPropertyStore(target); 98 synchronized (store) { 99 assertRunning(target, store); 100 store.removeAll(getPropertyKey(target), depth); 101 store.commit(); 102 } 103 break; 104 case IResource.PROJECT : 105 case IResource.ROOT : 106 deletePropertyStore(target, true); 107 } 108 } 109 110 115 public void deleteResource(IResource target) throws CoreException { 116 switch (target.getType()) { 117 case IResource.FILE : 118 case IResource.FOLDER : 119 case IResource.ROOT : 120 deleteProperties(target, IResource.DEPTH_INFINITE); 121 break; 122 case IResource.PROJECT : 123 deletePropertyStore(target, false); 125 } 126 } 127 128 private void deletePropertyStore(IResource target, boolean restart) throws CoreException { 129 PropertyStore store = getPropertyStoreOrNull(target); 130 if (store == null) 131 return; 132 synchronized (store) { 133 store.shutdown(null); 134 workspace.getMetaArea().getPropertyStoreLocation(target).toFile().delete(); 135 if (restart) { 137 ResourceInfo info = getPropertyHost(target).getResourceInfo(false, false); 138 if (info != null) 139 info.setPropertyStore(null); 140 } 141 } 142 } 143 144 148 public String getProperty(IResource target, QualifiedName name) throws CoreException { 149 PropertyStore store = getPropertyStore(target); 150 synchronized (store) { 151 assertRunning(target, store); 152 StoredProperty result = store.get(getPropertyKey(target), name); 153 return result == null ? null : result.getStringValue(); 154 } 155 } 156 157 161 private Resource getPropertyHost(IResource target) { 162 return (Resource) (target.getType() == IResource.ROOT ? target : target.getProject()); 163 } 164 165 169 private ResourceName getPropertyKey(IResource target) { 170 return new ResourceName("", target.getProjectRelativePath()); } 172 173 PropertyStore getPropertyStore(IResource target) throws CoreException { 174 return getPropertyStore(target, true); 175 } 176 177 182 PropertyStore getPropertyStore(IResource target, boolean createIfNeeded) throws CoreException { 183 try { 184 Resource host = getPropertyHost(target); 185 ResourceInfo info = host.getResourceInfo(false, false); 186 if (info == null) { 187 String message = NLS.bind(CompatibilityMessages.properties_storeNotAvailable, target.getFullPath()); 188 throw new ResourceException(IResourceStatus.FAILED_READ_LOCAL, target.getFullPath(), message, null); 189 } 190 PropertyStore store = (PropertyStore) info.getPropertyStore(); 191 if (store == null) 192 store = openPropertyStore(host, createIfNeeded); 193 return store; 194 } catch (Exception e) { 195 if (e instanceof CoreException) 196 throw (CoreException) e; 197 String message = NLS.bind(CompatibilityMessages.properties_storeNotAvailable, target.getFullPath()); 198 throw new ResourceException(IResourceStatus.FAILED_READ_LOCAL, target.getFullPath(), message, e); 199 } 200 } 201 202 206 private PropertyStore getPropertyStoreOrNull(IResource target) { 207 Resource host = getPropertyHost(target); 208 ResourceInfo info = host.getResourceInfo(false, false); 209 if (info != null) { 210 PropertyStore store = (PropertyStore) info.getPropertyStore(); 211 if (store != null) { 212 synchronized (store) { 214 if (store.isRunning()) 215 return store; 216 } 217 } 218 } 219 return null; 220 } 221 222 public void handleEvent(LifecycleEvent event) throws CoreException { 223 if (event.kind == LifecycleEvent.PRE_PROJECT_CLOSE) 224 closePropertyStore(event.resource); 225 } 226 227 private PropertyStore openPropertyStore(IResource target, boolean createIfNeeded) { 228 int type = target.getType(); 229 Assert.isTrue(type != IResource.FILE && type != IResource.FOLDER); 230 IPath location = workspace.getMetaArea().getPropertyStoreLocation(target); 231 java.io.File storeFile = location.toFile(); 232 if (!createIfNeeded && !storeFile.isFile()) 233 return null; 234 storeFile.getParentFile().mkdirs(); 235 PropertyStore store = new PropertyStore(location); 236 setPropertyStore(target, store); 237 return store; 238 } 239 240 public void setProperty(IResource target, QualifiedName key, String value) throws CoreException { 241 PropertyStore store = getPropertyStore(target); 242 synchronized (store) { 243 assertRunning(target, store); 244 if (value == null) { 245 store.remove(getPropertyKey(target), key); 246 } else { 247 StoredProperty prop = new StoredProperty(key, value); 248 store.set(getPropertyKey(target), prop); 249 } 250 store.commit(); 251 } 252 } 253 254 private void setPropertyStore(IResource target, PropertyStore value) { 255 ResourceInfo info = getPropertyHost(target).getResourceInfo(false, false); 259 if (info.getType() == IResource.PROJECT) 260 ((ProjectInfo) info).setPropertyStore(value); 261 else 262 ((RootInfo) info).setPropertyStore(value); 263 } 264 265 public void shutdown(IProgressMonitor monitor) throws CoreException { 266 closePropertyStore(workspace.getRoot()); 267 } 268 269 public void startup(IProgressMonitor monitor) throws CoreException { 270 workspace.addLifecycleListener(this); 271 } 272 273 public Map getProperties(IResource resource) throws CoreException { 274 PropertyStore store = getPropertyStore(resource); 275 if (store == null) 276 return Collections.EMPTY_MAP; 277 IPath path = resource.getProjectRelativePath(); 279 ResourceName resourceName = new ResourceName("", path); QueryResults results = store.getAll(resourceName, 1); 281 List projectProperties = results.getResults(resourceName); 282 int listSize = projectProperties.size(); 283 if (listSize == 0) 284 return Collections.EMPTY_MAP; 285 Map properties = new HashMap(); 286 for (int i = 0; i < listSize; i++) { 287 StoredProperty prop = (StoredProperty) projectProperties.get(i); 288 properties.put(prop.getName(), prop.getStringValue()); 289 } 290 return properties; 291 } 292 293 } 294 | Popular Tags |