KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > versioning > system > cvss > ui > actions > project > ImportExecutor


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

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 JavaDoc;
40 import java.util.Date JavaDoc;
41
42 /**
43  * Simple import command ExecutorSupport subclass.
44  *
45  * @author Petr Kuzel
46  */

47 final class ImportExecutor extends ExecutorSupport implements Runnable JavaDoc {
48
49     private final String JavaDoc module;
50     private final String JavaDoc cvsRoot;
51     private final boolean checkout;
52     private final String JavaDoc folder;
53     private CheckoutExecutor checkoutExecutor;
54     private File checkoutDir;
55     private final ExecutorGroup group;
56
57     /**
58      * Creates new executor that on succesfull import
59      * launches post checkout.
60      *
61      * @param cmd
62      * @param options
63      * @param checkout perform initial checkout
64      * @param folder structure that is imported
65      * @param group that this executor joins
66      */

67     public ImportExecutor(ImportCommand cmd, GlobalOptions options, boolean checkout, String JavaDoc 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     // TODO detect conflics
88
// test for "No conflicts created by this import"
89

90     /**
91      * afrer checkout barrier implementation
92      * @thread called asynchrously from random thread (ClientRuntime)
93      */

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); // checkout creates new subdir
105

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())) { // NOI18N
120
copyCvsMeta(file, dest);
121                 } else {
122                     File destDir = new File(dest, file.getName());
123                     if (destDir.isDirectory()) {
124                         copyFolderMeta(file, destDir); // RESURSION
125
}
126                 }
127             }
128         }
129     }
130
131     private void copyCvsMeta(File src, File dest) {
132         File destCvsDir = new File(dest, "CVS"); // NOI18N
133
if (destCvsDir.exists() == false || (destCvsDir.isDirectory() && destCvsDir.listFiles().length == 0) ) {
134             destCvsDir.mkdirs();
135             if (destCvsDir.isDirectory()) {
136                 // be on safe side copy only Root, Entries, Repository
137
try {
138                     File root = new File(src, "Root"); // NOI18N
139
copyFile(root, new File(destCvsDir, "Root")); // NOI18N
140
File repository = new File(src, "Repository"); // NOI18N
141
copyFile(repository, new File(destCvsDir, "Repository")); // NOI18N
142
File entries = new File(src, "Entries"); // NOI18N
143
copyFile(entries, new File(destCvsDir, "Entries")); // NOI18N
144

145                     // set file timestamps according to entries
146
StandardAdminHandler parser = new StandardAdminHandler();
147                     Iterator JavaDoc it = parser.getEntries(dest);
148                     while (it.hasNext()) {
149                         Entry entry = (Entry) it.next();
150                         String JavaDoc name = entry.getName();
151                         // TODO GMT conversions to local
152
Date JavaDoc 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; // NOI18N
173
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