KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > startup > preferences > 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.core.startup.preferences;
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.List JavaDoc;
30 import java.util.Properties JavaDoc;
31 import org.openide.filesystems.FileLock;
32 import org.openide.filesystems.FileObject;
33 import org.openide.filesystems.FileUtil;
34 import org.openide.filesystems.Repository;
35
36 /**
37  * No synchronization - must be called just from NbPreferences which
38  * ensures proper synchronization
39  * @author Radek Matous
40  */

41 class PropertiesStorage implements NbPreferences.FileStorage {
42     private static final String JavaDoc USERROOT_PREFIX = "/Preferences";//NOI18N
43
private static final String JavaDoc SYSTEMROOT_PREFIX = "/SystemPreferences";//NOI18N
44
private final static FileObject SFS_ROOT =
45             Repository.getDefault().getDefaultFileSystem().getRoot();
46     
47     private final String JavaDoc folderPath;
48     private String JavaDoc filePath;
49     private boolean isModified;
50     
51     
52     static NbPreferences.FileStorage instance(final String JavaDoc absolutePath) {
53         return new PropertiesStorage(absolutePath, true);
54     }
55     
56     FileObject preferencesRoot() throws IOException JavaDoc {
57         return FileUtil.createFolder(SFS_ROOT, USERROOT_PREFIX);
58     }
59     
60     static NbPreferences.FileStorage instanceReadOnly(final String JavaDoc absolutePath) {
61         return new PropertiesStorage(absolutePath, false) {
62             public boolean isReadOnly() {
63                 return true;
64             }
65             
66             public final String JavaDoc[] childrenNames() {
67                 return new String JavaDoc[0];
68             }
69             
70             public final Properties JavaDoc load() throws IOException JavaDoc {
71                 return new Properties JavaDoc();
72             }
73             
74             protected FileObject toPropertiesFile(boolean create) throws IOException JavaDoc {
75                 if (create) {
76                     throw new IOException JavaDoc();
77                 }
78                 return null;
79             }
80             
81             protected FileObject toFolder(boolean create) throws IOException JavaDoc {
82                 if (create) {
83                     throw new IOException JavaDoc();
84                 }
85                 return null;
86             }
87             
88             protected FileObject toPropertiesFile() {
89                 return null;
90             }
91             
92             protected FileObject toFolder() {
93                 return null;
94             }
95             
96             FileObject preferencesRoot() throws IOException JavaDoc {
97                 return FileUtil.createFolder(SFS_ROOT, SYSTEMROOT_PREFIX);
98             }
99             
100         };
101     }
102     
103     /** Creates a new instance */
104     private PropertiesStorage(final String JavaDoc absolutePath, boolean userRoot) {
105         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
106         String JavaDoc prefix = (userRoot) ? USERROOT_PREFIX : SYSTEMROOT_PREFIX;
107         sb.append(prefix).append(absolutePath);
108         folderPath = sb.toString();
109     }
110     
111     public boolean isReadOnly() {
112         return false;
113     }
114     
115     public void markModified() {
116         isModified = true;
117     }
118     
119     public final boolean existsNode() {
120         return (toPropertiesFile() != null) || (toFolder() != null);
121     }
122     
123     public String JavaDoc[] childrenNames() {
124         Statistics.StopWatch sw = Statistics.getStopWatch(Statistics.CHILDREN_NAMES, true);
125         try {
126             FileObject folder = toFolder();
127             List JavaDoc<String JavaDoc> folderNames = new ArrayList JavaDoc<String JavaDoc>();
128             
129             if (folder != null) {
130                 for (FileObject fo : Collections.list(folder.getFolders(false))) {
131                     folderNames.add(fo.getNameExt());
132                 }
133                 for (FileObject fo : Collections.list(folder.getData(false))) {
134                     if (fo.hasExt("properties")) { // NOI18N
135
folderNames.add(fo.getName());
136                     }
137                 }
138             }
139             
140             return folderNames.toArray(new String JavaDoc[folderNames.size()]);
141         } finally {
142             sw.stop();
143         }
144     }
145     
146     public final void removeNode() throws IOException JavaDoc {
147         Statistics.StopWatch sw = Statistics.getStopWatch(Statistics.REMOVE_NODE, true);
148         try {
149             FileObject propertiesFile = toPropertiesFile();
150             if (propertiesFile != null && propertiesFile.isValid()) {
151                 propertiesFile.delete();
152                 FileObject folder = propertiesFile.getParent();
153                 while (folder != null && folder != preferencesRoot() && folder.getChildren().length == 0) {
154                     folder.delete();
155                     folder = folder.getParent();
156                 }
157             }
158         } finally {
159             sw.stop();
160         }
161     }
162     
163     public Properties JavaDoc load() throws IOException JavaDoc {
164         Statistics.StopWatch sw = Statistics.getStopWatch(Statistics.LOAD, true);
165         try {
166             Properties JavaDoc retval = new Properties JavaDoc();
167             InputStream JavaDoc is = inputStream();
168             if (is != null) {
169                 try {
170                     retval.load(is);
171                 } finally {
172                     if (is != null) is.close();
173                 }
174             }
175             return retval;
176         } finally {
177             sw.stop();
178         }
179     }
180     
181     public void save(final Properties JavaDoc properties) throws IOException JavaDoc {
182         if (isModified) {
183             Statistics.StopWatch sw = Statistics.getStopWatch(Statistics.FLUSH, true);
184             try {
185                 isModified = false;
186                 if (!properties.isEmpty()) {
187                     OutputStream JavaDoc os = null;
188                     try {
189                         os = outputStream();
190                         properties.store(os, null);
191                     } finally {
192                         if (os != null) os.close();
193                     }
194                 } else {
195                     FileObject file = toPropertiesFile();
196                     if (file != null) {
197                         file.delete();
198                     }
199                     FileObject folder = toFolder();
200                     while (folder != null && folder != preferencesRoot() && folder.getChildren().length == 0) {
201                         folder.delete();
202                         folder = folder.getParent();
203                     }
204                 }
205             } finally {
206                 sw.stop();
207             }
208         }
209     }
210     
211     private InputStream JavaDoc inputStream() throws IOException JavaDoc {
212         FileObject file = toPropertiesFile(false);
213         return (file == null) ? null : file.getInputStream();
214     }
215     
216     private OutputStream JavaDoc outputStream() throws IOException JavaDoc {
217         FileObject fo = toPropertiesFile(true);
218         final FileLock lock = fo.lock();
219         final OutputStream JavaDoc os = fo.getOutputStream(lock);
220         return new FilterOutputStream JavaDoc(os) {
221             public void close() throws IOException JavaDoc {
222                 super.close();
223                 lock.releaseLock();
224             }
225         };
226     }
227     
228     private String JavaDoc folderPath() {
229         return folderPath;
230     }
231
232     private String JavaDoc filePath() {
233         if (filePath == null) {
234             String JavaDoc[] all = folderPath().split("/");//NOI18N
235
StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
236             for (int i = 0; i < all.length-1; i++) {
237                 sb.append(all[i]).append("/");//NOI18N
238
}
239             if (all.length > 0) {
240                 sb.append(all[all.length-1]).append(".properties");//NOI18N
241
} else {
242                 sb.append("root.properties");//NOI18N
243
}
244             filePath = sb.toString();
245         }
246         return filePath;
247     }
248
249     protected FileObject toFolder() {
250         return SFS_ROOT.getFileObject(folderPath());
251     }
252
253     protected FileObject toPropertiesFile() {
254         return SFS_ROOT.getFileObject(filePath());
255     }
256
257     protected FileObject toFolder(boolean create) throws IOException JavaDoc {
258         FileObject retval = toFolder();
259         if (retval == null && create) {
260             retval = FileUtil.createFolder(SFS_ROOT, folderPath);
261         }
262         assert (retval == null && !create) || (retval != null && retval.isFolder());
263         return retval;
264     }
265     
266     protected FileObject toPropertiesFile(boolean create) throws IOException JavaDoc {
267         FileObject retval = toPropertiesFile();
268         if (retval == null && create) {
269             retval = FileUtil.createData(SFS_ROOT,filePath());//NOI18N
270
}
271         assert (retval == null && !create) || (retval != null && retval.isData());
272         return retval;
273     }
274 }
275
Popular Tags