KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > resources > WorkspaceTreeReader_1


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.resources;
12
13 import java.io.*;
14 import java.util.*;
15 import org.eclipse.core.internal.events.BuilderPersistentInfo;
16 import org.eclipse.core.internal.utils.Messages;
17 import org.eclipse.core.internal.utils.Policy;
18 import org.eclipse.core.internal.watson.ElementTree;
19 import org.eclipse.core.internal.watson.ElementTreeReader;
20 import org.eclipse.core.resources.IProject;
21 import org.eclipse.core.resources.IResourceStatus;
22 import org.eclipse.core.runtime.*;
23
24 /**
25  * Reads version 1 of the workspace tree file format.
26  */

27 public class WorkspaceTreeReader_1 extends WorkspaceTreeReader {
28     protected Workspace workspace;
29
30     public WorkspaceTreeReader_1(Workspace workspace) {
31         this.workspace = workspace;
32     }
33
34     protected int getVersion() {
35         return ICoreConstants.WORKSPACE_TREE_VERSION_1;
36     }
37
38     protected void linkBuildersToTrees(List buildersToBeLinked, ElementTree[] trees, int index, IProgressMonitor monitor) {
39         monitor = Policy.monitorFor(monitor);
40         try {
41             ArrayList infos = null;
42             String JavaDoc projectName = null;
43             for (int i = 0; i < buildersToBeLinked.size(); i++) {
44                 BuilderPersistentInfo info = (BuilderPersistentInfo) buildersToBeLinked.get(i);
45                 if (!info.getProjectName().equals(projectName)) {
46                     if (infos != null) { // if it is not the first iteration
47
IProject project = workspace.getRoot().getProject(projectName);
48                         workspace.getBuildManager().setBuildersPersistentInfo(project, infos);
49                     }
50                     projectName = info.getProjectName();
51                     infos = new ArrayList(5);
52                 }
53                 info.setLastBuildTree(trees[index++]);
54                 infos.add(info);
55             }
56             if (infos != null) {
57                 IProject project = workspace.getRoot().getProject(projectName);
58                 workspace.getBuildManager().setBuildersPersistentInfo(project, infos);
59             }
60         } finally {
61             monitor.done();
62         }
63     }
64
65     protected void linkPluginsSavedStateToTrees(List states, ElementTree[] trees, IProgressMonitor monitor) {
66         monitor = Policy.monitorFor(monitor);
67         try {
68             for (int i = 0; i < states.size(); i++) {
69                 SavedState state = (SavedState) states.get(i);
70                 // If the tree is too old (depends on the policy), the plug-in should not
71
// get it back as a delta. It is expensive to maintain this information too long.
72
final SaveManager saveManager = workspace.getSaveManager();
73                 if (!saveManager.isOldPluginTree(state.pluginId)) {
74                     state.oldTree = trees[i];
75                 } else {
76                     //clear information for this plugin from master table
77
saveManager.clearDeltaExpiration(state.pluginId);
78                 }
79             }
80         } finally {
81             monitor.done();
82         }
83     }
84
85     protected BuilderPersistentInfo readBuilderInfo(IProject project, DataInputStream input, int index) throws IOException {
86         //read the project name
87
String JavaDoc projectName = input.readUTF();
88         //use the name of the project handle if available
89
if (project != null)
90             projectName = project.getName();
91         String JavaDoc builderName = input.readUTF();
92         return new BuilderPersistentInfo(projectName, builderName, index);
93     }
94
95     protected void readBuildersPersistentInfo(IProject project, DataInputStream input, List builders, IProgressMonitor monitor) throws IOException {
96         monitor = Policy.monitorFor(monitor);
97         try {
98             int builderCount = input.readInt();
99             for (int i = 0; i < builderCount; i++)
100                 builders.add(readBuilderInfo(project, input, i));
101         } finally {
102             monitor.done();
103         }
104     }
105
106     protected void readPluginsSavedStates(DataInputStream input, HashMap savedStates, List plugins, IProgressMonitor monitor) throws IOException, CoreException {
107         monitor = Policy.monitorFor(monitor);
108         try {
109             int stateCount = input.readInt();
110             for (int i = 0; i < stateCount; i++) {
111                 String JavaDoc pluginId = input.readUTF();
112                 SavedState state = new SavedState(workspace, pluginId, null, null);
113                 savedStates.put(pluginId, state);
114                 plugins.add(state);
115             }
116         } finally {
117             monitor.done();
118         }
119     }
120
121     public ElementTree readSnapshotTree(DataInputStream input, ElementTree complete, IProgressMonitor monitor) throws CoreException {
122         monitor = Policy.monitorFor(monitor);
123         String JavaDoc message;
124         try {
125             message = Messages.resources_readingSnap;
126             monitor.beginTask(message, Policy.totalWork);
127             ElementTreeReader reader = new ElementTreeReader(workspace.getSaveManager());
128             while (input.available() > 0) {
129                 readWorkspaceFields(input, Policy.subMonitorFor(monitor, Policy.totalWork / 2));
130                 complete = reader.readDelta(complete, input);
131                 try {
132                     // make sure each snapshot is read by the correct reader
133
int version = input.readInt();
134                     if (version != getVersion())
135                         return WorkspaceTreeReader.getReader(workspace, version).readSnapshotTree(input, complete, monitor);
136                 } catch (EOFException e) {
137                     break;
138                 }
139             }
140             return complete;
141         } catch (IOException e) {
142             message = Messages.resources_readWorkspaceSnap;
143             throw new ResourceException(IResourceStatus.FAILED_READ_METADATA, null, message, e);
144         } finally {
145             monitor.done();
146         }
147     }
148
149     public void readTree(DataInputStream input, IProgressMonitor monitor) throws CoreException {
150         monitor = Policy.monitorFor(monitor);
151         String JavaDoc message;
152         try {
153             message = Messages.resources_reading;
154             monitor.beginTask(message, Policy.totalWork);
155             readWorkspaceFields(input, Policy.subMonitorFor(monitor, Policy.opWork * 20 / 100));
156
157             HashMap savedStates = new HashMap(20);
158             List pluginsToBeLinked = new ArrayList(20);
159             readPluginsSavedStates(input, savedStates, pluginsToBeLinked, Policy.subMonitorFor(monitor, Policy.opWork * 10 / 100));
160             workspace.getSaveManager().setPluginsSavedState(savedStates);
161
162             List buildersToBeLinked = new ArrayList(20);
163             readBuildersPersistentInfo(null, input, buildersToBeLinked, Policy.subMonitorFor(monitor, Policy.opWork * 10 / 100));
164
165             ElementTree[] trees = readTrees(Path.ROOT, input, Policy.subMonitorFor(monitor, Policy.opWork * 40 / 100));
166             linkPluginsSavedStateToTrees(pluginsToBeLinked, trees, Policy.subMonitorFor(monitor, Policy.opWork * 10 / 100));
167             linkBuildersToTrees(buildersToBeLinked, trees, pluginsToBeLinked.size(), Policy.subMonitorFor(monitor, Policy.opWork * 10 / 100));
168
169         } catch (IOException e) {
170             message = Messages.resources_readWorkspaceTree;
171             throw new ResourceException(IResourceStatus.FAILED_READ_METADATA, null, message, e);
172         } finally {
173             monitor.done();
174         }
175     }
176
177     public void readTree(IProject project, DataInputStream input, IProgressMonitor monitor) throws CoreException {
178         monitor = Policy.monitorFor(monitor);
179         String JavaDoc message;
180         try {
181             message = Messages.resources_reading;
182             monitor.beginTask(message, 10);
183             /* read the number of builders */
184             int numBuilders = input.readInt();
185
186             /* read in the list of builder names */
187             String JavaDoc[] builderNames = new String JavaDoc[numBuilders];
188             for (int i = 0; i < numBuilders; i++) {
189                 String JavaDoc builderName = input.readUTF();
190                 builderNames[i] = builderName;
191             }
192             monitor.worked(1);
193
194             /* read and link the trees */
195             ElementTree[] trees = readTrees(project.getFullPath(), input, Policy.subMonitorFor(monitor, 8));
196
197             /* map builder names to trees */
198             if (numBuilders > 0) {
199                 ArrayList infos = new ArrayList(trees.length * 2 + 1);
200                 for (int i = 0; i < numBuilders; i++) {
201                     BuilderPersistentInfo info = new BuilderPersistentInfo(project.getName(), builderNames[i], -1);
202                     info.setLastBuildTree(trees[i]);
203                     infos.add(info);
204                 }
205                 workspace.getBuildManager().setBuildersPersistentInfo(project, infos);
206             }
207             monitor.worked(1);
208
209         } catch (IOException e) {
210             message = Messages.resources_readProjectTree;
211             throw new ResourceException(IResourceStatus.FAILED_READ_METADATA, null, message, e);
212         } finally {
213             monitor.done();
214         }
215     }
216
217     /**
218      * Read trees from disk and link them to the workspace tree.
219      */

220     protected ElementTree[] readTrees(IPath root, DataInputStream input, IProgressMonitor monitor) throws IOException {
221         monitor = Policy.monitorFor(monitor);
222         try {
223             String JavaDoc message = Messages.resources_reading;
224             monitor.beginTask(message, 4);
225             ElementTreeReader treeReader = new ElementTreeReader(workspace.getSaveManager());
226             ElementTree[] trees = treeReader.readDeltaChain(input);
227             monitor.worked(3);
228             if (root.isRoot()) {
229                 //Don't need to link because we're reading the whole workspace.
230
//The last tree in the chain is the complete tree.
231
ElementTree newTree = trees[trees.length - 1];
232                 newTree.setTreeData(workspace.tree.getTreeData());
233                 workspace.tree = newTree;
234             } else {
235                 //splice the restored tree into the current set of trees
236
workspace.linkTrees(root, trees);
237             }
238             monitor.worked(1);
239             return trees;
240         } finally {
241             monitor.done();
242         }
243     }
244
245     protected void readWorkspaceFields(DataInputStream input, IProgressMonitor monitor) throws IOException, CoreException {
246         monitor = Policy.monitorFor(monitor);
247         try {
248             // read the node id
249
workspace.nextNodeId = input.readLong();
250             // read the modification stamp (no longer used)
251
input.readLong();
252             // read the next marker id
253
workspace.nextMarkerId = input.readLong();
254             // read the synchronizer's registered sync partners
255
((Synchronizer) workspace.getSynchronizer()).readPartners(input);
256         } finally {
257             monitor.done();
258         }
259     }
260 }
261
Popular Tags