KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > startup > preferences > NbPreferences


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.IOException JavaDoc;
23 import java.util.Properties JavaDoc;
24 import java.util.prefs.AbstractPreferences JavaDoc;
25 import java.util.prefs.BackingStoreException JavaDoc;
26 import java.util.prefs.Preferences JavaDoc;
27 import org.openide.ErrorManager;
28 import org.openide.util.RequestProcessor;
29
30 /**
31  *
32  * @author Radek Matous
33  */

34 public abstract class NbPreferences extends AbstractPreferences JavaDoc {
35     private static Preferences JavaDoc USER_ROOT;
36     private static Preferences JavaDoc SYSTEM_ROOT;
37     
38     /*private*/Properties JavaDoc properties;
39     /*private*/FileStorage fileStorage;
40     
41     private static final RequestProcessor RP = new RequestProcessor();
42     /*private*/final RequestProcessor.Task flushTask = RP.create(new Runnable JavaDoc() {
43         public void run() {
44             synchronized(lock) {
45                 try {
46                     flushSpi();
47                 } catch (BackingStoreException JavaDoc ex) {
48                     ErrorManager.getDefault().notify(ex);
49                 }
50             }
51         }
52     },true);
53     
54     
55     static Preferences JavaDoc userRootImpl() {
56         if (USER_ROOT == null) {
57             USER_ROOT = new NbPreferences.UserPreferences();
58         }
59         assert USER_ROOT != null;
60         return USER_ROOT;
61     }
62     
63     static Preferences JavaDoc systemRootImpl() {
64         if (SYSTEM_ROOT == null) {
65             SYSTEM_ROOT = new NbPreferences.SystemPreferences();
66         }
67         assert SYSTEM_ROOT != null;
68         return SYSTEM_ROOT;
69     }
70
71     private NbPreferences(boolean user) {
72         super(null, "");
73         fileStorage = getFileStorage(absolutePath());
74     }
75     
76     /** Creates a new instance of PreferencesImpl */
77     private NbPreferences(NbPreferences parent, String JavaDoc name) {
78         super(parent, name);
79         fileStorage = getFileStorage(absolutePath());
80         newNode = !fileStorage.existsNode();
81     }
82         
83     protected final String JavaDoc getSpi(String JavaDoc key) {
84         return (String JavaDoc)properties().getProperty(key);
85     }
86     
87     protected final String JavaDoc[] childrenNamesSpi() throws BackingStoreException JavaDoc {
88         //TODO: cache it if necessary
89
return fileStorage.childrenNames();
90     }
91     
92     protected final String JavaDoc[] keysSpi() throws BackingStoreException JavaDoc {
93         return (String JavaDoc[])properties().keySet().toArray(new String JavaDoc[0]);
94     }
95     
96     protected final void putSpi(String JavaDoc key, String JavaDoc value) {
97         properties().put(key,value);
98         fileStorage.markModified();
99         asyncInvocationOfFlushSpi();
100     }
101     
102     @Override JavaDoc
103     public void put(String JavaDoc key, String JavaDoc value) {
104         String JavaDoc oldValue = getSpi(key);
105         if (value.equals(oldValue)) {return;}
106         try {
107             super.put(key, value);
108         } catch (IllegalArgumentException JavaDoc iae) {
109             if (iae.getMessage().contains("too long")) {
110                 // Not for us!
111
putSpi(key, value);
112             } else {
113                 throw iae;
114             }
115         }
116     }
117     
118     protected final void removeSpi(String JavaDoc key) {
119         properties().remove(key);
120         fileStorage.markModified();
121         asyncInvocationOfFlushSpi();
122     }
123     
124     protected final void removeNodeSpi() throws BackingStoreException JavaDoc {
125         try {
126             fileStorage.removeNode();
127         } catch (IOException JavaDoc ex) {
128             throw new BackingStoreException JavaDoc(ex);
129         }
130     }
131     
132     void asyncInvocationOfFlushSpi() {
133         if (!fileStorage.isReadOnly()) {
134             flushTask.schedule(200);
135         }
136     }
137     
138     protected void flushSpi() throws BackingStoreException JavaDoc {
139         try {
140             fileStorage.save(properties());
141         } catch (IOException JavaDoc ex) {
142             throw new BackingStoreException JavaDoc(ex);
143         }
144     }
145     
146     protected void syncSpi() throws BackingStoreException JavaDoc {
147         if (properties != null) {
148             try {
149                 properties.clear();
150                 properties().putAll(fileStorage.load());
151                 
152             } catch (IOException JavaDoc ex) {
153                 throw new BackingStoreException JavaDoc(ex);
154             }
155         }
156     }
157     
158     Properties JavaDoc properties() {
159         if (properties == null) {
160             properties = new Properties JavaDoc(/*loadDefaultProperties()*/);
161             try {
162                 properties().putAll(fileStorage.load());
163             } catch (IOException JavaDoc ex) {
164                 ErrorManager.getDefault().notify(ex);
165             }
166         }
167         return properties;
168     }
169
170     public final void removeNode() throws BackingStoreException JavaDoc {
171         if (fileStorage.isReadOnly()) {
172             throw new BackingStoreException JavaDoc("Unsupported operation: read-only storage");//NOI18N
173
} else {
174             properties().clear();
175             super.removeNode();
176         }
177     }
178     
179     public final void flush() throws BackingStoreException JavaDoc {
180         if (fileStorage.isReadOnly()) {
181             throw new BackingStoreException JavaDoc("Unsupported operation: read-only storage");//NOI18N
182
} else {
183             super.flush();
184         }
185     }
186     
187     public final void sync() throws BackingStoreException JavaDoc {
188         if (fileStorage.isReadOnly()) {
189             throw new BackingStoreException JavaDoc("Unsupported operation: read-only storage");//NOI18N
190
} else {
191             flushTask.waitFinished();
192             super.sync();
193         }
194     }
195
196     protected abstract FileStorage getFileStorage(String JavaDoc absolutePath);
197
198     public static class UserPreferences extends NbPreferences {
199         public UserPreferences() {
200             super(true);
201         }
202         
203         /** Creates a new instance */
204         private UserPreferences(NbPreferences parent, String JavaDoc name) {
205             super(parent, name);
206         }
207         
208         protected AbstractPreferences JavaDoc childSpi(String JavaDoc name) {
209             return new UserPreferences(this, name);
210         }
211
212         protected NbPreferences.FileStorage getFileStorage(String JavaDoc absolutePath) {
213             return PropertiesStorage.instance(absolutePath());
214         }
215     }
216     
217     private static final class SystemPreferences extends NbPreferences {
218         private SystemPreferences() {
219             super(false);
220         }
221         
222         private SystemPreferences(NbPreferences parent, String JavaDoc name) {
223             super(parent, name);
224         }
225         
226         protected AbstractPreferences JavaDoc childSpi(String JavaDoc name) {
227             return new SystemPreferences(this, name);
228         }
229
230         protected NbPreferences.FileStorage getFileStorage(String JavaDoc absolutePath) {
231             return PropertiesStorage.instanceReadOnly(absolutePath());
232         }
233     }
234     
235     interface FileStorage {
236         boolean isReadOnly();
237         String JavaDoc[] childrenNames();
238         boolean existsNode();
239         void removeNode() throws IOException JavaDoc;
240         void markModified();
241         Properties JavaDoc load() throws IOException JavaDoc;
242         void save(final Properties JavaDoc properties) throws IOException JavaDoc;
243     }
244 }
245
Popular Tags