KickJava   Java API By Example, From Geeks To Geeks.

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


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.resources;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.core.runtime.IProgressMonitor;
17 import org.eclipse.core.runtime.IStatus;
18 import org.eclipse.team.core.TeamException;
19 import org.eclipse.team.internal.ccvs.core.CVSException;
20 import org.eclipse.team.internal.ccvs.core.CVSProviderPlugin;
21 import org.eclipse.team.internal.ccvs.core.CVSTag;
22 import org.eclipse.team.internal.ccvs.core.ICVSFile;
23 import org.eclipse.team.internal.ccvs.core.ICVSFolder;
24 import org.eclipse.team.internal.ccvs.core.ICVSResource;
25 import org.eclipse.team.internal.ccvs.core.Policy;
26 import org.eclipse.team.internal.ccvs.core.client.Command;
27 import org.eclipse.team.internal.ccvs.core.client.Session;
28 import org.eclipse.team.internal.ccvs.core.client.Update;
29 import org.eclipse.team.internal.ccvs.core.client.Command.LocalOption;
30 import org.eclipse.team.internal.ccvs.core.connection.CVSRepositoryLocation;
31
32 /**
33  * This class can be used to fetch and cache file contents for remote files.
34  */

35 public class FileContentCachingService {
36
37     String JavaDoc[] fileDiffs;
38     private CVSRepositoryLocation repository;
39     private ICVSFolder remoteRoot;
40
41     public static RemoteFolderTree buildRemoteTree(CVSRepositoryLocation repository, ICVSFolder root, CVSTag tag, IProgressMonitor monitor) throws CVSException {
42         monitor.beginTask(null, 100);
43         try {
44             RemoteFolderTreeBuilder builder = new RemoteFolderTreeBuilder(repository, root, tag);
45             RemoteFolderTree tree = builder.buildTree(new ICVSResource[] { root }, Policy.subMonitorFor(monitor, 50));
46             FileContentCachingService service = new FileContentCachingService(repository, tree, builder.getFileDiffs());
47             service.cacheFileContents(Policy.subMonitorFor(monitor, 50));
48             return tree;
49         } finally {
50             monitor.done();
51         }
52     }
53     
54     /**
55      * Fetch and cache the file contents for the specified files.
56      * @param root the root folder for the files being fetched
57      * @param filePaths the root relative file paths
58      * @param monitor
59      * @throws CVSException
60      */

61     public static void fetchFileContents(RemoteFolderTree root, String JavaDoc[] filePaths, IProgressMonitor monitor) throws CVSException {
62         FileContentCachingService service = new FileContentCachingService((CVSRepositoryLocation)root.getRepository(), root, filePaths);
63         service.cacheFileContents(monitor);
64     }
65     
66     public static RemoteFile buildRemoteTree(CVSRepositoryLocation repository, ICVSFile file, CVSTag tag, IProgressMonitor monitor) throws CVSException {
67         monitor.beginTask(null, 100);
68         try {
69             RemoteFolderTreeBuilder builder = new RemoteFolderTreeBuilder(repository, file.getParent(), tag);
70             RemoteFile remote = builder.buildTree(file, monitor);
71             if (builder.getFileDiffs().length > 0) {
72                 // Getting the storage of the file will cache the contents
73
remote.getStorage(Policy.subMonitorFor(monitor, 50));
74             }
75             return remote;
76         } catch (TeamException e) {
77             throw CVSException.wrapException(e);
78         } finally {
79             monitor.done();
80         }
81     }
82     
83     public FileContentCachingService(CVSRepositoryLocation repository, RemoteFolderTree tree, String JavaDoc[] fileDiffs) {
84         this.repository = repository;
85         this.remoteRoot = tree;
86         this.fileDiffs = fileDiffs;
87     }
88     
89     private void cacheFileContents(IProgressMonitor monitor) throws CVSException {
90         String JavaDoc[] files = getUncachedFiles();
91         if (files.length == 0) return;
92         // Fetch the file contents for all out-of-sync files by running an update
93
// on the remote tree passing the known changed files as arguments
94
monitor.beginTask(null, 10 + files.length * 100);
95         Policy.checkCanceled(monitor);
96         Session session = new Session(repository, remoteRoot, false);
97         session.open(Policy.subMonitorFor(monitor, 10), false /* read-only */);
98         try {
99             Policy.checkCanceled(monitor);
100             IStatus status = Command.UPDATE.execute(session,
101                 Command.NO_GLOBAL_OPTIONS,
102                 new LocalOption[] { Update.IGNORE_LOCAL_CHANGES },
103                 files,
104                 null,
105                 Policy.subMonitorFor(monitor, files.length * 100));
106             if (!status.isOK()) {
107                 // No big deal but log the problem anyway
108
CVSProviderPlugin.log (new CVSException(status));
109             }
110         } finally {
111             session.close();
112             monitor.done();
113         }
114     }
115
116     /*
117      * Only return those file in the diff list that exist remotely and whose contents are not already cached
118      */

119     private String JavaDoc[] getUncachedFiles() {
120         if (fileDiffs.length == 0) return fileDiffs;
121         List JavaDoc existing = new ArrayList JavaDoc();
122         for (int i = 0; i < fileDiffs.length; i++) {
123             String JavaDoc filePath = fileDiffs[i];
124             try {
125                 ICVSFile file = remoteRoot.getFile(filePath);
126                 if (file instanceof RemoteFile) {
127                     if (!((RemoteFile)file).isContentsCached()) {
128                         existing.add(filePath);
129                     }
130                 }
131             } catch (CVSException e) {
132                 // The child does not exists so exclude it
133
}
134         }
135         return (String JavaDoc[]) existing.toArray(new String JavaDoc[existing.size()]);
136     }
137 }
138
Popular Tags