1 19 20 package org.netbeans.modules.versioning.system.cvss; 21 22 import org.netbeans.lib.cvsclient.CVSRoot; 23 import org.netbeans.lib.cvsclient.Client; 24 import org.netbeans.lib.cvsclient.event.TerminationEvent; 25 import org.netbeans.lib.cvsclient.connection.*; 26 import org.netbeans.lib.cvsclient.command.GlobalOptions; 27 import org.netbeans.lib.cvsclient.command.Command; 28 import org.netbeans.lib.cvsclient.command.BasicCommand; 29 import org.netbeans.lib.cvsclient.command.log.RlogCommand; 30 import org.netbeans.lib.cvsclient.command.importcmd.ImportCommand; 31 import org.netbeans.lib.cvsclient.command.checkout.CheckoutCommand; 32 import org.netbeans.lib.cvsclient.command.add.AddCommand; 33 import org.netbeans.modules.versioning.system.cvss.util.Utils; 34 import org.openide.util.RequestProcessor; 35 import org.openide.util.TaskListener; 36 import org.openide.util.Task; 37 import org.openide.ErrorManager; 38 import org.openide.windows.InputOutput; 39 import org.openide.windows.IOProvider; 40 import org.openide.windows.OutputListener; 41 42 import javax.net.SocketFactory; 43 import java.io.File ; 44 import java.io.IOException ; 45 46 53 public class ClientRuntime { 54 55 58 private final String cvsRoot; 59 60 63 private String cvsRootDisplay; 64 65 68 private RequestProcessor requestProcessor; 69 70 73 private InputOutput log; 74 75 ClientRuntime(String root) { 76 cvsRoot = root; 77 cvsRootDisplay = root; 78 try { 79 CVSRoot rt = CVSRoot.parse(cvsRoot); 80 if (rt.getPassword() != null) { 81 int idx = root.indexOf(rt.getPassword()); 83 if (idx != -1) { 84 cvsRootDisplay = cvsRoot.substring(0, idx - 1) + cvsRoot.substring(idx + rt.getPassword().length()); 85 } 86 } 87 } catch (Exception e) { 88 } 90 requestProcessor = new RequestProcessor("CVS: " + cvsRootDisplay); } 92 93 private void ensureValidCommand(File [] files) throws IllegalCommandException { 94 for (int i = 0; i < files.length; i++) { 95 File file = files[i]; 96 try { 97 String root = Utils.getCVSRootFor(file); 98 if (!root.equals(cvsRoot)) throw new IllegalCommandException("#63547 command includes files from different CVS root.\n Expected: " + cvsRoot + "\nGot: " + root); } catch (IOException e) { 100 throw new IllegalCommandException("Missing or invalid CVS/Root for: " + file); } 102 } 103 } 104 105 116 public RequestProcessor.Task createTask(Command cmd, GlobalOptions globalOptions, final ExecutorSupport mgr) 117 throws IllegalCommandException { 118 119 File [] files = getCommandFiles(cmd); 120 if ((cmd instanceof CheckoutCommand) == false && !(cmd instanceof RlogCommand)) { ensureValidCommand(files); 122 } 123 124 if (globalOptions.getCVSRoot() == null) { 125 globalOptions = (GlobalOptions) globalOptions.clone(); 126 globalOptions.setCVSRoot(cvsRoot); 127 } 128 129 Client client = createClient(); 130 if ((cmd instanceof RlogCommand)) { } 132 else if ((cmd instanceof CheckoutCommand)) { BasicCommand bc = (BasicCommand) cmd; 134 if (bc.getFiles() != null) { 135 String path = bc.getFiles()[0].getAbsolutePath(); 136 client.setLocalPath(path); 137 } else { 138 client.setLocalPath(System.getProperty("user.dir")); } 141 } else if (cmd instanceof ImportCommand) { 142 client.setLocalPath(((ImportCommand)cmd).getImportDirectory()); 143 } else { 144 setLocalDirectory(client, files); 145 } 146 147 client.getEventManager().addCVSListener(mgr); 148 final CommandRunnable cr = new CommandRunnable(client, globalOptions, cmd, mgr); 149 mgr.commandEnqueued(cr); 150 RequestProcessor.Task task = requestProcessor.create(cr); 151 task.addTaskListener(new TaskListener() { 152 public void taskFinished(Task task) { 153 try { 154 mgr.commandTerminated(new TerminationEvent(new Result(cr))); 157 } catch (Throwable e) { 158 ErrorManager.getDefault().notify(ErrorManager.WARNING, e); 159 } finally { 160 flushLog(); 161 } 162 } 163 }); 164 return task; 165 } 166 167 171 public void log(String message) { 172 log(message, null); 173 } 174 175 179 public void log(String message, OutputListener hyperlinkListener) { 180 openLog(); 181 if (hyperlinkListener != null) { 182 try { 183 log.getOut().println(message, hyperlinkListener); 184 } catch (IOException e) { 185 log.getOut().write(message); 186 } 187 } else { 188 log.getOut().write(message); 189 } 190 } 191 192 private void openLog() { 193 if (log == null || log.isClosed()) { 194 log = IOProvider.getDefault().getIO(cvsRootDisplay, false); 195 try { 196 log.getOut().reset(); 198 } catch (IOException e) { 199 ErrorManager err = ErrorManager.getDefault(); 200 err.notify(e); 201 } 202 } 204 } 205 206 public void logError(Throwable e) { 207 openLog(); 208 e.printStackTrace(log.getOut()); 209 } 210 211 214 void focusLog() { 215 if (log != null) log.select(); 216 } 217 218 public void flushLog() { 219 if (log != null) log.getOut().close(); 220 } 221 222 223 private File [] getCommandFiles(Command cmd) { 224 if (cmd instanceof AddCommand) { 225 AddCommand c = (AddCommand) cmd; 226 return c.getFiles(); 227 } else if (cmd instanceof BasicCommand) { 228 BasicCommand c = (BasicCommand) cmd; 229 return c.getFiles(); 230 } else { 231 return new File [0]; 232 } 233 } 234 235 private void setLocalDirectory(Client client, File [] files) throws IllegalCommandException { 236 if (files.length == 0) { 237 return; 238 } 239 240 File commonParent; 241 242 if (files[0].isDirectory()) { commonParent = files[0]; 244 } else { 245 commonParent = files[0].getParentFile(); 246 } 247 248 for (int i = 1; i < files.length; i++) { 249 if (!Utils.isParentOrEqual(commonParent, files[i])) { 250 for (;;) { 251 commonParent = commonParent.getParentFile(); 252 if (commonParent == null) throw new IllegalCommandException("Files do not have common parent!"); if (Utils.isParentOrEqual(commonParent, files[i])) { 254 break; 255 } 256 } 257 } 258 } 259 260 FileStatusCache cache = CvsVersioningSystem.getInstance().getStatusCache(); 262 for (File versionedCommonParent = commonParent; versionedCommonParent != null; versionedCommonParent = versionedCommonParent.getParentFile()) { 263 FileInformation info = cache.getStatus(versionedCommonParent); 264 if (info.getStatus() == FileInformation.STATUS_VERSIONED_UPTODATE) { 265 commonParent = versionedCommonParent; 266 break; 267 } 268 } 269 client.setLocalPath(commonParent.getAbsolutePath()); 270 } 271 272 277 private Client createClient() { 278 Connection connection = setupConnection(CVSRoot.parse(cvsRoot)); 279 Client client = new Client(connection, CvsVersioningSystem.getInstance().getAdminHandler()); 280 client.setUncompressedFileHandler(CvsVersioningSystem.getInstance().getFileHandler()); 281 client.setGzipFileHandler(CvsVersioningSystem.getInstance().getGzippedFileHandler()); 282 return client; 283 } 284 285 292 public static Connection setupConnection(CVSRoot cvsRoot) throws IllegalArgumentException { 293 294 String t9yRoot = System.getProperty("netbeans.t9y.cvs.connection.CVSROOT"); CVSRoot patchedCvsRoot = cvsRoot; 297 if (t9yRoot != null && t9yRoot.length() > 0) { 298 int idx = t9yRoot.indexOf(','); 299 if (idx != -1) { 300 System.setProperty("netbeans.t9y.cvs.connection.CVSROOT", t9yRoot.substring(idx + 1)); t9yRoot = t9yRoot.substring(0, idx); 302 } 303 try { 304 patchedCvsRoot = CVSRoot.parse(t9yRoot); 305 assert patchedCvsRoot.getRepository().equals(cvsRoot.getRepository()); 306 assert patchedCvsRoot.getHostName() == cvsRoot.getHostName() || patchedCvsRoot.getHostName().equals(cvsRoot.getHostName()); 307 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, "CVS.ClientRuntime: using patched CVSROOT " + t9yRoot); } catch (IllegalArgumentException ex) { 309 ErrorManager.getDefault().annotate(ex, "While parsing: " + t9yRoot); ErrorManager.getDefault().notify(ex); 311 } 312 } 313 314 if (cvsRoot.isLocal()) { 315 LocalConnection con = new LocalConnection(); 316 con.setRepository(cvsRoot.getRepository()); 317 return con; 318 } 319 320 SocketFactory factory = SocketFactory.getDefault(); 321 322 String method = cvsRoot.getMethod(); 323 if (CVSRoot.METHOD_PSERVER.equals(method)) { 324 PServerConnection con = new PServerConnection(patchedCvsRoot, factory); 325 String password = PasswordsFile.findPassword(cvsRoot.toString()); 326 con.setEncodedPassword(password); 327 return con; 328 } else if (CVSRoot.METHOD_EXT.equals(method)) { 329 CvsModuleConfig.ExtSettings extSettings = CvsModuleConfig.getDefault().getExtSettingsFor(cvsRoot); 330 String userName = cvsRoot.getUserName(); 331 String host = cvsRoot.getHostName(); 332 if (extSettings.extUseInternalSsh) { 333 int port = patchedCvsRoot.getPort(); 334 port = port == 0 ? 22 : port; String password = extSettings.extPassword; 336 if (password == null) { 337 password = "\n"; } 339 SSHConnection sshConnection = new SSHConnection(factory, host, port, userName, password); 340 sshConnection.setRepository(cvsRoot.getRepository()); 341 return sshConnection; 342 } else { 343 351 String command = extSettings.extCommand; 352 String cvs_server = System.getenv("CVS_SERVER"); 353 cvs_server = cvs_server != null? cvs_server + " server": "cvs server"; String userOption = ""; if ( userName != null ) { 356 userOption = " -l " + userName; } 358 command += " " + host + userOption + " " + cvs_server; ExtConnection connection = new ExtConnection(command); 360 connection.setRepository(cvsRoot.getRepository()); 361 return connection; 362 } 363 } 364 365 throw new IllegalArgumentException ("Unrecognized CVS Root: " + cvsRoot); } 367 368 public String toString() { 369 return "ClientRuntime queue=" + cvsRootDisplay + " processor=" + requestProcessor; } 371 372 376 public static class Result { 377 378 private final CommandRunnable runnable; 379 380 public Result(CommandRunnable runnable) { 381 this.runnable = runnable; 382 } 383 384 388 public Throwable getError() { 389 return runnable.getFailure(); 390 } 391 392 393 public boolean isAborted() { 394 return runnable.isAborted(); 395 } 396 } 397 } 398 | Popular Tags |