KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > bluej > upgrade > Copy


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.bluej.upgrade;
21
22 import java.io.*;
23 import java.util.*;
24 import org.openide.util.*;
25 import java.util.jar.*;
26 import org.w3c.dom.*;
27 import org.xml.sax.*;
28 import org.openide.xml.XMLUtil;
29
30 import org.openide.filesystems.*;
31 import org.openide.filesystems.FileSystem;
32
33
34 /** Does copy of objects on filesystems.
35  *
36  * @author Jaroslav Tulach
37  */

38 final class Copy extends Object JavaDoc {
39
40
41     /** Does a selective copy of one source tree to another.
42      * @param source file object to copy from
43      * @param target file object to copy to
44      * @param thoseToCopy set on which contains (relativeNameOfAFileToCopy)
45      * is being called to find out whether to copy or not
46      * @throws IOException if coping fails
47      */

48     public static void copyDeep (FileObject source, FileObject target, Set thoseToCopy)
49     throws IOException {
50         copyDeep (source, target, thoseToCopy, null);
51     }
52     
53     private static void copyDeep (
54         FileObject source, FileObject target, Set thoseToCopy, String JavaDoc prefix
55     ) throws IOException {
56         FileObject src = prefix == null ? source : FileUtil.createFolder (source, prefix);
57         
58         FileObject[] arr = src.getChildren();
59         for (int i = 0; i < arr.length; i++) {
60             String JavaDoc fullname;
61             if (prefix == null) {
62                 fullname = arr[i].getNameExt ();
63             } else {
64                 fullname = prefix + "/" + arr[i].getNameExt ();
65             }
66             if (arr[i].isData ()) {
67                 if (!thoseToCopy.contains (fullname)) {
68                     continue;
69                 }
70             }
71
72             
73             if (arr[i].isFolder()) {
74                 copyDeep (source, target, thoseToCopy, fullname);
75                 if (thoseToCopy.contains (fullname) && arr[i].getAttributes ().hasMoreElements ()) {
76                     FileObject tg = FileUtil.createFolder (target, fullname);
77                     FileUtil.copyAttributes (arr[i], tg);
78                 }
79             } else {
80                 FileObject folder = prefix == null ? target : FileUtil.createFolder (target, prefix);
81                 FileObject tg = folder.getFileObject (arr[i].getNameExt ());
82                 try {
83                     if (tg == null) {
84                         // copy the file otherwise keep old content
85
tg = FileUtil.copyFile (arr[i], folder, arr[i].getName(), arr[i].getExt ());
86                     }
87                 } catch (IOException ex) {
88                     if (arr[i].getNameExt().endsWith("_hidden")) {
89                         continue;
90                     }
91                     throw ex;
92                 }
93                 FileUtil.copyAttributes (arr[i], tg);
94             }
95         }
96         
97         
98     }
99     
100     
101
102     /** Updates the IDE.
103      * @param sourceDir original instalation of the IDE
104      * @param targetSystem target system to copy files to
105      * @param backupSystem filesystem to do backupSystemFo to (or null)
106      * @exception IOException if the copying fails
107      *
108     protected final void upgradeIde (String ver, File src, File trg) throws Exception {
109         
110         
111         int version = getIdeVersion (ver);
112         if (version < 0 || version >= versions.length) {
113             message (getString ("MSG_BAD_IDE"));
114             for (int i = 0 ; i < versions.length ; i++ ) {
115                 message (versions[i]);
116             }
117             throw new Exception ("Invalid IDE version"); //NOI18N
118         }
119
120         message (getString ("MSG_UPDATE_FROM", versions[version]));
121
122         FileSystem srcFS = null;
123         FileSystem trgFS = null;
124         FileSystem tmpFS = null;
125         Object filter [] = null;
126
127         if (-1 != ver.indexOf (DIRTYPE_INST)) {
128             File srcFile = new File (src, "system"); //NOI18N
129             File trgFile = new File (trg, "system"); //NOI18N
130             srcFS = createFileSystem (srcFile);
131             trgFS = createFileSystem (trgFile);
132
133             if (srcFS == null) {
134                 message (getString ("MSG_directory_not_exist", srcFile.getAbsolutePath ())); //NOI18N
135                 throw new Exception ("Directory doesn't exist - " + srcFile.getAbsolutePath ()); //NOI18N
136             }
137
138             if (trgFS == null) {
139                 message (getString ("MSG_directory_not_exist", trgFile.getAbsolutePath ()));
140                 throw new Exception ("Directory doesn't exist - " + trgFile.getAbsolutePath ()); //NOI18N
141             }
142
143             File tmpRoot = new File (trg, "system_backup"); //NOI18N
144             if (!tmpRoot.exists ()) {
145 // message (getString ("MSG_BackupDir_exists", tmpRoot.getAbsolutePath ())); //NOI18N
146 // throw new Exception ("Backup directory already exists - " + tmpRoot.getAbsolutePath ()); //NOI18N
147 // } else {
148                 tmpRoot.mkdirs ();
149             }
150             tmpFS = createFileSystem (tmpRoot);
151
152             filter = originalFiles (nonCpFiles[version]);
153         } else {
154             srcFS = createFileSystem (src); //NOI18N
155             trgFS = createFileSystem (trg); //NOI18N
156
157             if (srcFS == null) {
158                 message (getString ("MSG_directory_not_exist", src.getAbsolutePath ())); //NOI18N
159                 throw new Exception ("Directory doesn't exist - " + src.getAbsolutePath ()); //NOI18N
160             }
161
162             if (trgFS == null) {
163                 message (getString ("MSG_directory_not_exist", trg.getAbsolutePath ())); //NOI18N
164                 throw new Exception ("Directory doesn't exist - " + trg.getAbsolutePath ()); //NOI18N
165             }
166
167             File tmpRoot = new File (trg.getParentFile (), "userdir_backup"); //NOI18N
168             if (!tmpRoot.exists ()) {
169 // message (getString ("MSG_BackupDir_exists", tmpRoot.getAbsolutePath ())); //NOI18N
170 // throw new Exception ("Backup directory already exists - " + tmpRoot.getAbsolutePath ()); //NOI18N
171 // } else {
172                 tmpRoot.mkdirs ();
173             }
174             tmpFS = createFileSystem (tmpRoot);
175
176             filter = originalFiles (userdirNonCpFiles);
177         }
178
179         if (tmpFS != null) {
180             // clean up temporary filesystem
181             FileObject ch [] = tmpFS.getRoot ().getChildren ();
182             for (int i = 0; i < ch.length; i++) {
183                 deleteAll (ch[i]);
184             }
185             // make a backup copy
186             copyAttributes(trgFS.getRoot (), tmpFS.getRoot ());
187             recursiveCopy(trgFS.getRoot (), tmpFS.getRoot ());
188         }
189         
190         try {
191             update (srcFS, trgFS, getLastModified (src), filter);
192         }
193         catch (Exception e) {
194             if (tmpFS != null) {
195                 message (getString ("MSG_recovery_started")); //NOI18N
196                 deleteAll (trgFS.getRoot ());
197                 copyAttributes (tmpFS.getRoot (), trgFS.getRoot ());
198                 recursiveCopy (tmpFS.getRoot (), trgFS.getRoot ());
199                 message (getString ("MSG_recovery_finished")); //NOI18N
200             }
201             throw e;
202         }
203     }
204     
205     private FileSystem createFileSystem (File root) {
206         LocalFileSystem lfs = null;
207
208         if (root.exists () && root.isDirectory ()) {
209             try {
210                 lfs = new LocalFileSystem ();
211                 lfs.setRootDirectory (root);
212             }
213             catch (Exception e) {
214                 lfs = null;
215             }
216         }
217   
218         return lfs == null ? null : new AttrslessLocalFileSystem (lfs);
219     }
220
221     private void update(
222         FileSystem src, FileSystem trg, long sourceBaseTime, Object[] filter
223     ) throws IOException {
224
225         items = 0;
226         maxItems = 0;
227
228         copyAttributes (src.getRoot (),trg.getRoot ());
229         recursiveCopyWithFilter (
230             src.getRoot (),
231             trg.getRoot (),
232             filter,
233             sourceBaseTime
234         );
235     }
236     
237     /** copies recursively directory, skips files existing in target location
238      * @param source source directory
239      * @param dest destination directory
240      */

241     private void recursiveCopy (FileObject sourceFolder, FileObject destFolder) throws IOException {
242         FileObject childrens [] = sourceFolder.getChildren();
243         for (int i = 0 ; i < childrens.length ; i++ ) {
244             final FileObject subSourceFo = childrens[i];
245             FileObject subTargetFo = null;
246             
247             if (subSourceFo.isFolder()) {
248                 subTargetFo = destFolder.getFileObject(subSourceFo.getName());
249                 if (subTargetFo == null) {
250                     subTargetFo = destFolder.createFolder(subSourceFo.getName());
251                     
252                 }
253                 copyAttributes(subSourceFo,subTargetFo);
254                 recursiveCopy(subSourceFo,subTargetFo);
255             } else {
256                 subTargetFo = destFolder.getFileObject(subSourceFo.getNameExt());
257                 if (subTargetFo == null) {
258                      if ( Utilities.getOperatingSystem () == Utilities.OS_VMS
259                         && subSourceFo.getNameExt ().equalsIgnoreCase ( "_nbattrs.") )
260                         subTargetFo = FileUtil.copyFile(subSourceFo, destFolder, subSourceFo.getNameExt(), subSourceFo.getExt());
261                      else
262                         subTargetFo = FileUtil.copyFile(subSourceFo, destFolder, subSourceFo.getName(), subSourceFo.getExt());
263                 }
264                 copyAttributes(subSourceFo,subTargetFo);
265             }
266         }
267     }
268
269     private void message (String JavaDoc s) {
270         
271     }
272     private void progress (int x, int y) {
273         
274     }
275     private int maxItems;
276     private int items;
277     private int timeDev;
278     
279     /** Copies recursively dircectory. Files are copied when when basicTime + timeDev < time of file.
280      * @param source source directory
281      * @param #dest destination dirctory
282      */

283     private void recursiveCopyWithFilter (
284         FileObject source, FileObject dest, Object JavaDoc[] filter, long basicTime
285     ) throws IOException {
286         FileObject childrens [] = source.getChildren();
287         if (source.isFolder() == false ) {
288             message (getString("MSG_IS_NOT_FOLDER", source.getName()));
289         }
290
291         // adjust max number of items
292
maxItems += childrens.length;
293
294         for (int i = 0 ; i < childrens.length ; i++ ) {
295             FileObject subSourceFo = childrens[i];
296
297             // report progress
298
items++;
299             progress(items, maxItems);
300
301             if (!canCopy (subSourceFo, filter, basicTime))
302                 continue;
303             
304             FileObject subTargetFo = null;
305             if (subSourceFo.isFolder ()) {
306                 subTargetFo = dest.getFileObject (subSourceFo.getNameExt ());
307                 if (subTargetFo == null) {
308                     subTargetFo = dest.createFolder (subSourceFo.getNameExt ());
309                     
310                 }
311                 copyAttributes (subSourceFo, subTargetFo);
312                 recursiveCopyWithFilter (subSourceFo, subTargetFo, filter, basicTime);
313             } else {
314                 subTargetFo = dest.getFileObject (subSourceFo.getName (), subSourceFo.getExt ());
315                
316                 if (subTargetFo != null) {
317                     FileLock lock = subTargetFo.lock ();
318                     subTargetFo.delete (lock);
319                     lock.releaseLock ();
320                 }
321                 
322                 if ( Utilities.getOperatingSystem () == Utilities.OS_VMS
323                     && subSourceFo.getNameExt ().equalsIgnoreCase ( "_nbattrs.") )
324                     subTargetFo = copyFile (subSourceFo, dest, subSourceFo.getNameExt ());
325                 else
326                     subTargetFo = copyFile (subSourceFo, dest, subSourceFo.getName ());
327                 copyAttributes (subSourceFo, subTargetFo);
328             }
329         }
330     }
331
332     private FileObject copyFile (FileObject src, FileObject trg, String JavaDoc newName) throws IOException {
333         return FileUtil.copyFile (src, trg, newName);
334     }
335         
336     private static void copyAttributes (FileObject source, FileObject dest) throws IOException {
337         Enumeration attrKeys = source.getAttributes();
338         while (attrKeys.hasMoreElements()) {
339             String JavaDoc key = (String JavaDoc) attrKeys.nextElement();
340             Object JavaDoc value = source.getAttribute(key);
341             if (value != null) {
342                 dest.setAttribute(key, value);
343             }
344         }
345     }
346     /** test if file can be copied
347      */

348     private boolean canCopy (FileObject fo, Object JavaDoc[] filter, long basicTime) throws IOException {
349         String JavaDoc nonCopiedFiles [] = (String JavaDoc []) filter [0];
350         String JavaDoc wildcards [] = (String JavaDoc []) filter [1];
351         String JavaDoc name = fo.getPath();
352         
353         if (fo.isFolder ()) {
354             return Arrays.binarySearch (nonCopiedFiles, name + "/*") < 0; //NOI18N
355
}
356
357         for (int i = 0; i < wildcards.length; i++) {
358             if (name.endsWith (wildcards [i])) {
359                 return false;
360             }
361         }
362
363         long time = fo.lastModified().getTime();
364         
365         boolean canCopy = Arrays.binarySearch (nonCopiedFiles, name) < 0 &&
366                basicTime + timeDev <= time;
367         if (!canCopy) {
368             return false;
369         }
370         
371         // #31623 - the fastjavac settings should not be imported.
372
// In NB3.5 the fastjavac was separated into its own module.
373
// Its old settings (bounded to java module) must not be imported.
374
// For fastjavac settings created by NB3.5 this will work, because they
375
// will be bound to new "org.netbeans.modules.java.fastjavac" module.
376
if (fo.getExt().equals("settings")) { //NOI18N
377
boolean tag1 = false;
378             boolean tag2 = false;
379             BufferedReader reader = null;
380             try {
381                 reader = new BufferedReader(new InputStreamReader(fo.getInputStream()));
382                 String JavaDoc line;
383                 while (null != (line = reader.readLine())) {
384                     if (line.indexOf("<module name=") != -1) { //NOI18N
385
if (line.indexOf("<module name=\"org.netbeans.modules.java/1\"") != -1) { //NOI18N
386
tag1 = true; // it is java module setting
387
} else {
388                             break; // some other setting, ignore this file
389
}
390                     }
391                     if (line.indexOf("<serialdata class=") != -1) { //NOI18N
392
if (line.indexOf("<serialdata class=\"org.netbeans.modules.java.FastJavacCompilerType\">") != -1) { //NOI18N
393
tag2 = true; // it is fastjavac setting
394
if (tag1) {
395                                 break;
396                             }
397                         } else {
398                             break; // some other setting, ignore this file
399
}
400                     }
401                 }
402             } catch (IOException ex) {
403                 // ignore this problem.
404
// in worst case the fastjavac settings will be copied.
405
} finally {
406                 if (reader != null) {
407                     reader.close();
408                 }
409             }
410             if (tag1 && tag2) {
411                 return false; // ignore this file. it is fastjavac settings
412
}
413         }
414         
415         return true;
416     }
417     // ************************* version retrieving code ********************
418

419     
420     /** We support import just from release 3.6
421      * @param dir user dir to check for version
422      * @return either null or name of the version
423      */

424     public static String JavaDoc getIdeVersion (File dir) {
425         String JavaDoc version = null;
426         String JavaDoc dirType = null;
427         String JavaDoc branding = null;
428         
429         if (new File (dir, "system").exists ()) {
430             return "3.6";
431         }
432         return null;
433     }
434
435     // ************** strings from bundle ***************
436

437     protected static String JavaDoc getString (String JavaDoc key) {
438         return NbBundle.getMessage (Copy.class, key);
439     }
440
441     protected static String JavaDoc getString (String JavaDoc key,String JavaDoc param) {
442         return NbBundle.getMessage(Copy.class,key,param);
443     }
444     
445     private static class AttrslessLocalFileSystem extends AbstractFileSystem implements AbstractFileSystem.Attr {
446         public AttrslessLocalFileSystem (LocalFileSystem fs) {
447             super ();
448             this.change = new LocalFileSystem.Impl (fs);
449             this.info = (AbstractFileSystem.Info) this.change;
450             this.list = (AbstractFileSystem.List) this.change;
451             this.attr = this;
452         }
453         public boolean isReadOnly () {
454             return false;
455         }
456         public String JavaDoc getDisplayName () {
457             return getClass ().toString (); // this will never be shown to user
458
}
459
460         // ***** no-op implementation of AbstractFileSystem.Attr *****
461

462         public void deleteAttributes (String JavaDoc name) {
463         }
464         public Enumeration attributes (String JavaDoc name) {
465             return org.openide.util.Enumerations.empty ();
466         }
467         public void renameAttributes (String JavaDoc oldName, String JavaDoc newName) {
468         }
469         public void writeAttribute (String JavaDoc name, String JavaDoc attrName, Object JavaDoc value) throws IOException {
470         }
471         public Object JavaDoc readAttribute (String JavaDoc name, String JavaDoc attrName) {
472             return null;
473         }
474     }
475 }
476
Popular Tags