|
@@ -1,11 +1,10 @@ |
1 |
1 |
|
// This Source Code is in the Public Domain per: http://litesoft.org/License.txt |
2 |
2 |
|
package org.litesoft.configuration; |
3 |
3 |
|
|
4 |
|
- |
import java.util.*; |
5 |
|
- |
import java.util.prefs.*; |
|
4 |
+ |
import org.litesoft.util.Utils; |
6 |
5 |
|
|
7 |
|
- |
import org.litesoft.core.util.*; |
8 |
|
- |
import org.litesoft.util.*; |
|
6 |
+ |
import java.util.prefs.BackingStoreException; |
|
7 |
+ |
import java.util.prefs.Preferences; |
9 |
8 |
|
|
10 |
9 |
|
public class ConfigAccessorFactoryPreferences implements ConfigDataAccessorFactory |
11 |
10 |
|
{ |
|
@@ -30,74 +29,25 @@ |
30 |
29 |
|
return mAccessor; |
31 |
30 |
|
} |
32 |
31 |
|
|
33 |
|
- |
private static class ConfigDataAccessorImpl implements ConfigDataAccessor |
|
32 |
+ |
private static class ConfigDataAccessorImpl extends AbstractConfigDataAccessor |
34 |
33 |
|
{ |
35 |
|
- |
private final String mLoadedFrom; |
36 |
|
- |
private final ConfigDataAccessor.Level mLevel; |
37 |
34 |
|
private final Preferences mPrefs; |
38 |
|
- |
private final Map<String, String> mRunLevelKeyValues = new HashMap<String, String>(); |
39 |
35 |
|
|
40 |
|
- |
public ConfigDataAccessorImpl( String pLoadedFrom, ConfigDataAccessor.Level pLevel, Preferences pPrefs ) |
|
36 |
+ |
public ConfigDataAccessorImpl( String pLoadedFrom, Level pLevel, Preferences pPrefs ) |
41 |
37 |
|
{ |
42 |
|
- |
mLoadedFrom = pLoadedFrom; |
43 |
|
- |
mLevel = pLevel; |
|
38 |
+ |
super( null, pLoadedFrom, pLevel ); |
44 |
39 |
|
mPrefs = pPrefs; |
45 |
40 |
|
} |
46 |
41 |
|
|
47 |
42 |
|
@Override |
48 |
|
- |
public String loadedFrom() |
|
43 |
+ |
protected String getExceptionPlus() |
49 |
44 |
|
{ |
50 |
|
- |
return mLoadedFrom; |
|
45 |
+ |
return " Preference Node: " + mPrefs; |
51 |
46 |
|
} |
52 |
47 |
|
|
53 |
48 |
|
@Override |
54 |
|
- |
public String loadedFromDirectory() |
|
49 |
+ |
protected void setOtherLevelKeyValue(String pKey, String pValue) |
55 |
50 |
|
{ |
56 |
|
- |
return null; |
57 |
|
- |
} |
58 |
|
- |
|
59 |
|
- |
/** |
60 |
|
- |
* All implementations MUST support RUN level, but may not support other levels |
61 |
|
- |
* |
62 |
|
- |
* @return an array with at least 1 (RUN) entry |
63 |
|
- |
*/ |
64 |
|
- |
@Override |
65 |
|
- |
public Level[] getSetableSupportedLevels() |
66 |
|
- |
{ |
67 |
|
- |
return new Level[]{Level.RUN, mLevel}; |
68 |
|
- |
} |
69 |
|
- |
|
70 |
|
- |
/** |
71 |
|
- |
* All implementations MUST support RUN level, but may not support other levels |
72 |
|
- |
* |
73 |
|
- |
* @param pLevel null interpreted as RUN |
74 |
|
- |
* @param pKey not null or empty |
75 |
|
- |
* @param pValue null means remove |
76 |
|
- |
* |
77 |
|
- |
* @throws UnsupportedOperationException if requested level not supported |
78 |
|
- |
*/ |
79 |
|
- |
@Override |
80 |
|
- |
public synchronized void setKeyValue( Level pLevel, String pKey, String pValue ) |
81 |
|
- |
throws UnsupportedOperationException |
82 |
|
- |
{ |
83 |
|
- |
pKey = Utils.assertNotNullNotEmpty( "Key", pKey ); |
84 |
|
- |
Level zLevel = Utils.deNull( pLevel, Level.RUN ); |
85 |
|
- |
if ( Level.RUN.equals( zLevel ) ) |
86 |
|
- |
{ |
87 |
|
- |
if ( pValue == null ) |
88 |
|
- |
{ |
89 |
|
- |
mRunLevelKeyValues.remove( pKey ); |
90 |
|
- |
} |
91 |
|
- |
else |
92 |
|
- |
{ |
93 |
|
- |
mRunLevelKeyValues.put( pKey, pValue ); |
94 |
|
- |
} |
95 |
|
- |
return; |
96 |
|
- |
} |
97 |
|
- |
if ( !zLevel.equals( mLevel ) ) |
98 |
|
- |
{ |
99 |
|
- |
throw new UnsupportedOperationException( "setKeyValue for Level '" + pLevel + "' not supported" ); |
100 |
|
- |
} |
101 |
51 |
|
if ( pValue == null ) |
102 |
52 |
|
{ |
103 |
53 |
|
try |
|
@@ -106,19 +56,12 @@ |
106 |
56 |
|
} |
107 |
57 |
|
catch ( IllegalStateException e ) |
108 |
58 |
|
{ |
109 |
|
- |
// No Node so technically already Removed! |
|
59 |
+ |
return; // No Node so technically already Removed! |
110 |
60 |
|
} |
111 |
61 |
|
} |
112 |
62 |
|
else |
113 |
63 |
|
{ |
114 |
|
- |
try |
115 |
|
- |
{ |
116 |
|
- |
mPrefs.put( pKey, pValue ); |
117 |
|
- |
} |
118 |
|
- |
catch ( IllegalStateException e ) |
119 |
|
- |
{ |
120 |
|
- |
throw new PersistenceException( "Unable to setKeyValue( " + mLevel + ", \"" + pKey + "\", " + pValue + " ) keys for Preference Node: " + mPrefs, e ); |
121 |
|
- |
} |
|
64 |
+ |
mPrefs.put( pKey, pValue ); |
122 |
65 |
|
} |
123 |
66 |
|
try |
124 |
67 |
|
{ |
|
@@ -126,59 +69,34 @@ |
126 |
69 |
|
} |
127 |
70 |
|
catch ( BackingStoreException e ) |
128 |
71 |
|
{ |
129 |
|
- |
if ( pValue != null ) |
130 |
|
- |
{ |
131 |
|
- |
pValue = "\"" + pValue + "\""; |
132 |
|
- |
} |
133 |
|
- |
throw new PersistenceException( "Unable to setKeyValue( " + mLevel + ", \"" + pKey + "\", " + pValue + " ) keys for Preference Node: " + mPrefs, e ); |
|
72 |
+ |
throw new RuntimeException(e); |
134 |
73 |
|
} |
135 |
74 |
|
} |
136 |
75 |
|
|
137 |
76 |
|
@Override |
138 |
|
- |
public synchronized String[] getAllKeys() |
|
77 |
+ |
protected String[] getBaseKeys() |
139 |
78 |
|
{ |
140 |
|
- |
Set<String> zKeys = new HashSet<String>(); |
141 |
79 |
|
try |
142 |
80 |
|
{ |
143 |
|
- |
String[] zPrefKeys = mPrefs.keys(); |
144 |
|
- |
zKeys.addAll( Arrays.asList( zPrefKeys ) ); |
|
81 |
+ |
return mPrefs.keys(); |
145 |
82 |
|
} |
146 |
83 |
|
catch ( BackingStoreException e ) |
147 |
84 |
|
{ |
148 |
|
- |
throw new PersistenceException( "Unable to get keys for Preference Node: " + mPrefs, e ); |
149 |
|
- |
} |
150 |
|
- |
catch ( IllegalStateException e ) |
151 |
|
- |
{ |
152 |
|
- |
// No Node == No Keys! |
|
85 |
+ |
throw new RuntimeException(e); |
153 |
86 |
|
} |
154 |
|
- |
|
155 |
|
- |
zKeys.addAll( mRunLevelKeyValues.keySet() ); |
156 |
|
- |
|
157 |
|
- |
return zKeys.toArray( new String[zKeys.size()] ); |
158 |
87 |
|
} |
159 |
88 |
|
|
160 |
|
- |
/** |
161 |
|
- |
* @param pKey not null or empty |
162 |
|
- |
* |
163 |
|
- |
* @return null if not found or value associated with key |
164 |
|
- |
*/ |
165 |
89 |
|
@Override |
166 |
|
- |
public synchronized String getString( String pKey ) |
|
90 |
+ |
protected String getBaseValue(String pKey) |
167 |
91 |
|
{ |
168 |
|
- |
pKey = Utils.assertNotNullNotEmpty( "Key", pKey ); |
169 |
|
- |
String rv = mRunLevelKeyValues.get( pKey ); |
170 |
|
- |
if ( rv == null ) |
|
92 |
+ |
try |
171 |
93 |
|
{ |
172 |
|
- |
try |
173 |
|
- |
{ |
174 |
|
- |
rv = mPrefs.get( pKey, null ); |
175 |
|
- |
} |
176 |
|
- |
catch ( IllegalStateException e ) |
177 |
|
- |
{ |
178 |
|
- |
// No Node == No Value -> null! |
179 |
|
- |
} |
|
94 |
+ |
return mPrefs.get( pKey, null ); |
|
95 |
+ |
} |
|
96 |
+ |
catch ( IllegalStateException e ) |
|
97 |
+ |
{ |
|
98 |
+ |
return null; // No Node == No Value -> null! |
180 |
99 |
|
} |
181 |
|
- |
return rv; |
182 |
100 |
|
} |
183 |
101 |
|
} |
184 |
102 |
|
} |