KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > core > resources > CVSWorkspaceRoot


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.team.internal.ccvs.core.resources;
12
13 import org.eclipse.core.resources.*;
14 import org.eclipse.core.runtime.IProgressMonitor;
15 import org.eclipse.core.runtime.IStatus;
16 import org.eclipse.osgi.util.NLS;
17 import org.eclipse.team.core.RepositoryProvider;
18 import org.eclipse.team.core.TeamException;
19 import org.eclipse.team.internal.ccvs.core.*;
20 import org.eclipse.team.internal.ccvs.core.connection.CVSRepositoryLocation;
21 import org.eclipse.team.internal.ccvs.core.syncinfo.FolderSyncInfo;
22 import org.eclipse.team.internal.ccvs.core.syncinfo.ResourceSyncInfo;
23 import org.eclipse.team.internal.ccvs.core.util.KnownRepositories;
24 import org.eclipse.team.internal.ccvs.core.util.Util;
25
26 /**
27  * This class provides static methods for checking out projects from a repository
28  * into the local workspace and for converting IResources into CVSRespources
29  * and sync trees.
30  * Instances of this class represent a local workspace root (i.e. a project).
31  */

32 public class CVSWorkspaceRoot {
33
34     private ICVSFolder localRoot;
35     
36     public CVSWorkspaceRoot(IContainer resource){
37         this.localRoot = getCVSFolderFor(resource);
38     }
39                     
40     /**
41      * Set the sharing for a project to enable it to be used with the CVSTeamProvider.
42      * This method ensure that the repository in the FolderSyncInfo is known and that
43      * the project is mapped to a CVS repository provider. It does not modify the sync
44      * info associated with the project's resources in any way.
45      */

46     public static void setSharing(IProject project, FolderSyncInfo info, IProgressMonitor monitor) throws TeamException {
47         
48         // Ensure provided info matches that of the project
49
ICVSFolder folder = (ICVSFolder)CVSWorkspaceRoot.getCVSResourceFor(project);
50         FolderSyncInfo folderInfo = folder.getFolderSyncInfo();
51         if ( ! info.equals(folderInfo)) {
52             throw new CVSException(new CVSStatus(IStatus.ERROR, NLS.bind(CVSMessages.CVSProvider_infoMismatch, new String JavaDoc[] { project.getName() })));
53         }
54         
55         // Ensure that the repository location format is supported
56
String JavaDoc root = info.getRoot();
57         // This will try to create a repository location for the root.
58
// If it fails, an exception is thrown.
59
KnownRepositories.getInstance().getRepository(root);
60         
61         // Register the project with Team
62
RepositoryProvider.map(project, CVSProviderPlugin.getTypeId());
63     }
64                 
65     public static ICVSFolder getCVSFolderFor(IContainer resource) {
66         return new EclipseFolder(resource);
67     }
68
69
70     public static ICVSFile getCVSFileFor(IFile resource) {
71         return new EclipseFile(resource);
72     }
73
74
75     public static ICVSResource getCVSResourceFor(IResource resource) {
76         if (resource.getType() == IResource.FILE)
77             return getCVSFileFor((IFile) resource);
78         else
79             return getCVSFolderFor((IContainer) resource);
80     }
81     
82     public static ICVSRemoteResource getRemoteResourceFor(IResource resource) throws CVSException {
83         ICVSResource managed = getCVSResourceFor(resource);
84         return getRemoteResourceFor(managed);
85     }
86     
87     public static ICVSRemoteResource getRemoteResourceFor(ICVSResource resource) throws CVSException {
88         if (resource.isFolder()) {
89             ICVSFolder folder = (ICVSFolder)resource;
90             FolderSyncInfo syncInfo = folder.getFolderSyncInfo();
91             if (syncInfo != null) {
92                 return new RemoteFolder(null, KnownRepositories.getInstance().getRepository(syncInfo.getRoot()), syncInfo.getRepository(), syncInfo.getTag());
93             }
94         } else {
95             if (resource.isManaged()) {
96                 RemoteFolder parent = (RemoteFolder)getRemoteResourceFor(resource.getParent());
97                 if (parent == null) {
98                     // This could be caused by another thread changing the state in the
99
// instant between when we did the managed check and we obtained the
100
// parent handle. If this is the case, isManaged should return false
101
// now. If it doesn't, then we should log an error.
102
if (resource.isManaged()) {
103                         CVSProviderPlugin.log(new CVSStatus(IStatus.ERROR,CVSStatus.ERROR, NLS.bind(CVSMessages.CVSWorkspaceRoot_11, new String JavaDoc[] { Util.getFullestPath(resource) }),resource.getIResource()));
104                     }
105                 } else {
106                     return RemoteFile.getBase(parent, (ICVSFile)resource);
107                 }
108             }
109         }
110         return null;
111     }
112         
113     /*
114      * Helper method that uses the parent of a local resource that has no base to ensure that the resource
115      * wasn't added remotely by a third party
116      */

117     private static ICVSRemoteResource getRemoteTreeFromParent(IResource resource, ICVSResource managed, CVSTag tag, IProgressMonitor progress) throws TeamException {
118         // If the parent isn't mapped to CVS, there's nothing we can do
119
ICVSFolder parent = managed.getParent();
120         FolderSyncInfo syncInfo = parent.getFolderSyncInfo();
121         if (syncInfo == null) {
122             // The parent is managed so just indicate that there is no remote
123
return null;
124         }
125         ICVSRepositoryLocation location = KnownRepositories.getInstance().getRepository(parent.getFolderSyncInfo().getRoot());
126         RemoteFolder remoteParent = RemoteFolderTreeBuilder.buildRemoteTree((CVSRepositoryLocation)location, parent, tag, progress);
127         ICVSRemoteResource remote = null;
128         if (remoteParent != null) {
129             try {
130                 remote = (ICVSRemoteResource)remoteParent.getChild(resource.getName());
131             } catch (CVSException e) {
132                 remote = null;
133             }
134             // The types need to match or we're in trouble
135
if (remote != null && !(remote.isContainer() == managed.isFolder()))
136                 throw new CVSException(new CVSStatus(IStatus.ERROR, CVSStatus.ERROR, NLS.bind(CVSMessages.CVSTeamProvider_typesDiffer, new String JavaDoc[] { resource.getFullPath().toString() }), resource));
137         }
138         return remote;
139     }
140     
141     /**
142      * Return the remote tree that corresponds to the given local resource. Return
143      * <code>null</code> if the remote tree doesn't exist remotely or if the local
144      * resource is not mapped to a remote (i.e. is not managed by CVS).
145      *
146      * @param resource the local resource
147      * @param tag the tag to be queried remotely
148      * @param cacheFileContentsHint hint which indicates whether file contents will be required
149      * @param depth the depth
150      * @param progress
151      * @return the remote tree or <code>null</code>
152      * @throws TeamException
153      */

154     public static ICVSRemoteResource getRemoteTree(IResource resource, CVSTag tag, boolean cacheFileContentsHint, int depth, IProgressMonitor progress) throws TeamException {
155         ICVSResource managed = CVSWorkspaceRoot.getCVSResourceFor(resource);
156         ICVSRemoteResource remote = CVSWorkspaceRoot.getRemoteResourceFor(resource);
157         if (remote == null) {
158             progress.beginTask(null, 100);
159             remote = getRemoteTreeFromParent(resource, managed, tag, Policy.subMonitorFor(progress, 50));
160             if (cacheFileContentsHint && remote != null && remote instanceof RemoteFile) {
161                 RemoteFile file = (RemoteFile)remote;
162                 // get the storage for the file to ensure that the contents are cached
163
file.getStorage(Policy.subMonitorFor(progress, 50));
164             }
165             progress.done();
166         } else if(resource.getType() == IResource.FILE) {
167             ICVSRepositoryLocation location = remote.getRepository();
168             if (cacheFileContentsHint) {
169                 remote = UpdateContentCachingService.buildRemoteTree((CVSRepositoryLocation)location, (ICVSFile)managed, tag, progress);
170             } else {
171                 remote = RemoteFolderTreeBuilder.buildRemoteTree((CVSRepositoryLocation)location, (ICVSFile)managed, tag, progress);
172             }
173         } else {
174             ICVSRepositoryLocation location = remote.getRepository();
175             if (cacheFileContentsHint) {
176                 remote = UpdateContentCachingService.buildRemoteTree((CVSRepositoryLocation)location, (ICVSFolder)managed, tag, depth, progress);
177             } else {
178                 remote = RemoteFolderTreeBuilder.buildRemoteTree((CVSRepositoryLocation)location, (ICVSFolder)managed, tag, progress);
179             }
180         }
181         return remote;
182     }
183     
184     public static boolean hasRemote(IResource resource) {
185         try {
186             ICVSResource cvsResource = getCVSResourceFor(resource);
187             int type = resource.getType();
188             if(type!=IResource.FILE) {
189                 if(type==IResource.PROJECT) {
190                     return ((ICVSFolder)cvsResource).isCVSFolder();
191                 } else {
192                     return cvsResource.isManaged();
193                 }
194             } else {
195                 byte[] syncBytes = ((ICVSFile)cvsResource).getSyncBytes();
196                 if(syncBytes!=null) {
197                     return !ResourceSyncInfo.isAddition(syncBytes);
198                 } else {
199                     return false;
200                 }
201             }
202         } catch(CVSException e) {
203             return false;
204         }
205     }
206     
207     public ICVSRepositoryLocation getRemoteLocation() throws CVSException {
208         FolderSyncInfo info = localRoot.getFolderSyncInfo();
209         if (info == null) {
210             IStatus status = new CVSStatus(IStatus.ERROR,CVSStatus.RESOURCE_SYNC_INFO_ERROR,NLS.bind(CVSMessages.CVSWorkspaceRoot_notCVSFolder, new String JavaDoc[] { localRoot.getName() }),localRoot);
211             throw new CVSException(status);
212         }
213         return KnownRepositories.getInstance().getRepository(info.getRoot());
214     }
215
216     public ICVSFolder getLocalRoot() {
217         return localRoot;
218     }
219     
220     
221     /**
222      * Return true if the resource is part of a link (i.e. a linked resource or
223      * one of it's children.
224      *
225      * @param container
226      * @return boolean
227      */

228     public static boolean isLinkedResource(IResource resource) {
229         return resource.isLinked(IResource.CHECK_ANCESTORS);
230     }
231     
232     /**
233      * A resource is considered shared
234      * @param resource
235      * @return boolean
236      */

237     public static boolean isSharedWithCVS(IResource resource) throws CVSException {
238         if (!resource.isAccessible()) return false;
239         if(isLinkedResource(resource)) return false;
240     
241         if(RepositoryProvider.getProvider(resource.getProject(), CVSProviderPlugin.getTypeId()) == null) {
242             return false;
243         }
244     
245         ICVSResource cvsResource = CVSWorkspaceRoot.getCVSResourceFor(resource);
246         if (cvsResource.isManaged()) return true;
247         if (!cvsResource.exists()) return false;
248         if (cvsResource.isFolder() && ((ICVSFolder) cvsResource).isCVSFolder()) return true;
249         if (cvsResource.isIgnored()) return false;
250         return cvsResource.getParent().isCVSFolder();
251     }
252     
253     /**
254      * Return whether the given container is an orphaned subtree. An orphaned subtree
255      * is folder (i.e. non-project) that is a CVS folder but is not managed and is not
256      * a linked resource. To know if the resource is a descendant of an orphaned subtree,
257      * the client must invoked this method for each ancestor of a resource.
258      * @param container the container being tested
259      * @return whether the container is an orphaned CVS folder
260      * @throws CVSException
261      */

262     public static boolean isOrphanedSubtree(IContainer container) throws CVSException {
263         ICVSFolder mFolder = CVSWorkspaceRoot.getCVSFolderFor(container);
264         return (mFolder.isCVSFolder()
265                 && ! mFolder.isManaged()
266                 && mFolder.getIResource().getType() == IResource.FOLDER
267                 && !isLinkedResource(container));
268     }
269 }
270
Popular Tags