1 19 20 package org.netbeans.upgrade; 21 import java.io.*; 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.net.URL ; 25 import java.util.ArrayList ; 26 import java.util.Arrays ; 27 import java.util.Collections ; 28 import java.util.Enumeration ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 import javax.swing.JOptionPane ; 32 import org.netbeans.upgrade.systemoptions.Importer; 33 34 import org.netbeans.util.Util; 35 import org.openide.ErrorManager; 36 import org.openide.filesystems.FileObject; 37 import org.openide.filesystems.FileUtil; 38 import org.openide.filesystems.LocalFileSystem; 39 import org.openide.filesystems.MultiFileSystem; 40 import org.openide.filesystems.Repository; 41 import org.openide.filesystems.XMLFileSystem; 42 import org.openide.util.NbBundle; 43 import org.xml.sax.SAXException ; 44 45 49 public final class AutoUpgrade { 50 51 public static void main (String [] args) throws Exception { 52 String [] version = new String [1]; 53 File sourceFolder = checkPrevious (version); 54 if (sourceFolder != null) { 55 if (!showUpgradeDialog (sourceFolder)) { 56 throw new org.openide.util.UserCancelException (); 57 } 58 doUpgrade (sourceFolder, version[0]); 59 upgradeBuildProperties(sourceFolder, version); 61 Importer.doImport(); 63 } 64 } 65 66 private static void upgradeBuildProperties(final File sourceFolder, final String [] version) throws IOException { 68 try { 69 float f = Float.parseFloat(version[0]); 70 if (f >= 5.0) { 71 File userdir = new File (System.getProperty("netbeans.user", "")); String [] regexForSelection = new String [] { 73 "^nbplatform[.](?!default[.]netbeans[.]dest[.]dir).+[.].+=.+$" }; 75 Copy.appendSelectedLines(new File (sourceFolder,"build.properties"), userdir,regexForSelection); 77 } 78 } catch(NumberFormatException nex) { 79 return; 80 } 81 } 82 83 final static private List VERSION_TO_CHECK = Arrays.asList (new String [] { ".netbeans/5.5",".netbeans/5.0" }); 86 87 static private File checkPrevious (String [] version) { 88 boolean exists; 89 90 String userHome = System.getProperty ("user.home"); File sourceFolder = null; 92 93 if (userHome != null) { 94 File userHomeFile = new File (userHome); 95 exists = userHomeFile.isDirectory (); 96 97 Iterator it = VERSION_TO_CHECK.iterator (); 98 String ver; 99 while (it.hasNext () && sourceFolder == null) { 100 ver = (String ) it.next (); 101 sourceFolder = new File (userHomeFile.getAbsolutePath (), ver); 102 103 if (sourceFolder.isDirectory ()) { 104 version[0] = sourceFolder.getName(); 105 break; 106 } 107 sourceFolder = null; 108 } 109 return sourceFolder; 110 } else { 111 return null; 112 } 113 } 114 115 private static boolean showUpgradeDialog (final File source) { 116 Util.setDefaultLookAndFeel(); 117 JOptionPane p = new JOptionPane ( 118 new AutoUpgradePanel (source.getAbsolutePath ()), 119 JOptionPane.QUESTION_MESSAGE, 120 JOptionPane.YES_NO_OPTION 121 ); 122 javax.swing.JDialog d = p.createDialog ( 123 null, 124 NbBundle.getMessage (AutoUpgrade.class, "MSG_Confirmation_Title") ); 126 d.setModal (true); 127 d.setVisible (true); 128 129 return new Integer (JOptionPane.YES_OPTION).equals (p.getValue ()); 130 } 131 132 static void doUpgrade (File source, String oldVersion) 133 throws java.io.IOException , java.beans.PropertyVetoException { 134 File userdir = new File (System.getProperty ("netbeans.user", "")); 136 java.util.Set includeExclude; 137 try { 138 Reader r = new InputStreamReader ( 139 AutoUpgrade.class.getResourceAsStream ("copy" + oldVersion), "utf-8"); includeExclude = IncludeExclude.create (r); 142 r.close (); 143 } catch (IOException ex) { 144 IOException e = new IOException ("Cannot import from version: " + oldVersion); 145 e.initCause (ex); 146 throw e; 147 } 148 149 ErrorManager.getDefault ().log ( 150 ErrorManager.USER, "Import: Old version: " + oldVersion + ". Importing from " + source + " to " + userdir ); 153 154 File oldConfig = new File (source, "config"); org.openide.filesystems.FileSystem old; 156 { 157 LocalFileSystem lfs = new LocalFileSystem (); 158 lfs.setRootDirectory (oldConfig); 159 160 XMLFileSystem xmlfs = null; 161 try { 162 URL url = AutoUpgrade.class.getResource("layer" + oldVersion + ".xml"); xmlfs = new XMLFileSystem(url); 164 } catch (SAXException ex) { 165 IOException e = new IOException ("Cannot import from version: " + oldVersion); e.initCause (ex); 167 throw e; 168 } 169 170 old = createLayeredSystem(lfs, xmlfs); 171 } 172 org.openide.filesystems.FileSystem mine = Repository.getDefault (). 173 getDefaultFileSystem (); 174 175 Copy.copyDeep (old.getRoot (), mine.getRoot (), includeExclude); 176 177 } 178 179 static MultiFileSystem createLayeredSystem(final LocalFileSystem lfs, final XMLFileSystem xmlfs) { 180 MultiFileSystem old; 181 182 old = new MultiFileSystem ( 183 new org.openide.filesystems.FileSystem[] { lfs, xmlfs } 184 ) { 185 { 186 setPropagateMasks(true); 187 } 188 }; 189 return old; 190 } 191 192 private static List <FileObject> getFiles ( 193 FileObject folder, 194 int depth, 195 String fileName, 196 String extension 197 ) { 198 if (depth == 0) { 199 FileObject result = folder.getFileObject (fileName, extension); 200 if (result == null) return Collections.emptyList(); 201 return Collections.singletonList (result); 202 } 203 Enumeration <? extends FileObject> en = folder.getChildren (false); 204 List <FileObject> result = new ArrayList <FileObject> (); 205 while (en.hasMoreElements ()) { 206 FileObject fo = en.nextElement (); 207 if (!fo.isFolder ()) continue; 208 result.addAll (getFiles (fo, depth - 1, fileName, extension)); 209 } 210 return result; 211 } 212 213 private static void copy (FileObject sourceDir, FileObject destDir) 214 throws IOException { 215 Enumeration en = sourceDir.getData (false); 216 while (en.hasMoreElements ()) { 217 FileObject fo = (FileObject) en.nextElement (); 218 if (fo.isFolder ()) { 219 FileObject newDestDir = destDir.createFolder (fo.getName ()); 220 copy (fo, newDestDir); 221 } else { 222 try { 223 FileObject destFile = FileUtil.copyFile 224 (fo, destDir, fo.getName (), fo.getExt ()); 225 FileUtil.copyAttributes (fo, destFile); 226 } catch (IOException ex) { 227 if (!fo.getNameExt ().endsWith ("_hidden")) 228 throw ex; 229 } 230 } 231 } 232 } 233 } | Popular Tags |