KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > core > variants > PersistantResourceVariantByteStore


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.core.variants;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.core.resources.*;
17 import org.eclipse.core.runtime.*;
18 import org.eclipse.team.core.TeamException;
19
20 /**
21  * A <code>ResourceVariantByteStore</code> that caches the variant bytes using
22  * the <code>org.eclipse.core.resources.ISynchronizer</code> so that
23  * the tree is cached across workbench invocations.
24  * <p>
25  * This class is not intended to be subclassed by clients.
26  *
27  * @since 3.0
28  */

29 public class PersistantResourceVariantByteStore extends ResourceVariantByteStore {
30
31     private static final byte[] NO_REMOTE = new byte[0];
32     
33     private QualifiedName syncName;
34     
35     /**
36      * Create a persistent tree that uses the given qualified name
37      * as the key in the <code>org.eclipse.core.resources.ISynchronizer</code>.
38      * It must be unique and should use the plugin as the local name
39      * and a unique id within the plugin as the qualifier name.
40      * @param name the key used in the Core synchronizer
41      */

42     public PersistantResourceVariantByteStore(QualifiedName name) {
43         syncName = name;
44         getSynchronizer().add(syncName);
45     }
46     
47     /* (non-Javadoc)
48      * @see org.eclipse.team.core.variants.ResourceVariantByteStore#dispose()
49      */

50     public void dispose() {
51         getSynchronizer().remove(getSyncName());
52     }
53
54     /**
55      * Return the qualified name that uniquely identifies this tree.
56      * @return the qualified name that uniquely identifies this tree.
57      */

58     public QualifiedName getSyncName() {
59         return syncName;
60     }
61
62     /* (non-Javadoc)
63      * @see org.eclipse.team.core.variants.ResourceVariantByteStore#getBytes(org.eclipse.core.resources.IResource)
64      */

65     public byte[] getBytes(IResource resource) throws TeamException {
66         byte[] syncBytes = internalGetSyncBytes(resource);
67         if (syncBytes != null && equals(syncBytes, NO_REMOTE)) {
68             // If it is known that there is no remote, return null
69
return null;
70         }
71         return syncBytes;
72     }
73     
74     /* (non-Javadoc)
75      * @see org.eclipse.team.core.variants.ResourceVariantByteStore#setBytes(org.eclipse.core.resources.IResource, byte[])
76      */

77     public boolean setBytes(IResource resource, byte[] bytes) throws TeamException {
78         Assert.isNotNull(bytes);
79         byte[] oldBytes = internalGetSyncBytes(resource);
80         if (oldBytes != null && equals(oldBytes, bytes)) return false;
81         try {
82             getSynchronizer().setSyncInfo(getSyncName(), resource, bytes);
83             return true;
84         } catch (CoreException e) {
85             throw TeamException.asTeamException(e);
86         }
87     }
88
89     /* (non-Javadoc)
90      * @see org.eclipse.team.core.variants.ResourceVariantByteStore#flushBytes(org.eclipse.core.resources.IResource, int)
91      */

92     public boolean flushBytes(IResource resource, int depth) throws TeamException {
93         if (resource.exists() || resource.isPhantom()) {
94             try {
95                 if (depth != IResource.DEPTH_ZERO || internalGetSyncBytes(resource) != null) {
96                     getSynchronizer().flushSyncInfo(getSyncName(), resource, depth);
97                     return true;
98                 }
99             } catch (CoreException e) {
100                 throw TeamException.asTeamException(e);
101             }
102         }
103         return false;
104     }
105     
106     /**
107      * Return whether the resource variant state for this resource is known.
108      * This is used to differentiate the case where a resource variant has never been fetched
109      * from the case where the resource variant is known to not exist. In the later
110      * case, this method returns <code>true</code> while <code>getBytes</code> returns <code>null</code>
111      * @param resource the local resource
112      * @return whether the resource variant state for this resource is known
113      * @throws TeamException
114      */

115     public boolean isVariantKnown(IResource resource) throws TeamException {
116         return internalGetSyncBytes(resource) != null;
117     }
118     
119     /**
120      * This method should be invoked by a client to indicate that it is known that
121      * there is no remote resource associated with the local resource. After this method
122      * is invoked, <code>isVariantKnown(resource)</code> will return <code>true</code> and
123      * <code>getBytes(resource)</code> will return <code>null</code>.
124      * @return <code>true</code> if this changes the remote sync bytes
125      */

126     public boolean deleteBytes(IResource resource) throws TeamException {
127         return setBytes(resource, NO_REMOTE);
128     }
129
130     /* (non-Javadoc)
131      * @see org.eclipse.team.core.variants.ResourceVariantByteStore#members(org.eclipse.core.resources.IResource)
132      */

133     public IResource[] members(IResource resource) throws TeamException {
134         if(resource.getType() == IResource.FILE) {
135             return new IResource[0];
136         }
137         try {
138             // Filter and return only resources that have sync bytes in the cache.
139
IResource[] members = ((IContainer)resource).members(true /* include phantoms */);
140             List JavaDoc filteredMembers = new ArrayList JavaDoc(members.length);
141             for (int i = 0; i < members.length; i++) {
142                 IResource member = members[i];
143                 if(getBytes(member) != null) {
144                     filteredMembers.add(member);
145                 }
146             }
147             return (IResource[]) filteredMembers.toArray(new IResource[filteredMembers.size()]);
148         } catch (CoreException e) {
149             throw TeamException.asTeamException(e);
150         }
151     }
152
153     private ISynchronizer getSynchronizer() {
154         return ResourcesPlugin.getWorkspace().getSynchronizer();
155     }
156     
157     private byte[] internalGetSyncBytes(IResource resource) throws TeamException {
158         try {
159             return getSynchronizer().getSyncInfo(getSyncName(), resource);
160         } catch (CoreException e) {
161             throw TeamException.asTeamException(e);
162         }
163     }
164     
165     /* (non-Javadoc)
166      * @see org.eclipse.team.core.variants.ResourceVariantByteStore#run(org.eclipse.core.resources.IWorkspaceRunnable, org.eclipse.core.runtime.IProgressMonitor)
167      */

168     public void run(IResource root, IWorkspaceRunnable runnable, IProgressMonitor monitor)
169             throws TeamException {
170         try {
171             ResourcesPlugin.getWorkspace().run(runnable, root, 0, monitor);
172         } catch (CoreException e) {
173             throw TeamException.asTeamException(e);
174         }
175     }
176 }
177
Popular Tags