KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > upgrade > systemoptions > Importer


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.systemoptions;
21
22 import java.io.*;
23 import java.util.*;
24 import org.openide.filesystems.*;
25
26 /**
27  *
28  * @author Radek Matous
29  */

30 public class Importer {
31     private static final String JavaDoc DEFINITION_OF_FILES = "systemoptionsimport";//NOI18N
32

33     private static FileObject getRootOfSystemFileSystem() {
34         return Repository.getDefault().getDefaultFileSystem().getRoot();
35     }
36     
37     public static void doImport() throws IOException {
38         Set<FileObject> files = getImportFiles(loadImportFilesDefinition());
39         for (Iterator<DefaultResult> it = parse(files).iterator(); it.hasNext();) {
40             saveResult(it.next());
41         }
42         for (Iterator it = files.iterator(); it.hasNext();) {
43             FileObject fo = (FileObject) it.next();
44             FileLock fLock = fo.lock();
45             try {
46                 fo.rename(fLock, fo.getName(), "imported");//NOI18N
47
} finally {
48                 fLock.releaseLock();
49             }
50         }
51     }
52     
53     private static void saveResult(final DefaultResult result) throws IOException {
54         String JavaDoc absolutePath = "/"+result.getModuleName();
55         PropertiesStorage ps = PropertiesStorage.instance(absolutePath);
56         Properties props = ps.load();
57         String JavaDoc[] propertyNames = result.getPropertyNames();
58         for (int i = 0; i < propertyNames.length; i++) {
59             String JavaDoc val = result.getProperty(propertyNames[i]);
60             if (val != null) {
61                 props.put(propertyNames[i], val);
62             }
63         }
64         if (props.size() > 0) {
65             ps.save(props);
66         }
67     }
68     
69     private static Set<DefaultResult> parse(final Set<FileObject> files) {
70         Set<DefaultResult> retval = new HashSet<DefaultResult>();
71         for (FileObject f: files) {
72             try {
73                 retval.add(SystemOptionsParser.parse(f, false));
74             } catch (ClassNotFoundException JavaDoc ex) {
75                 continue;
76             } catch (IOException ex) {
77                 continue;
78             }
79         }
80         return retval;
81     }
82     
83
84     static Properties loadImportFilesDefinition() throws IOException {
85         Properties props = new Properties();
86         InputStream is = Importer.class.getResourceAsStream(DEFINITION_OF_FILES);
87         try {
88             props.load(is);
89         } finally {
90             is.close();
91         }
92         return props;
93     }
94
95     private static Set<FileObject> getImportFiles(final Properties props) {
96         Set<FileObject> fileobjects = new HashSet<FileObject>();
97         for (Iterator it = props.keySet().iterator(); it.hasNext();) {
98             String JavaDoc path = (String JavaDoc) it.next();
99             FileObject f = getRootOfSystemFileSystem().getFileObject(path);
100             if (f != null) {
101                 fileobjects.add(f);
102             }
103         }
104         return fileobjects;
105     }
106     
107     /** Creates a new instance of SettingsReadSupport */
108     private Importer() {}
109 }
110
Popular Tags