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.systemoptions;
043:
044: import java.io.*;
045: import java.util.*;
046: import org.openide.filesystems.*;
047:
048: /**
049: *
050: * @author Radek Matous
051: */
052: public class Importer {
053: private static final String DEFINITION_OF_FILES = "systemoptionsimport";//NOI18N
054:
055: private static FileObject getRootOfSystemFileSystem() {
056: return Repository.getDefault().getDefaultFileSystem().getRoot();
057: }
058:
059: public static void doImport() throws IOException {
060: Set<FileObject> files = getImportFiles(loadImportFilesDefinition());
061: for (Iterator<DefaultResult> it = parse(files).iterator(); it
062: .hasNext();) {
063: saveResult(it.next());
064: }
065: for (Iterator it = files.iterator(); it.hasNext();) {
066: FileObject fo = (FileObject) it.next();
067: FileLock fLock = fo.lock();
068: try {
069: fo.rename(fLock, fo.getName(), "imported");//NOI18N
070: } finally {
071: fLock.releaseLock();
072: }
073: }
074: }
075:
076: private static void saveResult(final DefaultResult result)
077: throws IOException {
078: String absolutePath = "/" + result.getModuleName();
079: PropertiesStorage ps = PropertiesStorage.instance(absolutePath);
080: Properties props = ps.load();
081: String[] propertyNames = result.getPropertyNames();
082: for (int i = 0; i < propertyNames.length; i++) {
083: String val = result.getProperty(propertyNames[i]);
084: if (val != null) {
085: props.put(propertyNames[i], val);
086: }
087: }
088: if (props.size() > 0) {
089: ps.save(props);
090: }
091: }
092:
093: private static Set<DefaultResult> parse(final Set<FileObject> files) {
094: Set<DefaultResult> retval = new HashSet<DefaultResult>();
095: for (FileObject f : files) {
096: try {
097: retval.add(SystemOptionsParser.parse(f, false));
098: } catch (ClassNotFoundException ex) {
099: continue;
100: } catch (IOException ex) {
101: continue;
102: }
103: }
104: return retval;
105: }
106:
107: static Properties loadImportFilesDefinition() throws IOException {
108: Properties props = new Properties();
109: InputStream is = Importer.class
110: .getResourceAsStream(DEFINITION_OF_FILES);
111: try {
112: props.load(is);
113: } finally {
114: is.close();
115: }
116: return props;
117: }
118:
119: private static Set<FileObject> getImportFiles(final Properties props) {
120: Set<FileObject> fileobjects = new HashSet<FileObject>();
121: for (Iterator it = props.keySet().iterator(); it.hasNext();) {
122: String path = (String) it.next();
123: FileObject f = getRootOfSystemFileSystem().getFileObject(
124: path);
125: if (f != null) {
126: fileobjects.add(f);
127: }
128: }
129: return fileobjects;
130: }
131:
132: /** Creates a new instance of SettingsReadSupport */
133: private Importer() {
134: }
135: }
|