KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.filesystem.EFS;
14 import org.eclipse.core.filesystem.IFileStore;
15 import org.eclipse.core.internal.resources.*;
16 import org.eclipse.core.internal.utils.Messages;
17 import org.eclipse.core.internal.utils.Policy;
18 import org.eclipse.core.resources.*;
19 import org.eclipse.core.runtime.*;
20 import org.eclipse.osgi.util.NLS;
21
22 //
23
public class CopyVisitor implements IUnifiedTreeVisitor {
24
25     /** root destination */
26     protected IResource rootDestination;
27
28     /** reports progress */
29     protected IProgressMonitor monitor;
30
31     /** update flags */
32     protected int updateFlags;
33
34     /** force flag */
35     protected boolean force;
36
37     /** deep copy flag */
38     protected boolean isDeep;
39
40     /** segments to drop from the source name */
41     protected int segmentsToDrop;
42
43     /** stores problems encountered while copying */
44     protected MultiStatus status;
45
46     /** visitor to refresh unsynchronized nodes */
47     protected RefreshLocalVisitor refreshLocalVisitor;
48
49     private FileSystemResourceManager localManager;
50
51     public CopyVisitor(IResource rootSource, IResource destination, int updateFlags, IProgressMonitor monitor) {
52         this.localManager = ((Resource)rootSource).getLocalManager();
53         this.rootDestination = destination;
54         this.updateFlags = updateFlags;
55         this.isDeep = (updateFlags & IResource.SHALLOW) == 0;
56         this.force = (updateFlags & IResource.FORCE) != 0;
57         this.monitor = monitor;
58         this.segmentsToDrop = rootSource.getFullPath().segmentCount();
59         this.status = new MultiStatus(ResourcesPlugin.PI_RESOURCES, IStatus.INFO, Messages.localstore_copyProblem, null);
60     }
61
62     protected boolean copy(UnifiedTreeNode node) {
63         Resource source = (Resource) node.getResource();
64         IPath sufix = source.getFullPath().removeFirstSegments(segmentsToDrop);
65         Resource destination = getDestinationResource(source, sufix);
66         if (!copyProperties(source, destination))
67             return false;
68         return copyContents(node, source, destination);
69     }
70
71     protected boolean copyContents(UnifiedTreeNode node, Resource source, Resource destination) {
72         try {
73             if (!isDeep && source.isLinked()) {
74                 destination.createLink(source.getRawLocation(), updateFlags & IResource.ALLOW_MISSING_LOCAL, null);
75                 return false;
76             }
77             IFileStore sourceStore = node.getStore();
78             IFileStore destinationStore = destination.getStore();
79             //ensure the parent of the root destination exists (bug 126104)
80
if (destination == rootDestination)
81                 destinationStore.getParent().mkdir(EFS.NONE, Policy.subMonitorFor(monitor, 0));
82             sourceStore.copy(destinationStore, EFS.SHALLOW, Policy.subMonitorFor(monitor, 0));
83             //create the destination in the workspace
84
ResourceInfo info = localManager.getWorkspace().createResource(destination, updateFlags);
85             localManager.updateLocalSync(info, destinationStore.fetchInfo().getLastModified());
86             //update timestamps on aliases
87
getWorkspace().getAliasManager().updateAliases(destination, destinationStore, IResource.DEPTH_ZERO, monitor);
88         } catch (CoreException e) {
89             status.add(e.getStatus());
90         }
91         return true;
92     }
93
94     protected boolean copyProperties(Resource target, Resource destination) {
95         try {
96             target.getPropertyManager().copy(target, destination, IResource.DEPTH_ZERO);
97             return true;
98         } catch (CoreException e) {
99             status.add(e.getStatus());
100             return false;
101         }
102     }
103
104     protected Resource getDestinationResource(Resource source, IPath suffix) {
105         if (suffix.segmentCount() == 0)
106             return (Resource)rootDestination;
107         IPath destinationPath = rootDestination.getFullPath().append(suffix);
108         return getWorkspace().newResource(destinationPath, source.getType());
109     }
110
111     /**
112      * This is done in order to generate less garbage.
113      */

114     protected RefreshLocalVisitor getRefreshLocalVisitor() {
115         if (refreshLocalVisitor == null)
116             refreshLocalVisitor = new RefreshLocalVisitor(Policy.monitorFor(null));
117         return refreshLocalVisitor;
118     }
119
120     public IStatus getStatus() {
121         return status;
122     }
123
124     protected Workspace getWorkspace() {
125         return (Workspace) rootDestination.getWorkspace();
126     }
127
128     protected boolean isSynchronized(UnifiedTreeNode node) {
129         /* does the resource exist in workspace and file system? */
130         if (!node.existsInWorkspace() || !node.existsInFileSystem())
131             return false;
132         /* we don't care about folder last modified */
133         if (node.isFolder() && node.getResource().getType() == IResource.FOLDER)
134             return true;
135         /* is lastModified different? */
136         Resource target = (Resource) node.getResource();
137         long lastModifed = target.getResourceInfo(false, false).getLocalSyncInfo();
138         if (lastModifed != node.getLastModified())
139             return false;
140         return true;
141     }
142
143     protected void synchronize(UnifiedTreeNode node) throws CoreException {
144         getRefreshLocalVisitor().visit(node);
145     }
146
147     public boolean visit(UnifiedTreeNode node) throws CoreException {
148         Policy.checkCanceled(monitor);
149         int work = 1;
150         try {
151             //location can be null if based on an undefined variable
152
if (node.getStore() == null) {
153                 //should still be a best effort copy
154
IPath path = node.getResource().getFullPath();
155                 String JavaDoc message = NLS.bind(Messages.localstore_locationUndefined, path);
156                 status.add(new ResourceStatus(IResourceStatus.FAILED_READ_LOCAL, path, message, null));
157                 return false;
158             }
159             boolean wasSynchronized = isSynchronized(node);
160             if (force && !wasSynchronized) {
161                 synchronize(node);
162                 // If not synchronized, the monitor did not take this resource into account.
163
// So, do not report work on it.
164
work = 0;
165                 //if source still doesn't exist, then fail because we can't copy a missing resource
166
if (!node.existsInFileSystem()) {
167                     IPath path = node.getResource().getFullPath();
168                     String JavaDoc message = NLS.bind(Messages.resources_mustExist, path);
169                     status.add(new ResourceStatus(IResourceStatus.RESOURCE_NOT_FOUND, path, message, null));
170                     return false;
171                 }
172             }
173             if (!force && !wasSynchronized) {
174                 IPath path = node.getResource().getFullPath();
175                 String JavaDoc message = NLS.bind(Messages.localstore_resourceIsOutOfSync, path);
176                 status.add(new ResourceStatus(IResourceStatus.OUT_OF_SYNC_LOCAL, path, message, null));
177                 return true;
178             }
179             return copy(node);
180         } finally {
181             monitor.worked(work);
182         }
183     }
184
185 }
186
Popular Tags