1 19 20 package org.netbeans.modules.versioning.system.cvss.ui.actions.update; 21 22 import org.netbeans.modules.versioning.system.cvss.*; 23 import org.netbeans.modules.versioning.VersioningManager; 24 import org.netbeans.modules.versioning.VersioningOutputManager; 25 import org.netbeans.lib.cvsclient.command.update.UpdateCommand; 26 import org.netbeans.lib.cvsclient.command.GlobalOptions; 27 import org.netbeans.lib.cvsclient.command.DefaultFileInfoContainer; 28 import org.netbeans.lib.cvsclient.command.Command; 29 import org.netbeans.lib.cvsclient.event.*; 30 import org.openide.filesystems.*; 31 import org.openide.ErrorManager; 32 import org.openide.DialogDisplayer; 33 import org.openide.NotifyDescriptor; 34 import org.openide.util.NbBundle; 35 36 import javax.swing.*; 37 import java.io.File ; 38 import java.io.IOException ; 39 import java.util.*; 40 import java.util.logging.Logger ; 41 import java.util.logging.Level ; 42 43 48 public class UpdateExecutor extends ExecutorSupport { 49 50 53 private Set<File > refreshedFiles = Collections.synchronizedSet(new HashSet<File >()); 54 private boolean rwUpdate; 55 private boolean mergeUpdate; 56 57 60 private final String contextDisplayName; 61 62 65 private long updateStartTimestamp; 66 67 80 public static UpdateExecutor [] splitCommand(UpdateCommand cmd, CvsVersioningSystem cvs, GlobalOptions options, String contextDisplayName) { 81 Command [] cmds = new org.netbeans.lib.cvsclient.command.Command[0]; 82 if (cmd.getDisplayName() == null) cmd.setDisplayName(NbBundle.getMessage(UpdateExecutor.class, "MSG_UpdateExecutor_CmdDisplayName")); 83 try { 84 cmds = prepareBasicCommand(cmd); 85 } catch (IOException e) { 86 ErrorManager.getDefault().notify(e); 87 return null; 88 } 89 UpdateExecutor [] executors = new UpdateExecutor[cmds.length]; 90 for (int i = 0; i < cmds.length; i++) { 91 Command command = cmds[i]; 92 executors[i] = new UpdateExecutor(cvs, (UpdateCommand) command, options, contextDisplayName); 93 } 94 return executors; 95 } 96 97 private UpdateExecutor(CvsVersioningSystem cvs, UpdateCommand cmd, GlobalOptions options, String contextDisplayName) { 98 super(cvs, cmd, options); 99 this.contextDisplayName = contextDisplayName; 100 rwUpdate = options == null || !options.isDoNoChanges(); 101 mergeUpdate = cmd.getMergeRevision1() != null; 102 } 103 104 protected void setup() { 105 super.setup(); 106 updateStartTimestamp = System.currentTimeMillis(); 107 } 108 109 public void fileInfoGenerated(FileInfoEvent e) { 110 super.fileInfoGenerated(e); 111 } 112 113 116 protected void commandFinished(ClientRuntime.Result result) { 117 118 UpdateCommand ucmd = (UpdateCommand) cmd; 119 120 if (ucmd.getGlobalOptions() == null) { 124 if (!cmd.hasFailed()) { 125 Logger.getLogger("org.netbeans.modules.versioning.system.cvss").log(Level.INFO, "Warning: Update command did not fail but global options are null."); 127 } 128 return; 129 } 130 131 cvs.setParameter(CvsVersioningSystem.PARAM_BATCH_REFRESH_RUNNING, Boolean.TRUE); 132 133 File [] files = ucmd.getFiles(); 134 135 for (int i = 0; i < files.length; i++) { 136 cache.clearVirtualDirectoryContents(files[i], ucmd.isRecursive(), ucmd.getGlobalOptions().getExclusions()); 137 } 138 139 if (rwUpdate && contextDisplayName != null) { 140 openOutputResults(); 141 } 142 143 Set<FileSystem> filesystems = new HashSet<FileSystem>(2); 144 boolean hasConflict = false; 145 for (Iterator i = toRefresh.iterator(); i.hasNext();) { 146 DefaultFileInfoContainer info = (DefaultFileInfoContainer) i.next(); 147 File file = info.getFile(); 148 if (refreshedFiles.contains(file)) continue; 149 int c = info.getType().charAt(0); 150 if (c == 'P') c = 'U'; 151 if (rwUpdate) { 152 if (c == 'U') { 153 if (mergeUpdate) { 154 c = FileStatusCache.REPOSITORY_STATUS_MODIFIED; 155 } else { 156 c = FileStatusCache.REPOSITORY_STATUS_UPTODATE; 157 } 158 } 159 if (c == 'G') c = FileStatusCache.REPOSITORY_STATUS_MODIFIED; 160 if (c == 'C') hasConflict = true; 161 } 162 cache.refresh(file, c, true); 163 refreshedFiles.add(file); 164 } 165 166 for (int i = 0; i < files.length; i++) { 169 if (ucmd.isRecursive()) { 170 refreshRecursively(files[i]); 171 } else { 172 refreshFlat(files[i]); 173 } 174 addFileSystem(filesystems, files[i]); 175 if (files[i].isFile()) { 176 cache.refreshCached(files[i].getParentFile(), FileStatusCache.REPOSITORY_STATUS_UNKNOWN); 177 } 178 } 179 180 cvs.setParameter(CvsVersioningSystem.PARAM_BATCH_REFRESH_RUNNING, null); 181 if (hasConflict) { 182 NotifyDescriptor nd = new NotifyDescriptor.Message( 183 NbBundle.getMessage(UpdateExecutor.class, "MSG_UpdateGeneratedConflicts_Prompt"), 184 NotifyDescriptor.WARNING_MESSAGE); 185 DialogDisplayer.getDefault().notify(nd); 186 } 187 188 for (Iterator i = filesystems.iterator(); i.hasNext();) { 189 FileSystem fileSystem = (FileSystem) i.next(); 190 try { 191 CvsVersioningSystem.ignoreFilesystemEvents(true); 192 fileSystem.refresh(true); } finally { 194 CvsVersioningSystem.ignoreFilesystemEvents(false); 195 } 196 } 197 198 if (ucmd.getUpdateByRevision() != null || ucmd.isResetStickyOnes()) { 200 CvsVersioningSystem.getInstance().refreshAllAnnotations(); 201 } 202 } 203 204 private void openOutputResults() { 205 SwingUtilities.invokeLater(new Runnable () { 206 public void run() { 207 UpdateResults results = new UpdateResults(toRefresh, cmd.getGlobalOptions().getCVSRoot(), contextDisplayName); 208 VersioningOutputManager vom = VersioningManager.getInstance().getOutputManager(); 209 vom.addComponent(cmd.getGlobalOptions().getCVSRoot() + "-UpdateExecutor", results); } 211 }); 212 } 213 214 private void addFileSystem(Set<FileSystem> filesystems, File file) { 215 FileObject fo; 216 for (;;) { 217 fo = FileUtil.toFileObject(file); 218 if (fo != null) break; 219 file = file.getParentFile(); 220 if (file == null) return; 221 } 222 try { 223 filesystems.add(fo.getFileSystem()); 224 } catch (FileStateInvalidException e) { 225 } 227 } 228 229 private void refreshRecursively(File file) { 230 try { 231 if (cvs.isIgnoredFilename(file)) return; 232 if (cmd.getGlobalOptions().isExcluded(file)) return; 233 if (file.isDirectory()) { 234 if (cache.getStatus(file).getStatus() == FileInformation.STATUS_NOTVERSIONED_EXCLUDED) return; 235 File [] files = file.listFiles(); 236 for (int i = 0; i < files.length; i++) { 237 refreshRecursively(files[i]); 238 } 239 if (!refreshedFiles.contains(file)) cache.refreshCached(file, FileStatusCache.REPOSITORY_STATUS_UNKNOWN); 240 } else { 241 if (!refreshedFiles.contains(file)) refreshFile(file); 242 } 243 } catch (Throwable e) { 244 ErrorManager.getDefault().notify(e); 246 } 247 } 248 249 private void refreshFlat(File file) { 250 if (cvs.isIgnoredFilename(file)) return; 251 if (cmd.getGlobalOptions().isExcluded(file)) return; 252 if (refreshedFiles.contains(file)) return; 253 if (file.isDirectory()) { 254 File [] files = file.listFiles(); 255 for (int i = 0; i < files.length; i++) { 256 if (cvs.isIgnoredFilename(files[i])) return; 257 if (refreshedFiles.contains(files[i])) return; 258 if (files[i].isDirectory()) continue; 259 refreshFile(files[i]); 260 } 261 cache.refreshCached(file, FileStatusCache.REPOSITORY_STATUS_UNKNOWN); 262 } else { 263 refreshFile(file); 264 } 265 } 266 267 private void refreshFile(File file) { 268 long lastModified = file.lastModified(); 269 if (!cmd.hasFailed() && cache.getStatus(file.getParentFile()).getStatus() == FileInformation.STATUS_VERSIONED_UPTODATE && 270 lastModified > 0 && lastModified < updateStartTimestamp) { 271 cache.refreshCached(file, FileStatusCache.REPOSITORY_STATUS_UPTODATE); 272 } else { 273 cache.refreshCached(file, FileStatusCache.REPOSITORY_STATUS_UNKNOWN); 274 } 275 } 276 } 277 | Popular Tags |