KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > upgrade > AutoUpgrade


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.upgrade;
21 import java.io.*;
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Arrays JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.Enumeration JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31 import javax.swing.JOptionPane JavaDoc;
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 JavaDoc;
44
45 /** pending
46  *
47  * @author Jiri Rechtacek
48  */

49 public final class AutoUpgrade {
50
51     public static void main (String JavaDoc[] args) throws Exception JavaDoc {
52         String JavaDoc[] version = new String JavaDoc[1];
53         File JavaDoc 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             //#75324 NBplatform settings are not imported
60
upgradeBuildProperties(sourceFolder, version);
61             //migrates SystemOptions, converts them as a Preferences
62
Importer.doImport();
63         }
64     }
65
66     //#75324 NBplatform settings are not imported
67
private static void upgradeBuildProperties(final File JavaDoc sourceFolder, final String JavaDoc[] version) throws IOException JavaDoc {
68         try {
69             float f = Float.parseFloat(version[0]);
70             if (f >= 5.0) {
71                 File JavaDoc userdir = new File JavaDoc(System.getProperty("netbeans.user", ""));//NOI18N
72
String JavaDoc[] regexForSelection = new String JavaDoc[] {
73                     "^nbplatform[.](?!default[.]netbeans[.]dest[.]dir).+[.].+=.+$"//NOI18N
74
};
75                 Copy.appendSelectedLines(new File JavaDoc(sourceFolder,"build.properties"), //NOI18N
76
userdir,regexForSelection);
77             }
78         } catch(NumberFormatException JavaDoc nex) {
79             return;
80         }
81     }
82     
83     // the order of VERSION_TO_CHECK here defines the precedence of imports
84
// the first one will be choosen for import
85
final static private List JavaDoc VERSION_TO_CHECK = Arrays.asList (new String JavaDoc[] { ".netbeans/5.5",".netbeans/5.0" });
86     
87     static private File JavaDoc checkPrevious (String JavaDoc[] version) {
88         boolean exists;
89         
90         String JavaDoc userHome = System.getProperty ("user.home"); // NOI18N
91
File JavaDoc sourceFolder = null;
92         
93         if (userHome != null) {
94             File JavaDoc userHomeFile = new File JavaDoc (userHome);
95             exists = userHomeFile.isDirectory ();
96
97             Iterator JavaDoc it = VERSION_TO_CHECK.iterator ();
98             String JavaDoc ver;
99             while (it.hasNext () && sourceFolder == null) {
100                 ver = (String JavaDoc) it.next ();
101                 sourceFolder = new File JavaDoc (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 JavaDoc source) {
116         Util.setDefaultLookAndFeel();
117         JOptionPane JavaDoc p = new JOptionPane JavaDoc (
118             new AutoUpgradePanel (source.getAbsolutePath ()),
119             JOptionPane.QUESTION_MESSAGE,
120             JOptionPane.YES_NO_OPTION
121         );
122         javax.swing.JDialog JavaDoc d = p.createDialog (
123             null,
124             NbBundle.getMessage (AutoUpgrade.class, "MSG_Confirmation_Title") // NOI18N
125
);
126         d.setModal (true);
127         d.setVisible (true);
128
129         return new Integer JavaDoc (JOptionPane.YES_OPTION).equals (p.getValue ());
130     }
131     
132     static void doUpgrade (File JavaDoc source, String JavaDoc oldVersion)
133     throws java.io.IOException JavaDoc, java.beans.PropertyVetoException JavaDoc {
134         File JavaDoc userdir = new File JavaDoc(System.getProperty ("netbeans.user", "")); // NOI18N
135

136         java.util.Set JavaDoc includeExclude;
137         try {
138             Reader r = new InputStreamReader (
139                     AutoUpgrade.class.getResourceAsStream ("copy" + oldVersion), // NOI18N
140
"utf-8"); // NOI18N
141
includeExclude = IncludeExclude.create (r);
142             r.close ();
143         } catch (IOException JavaDoc ex) {
144             IOException JavaDoc e = new IOException JavaDoc ("Cannot import from version: " + oldVersion);
145             e.initCause (ex);
146             throw e;
147         }
148
149         ErrorManager.getDefault ().log (
150             ErrorManager.USER, "Import: Old version: " // NOI18N
151
+ oldVersion + ". Importing from " + source + " to " + userdir // NOI18N
152
);
153         
154         File JavaDoc oldConfig = new File JavaDoc (source, "config"); // NOI18N
155
org.openide.filesystems.FileSystem old;
156         {
157             LocalFileSystem lfs = new LocalFileSystem ();
158             lfs.setRootDirectory (oldConfig);
159             
160             XMLFileSystem xmlfs = null;
161             try {
162                 URL JavaDoc url = AutoUpgrade.class.getResource("layer" + oldVersion + ".xml"); // NOI18N
163
xmlfs = new XMLFileSystem(url);
164             } catch (SAXException JavaDoc ex) {
165                 IOException JavaDoc e = new IOException JavaDoc ("Cannot import from version: " + oldVersion); // NOI18N
166
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 JavaDoc<FileObject> getFiles (
193         FileObject folder,
194         int depth,
195         String JavaDoc fileName,
196         String JavaDoc 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 JavaDoc<? extends FileObject> en = folder.getChildren (false);
204         List JavaDoc<FileObject> result = new ArrayList JavaDoc<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 JavaDoc {
215         Enumeration JavaDoc 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 JavaDoc ex) {
227                     if (!fo.getNameExt ().endsWith ("_hidden"))
228                         throw ex;
229                 }
230             }
231         }
232     }
233 }
Popular Tags