1 19 20 package org.netbeans.modules.versioning.system.cvss.ui.actions.project; 21 22 import org.netbeans.modules.versioning.system.cvss.*; 23 import org.netbeans.modules.versioning.system.cvss.executor.CheckoutExecutor; 24 import org.netbeans.modules.versioning.system.cvss.ui.actions.checkout.CheckoutAction; 25 import org.netbeans.modules.versioning.system.cvss.ui.selectors.Kit; 26 import org.netbeans.lib.cvsclient.command.GlobalOptions; 27 import org.netbeans.lib.cvsclient.command.importcmd.ImportCommand; 28 import org.netbeans.lib.cvsclient.admin.StandardAdminHandler; 29 import org.netbeans.lib.cvsclient.admin.Entry; 30 import org.openide.util.NbBundle; 31 import org.openide.util.TaskListener; 32 import org.openide.util.Task; 33 import org.openide.util.actions.SystemAction; 34 import org.openide.NotifyDescriptor; 35 import org.openide.DialogDisplayer; 36 import org.openide.ErrorManager; 37 38 import java.io.*; 39 import java.util.Iterator ; 40 import java.util.Date ; 41 42 47 final class ImportExecutor extends ExecutorSupport implements Runnable { 48 49 private final String module; 50 private final String cvsRoot; 51 private final boolean checkout; 52 private final String folder; 53 private CheckoutExecutor checkoutExecutor; 54 private File checkoutDir; 55 private final ExecutorGroup group; 56 57 67 public ImportExecutor(ImportCommand cmd, GlobalOptions options, boolean checkout, String folder, ExecutorGroup group) { 68 super(CvsVersioningSystem.getInstance(), cmd, options); 69 module = cmd.getModule(); 70 cvsRoot = options.getCVSRoot(); 71 this.checkout = checkout; 72 this.folder = folder; 73 this.group = group; 74 75 group.addExecutor(this); 76 if (checkout) { 77 checkoutDir = Kit.createTmpFolder(); 78 CheckoutAction checkoutAction = (CheckoutAction) SystemAction.get(CheckoutAction.class); 79 checkoutExecutor = checkoutAction.checkout(cvsRoot, module, null, checkoutDir.getAbsolutePath(), false, group); 80 group.addBarrier(this); 81 } 82 } 83 84 protected void commandFinished(ClientRuntime.Result result) { 85 } 86 87 90 94 public void run() { 95 CvsVersioningSystem.getInstance().versionedFilesChanged(); 96 if (checkoutExecutor.isSuccessful()) { 97 copyMetadata(); 98 } 99 Kit.deleteRecursively(checkoutDir); 100 } 101 102 private void copyMetadata() { 103 File dest = new File(folder); 104 File src = new File(checkoutDir, module); 106 assert src.isDirectory() : src.getAbsolutePath(); 107 108 copyFolderMeta(src, dest); 109 110 FileStatusCache cache = CvsVersioningSystem.getInstance().getStatusCache(); 111 cache.refresh(dest, FileStatusCache.REPOSITORY_STATUS_UNKNOWN); 112 } 113 114 private void copyFolderMeta(File src, File dest) { 115 File[] files = src.listFiles(); 116 for (int i = 0; i < files.length; i++) { 117 File file = files[i]; 118 if (file.isDirectory()) { 119 if ("CVS".equals(file.getName())) { copyCvsMeta(file, dest); 121 } else { 122 File destDir = new File(dest, file.getName()); 123 if (destDir.isDirectory()) { 124 copyFolderMeta(file, destDir); } 126 } 127 } 128 } 129 } 130 131 private void copyCvsMeta(File src, File dest) { 132 File destCvsDir = new File(dest, "CVS"); if (destCvsDir.exists() == false || (destCvsDir.isDirectory() && destCvsDir.listFiles().length == 0) ) { 134 destCvsDir.mkdirs(); 135 if (destCvsDir.isDirectory()) { 136 try { 138 File root = new File(src, "Root"); copyFile(root, new File(destCvsDir, "Root")); File repository = new File(src, "Repository"); copyFile(repository, new File(destCvsDir, "Repository")); File entries = new File(src, "Entries"); copyFile(entries, new File(destCvsDir, "Entries")); 145 StandardAdminHandler parser = new StandardAdminHandler(); 147 Iterator it = parser.getEntries(dest); 148 while (it.hasNext()) { 149 Entry entry = (Entry) it.next(); 150 String name = entry.getName(); 151 Date date = entry.getLastModified(); 153 154 File sourceFile = new File(dest, name); 155 if (sourceFile.isFile()) { 156 sourceFile.setLastModified(date.getTime()); 157 } 158 } 159 } catch (IOException e) { 160 ErrorManager err = ErrorManager.getDefault(); 161 err.annotate(e, NbBundle.getMessage(ImportExecutor.class, "BK3001")); 162 err.notify(e); 163 } 164 } 165 } 166 } 167 168 private static void copyFile(File src, File dst) throws IOException { 169 FileOutputStream fos = new FileOutputStream(dst); 170 FileInputStream fis = new FileInputStream(src); 171 long len = src.length(); 172 assert ((int) len) == len : "Unsupported file size:" + len; copyStream(fos, fis, (int) len); 174 } 175 176 private static void copyStream(OutputStream out, InputStream in, int len) throws IOException { 177 byte [] buffer = new byte[4096]; 178 for (;;) { 179 int n = (len <= 4096) ? len : 4096; 180 n = in.read(buffer, 0, n); 181 if (n < 0) throw new EOFException(); 182 out.write(buffer, 0, n); 183 if ((len -= n) == 0) break; 184 } 185 } 186 187 } 188 | Popular Tags |