|
What this is
Other links
The source code/* * Sun Public License Notice * * The contents of this file are subject to the Sun Public License * Version 1.0 (the "License"). You may not use this file except in * compliance with the License. A copy of the License is available at * http://www.sun.com/ * * The Original Code is NetBeans. The Initial Developer of the Original * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun * Microsystems, Inc. All Rights Reserved. */ package org.netbeans.upgrade; import java.io.*; import java.util.*; import org.openide.util.*; import java.util.jar.*; import org.w3c.dom.*; import org.xml.sax.*; import org.openide.xml.XMLUtil; import org.openide.filesystems.*; import org.openide.filesystems.FileSystem; /** Does copy of objects on filesystems. * * @author Jaroslav Tulach */ final class Copy extends Object { /** Does a selective copy of one source tree to another. * @param source file object to copy from * @param target file object to copy to * @param thoseToCopy set on which contains (relativeNameOfAFileToCopy) * is being called to find out whether to copy or not * @throws IOException if coping fails */ public static void copyDeep (FileObject source, FileObject target, Set thoseToCopy) throws IOException { copyDeep (source, target, thoseToCopy, null); } private static void copyDeep ( FileObject source, FileObject target, Set thoseToCopy, String prefix ) throws IOException { FileObject src = prefix == null ? source : FileUtil.createFolder (source, prefix); FileObject[] arr = src.getChildren(); for (int i = 0; i < arr.length; i++) { String fullname; if (prefix == null) { fullname = arr[i].getNameExt (); } else { fullname = prefix + "/" + arr[i].getNameExt (); } if (arr[i].isData ()) { if (!thoseToCopy.contains (fullname)) { continue; } } if (arr[i].isFolder()) { copyDeep (source, target, thoseToCopy, fullname); if (thoseToCopy.contains (fullname) && arr[i].getAttributes ().hasMoreElements ()) { FileObject tg = FileUtil.createFolder (target, fullname); FileUtil.copyAttributes (arr[i], tg); } } else { FileObject folder = prefix == null ? target : FileUtil.createFolder (target, prefix); FileObject tg = folder.getFileObject (arr[i].getNameExt ()); if (tg == null) { // copy the file otherwise keep old content tg = FileUtil.copyFile (arr[i], folder, arr[i].getName(), arr[i].getExt ()); } FileUtil.copyAttributes (arr[i], tg); } } } /** Updates the IDE. * @param sourceDir original instalation of the IDE * @param targetSystem target system to copy files to * @param backupSystem filesystem to do backupSystemFo to (or null) * @exception IOException if the copying fails * protected final void upgradeIde (String ver, File src, File trg) throws Exception { int version = getIdeVersion (ver); if (version < 0 || version >= versions.length) { message (getString ("MSG_BAD_IDE")); for (int i = 0 ; i < versions.length ; i++ ) { message (versions[i]); } throw new Exception ("Invalid IDE version"); //NOI18N } message (getString ("MSG_UPDATE_FROM", versions[version])); FileSystem srcFS = null; FileSystem trgFS = null; FileSystem tmpFS = null; Object filter [] = null; if (-1 != ver.indexOf (DIRTYPE_INST)) { File srcFile = new File (src, "system"); //NOI18N File trgFile = new File (trg, "system"); //NOI18N srcFS = createFileSystem (srcFile); trgFS = createFileSystem (trgFile); if (srcFS == null) { message (getString ("MSG_directory_not_exist", srcFile.getAbsolutePath ())); //NOI18N throw new Exception ("Directory doesn't exist - " + srcFile.getAbsolutePath ()); //NOI18N } if (trgFS == null) { message (getString ("MSG_directory_not_exist", trgFile.getAbsolutePath ())); throw new Exception ("Directory doesn't exist - " + trgFile.getAbsolutePath ()); //NOI18N } File tmpRoot = new File (trg, "system_backup"); //NOI18N if (!tmpRoot.exists ()) { // message (getString ("MSG_BackupDir_exists", tmpRoot.getAbsolutePath ())); //NOI18N // throw new Exception ("Backup directory already exists - " + tmpRoot.getAbsolutePath ()); //NOI18N // } else { tmpRoot.mkdirs (); } tmpFS = createFileSystem (tmpRoot); filter = originalFiles (nonCpFiles[version]); } else { srcFS = createFileSystem (src); //NOI18N trgFS = createFileSystem (trg); //NOI18N if (srcFS == null) { message (getString ("MSG_directory_not_exist", src.getAbsolutePath ())); //NOI18N throw new Exception ("Directory doesn't exist - " + src.getAbsolutePath ()); //NOI18N } if (trgFS == null) { message (getString ("MSG_directory_not_exist", trg.getAbsolutePath ())); //NOI18N throw new Exception ("Directory doesn't exist - " + trg.getAbsolutePath ()); //NOI18N } File tmpRoot = new File (trg.getParentFile (), "userdir_backup"); //NOI18N if (!tmpRoot.exists ()) { // message (getString ("MSG_BackupDir_exists", tmpRoot.getAbsolutePath ())); //NOI18N // throw new Exception ("Backup directory already exists - " + tmpRoot.getAbsolutePath ()); //NOI18N // } else { tmpRoot.mkdirs (); } tmpFS = createFileSystem (tmpRoot); filter = originalFiles (userdirNonCpFiles); } if (tmpFS != null) { // clean up temporary filesystem FileObject ch [] = tmpFS.getRoot ().getChildren (); for (int i = 0; i < ch.length; i++) { deleteAll (ch[i]); } // make a backup copy copyAttributes(trgFS.getRoot (), tmpFS.getRoot ()); recursiveCopy(trgFS.getRoot (), tmpFS.getRoot ()); } try { update (srcFS, trgFS, getLastModified (src), filter); } catch (Exception e) { if (tmpFS != null) { message (getString ("MSG_recovery_started")); //NOI18N deleteAll (trgFS.getRoot ()); copyAttributes (tmpFS.getRoot (), trgFS.getRoot ()); recursiveCopy (tmpFS.getRoot (), trgFS.getRoot ()); message (getString ("MSG_recovery_finished")); //NOI18N } throw e; } } private FileSystem createFileSystem (File root) { LocalFileSystem lfs = null; if (root.exists () && root.isDirectory ()) { try { lfs = new LocalFileSystem (); lfs.setRootDirectory (root); } catch (Exception e) { lfs = null; } } return lfs == null ? null : new AttrslessLocalFileSystem (lfs); } private void update( FileSystem src, FileSystem trg, long sourceBaseTime, Object[] filter ) throws IOException { items = 0; maxItems = 0; copyAttributes (src.getRoot (),trg.getRoot ()); recursiveCopyWithFilter ( src.getRoot (), trg.getRoot (), filter, sourceBaseTime ); } /** copies recursively directory, skips files existing in target location * @param source source directory * @param dest destination directory */ private void recursiveCopy (FileObject sourceFolder, FileObject destFolder) throws IOException { FileObject childrens [] = sourceFolder.getChildren(); for (int i = 0 ; i < childrens.length ; i++ ) { final FileObject subSourceFo = childrens[i]; FileObject subTargetFo = null; if (subSourceFo.isFolder()) { subTargetFo = destFolder.getFileObject(subSourceFo.getName()); if (subTargetFo == null) { subTargetFo = destFolder.createFolder(subSourceFo.getName()); } copyAttributes(subSourceFo,subTargetFo); recursiveCopy(subSourceFo,subTargetFo); } else { subTargetFo = destFolder.getFileObject(subSourceFo.getNameExt()); if (subTargetFo == null) { if ( Utilities.getOperatingSystem () == Utilities.OS_VMS && subSourceFo.getNameExt ().equalsIgnoreCase ( "_nbattrs.") ) subTargetFo = FileUtil.copyFile(subSourceFo, destFolder, subSourceFo.getNameExt(), subSourceFo.getExt()); else subTargetFo = FileUtil.copyFile(subSourceFo, destFolder, subSourceFo.getName(), subSourceFo.getExt()); } copyAttributes(subSourceFo,subTargetFo); } } } private void message (String s) { } private void progress (int x, int y) { } private int maxItems; private int items; private int timeDev; /** Copies recursively dircectory. Files are copied when when basicTime + timeDev < time of file. * @param source source directory * @param #dest destination dirctory */ private void recursiveCopyWithFilter ( FileObject source, FileObject dest, Object[] filter, long basicTime ) throws IOException { FileObject childrens [] = source.getChildren(); if (source.isFolder() == false ) { message (getString("MSG_IS_NOT_FOLDER", source.getName())); } // adjust max number of items maxItems += childrens.length; for (int i = 0 ; i < childrens.length ; i++ ) { FileObject subSourceFo = childrens[i]; // report progress items++; progress(items, maxItems); if (!canCopy (subSourceFo, filter, basicTime)) continue; FileObject subTargetFo = null; if (subSourceFo.isFolder ()) { subTargetFo = dest.getFileObject (subSourceFo.getNameExt ()); if (subTargetFo == null) { subTargetFo = dest.createFolder (subSourceFo.getNameExt ()); } copyAttributes (subSourceFo, subTargetFo); recursiveCopyWithFilter (subSourceFo, subTargetFo, filter, basicTime); } else { subTargetFo = dest.getFileObject (subSourceFo.getName (), subSourceFo.getExt ()); if (subTargetFo != null) { FileLock lock = subTargetFo.lock (); subTargetFo.delete (lock); lock.releaseLock (); } if ( Utilities.getOperatingSystem () == Utilities.OS_VMS && subSourceFo.getNameExt ().equalsIgnoreCase ( "_nbattrs.") ) subTargetFo = copyFile (subSourceFo, dest, subSourceFo.getNameExt ()); else subTargetFo = copyFile (subSourceFo, dest, subSourceFo.getName ()); copyAttributes (subSourceFo, subTargetFo); } } } private FileObject copyFile (FileObject src, FileObject trg, String newName) throws IOException { return FileUtil.copyFile (src, trg, newName); } private static void copyAttributes (FileObject source, FileObject dest) throws IOException { Enumeration attrKeys = source.getAttributes(); while (attrKeys.hasMoreElements()) { String key = (String) attrKeys.nextElement(); Object value = source.getAttribute(key); if (value != null) { dest.setAttribute(key, value); } } } /** test if file can be copied */ private boolean canCopy (FileObject fo, Object[] filter, long basicTime) throws IOException { String nonCopiedFiles [] = (String []) filter [0]; String wildcards [] = (String []) filter [1]; String name = fo.getPath(); if (fo.isFolder ()) { return Arrays.binarySearch (nonCopiedFiles, name + "/*") < 0; //NOI18N } for (int i = 0; i < wildcards.length; i++) { if (name.endsWith (wildcards [i])) { return false; } } long time = fo.lastModified().getTime(); boolean canCopy = Arrays.binarySearch (nonCopiedFiles, name) < 0 && basicTime + timeDev <= time; if (!canCopy) { return false; } // #31623 - the fastjavac settings should not be imported. // In NB3.5 the fastjavac was separated into its own module. // Its old settings (bounded to java module) must not be imported. // For fastjavac settings created by NB3.5 this will work, because they // will be bound to new "org.netbeans.modules.java.fastjavac" module. if (fo.getExt().equals("settings")) { //NOI18N boolean tag1 = false; boolean tag2 = false; BufferedReader reader = null; try { reader = new BufferedReader(new InputStreamReader(fo.getInputStream())); String line; while (null != (line = reader.readLine())) { if (line.indexOf(" |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.