001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.upgrade;
043:
044: import java.beans.PropertyVetoException;
045: import java.io.*;
046: import java.io.File;
047: import java.io.IOException;
048: import java.net.URL;
049: import java.util.ArrayList;
050: import java.util.Arrays;
051: import java.util.Collections;
052: import java.util.Enumeration;
053: import java.util.Iterator;
054: import java.util.List;
055: import javax.swing.JOptionPane;
056: import org.netbeans.upgrade.systemoptions.Importer;
057:
058: import org.netbeans.util.Util;
059: import org.openide.ErrorManager;
060: import org.openide.filesystems.FileObject;
061: import org.openide.filesystems.FileUtil;
062: import org.openide.filesystems.LocalFileSystem;
063: import org.openide.filesystems.MultiFileSystem;
064: import org.openide.filesystems.Repository;
065: import org.openide.filesystems.XMLFileSystem;
066: import org.openide.modules.InstalledFileLocator;
067: import org.openide.util.NbBundle;
068: import org.xml.sax.SAXException;
069:
070: /** pending
071: *
072: * @author Jiri Rechtacek
073: */
074: public final class AutoUpgrade {
075:
076: public static void main(String[] args) throws Exception {
077: String[] version = new String[1];
078: File sourceFolder = checkPrevious(version, VERSION_TO_CHECK);
079: if (sourceFolder != null) {
080: if (!showUpgradeDialog(sourceFolder)) {
081: throw new org.openide.util.UserCancelException();
082: }
083: doUpgrade(sourceFolder, version[0]);
084: //support for non standard configuration files
085: doNonStandardUpgrade(sourceFolder, version[0]);
086: //#75324 NBplatform settings are not imported
087: upgradeBuildProperties(sourceFolder, version);
088: //migrates SystemOptions, converts them as a Preferences
089: Importer.doImport();
090: }
091: }
092:
093: //#75324 NBplatform settings are not imported
094: private static void upgradeBuildProperties(final File sourceFolder,
095: final String[] version) throws IOException {
096: try {
097: //TODO: review and implement less version specific
098: if (version[0].startsWith("2_")) {//CREATOR
099: File userdir = new File(System.getProperty(
100: "netbeans.user", ""));//NOI18N
101: Copy.appendSelectedLines(new File(sourceFolder,
102: "build.properties"), //NOI18N
103: userdir, new String[] { ".*" });
104: } else if (Float.parseFloat(version[0]) >= 5.0) {//NOI18N
105: File userdir = new File(System.getProperty(
106: "netbeans.user", ""));//NOI18N
107: String[] regexForSelection = new String[] { "^nbplatform[.](?!default[.]netbeans[.]dest[.]dir).+[.].+=.+$"//NOI18N
108: };
109: Copy.appendSelectedLines(new File(sourceFolder,
110: "build.properties"), //NOI18N
111: userdir, regexForSelection);
112: }
113: } catch (NumberFormatException nex) {
114: return;
115: }
116: }
117:
118: private static final String CREATOR = ".Creator/2_1"; //NOI18N
119: private static final String VISUALWEB_REPRESENTATION = "modules/org-netbeans-modules-visualweb-insync.jar";//NOI18N
120:
121: // the order of VERSION_TO_CHECK here defines the precedence of imports
122: // the first one will be choosen for import
123: final static private List VERSION_TO_CHECK = Arrays
124: .asList(new String[] { ".netbeans/6.0", ".netbeans/5.5.1",
125: ".netbeans/5.5", ".netbeans/5.0", CREATOR });//NOI18N
126:
127: static private File checkPrevious(String[] version,
128: final List versionsToCheck) {
129: String userHome = System.getProperty("user.home"); // NOI18N
130: File sourceFolder = null;
131:
132: if (userHome != null) {
133: File userHomeFile = new File(userHome);
134: Iterator it = versionsToCheck.iterator();
135: String ver;
136: while (it.hasNext() && sourceFolder == null) {
137: ver = (String) it.next();
138: if (ver.equals(CREATOR)) {//NOI18N
139: final boolean visualWebPresent = InstalledFileLocator
140: .getDefault().locate(
141: VISUALWEB_REPRESENTATION, null,
142: false) != null;
143: if (!visualWebPresent) {
144: continue;
145: }
146: }
147: sourceFolder = new File(userHomeFile.getAbsolutePath(),
148: ver);
149:
150: if (sourceFolder.isDirectory()) {
151: version[0] = sourceFolder.getName();
152: break;
153: }
154: sourceFolder = null;
155: }
156: return sourceFolder;
157: } else {
158: return null;
159: }
160: }
161:
162: private static boolean showUpgradeDialog(final File source) {
163: Util.setDefaultLookAndFeel();
164: JOptionPane p = new JOptionPane(new AutoUpgradePanel(source
165: .getAbsolutePath()), JOptionPane.QUESTION_MESSAGE,
166: JOptionPane.YES_NO_OPTION);
167: javax.swing.JDialog d = p.createDialog(null,
168: NbBundle.getMessage(AutoUpgrade.class,
169: "MSG_Confirmation_Title") // NOI18N
170: );
171: d.setModal(true);
172: d.setVisible(true);
173:
174: return new Integer(JOptionPane.YES_OPTION).equals(p.getValue());
175: }
176:
177: static void doUpgrade(File source, String oldVersion)
178: throws java.io.IOException,
179: java.beans.PropertyVetoException {
180: File userdir = new File(System.getProperty("netbeans.user", "")); // NOI18N
181:
182: java.util.Set includeExclude;
183: try {
184: Reader r = new InputStreamReader(AutoUpgrade.class
185: .getResourceAsStream("copy" + oldVersion), // NOI18N
186: "utf-8"); // NOI18N
187: includeExclude = IncludeExclude.create(r);
188: r.close();
189: } catch (IOException ex) {
190: IOException e = new IOException(
191: "Cannot import from version: " + oldVersion);
192: e.initCause(ex);
193: throw e;
194: }
195:
196: ErrorManager.getDefault().log(
197: ErrorManager.USER,
198: "Import: Old version: " // NOI18N
199: + oldVersion + ". Importing from "
200: + source
201: + " to " + userdir // NOI18N
202: );
203:
204: File oldConfig = new File(source, "config"); // NOI18N
205: org.openide.filesystems.FileSystem old;
206: {
207: LocalFileSystem lfs = new LocalFileSystem();
208: lfs.setRootDirectory(oldConfig);
209:
210: XMLFileSystem xmlfs = null;
211: try {
212: URL url = AutoUpgrade.class.getResource("layer"
213: + oldVersion + ".xml"); // NOI18N
214: xmlfs = (url != null) ? new XMLFileSystem(url) : null;
215: } catch (SAXException ex) {
216: IOException e = new IOException(
217: "Cannot import from version: " + oldVersion); // NOI18N
218: e.initCause(ex);
219: throw e;
220: }
221:
222: old = (xmlfs != null) ? createLayeredSystem(lfs, xmlfs)
223: : lfs;
224: }
225: org.openide.filesystems.FileSystem mine = Repository
226: .getDefault().getDefaultFileSystem();
227:
228: Copy.copyDeep(old.getRoot(), mine.getRoot(), includeExclude,
229: PathTransformation.getInstance(oldVersion));
230:
231: }
232:
233: /* copy-pasted method doUpgrade and slightly modified to copy files relative
234: * to userdir.
235: */
236: private static void doNonStandardUpgrade(File source,
237: String oldVersion) throws IOException,
238: PropertyVetoException {
239: File userdir = new File(System.getProperty("netbeans.user", "")); // NOI18N
240: java.util.Set includeExclude;
241: try {
242: InputStream is = AutoUpgrade.class
243: .getResourceAsStream("nonstandard" + oldVersion);
244: if (is == null)
245: return;
246: Reader r = new InputStreamReader(is, "utf-8"); // NOI18N
247: includeExclude = IncludeExclude.create(r);
248: r.close();
249: } catch (IOException ex) {
250: IOException e = new IOException(
251: "Cannot import from version: " + oldVersion
252: + "nonstandard");
253: e.initCause(ex);
254: throw e;
255: }
256: ErrorManager.getDefault().log(
257: ErrorManager.USER,
258: "Import: Old version: " // NOI18N
259: + oldVersion
260: + "nonstandard"
261: + ". Importing from " + source
262: + " to "
263: + userdir // NOI18N
264: );
265:
266: LocalFileSystem old = new LocalFileSystem();
267: old.setRootDirectory(source);
268:
269: LocalFileSystem nfs = new LocalFileSystem();
270: nfs.setRootDirectory(userdir);
271: Copy.copyDeep(old.getRoot(), nfs.getRoot(), includeExclude,
272: PathTransformation.getInstance(oldVersion));
273: }
274:
275: static MultiFileSystem createLayeredSystem(
276: final LocalFileSystem lfs, final XMLFileSystem xmlfs) {
277: MultiFileSystem old;
278:
279: old = new MultiFileSystem(
280: new org.openide.filesystems.FileSystem[] { lfs, xmlfs }) {
281: {
282: setPropagateMasks(true);
283: }
284: };
285: return old;
286: }
287:
288: private static List<FileObject> getFiles(FileObject folder,
289: int depth, String fileName, String extension) {
290: if (depth == 0) {
291: FileObject result = folder.getFileObject(fileName,
292: extension);
293: if (result == null)
294: return Collections.emptyList();
295: return Collections.singletonList(result);
296: }
297: Enumeration<? extends FileObject> en = folder
298: .getChildren(false);
299: List<FileObject> result = new ArrayList<FileObject>();
300: while (en.hasMoreElements()) {
301: FileObject fo = en.nextElement();
302: if (!fo.isFolder())
303: continue;
304: result.addAll(getFiles(fo, depth - 1, fileName, extension));
305: }
306: return result;
307: }
308:
309: private static void copy(FileObject sourceDir, FileObject destDir)
310: throws IOException {
311: Enumeration en = sourceDir.getData(false);
312: while (en.hasMoreElements()) {
313: FileObject fo = (FileObject) en.nextElement();
314: if (fo.isFolder()) {
315: FileObject newDestDir = destDir.createFolder(fo
316: .getName());
317: copy(fo, newDestDir);
318: } else {
319: try {
320: FileObject destFile = FileUtil.copyFile(fo,
321: destDir, fo.getName(), fo.getExt());
322: FileUtil.copyAttributes(fo, destFile);
323: } catch (IOException ex) {
324: if (!fo.getNameExt().endsWith("_hidden"))
325: throw ex;
326: }
327: }
328: }
329: }
330: }
|