KickJava   Java API By Example, From Geeks To Geeks.

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


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.FilterOutputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.io.OutputStream JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.Date JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Properties JavaDoc;
32 import org.openide.filesystems.FileLock;
33 import org.openide.filesystems.FileObject;
34 import org.openide.filesystems.FileUtil;
35 import org.openide.filesystems.Repository;
36
37 /**
38  * @author Radek Matous
39  */

40 class PropertiesStorage {
41     private static final String JavaDoc USERROOT_PREFIX = "/Preferences";//NOI18N
42
private final static FileObject SFS_ROOT =
43             Repository.getDefault().getDefaultFileSystem().getRoot();
44     
45     private final String JavaDoc folderPath;
46     private String JavaDoc filePath;
47             
48     static PropertiesStorage instance(final String JavaDoc absolutePath) {
49         return new PropertiesStorage(absolutePath);
50     }
51     
52     FileObject preferencesRoot() throws IOException JavaDoc {
53         return FileUtil.createFolder(SFS_ROOT, USERROOT_PREFIX);
54     }
55     
56     
57     /** Creates a new instance */
58     private PropertiesStorage(final String JavaDoc absolutePath) {
59         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
60         sb.append(USERROOT_PREFIX).append(absolutePath);
61         folderPath = sb.toString();
62     }
63         
64     
65     public Properties JavaDoc load() throws IOException JavaDoc {
66         try {
67             Properties JavaDoc retval = new Properties JavaDoc();
68             InputStream JavaDoc is = inputStream();
69             if (is != null) {
70                 try {
71                     retval.load(is);
72                 } finally {
73                     if (is != null) is.close();
74                 }
75             }
76             return retval;
77         } finally {
78         }
79     }
80     
81     public void save(final Properties JavaDoc properties) throws IOException JavaDoc {
82         if (!properties.isEmpty()) {
83             OutputStream JavaDoc os = null;
84             try {
85                 os = outputStream();
86                 properties.store(os,new Date JavaDoc().toString());//NOI18N
87
} finally {
88                 if (os != null) os.close();
89             }
90         } else {
91             FileObject file = toPropertiesFile();
92             if (file != null) {
93                 file.delete();
94             }
95             FileObject folder = toFolder();
96             while (folder != null && folder != preferencesRoot() && folder.getChildren().length == 0) {
97                 folder.delete();
98                 folder = folder.getParent();
99             }
100         }
101     }
102     
103     private InputStream JavaDoc inputStream() throws IOException JavaDoc {
104         FileObject file = toPropertiesFile(false);
105         return (file == null) ? null : file.getInputStream();
106     }
107     
108     private OutputStream JavaDoc outputStream() throws IOException JavaDoc {
109         FileObject fo = toPropertiesFile(true);
110         final FileLock lock = fo.lock();
111         final OutputStream JavaDoc os = fo.getOutputStream(lock);
112         return new FilterOutputStream JavaDoc(os) {
113             public void close() throws IOException JavaDoc {
114                 super.close();
115                 lock.releaseLock();
116             }
117         };
118     }
119     
120     private String JavaDoc folderPath() {
121         return folderPath;
122     }
123     
124     private String JavaDoc filePath() {
125         if (filePath == null) {
126             String JavaDoc[] all = folderPath().split("/");//NOI18N
127
StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
128             for (int i = 0; i < all.length-1; i++) {
129                 sb.append(all[i]).append("/");//NOI18N
130
}
131             if (all.length > 0) {
132                 sb.append(all[all.length-1]).append(".properties");//NOI18N
133
} else {
134                 sb.append("root.properties");//NOI18N
135
}
136             filePath = sb.toString();
137         }
138         return filePath;
139     }
140     
141     protected FileObject toFolder() {
142         return SFS_ROOT.getFileObject(folderPath);
143     }
144     
145     protected FileObject toPropertiesFile() {
146         return SFS_ROOT.getFileObject(filePath());
147     }
148     
149     protected FileObject toFolder(boolean create) throws IOException JavaDoc {
150         FileObject retval = toFolder();
151         if (retval == null && create) {
152             retval = FileUtil.createFolder(SFS_ROOT, folderPath);
153         }
154         assert (retval == null && !create) || (retval != null && retval.isFolder());
155         return retval;
156     }
157     
158     protected FileObject toPropertiesFile(boolean create) throws IOException JavaDoc {
159         FileObject retval = toPropertiesFile();
160         if (retval == null && create) {
161             retval = FileUtil.createData(SFS_ROOT,filePath());//NOI18N
162
}
163         assert (retval == null && !create) || (retval != null && retval.isData());
164         return retval;
165     }
166 }
Popular Tags