KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.eclipse.core.internal.indexing.IndexCursor;
15 import org.eclipse.core.internal.localstore.BucketTree;
16 import org.eclipse.core.internal.resources.CompatibilityMessages;
17 import org.eclipse.core.internal.resources.Workspace;
18 import org.eclipse.core.resources.*;
19 import org.eclipse.core.runtime.*;
20
21 public class PropertyStoreConverter {
22     class ConversionVisitor implements IVisitor {
23         private IPath basePath;
24         private BucketTree target;
25         private boolean worked;
26
27         public ConversionVisitor(IPath basePath, BucketTree target) {
28             this.target = target;
29             this.basePath = basePath;
30         }
31
32         public boolean hasWorked() {
33             return worked;
34         }
35
36         public boolean requiresValue(ResourceName resourceName, QualifiedName propertyName) {
37             // we need the values so we can convert
38
return true;
39         }
40
41         public void visit(ResourceName resourceName, StoredProperty property, IndexCursor cursor) throws CoreException {
42             IPath fullPath = basePath.append(resourceName.getPath());
43             target.loadBucketFor(fullPath);
44             // copies a single property
45
((PropertyBucket) target.getCurrent()).setProperty(fullPath, property.getName(), property.getStringValue());
46             worked = true;
47         }
48     }
49
50     /**
51      * Converts existing persistent property data lying on disk to the new
52      * property store format.
53      * Returns Status.OK_STATUS if nothing is done, an IStatus.INFO status if
54      * the conversion happens successfully or an IStatus.ERROR status if an error
55      * happened during the conversion process.
56      */

57     public IStatus convertProperties(Workspace workspace, final PropertyManager2 destination) {
58         // Quickly check whether should try converting persistent properties
59
// We cannot pay the cost of checking every project so, instead, we try to find
60
// a single file used by the new implementation
61
File JavaDoc versionFile = destination.getVersionFile();
62         if (versionFile.isFile())
63             // conversion already done, won't try doing it again
64
return Status.OK_STATUS;
65         final boolean[] worked = {false};
66         final PropertyManager source = new PropertyManager(workspace);
67         try {
68             // convert the property store for the root and every project
69
workspace.getRoot().accept(new IResourceVisitor() {
70                 public boolean visit(org.eclipse.core.resources.IResource resource) throws CoreException {
71                     ConversionVisitor propertyConverter = new ConversionVisitor(resource.getFullPath(), destination.getTree());
72                     PropertyStore store = source.getPropertyStore(resource, false);
73                     if (store == null)
74                         return true;
75                     store.recordsDeepMatching(new ResourceName("", resource.getProjectRelativePath()), propertyConverter); //$NON-NLS-1$
76
source.closePropertyStore(resource);
77                     worked[0] = worked[0] || propertyConverter.hasWorked();
78                     return true;
79                 }
80             }, IResource.DEPTH_ONE, IResource.NONE);
81             // the last bucket changed will not have been saved
82
destination.getTree().getCurrent().save();
83         } catch (CoreException e) {
84             // failed while visiting the old data or saving the new data
85
String JavaDoc conversionFailed = CompatibilityMessages.properties_conversionFailed;
86             return new MultiStatus(ResourcesPlugin.PI_RESOURCES, IResourceStatus.FAILED_READ_METADATA, new IStatus[] {e.getStatus()}, conversionFailed, null);
87         }
88         if (!worked[0])
89             // nothing was found to be converted
90
return Status.OK_STATUS;
91         // conversion actually happened, and everything went fine
92
// leave a note to the user so this does not happen silently
93
String JavaDoc conversionOk = CompatibilityMessages.properties_conversionSucceeded;
94         return new Status(IStatus.INFO, ResourcesPlugin.PI_RESOURCES, IStatus.OK, conversionOk, null);
95     }
96 }
97
Popular Tags