KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > localstore > HistoryStoreConverter


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.localstore;
12
13 import org.eclipse.core.internal.resources.CompatibilityMessages;
14 import org.eclipse.core.internal.resources.Workspace;
15 import org.eclipse.core.internal.utils.Policy;
16 import org.eclipse.core.resources.IResourceStatus;
17 import org.eclipse.core.resources.ResourcesPlugin;
18 import org.eclipse.core.runtime.*;
19
20 public class HistoryStoreConverter {
21     /**
22      * Converts an existing history store lying on disk to the new history store.
23      * Returns Status.OK_STATUS if nothing is done, an IStatus.INFO status if
24      * the conversion happens successfully or an IStatus.ERROR status if an error
25      * happened during the conversion process.
26      */

27     public IStatus convertHistory(Workspace workspace, IPath location, int limit, final HistoryStore2 destination, boolean rename) {
28         if (!location.toFile().isDirectory())
29             // nothing to be converted
30
return Status.OK_STATUS;
31         IPath indexFile = location.append(HistoryStore.INDEX_FILE);
32         if (!indexFile.toFile().isFile())
33             // nothing to be converted
34
return Status.OK_STATUS;
35         // visit all existing entries and add them to the new history store
36
long start = System.currentTimeMillis();
37         final CoreException[] exception = new CoreException[1];
38         final BucketTree tree = destination.getTree();
39         final HistoryBucket currentBucket = (HistoryBucket) tree.getCurrent();
40         HistoryStore source = new HistoryStore(workspace, location, limit);
41         source.accept(Path.ROOT, new IHistoryStoreVisitor() {
42             public boolean visit(HistoryStoreEntry state) {
43                 try {
44                     tree.loadBucketFor(state.getPath());
45                 } catch (CoreException e) {
46                     // failed while loading bucket
47
exception[0] = e;
48                     return false;
49                 }
50                 currentBucket.addBlob(state.getPath(), state.getUUID(), state.getLastModified());
51                 return true;
52             }
53         }, true);
54         try {
55             // the last bucket changed will not have been saved
56
tree.getCurrent().save();
57             // we are done using the old history store instance
58
source.shutdown(null);
59         } catch (CoreException e) {
60             // failed during save
61
exception[0] = e;
62         }
63         if (Policy.DEBUG_HISTORY)
64             Policy.debug("Time to convert local history: " + (System.currentTimeMillis() - start) + "ms."); //$NON-NLS-1$ //$NON-NLS-2$
65
if (exception[0] != null) {
66             // failed while visiting the old data or saving the new data
67
String JavaDoc conversionFailed = CompatibilityMessages.history_conversionFailed;
68             Status failure = new MultiStatus(ResourcesPlugin.PI_RESOURCES, IResourceStatus.FAILED_READ_METADATA, new IStatus[] {exception[0].getStatus()}, conversionFailed, null);
69             // we failed, so don't do anything else - we might try converting again later
70
return failure;
71         }
72         // everything went fine
73
// if requested rename the index file to something else
74
// so we don't try converting again in the future
75
if (rename)
76             indexFile.toFile().renameTo(indexFile.addFileExtension(Long.toString(System.currentTimeMillis())).toFile());
77         String JavaDoc conversionOk = CompatibilityMessages.history_conversionSucceeded;
78         // leave a note to the user so this does not happen silently
79
return new Status(IStatus.INFO, ResourcesPlugin.PI_RESOURCES, IStatus.OK, conversionOk, null);
80     }
81 }
82
Popular Tags