KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2004, 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.io.File JavaDoc;
14 import java.util.*;
15 import org.eclipse.core.internal.localstore.Bucket;
16 import org.eclipse.core.internal.localstore.BucketTree;
17 import org.eclipse.core.internal.localstore.Bucket.Entry;
18 import org.eclipse.core.internal.properties.PropertyBucket.PropertyEntry;
19 import org.eclipse.core.internal.resources.*;
20 import org.eclipse.core.internal.utils.Messages;
21 import org.eclipse.core.resources.IResource;
22 import org.eclipse.core.resources.IResourceStatus;
23 import org.eclipse.core.runtime.*;
24 import org.eclipse.osgi.util.NLS;
25
26 /**
27  * @see org.eclipse.core.internal.properties.IPropertyManager
28  */

29 public class PropertyManager2 implements IPropertyManager {
30     class PropertyCopyVisitor extends Bucket.Visitor {
31         private List changes = new ArrayList();
32         private IPath destination;
33         private IPath source;
34
35         public PropertyCopyVisitor(IPath source, IPath destination) {
36             this.source = source;
37             this.destination = destination;
38         }
39
40         public void afterSaving(Bucket bucket) throws CoreException {
41             saveChanges((PropertyBucket) bucket);
42             changes.clear();
43         }
44
45         private void saveChanges(PropertyBucket bucket) throws CoreException {
46             if (changes.isEmpty())
47                 return;
48             // make effective all changes collected
49
Iterator i = changes.iterator();
50             PropertyEntry entry = (PropertyEntry) i.next();
51             tree.loadBucketFor(entry.getPath());
52             bucket.setProperties(entry);
53             while (i.hasNext())
54                 bucket.setProperties((PropertyEntry) i.next());
55             bucket.save();
56         }
57
58         public int visit(Entry entry) {
59             PropertyEntry sourceEntry = (PropertyEntry) entry;
60             IPath destinationPath = destination.append(sourceEntry.getPath().removeFirstSegments(source.segmentCount()));
61             PropertyEntry destinationEntry = new PropertyEntry(destinationPath, sourceEntry);
62             changes.add(destinationEntry);
63             return CONTINUE;
64         }
65     }
66
67     BucketTree tree;
68
69     public PropertyManager2(Workspace workspace) {
70         this.tree = new BucketTree(workspace, new PropertyBucket());
71     }
72
73     public void closePropertyStore(IResource target) throws CoreException {
74         // ensure any uncommitted are written to disk
75
tree.getCurrent().save();
76         // flush in-memory state to avoid confusion if another project is later
77
// created with the same name
78
tree.getCurrent().flush();
79     }
80
81     public synchronized void copy(IResource source, IResource destination, int depth) throws CoreException {
82         copyProperties(source.getFullPath(), destination.getFullPath(), depth);
83     }
84
85     /**
86      * Copies all properties from the source path to the target path, to the given depth.
87      */

88     private void copyProperties(final IPath source, final IPath destination, int depth) throws CoreException {
89         Assert.isLegal(source.segmentCount() > 0);
90         Assert.isLegal(destination.segmentCount() > 0);
91         Assert.isLegal(source.segmentCount() > 1 || destination.segmentCount() == 1);
92
93         // copy history by visiting the source tree
94
PropertyCopyVisitor copyVisitor = new PropertyCopyVisitor(source, destination);
95         tree.accept(copyVisitor, source, BucketTree.DEPTH_INFINITE);
96     }
97
98     public synchronized void deleteProperties(IResource target, int depth) throws CoreException {
99         tree.accept(new PropertyBucket.Visitor() {
100             public int visit(Entry entry) {
101                 entry.delete();
102                 return CONTINUE;
103             }
104         }, target.getFullPath(), depth == IResource.DEPTH_INFINITE ? BucketTree.DEPTH_INFINITE : depth);
105     }
106
107     public void deleteResource(IResource target) throws CoreException {
108         deleteProperties(target, IResource.DEPTH_INFINITE);
109     }
110
111     public synchronized Map getProperties(IResource target) throws CoreException {
112         final Map result = new HashMap();
113         tree.accept(new PropertyBucket.Visitor() {
114             public int visit(Entry entry) {
115                 PropertyEntry propertyEntry = (PropertyEntry) entry;
116                 int propertyCount = propertyEntry.getOccurrences();
117                 for (int i = 0; i < propertyCount; i++)
118                     result.put(propertyEntry.getPropertyName(i), propertyEntry.getPropertyValue(i));
119                 return CONTINUE;
120             }
121         }, target.getFullPath(), BucketTree.DEPTH_ZERO);
122         return result;
123     }
124
125     public synchronized String JavaDoc getProperty(IResource target, QualifiedName name) throws CoreException {
126         if (name.getQualifier() == null) {
127             String JavaDoc message = Messages.properties_qualifierIsNull;
128             throw new ResourceException(IResourceStatus.FAILED_READ_METADATA, target.getFullPath(), message, null);
129         }
130         IPath resourcePath = target.getFullPath();
131         PropertyBucket current = (PropertyBucket) tree.getCurrent();
132         tree.loadBucketFor(resourcePath);
133         return current.getProperty(resourcePath, name);
134     }
135
136     public BucketTree getTree() {
137         return tree;
138     }
139
140     public File JavaDoc getVersionFile() {
141         return tree.getVersionFile();
142     }
143
144     public synchronized void setProperty(IResource target, QualifiedName name, String JavaDoc value) throws CoreException {
145         //resource may have been deleted concurrently
146
//must check for existence within synchronized method
147
Resource resource = (Resource) target;
148         ResourceInfo info = resource.getResourceInfo(false, false);
149         int flags = resource.getFlags(info);
150         resource.checkAccessible(flags);
151         // enforce the limit stated by the spec
152
if (value != null && value.length() > 2 * 1024) {
153             String JavaDoc message = NLS.bind(Messages.properties_valueTooLong, name.getQualifier(), name.getLocalName());
154             throw new ResourceException(IResourceStatus.FAILED_WRITE_METADATA, target.getFullPath(), message, null);
155         }
156         if (name.getQualifier() == null) {
157             String JavaDoc message = Messages.properties_qualifierIsNull;
158             throw new ResourceException(IResourceStatus.FAILED_WRITE_METADATA, target.getFullPath(), message, null);
159         }
160
161         IPath resourcePath = target.getFullPath();
162         tree.loadBucketFor(resourcePath);
163         PropertyBucket current = (PropertyBucket) tree.getCurrent();
164         current.setProperty(resourcePath, name, value);
165         current.save();
166     }
167
168     public void shutdown(IProgressMonitor monitor) throws CoreException {
169         tree.close();
170     }
171
172     public void startup(IProgressMonitor monitor) {
173         // nothing to do
174
}
175 }
176
Popular Tags