|
Added
Link Here
|
| 1 |
package org.eclipse.team.internal.ccvs.core; |
| 2 |
|
| 3 |
import java.util.ArrayList; |
| 4 |
import java.util.Collection; |
| 5 |
import java.util.HashMap; |
| 6 |
import java.util.List; |
| 7 |
import java.util.Map; |
| 8 |
import java.util.StringTokenizer; |
| 9 |
|
| 10 |
import org.eclipse.core.resources.IProject; |
| 11 |
import org.eclipse.core.resources.ResourcesPlugin; |
| 12 |
import org.eclipse.core.runtime.IProgressMonitor; |
| 13 |
import org.eclipse.core.runtime.NullProgressMonitor; |
| 14 |
import org.eclipse.core.runtime.SubProgressMonitor; |
| 15 |
import org.eclipse.team.core.ProjectSetSerializationContext; |
| 16 |
import org.eclipse.team.core.ProjectSetSerializer; |
| 17 |
import org.eclipse.team.core.RepositoryProvider; |
| 18 |
import org.eclipse.team.core.TeamException; |
| 19 |
import org.eclipse.team.internal.ccvs.core.connection.CVSRepositoryLocation; |
| 20 |
import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot; |
| 21 |
import org.eclipse.team.internal.ccvs.core.syncinfo.FolderSyncInfo; |
| 22 |
|
| 23 |
/** |
| 24 |
* An object for serializing and deserializing |
| 25 |
* of references to CVS based projects. Given a project, it can produce a |
| 26 |
* UTF-8 encoded String which can be stored in a file. |
| 27 |
* Given this String, it can load a project into the workspace. |
| 28 |
* |
| 29 |
* @since 3.0 |
| 30 |
*/ |
| 31 |
public class CVSProjectSetSerializer extends ProjectSetSerializer { |
| 32 |
|
| 33 |
/** |
| 34 |
* Override superclass implementation to return an array of project references. |
| 35 |
* |
| 36 |
* @see ProjectSetSerializer#asReference(IProject[], ProjectSetSerializationContext, IProgressMonitor) |
| 37 |
*/ |
| 38 |
public String[] asReference( |
| 39 |
IProject[] projects, |
| 40 |
ProjectSetSerializationContext context, |
| 41 |
IProgressMonitor monitor) |
| 42 |
throws TeamException { |
| 43 |
|
| 44 |
String[] result = new String[projects.length]; |
| 45 |
for (int i = 0; i < projects.length; i++) |
| 46 |
result[i] = asReference(projects[i]); |
| 47 |
return result; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Answer a string representing the specified project |
| 52 |
* |
| 53 |
* @param project the project (not <code>null</code>) |
| 54 |
* @return the project reference (not <code>null</code>) |
| 55 |
* @throws CVSException |
| 56 |
*/ |
| 57 |
private String asReference(IProject project) throws TeamException { |
| 58 |
StringBuffer buffer = new StringBuffer(); |
| 59 |
buffer.append("1.0,"); //$NON-NLS-1$ |
| 60 |
|
| 61 |
CVSTeamProvider provider = (CVSTeamProvider)RepositoryProvider.getProvider(project); |
| 62 |
CVSWorkspaceRoot root = provider.getCVSWorkspaceRoot(); |
| 63 |
CVSRepositoryLocation location = CVSRepositoryLocation.fromString(root.getRemoteLocation().getLocation()); |
| 64 |
location.setUserMuteable(true); |
| 65 |
String repoLocation = location.getLocation(); |
| 66 |
buffer.append(repoLocation); |
| 67 |
buffer.append(","); //$NON-NLS-1$ |
| 68 |
|
| 69 |
ICVSFolder folder = root.getLocalRoot(); |
| 70 |
FolderSyncInfo syncInfo = folder.getFolderSyncInfo(); |
| 71 |
String module = syncInfo.getRepository(); |
| 72 |
buffer.append(module); |
| 73 |
buffer.append(","); //$NON-NLS-1$ |
| 74 |
|
| 75 |
String projectName = folder.getName(); |
| 76 |
buffer.append(projectName); |
| 77 |
CVSTag tag = syncInfo.getTag(); |
| 78 |
if (tag != null) { |
| 79 |
if (tag.getType() != CVSTag.DATE) { |
| 80 |
buffer.append(","); //$NON-NLS-1$ |
| 81 |
String tagName = tag.getName(); |
| 82 |
buffer.append(tagName); |
| 83 |
} |
| 84 |
} |
| 85 |
return buffer.toString(); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Override superclass implementation to load the referenced projects into the workspace. |
| 90 |
* |
| 91 |
* @see org.eclipse.team.core.ProjectSetSerializer#addToWorkspace(java.lang.String[], org.eclipse.team.core.ProjectSetSerializationContext, org.eclipse.core.runtime.IProgressMonitor) |
| 92 |
*/ |
| 93 |
public IProject[] addToWorkspace( |
| 94 |
String[] referenceStrings, |
| 95 |
ProjectSetSerializationContext context, |
| 96 |
IProgressMonitor originalMonitor) |
| 97 |
throws TeamException { |
| 98 |
|
| 99 |
IProgressMonitor monitor = originalMonitor; |
| 100 |
if (monitor == null) |
| 101 |
monitor = new NullProgressMonitor(); |
| 102 |
else if (monitor.isCanceled()) |
| 103 |
return new IProject[0]; |
| 104 |
|
| 105 |
// Confirm the projects to be loaded |
| 106 |
Map infoMap = new HashMap(referenceStrings.length); |
| 107 |
IProject[] projects = asProjects(referenceStrings, infoMap); |
| 108 |
projects = confirmOverwrite(context, projects); |
| 109 |
if (projects == null) |
| 110 |
return new IProject[0]; |
| 111 |
|
| 112 |
// Load the projects |
| 113 |
monitor.beginTask("", 20); //$NON-NLS-1$ |
| 114 |
try { |
| 115 |
projects = createProjects(context, projects, new SubProgressMonitor(monitor, 1)); |
| 116 |
if (projects == null) |
| 117 |
return new IProject[0]; |
| 118 |
return checkout(projects, infoMap, new SubProgressMonitor(monitor, 19)); |
| 119 |
} |
| 120 |
finally { |
| 121 |
monitor.done(); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Translate the reference strings into projects to be loaded |
| 127 |
* and build a mapping of project to project load information. |
| 128 |
* |
| 129 |
* @param referenceStrings project references |
| 130 |
* @param infoMap a mapping of project to project load information |
| 131 |
* @return the projects to be loaded |
| 132 |
*/ |
| 133 |
private IProject[] asProjects(String[] referenceStrings, Map infoMap) throws CVSException { |
| 134 |
Collection result = new ArrayList(); |
| 135 |
for (int i = 0; i < referenceStrings.length; i++) { |
| 136 |
StringTokenizer tokenizer = new StringTokenizer(referenceStrings[i], ","); //$NON-NLS-1$ |
| 137 |
String version = tokenizer.nextToken(); |
| 138 |
// If this is a newer version, then ignore it |
| 139 |
if (!version.equals("1.0")) //$NON-NLS-1$ |
| 140 |
continue; |
| 141 |
LoadInfo info = new LoadInfo(tokenizer); |
| 142 |
IProject proj = info.getProject(); |
| 143 |
result.add(proj); |
| 144 |
infoMap.put(proj, info); |
| 145 |
} |
| 146 |
return (IProject[]) result.toArray(new IProject[result.size()]); |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Checkout projects from the CVS repository |
| 151 |
* |
| 152 |
* @param projects the projects to be loaded from the repository |
| 153 |
* @param infoMap a mapping of project to project load information |
| 154 |
* @param monitor the progress monitor (not <code>null</code>) |
| 155 |
*/ |
| 156 |
private IProject[] checkout( |
| 157 |
IProject[] projects, |
| 158 |
Map infoMap, |
| 159 |
IProgressMonitor monitor) |
| 160 |
throws TeamException { |
| 161 |
|
| 162 |
monitor.beginTask("", 1000 * projects.length); //$NON-NLS-1$ |
| 163 |
List result = new ArrayList(); |
| 164 |
try { |
| 165 |
for (int i = 0; i < projects.length; i++) { |
| 166 |
if (monitor.isCanceled()) |
| 167 |
break; |
| 168 |
IProject project = projects[i]; |
| 169 |
LoadInfo info = (LoadInfo) infoMap.get(project); |
| 170 |
if (info != null && info.checkout(new SubProgressMonitor(monitor, 1000))) |
| 171 |
result.add(project); |
| 172 |
} |
| 173 |
} |
| 174 |
finally { |
| 175 |
monitor.done(); |
| 176 |
} |
| 177 |
return (IProject[])result.toArray(new IProject[result.size()]); |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Internal class for adding projects to the workspace |
| 182 |
*/ |
| 183 |
class LoadInfo { |
| 184 |
private final ICVSRepositoryLocation repositoryLocation; |
| 185 |
private final String module; |
| 186 |
private final IProject project; |
| 187 |
private final CVSTag tag; |
| 188 |
|
| 189 |
/** |
| 190 |
* Construct a new instance wrappering the specified project reference |
| 191 |
* |
| 192 |
* @param projRef the project reference |
| 193 |
*/ |
| 194 |
LoadInfo(StringTokenizer tokenizer) throws CVSException { |
| 195 |
String repo = tokenizer.nextToken(); |
| 196 |
repositoryLocation = getRepositoryLocationFromString(repo); |
| 197 |
module = tokenizer.nextToken(); |
| 198 |
String projectName = tokenizer.nextToken(); |
| 199 |
project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName); |
| 200 |
if (tokenizer.hasMoreTokens()) { |
| 201 |
String tagName = tokenizer.nextToken(); |
| 202 |
tag = new CVSTag(tagName, CVSTag.BRANCH); |
| 203 |
} |
| 204 |
else { |
| 205 |
tag = null; |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Answer the project referenced by this object. |
| 211 |
* The project may or may not already exist. |
| 212 |
* |
| 213 |
* @return the project (not <code>null</code>) |
| 214 |
*/ |
| 215 |
private IProject getProject() { |
| 216 |
return project; |
| 217 |
} |
| 218 |
|
| 219 |
/** |
| 220 |
* Checkout the project specified by this reference. |
| 221 |
* |
| 222 |
* @param monitor project monitor |
| 223 |
* @return true if loaded, else false |
| 224 |
* @throws TeamException |
| 225 |
*/ |
| 226 |
boolean checkout(IProgressMonitor monitor) throws TeamException { |
| 227 |
if (repositoryLocation == null) |
| 228 |
return false; |
| 229 |
CVSWorkspaceRoot.checkout( |
| 230 |
repositoryLocation, |
| 231 |
project, |
| 232 |
module, |
| 233 |
tag, |
| 234 |
monitor); |
| 235 |
return true; |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* Extract the CVS repository location information from the specified string |
| 241 |
* |
| 242 |
* @param repo the repository location as a string |
| 243 |
* @return the CVS repository information |
| 244 |
* @throws CVSException |
| 245 |
*/ |
| 246 |
private static ICVSRepositoryLocation getRepositoryLocationFromString(String repo) throws CVSException { |
| 247 |
// create the new location |
| 248 |
ICVSRepositoryLocation newLocation = CVSRepositoryLocation.fromString(repo); |
| 249 |
if (newLocation.getUsername() == null || newLocation.getUsername().length() == 0) { |
| 250 |
// look for an existing location that matched |
| 251 |
ICVSRepositoryLocation[] locations = CVSProviderPlugin.getPlugin().getKnownRepositories(); |
| 252 |
for (int i = 0; i < locations.length; i++) { |
| 253 |
ICVSRepositoryLocation location = locations[i]; |
| 254 |
if (location.getMethod() == newLocation.getMethod() |
| 255 |
&& location.getHost().equals(newLocation.getHost()) |
| 256 |
&& location.getPort() == newLocation.getPort() |
| 257 |
&& location.getRootDirectory().equals(newLocation.getRootDirectory())) |
| 258 |
return location; |
| 259 |
} |
| 260 |
} |
| 261 |
return newLocation; |
| 262 |
} |
| 263 |
|
| 264 |
} |