KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > properties > PropertyManager


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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 /**
23  * @see org.eclipse.core.internal.properties.IPropertyManager
24  */

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     /**
43      * Copy all the properties of one resource to another. Both resources
44      * must have a property store available.
45      */

46     public void copy(IResource source, IResource destination, int depth) throws CoreException {
47         // cache stores to avoid problems in concurrency
48
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     /**
62      * Throws an exception if the store has been shut down
63      */

64     private void assertRunning(IResource target, PropertyStore store) throws CoreException {
65         if (!store.isRunning()) {
66             //if the store is not running then the resource is in the process of being deleted,
67
//so report the error as if the resource was not found
68
String JavaDoc 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     /**
111      * The resource is being deleted so permanently erase its properties.
112      * In the case of projects, this means the property store will not be
113      * accessible again.
114      */

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                 //permanently delete the store
124
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 we want to allow restart, null the store and it will be recreated lazily
136
if (restart) {
137                 ResourceInfo info = getPropertyHost(target).getResourceInfo(false, false);
138                 if (info != null)
139                     info.setPropertyStore(null);
140             }
141         }
142     }
143
144     /**
145      * Returns the value of the identified property on the given resource as
146      * maintained by this store.
147      */

148     public String JavaDoc 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     /**
158      * Returns the resource which hosts the property store
159      * for the given resource.
160      */

161     private Resource getPropertyHost(IResource target) {
162         return (Resource) (target.getType() == IResource.ROOT ? target : target.getProject());
163     }
164
165     /**
166      * Returns the key to use in the property store when accessing
167      * the properties of the given resource.
168      */

169     private ResourceName getPropertyKey(IResource target) {
170         return new ResourceName("", target.getProjectRelativePath()); //$NON-NLS-1$
171
}
172
173     PropertyStore getPropertyStore(IResource target) throws CoreException {
174         return getPropertyStore(target, true);
175     }
176
177     /**
178      * Returns the property store to use when storing a property for the
179      * given resource.
180      * @throws CoreException if the store could not be obtained for any reason.
181      */

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 JavaDoc 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 JavaDoc e) {
195             if (e instanceof CoreException)
196                 throw (CoreException) e;
197             String JavaDoc message = NLS.bind(CompatibilityMessages.properties_storeNotAvailable, target.getFullPath());
198             throw new ResourceException(IResourceStatus.FAILED_READ_LOCAL, target.getFullPath(), message, e);
199         }
200     }
201
202     /**
203      * Returns the property store to use when storing a property for the
204      * given resource, or null if the store is not available.
205      */

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                 //sync on the store in case of concurrent deletion
213
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 JavaDoc 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 JavaDoc 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         // fetch the info but don't bother making it mutable even though we are going
256
// to modify it. We don't know whether or not the tree is open and it really doesn't
257
// matter as the change we are doing does not show up in deltas.
258
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         // retrieves the properties for the selected resource
278
IPath path = resource.getProjectRelativePath();
279         ResourceName resourceName = new ResourceName("", path); //$NON-NLS-1$
280
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