KickJava   Java API By Example, From Geeks To Geeks.

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


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.syncinfo;
12
13  
14 import java.io.*;
15
16 import org.eclipse.core.runtime.*;
17 import org.eclipse.osgi.util.NLS;
18 import org.eclipse.team.internal.ccvs.core.*;
19 import org.eclipse.team.internal.ccvs.core.connection.CVSRepositoryLocation;
20 import org.eclipse.team.internal.ccvs.core.resources.CVSEntryLineTag;
21 import org.eclipse.team.internal.ccvs.core.util.Util;
22
23 /**
24  * Value (immutable) object that represents workspace state information about the contents of a
25  * folder that was retreived from a CVS repository. It is a specialized representation of the files from
26  * the CVS sub-directory that contain folder specific connection information (e.g. Root, Repository, Tag).
27  *
28  * @see ICVSFolder#getFolderSyncInfo()
29  */

30 public class FolderSyncInfo {
31
32     // The Repository value for virtual directories (i.e. local with no corresponding remote)
33
public static final String JavaDoc VIRTUAL_DIRECTORY = "CVSROOT/Emptydir"; //$NON-NLS-1$
34

35     // relative path of this folder in the repository, project1/folder1/folder2
36
protected String JavaDoc repository;
37     
38     // :pserver:user@host:/home/user/repo
39
protected String JavaDoc root;
40     
41     // sticky tag (e.g. version, date, or branch tag applied to folder)
42
private CVSEntryLineTag tag;
43     
44     // if true then it means only part of the folder was fetched from the repository, and CVS will not create
45
// additional files in that folder.
46
protected boolean isStatic;
47
48     /**
49      * Construct a folder sync object.
50      *
51      * @param repo the relative path of this folder in the repository, cannot be <code>null</code>.
52      * @param root the location of the repository, cannot be <code>null</code>.
53      * @param tag the tag set for the folder or <code>null</code> if there is no tag applied.
54      * @param isStatic to indicate is only part of the folder was fetched from the server.
55      */

56     public FolderSyncInfo(String JavaDoc repo, String JavaDoc root, CVSTag tag, boolean isStatic) {
57         Assert.isNotNull(repo);
58         Assert.isNotNull(root);
59         this.repository = repo;
60         // intern the root so that caching of FolderSyncInfos for folders will use less space
61
this.root = root.intern();
62         ensureRepositoryRelativeToRoot();
63         this.isStatic = isStatic;
64         setTag(tag);
65     }
66
67     /**
68      * Method ensureRepositoryRelativeToRoot.
69      */

70     private void ensureRepositoryRelativeToRoot() {
71         String JavaDoc rootDir;
72         try {
73             rootDir = getRootDirectory();
74         } catch (CVSException e) {
75             // Ignore the for now. Using the root will show the error to the user.
76
return;
77         }
78         if (repository.startsWith(rootDir)) {
79             repository = repository.substring(rootDir.length());
80         }
81         if (repository.startsWith(ResourceSyncInfo.SEPARATOR)) {
82             repository = repository.substring(ResourceSyncInfo.SEPARATOR.length());
83         }
84     }
85     
86     public boolean equals(Object JavaDoc other) {
87         if(other == this) return true;
88         if (!(other instanceof FolderSyncInfo)) return false;
89             
90         FolderSyncInfo syncInfo = ((FolderSyncInfo)other);
91         if (!getRoot().equals(syncInfo.getRoot())) return false;
92         if (!getRepository().equals(syncInfo.getRepository())) return false;
93         if (getIsStatic() != syncInfo.getIsStatic()) return false;
94         if ((getTag() == null) || (syncInfo.getTag() == null)) {
95             if ((getTag() == null) && (syncInfo.getTag() != null) && (syncInfo.getTag().getType() != CVSTag.HEAD)) {
96                 return false;
97             } else if ((syncInfo.getTag() == null) && (getTag() != null) && (getTag().getType() != CVSTag.HEAD)) {
98                 return false;
99             }
100         } else if (!getTag().equals(syncInfo.getTag())) {
101             return false;
102         }
103         return true;
104     }
105     /**
106      * Gets the root, cannot be <code>null.
107      *
108      * @return Returns a String
109      */

110     public String JavaDoc getRoot() {
111         return root;
112     }
113
114     /**
115      * Answer the directory portion of the root. For example, if
116      * root = :pserver:user@host:/home/user/repo
117      * then /home/user/repo is return.
118      * <p>
119      * The root does not neccesarily contain a user name, in which cas the format is
120      * :pserver:host:/home/user/repo.
121      *
122      *
123      * @return String
124      */

125     private String JavaDoc getRootDirectory() throws CVSException {
126         try {
127             String JavaDoc root = getRoot();
128             int index = root.indexOf(CVSRepositoryLocation.HOST_SEPARATOR);
129             if (index == -1) {
130                 // If the username is missing, we have to find the third ':'.
131
index = root.indexOf(CVSRepositoryLocation.COLON);
132                 if (index == 0) {
133                     // This indicates that the conection method is present.
134
// It is surrounded by two colons so skip them.
135
index = root.indexOf(CVSRepositoryLocation.COLON, index + 1);
136                     index = root.indexOf(CVSRepositoryLocation.COLON, index + 1);
137                 }
138                 if (index == -1) {
139                     // The host colon is missing.
140
// Look for a slash to find the path
141
index = root.indexOf(ResourceSyncInfo.SEPARATOR);
142                     // Decrement the index since the slash is part of the path
143
if (index != -1) index--;
144                 }
145             } else {
146                 // If the username was there, we find the first ':' past the '@'
147
index = root.indexOf(CVSRepositoryLocation.COLON, index + 1);
148             }
149             index++;
150             // strip off a leading port if there is one
151
char c = root.charAt(index);
152             while (Character.isDigit(c)) {
153                 c = root.charAt(++index);
154             }
155             return root.substring(index);
156         } catch (IndexOutOfBoundsException JavaDoc e) {
157             IStatus status = new CVSStatus(IStatus.ERROR,CVSMessages.FolderSyncInfo_Maleformed_root_4, e);
158             throw new CVSException(status);
159         }
160     }
161     
162     /**
163      * Gets the tag, may be <code>null</code>.
164      *
165      * @return Returns a String
166      */

167     public CVSEntryLineTag getTag() {
168         return tag;
169     }
170
171     /**
172      * Gets the repository, may be <code>null</code>.
173      *
174      * @return Returns a String
175      */

176     public String JavaDoc getRepository() {
177         return repository;
178     }
179
180     /**
181      * Gets the isStatic.
182      *
183      * @return Returns a boolean
184      */

185     public boolean getIsStatic() {
186         return isStatic;
187     }
188
189     /**
190      * Answers a full path to the folder on the remote server. This by appending the repository to the
191      * repository location speficied in the root.
192      *
193      * Example:
194      * root = :pserver:user@host:/home/user/repo
195      * repository = folder1/folder2
196      *
197      * Returns:
198      * /home/users/repo/folder1/folder2
199      *
200      * Note: CVS supports repository root directories that end in a slash (/).
201      * For these directories, the remote location must contain two slashes (//)
202      * between the root directory and the rest of the path. For example:
203      * root = :pserver:user@host:/home/user/repo/
204      * repository = folder1/folder2
205      * must return:
206      * /home/users/repo//folder1/folder2
207      *
208      * @return the full path of this folder on the server.
209      * @throws a CVSException if the root or repository is malformed.
210      */

211     public String JavaDoc getRemoteLocation() throws CVSException {
212         return Util.appendPath(getRootDirectory(), getRepository());
213     }
214     
215     /*
216      * Provide a hashCode() method that gaurentees that equal object will have the
217      * same hashCode
218      */

219     public int hashCode() {
220         return getRoot().hashCode() | getRepository().hashCode();
221     }
222     
223     /**
224      * Sets the tag for the folder.
225      *
226      * @param tag The tag to set
227      */

228     protected void setTag(CVSTag tag) {
229         if (tag == null || tag.equals(CVSTag.DEFAULT)) {
230             this.tag = null;
231         } else {
232             this.tag = new CVSEntryLineTag(tag);
233         }
234     }
235     /*
236      * @see Object#toString()
237      */

238     public String JavaDoc toString() {
239         return getRoot() + "/" + getRepository() + "/" + getTag(); //$NON-NLS-1$ //$NON-NLS-2$
240
}
241     
242     public MutableFolderSyncInfo cloneMutable() {
243         MutableFolderSyncInfo newSync = new MutableFolderSyncInfo(this);
244         return newSync;
245     }
246
247     /**
248      * Return true if this FolderSyncInfo is mapped to the same remote directory
249      * as the other FolderSyncInfo passed as a parameter.
250      *
251      * @param remoteInfo
252      * @return
253      */

254     public boolean isSameMapping(FolderSyncInfo other) {
255         if (other == null) return false;
256         return (this.getRoot().equals(other.getRoot())
257             && this.getRepository().equals(other.getRepository())) ;
258     }
259
260 /**
261      * Convert a FolderSyncInfo into a byte array that can be stored
262      * in the workspace synchronizer
263      */

264     public byte[] getBytes() throws CVSException {
265         ByteArrayOutputStream out = new ByteArrayOutputStream();
266         DataOutputStream dos = new DataOutputStream(out);
267         try {
268             dos.writeUTF(getRoot());
269             dos.writeUTF(getRepository());
270             CVSEntryLineTag t = getTag();
271             if (t == null) {
272                 dos.writeUTF(""); //$NON-NLS-1$
273
} else {
274                 dos.writeUTF(t.toString());
275             }
276             dos.writeBoolean(getIsStatic());
277             dos.close();
278         } catch (IOException e) {
279             throw CVSException.wrapException(e);
280         }
281         return out.toByteArray();
282     }
283
284     /**
285      * Convert a byte array that was created using getBytes(FolderSyncInfo)
286      * into a FolderSyncInfo
287      */

288     public static FolderSyncInfo getFolderSyncInfo(byte[] bytes) throws CVSException {
289         Assert.isNotNull(bytes, "getFolderSyncInfo cannot be called with null parameter"); //$NON-NLS-1$
290
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
291         DataInputStream dis = new DataInputStream(in);
292         String JavaDoc root;
293         String JavaDoc repository;
294         CVSEntryLineTag tag;
295         boolean isStatic;
296         try {
297             root = dis.readUTF();
298             repository = dis.readUTF();
299             String JavaDoc tagName = dis.readUTF();
300             if (tagName.length() == 0) {
301                 tag = null;
302             } else {
303                 tag = new CVSEntryLineTag(tagName);
304             }
305             isStatic = dis.readBoolean();
306         } catch (IOException e) {
307             Status status = new Status(Status.ERROR, CVSProviderPlugin.ID, NLS.bind(CVSMessages.FolderSyncInfo_InvalidSyncInfoBytes, new String JavaDoc(bytes)), e);
308             CVSException ex = new CVSException(status);
309             throw ex;
310         }
311         return new FolderSyncInfo(repository, root, tag, isStatic);
312     }
313     
314     /**
315      * Return whether the local directory is mapped to an existing remote
316      * directory or is just a local placeholder for child folders. a return type
317      * of <code>true</code> indicates that the local folder is not mapped to a
318      * remote folder.
319      * @return whether the directory is a local placeholder
320      */

321     public boolean isVirtualDirectory() {
322         return getRepository().equals(VIRTUAL_DIRECTORY);
323     }
324
325     public FolderSyncInfo asImmutable() {
326         return this;
327     }
328 }
329
Popular Tags