KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > core > syncinfo > CVSDescendantResourceVariantByteStore


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.team.internal.ccvs.core.syncinfo;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.resources.IResource;
18 import org.eclipse.core.runtime.IStatus;
19 import org.eclipse.core.runtime.Status;
20 import org.eclipse.osgi.util.NLS;
21 import org.eclipse.team.core.RepositoryProvider;
22 import org.eclipse.team.core.TeamException;
23 import org.eclipse.team.core.variants.PersistantResourceVariantByteStore;
24 import org.eclipse.team.core.variants.ResourceVariantByteStore;
25 import org.eclipse.team.internal.ccvs.core.*;
26 import org.eclipse.team.internal.core.subscribers.DescendantResourceVariantByteStore;
27
28 /**
29  * CVS sycnrhonization cache that ignores stale remote bytes
30  */

31 public class CVSDescendantResourceVariantByteStore extends DescendantResourceVariantByteStore {
32
33     public CVSDescendantResourceVariantByteStore(ResourceVariantByteStore baseCache, PersistantResourceVariantByteStore remoteCache) {
34         super(baseCache, remoteCache);
35     }
36
37     /* (non-Javadoc)
38      * @see org.eclipse.team.core.subscribers.DescendantSynchronizationCache#isDescendant(org.eclipse.core.resources.IResource, byte[], byte[])
39      */

40     protected boolean isDescendant(IResource resource, byte[] baseBytes, byte[] remoteBytes) throws TeamException {
41         if (resource.getType() != IResource.FILE) return true;
42         try {
43             return ResourceSyncInfo.isLaterRevisionOnSameBranch(remoteBytes, baseBytes);
44         } catch (CVSException e) {
45             throw TeamException.asTeamException(e);
46         }
47     }
48     
49     /* (non-Javadoc)
50      * @see org.eclipse.team.core.subscribers.helpers.SynchronizationCache#setSyncBytes(org.eclipse.core.resources.IResource, byte[])
51      */

52     public boolean setBytes(IResource resource, byte[] bytes) throws TeamException {
53         boolean changed = super.setBytes(resource, bytes);
54         if (resource.getType() == IResource.FILE && getBytes(resource) != null && !parentHasSyncBytes(resource)) {
55             // Log a warning if there is no sync bytes available for the resource's
56
// parent but there is valid sync bytes for the child
57
CVSProviderPlugin.log(new TeamException(NLS.bind(CVSMessages.ResourceSynchronizer_missingParentBytesOnSet, new String JavaDoc[] { ((PersistantResourceVariantByteStore)getRemoteStore()).getSyncName().toString(), resource.getFullPath().toString() })));
58         }
59         return changed;
60     }
61
62     /**
63      * Indicates whether the parent of the given local resource has sync bytes for its
64      * corresponding remote resource. The parent bytes of a remote resource are required
65      * (by CVS) to create a handle to the remote resource.
66      */

67     protected boolean parentHasSyncBytes(IResource resource) throws TeamException {
68         if (resource.getType() == IResource.PROJECT) return true;
69         return (getBytes(resource.getParent()) != null);
70     }
71
72     /* (non-Javadoc)
73      * @see org.eclipse.team.internal.core.subscribers.caches.ResourceVariantByteStore#isVariantKnown(org.eclipse.core.resources.IResource)
74      */

75     public boolean isVariantKnown(IResource resource) throws TeamException {
76         return ((PersistantResourceVariantByteStore)getRemoteStore()).isVariantKnown(resource);
77     }
78
79     /*
80      * TODO: Could possibly be generalized and moved up
81      */

82     public IStatus handleResourceChanges(IResource[] changedResources, boolean canModifyWorkspace) {
83         // IMPORTANT NOTE: This will throw exceptions if performed during the POST_CHANGE delta phase!!!
84
List JavaDoc errors = new ArrayList JavaDoc();
85         for (int i = 0; i < changedResources.length; i++) {
86             IResource resource = changedResources[i];
87             try {
88                 if (!isInCVSProject(resource)) continue;
89                 if (resource.getType() == IResource.FILE
90                         && (resource.exists() || resource.isPhantom())) {
91                     byte[] remoteBytes = getBytes(resource);
92                     if (remoteBytes == null) {
93                         if (isVariantKnown(resource)) {
94                             // The remote is known not to exist. If the local resource is
95
// managed then this information is stale
96
if (getBaseStore().getBytes(resource) != null) {
97                                 if (canModifyWorkspace) {
98                                     flushBytes(resource, IResource.DEPTH_ZERO);
99                                 } else {
100                                     // The revision comparison will handle the stale sync bytes
101
// TODO: Unless the remote is known not to exist (see bug 52936)
102
}
103                             }
104                         }
105                     } else {
106                         byte[] localBytes = getBaseStore().getBytes(resource);
107                         if (localBytes == null || !isDescendant(resource, localBytes, remoteBytes)) {
108                             if (canModifyWorkspace) {
109                                 flushBytes(resource, IResource.DEPTH_ZERO);
110                             } else {
111                                 // The remote byte store handles the stale sync bytes
112
}
113                         }
114                     }
115                 } else if (resource.getType() == IResource.FOLDER) {
116                     // If the base has sync info for the folder, purge the remote bytes
117
if (getBaseStore().getBytes(resource) != null && canModifyWorkspace) {
118                         flushBytes(resource, IResource.DEPTH_ZERO);
119                     }
120                 }
121             } catch (TeamException e) {
122                 errors.add(e);
123             }
124         }
125         for (Iterator JavaDoc iter = errors.iterator(); iter.hasNext();) {
126             TeamException e = (TeamException) iter.next();
127             CVSProviderPlugin.log(e);
128         }
129         return Status.OK_STATUS; // TODO
130
}
131
132     private boolean isInCVSProject(IResource resource) {
133         return RepositoryProvider.getProvider(resource.getProject(), CVSProviderPlugin.getTypeId()) != null;
134     }
135 }
136
Popular Tags