Skip to content

Commit be36581

Browse files
authored
Fixed cyclic failure in Preferences and logging (#4064)
* Fixed cyclic failure in Preferences and logging This exception caused a cycle as the Logger depends on Preferences. See: https://www.reddit.com/r/cn1/comments/1oe6fvq/error_on_preferences/ * Revised based on review
1 parent 8fea660 commit be36581

File tree

2 files changed

+46
-9
lines changed

2 files changed

+46
-9
lines changed

CodenameOne/src/com/codename1/io/Preferences.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public static void setPreferencesLocation(String storageFileName) {
8282
private synchronized static Hashtable<String, Object> get() {
8383
if (p == null) {
8484
if (Storage.getInstance().exists(preferencesLocation)) {
85-
p = (Hashtable<String, Object>) Storage.getInstance().readObject(preferencesLocation);
85+
p = (Hashtable<String, Object>) Storage.getInstance().readObject(preferencesLocation, false);
8686
}
8787
if (p == null) {
8888
p = new Hashtable<String, Object>();
@@ -92,7 +92,7 @@ private synchronized static Hashtable<String, Object> get() {
9292
}
9393

9494
private static synchronized void save() {
95-
Storage.getInstance().writeObject(preferencesLocation, p);
95+
Storage.getInstance().writeObject(preferencesLocation, p, false);
9696
}
9797

9898
/**

CodenameOne/src/com/codename1/io/Storage.java

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,24 @@ public int entrySize(String name) {
233233
* @return true for success, false for failure
234234
*/
235235
public boolean writeObject(String name, Object o) {
236+
return writeObject(name, o, true);
237+
}
238+
239+
/**
240+
* <p>Writes the given object to storage assuming it is an externalizable type
241+
* or one of the supported types.</p>
242+
*
243+
* <p>
244+
* The sample below demonstrates the usage and registration of the {@link com.codename1.io.Externalizable} interface:
245+
* </p>
246+
* <script src="https://gist.github.com/codenameone/858d8634e3cf1a82a1eb.js"></script>
247+
*
248+
* @param name store name
249+
* @param o object to store
250+
* @param includeLogging During app initialization, the logging on error might impact the apps stability
251+
* @return true for success, false for failure
252+
*/
253+
public boolean writeObject(String name, Object o, boolean includeLogging) {
236254
name = fixFileName(name);
237255
cache.put(name, o);
238256
DataOutputStream d = null;
@@ -242,9 +260,11 @@ public boolean writeObject(String name, Object o) {
242260
d.close();
243261
return true;
244262
} catch (Exception err) {
245-
Log.e(err);
246-
if (Log.isCrashBound()) {
247-
Log.sendLog();
263+
if(includeLogging) {
264+
Log.e(err);
265+
if (Log.isCrashBound()) {
266+
Log.sendLog();
267+
}
248268
}
249269
Util.getImplementation().deleteStorageFile(name);
250270
Util.getImplementation().cleanup(d);
@@ -263,6 +283,21 @@ public boolean writeObject(String name, Object o) {
263283
* @return object stored under that name
264284
*/
265285
public Object readObject(String name) {
286+
return readObject(name, true);
287+
}
288+
289+
/**
290+
* <p>Reads the object from the storage, returns null if the object isn't there</p>
291+
* <p>
292+
* The sample below demonstrates the usage and registration of the {@link com.codename1.io.Externalizable} interface:
293+
* </p>
294+
* <script src="https://gist.github.com/codenameone/858d8634e3cf1a82a1eb.js"></script>
295+
*
296+
* @param name name of the store
297+
* @param includeLogging During app initialization, the logging on error might impact the apps stability
298+
* @return object stored under that name
299+
*/
300+
public Object readObject(String name, boolean includeLogging) {
266301
name = fixFileName(name);
267302
Object o = cache.get(name);
268303
if (o != null) {
@@ -279,10 +314,12 @@ public Object readObject(String name) {
279314
cache.put(name, o);
280315
return o;
281316
} catch (Throwable err) {
282-
Log.p("Error while reading: " + name);
283-
Log.e(err);
284-
if (Log.isCrashBound()) {
285-
Log.sendLog();
317+
if(includeLogging) {
318+
Log.p("Error while reading: " + name);
319+
Log.e(err);
320+
if (Log.isCrashBound()) {
321+
Log.sendLog();
322+
}
286323
}
287324
Util.getImplementation().cleanup(d);
288325
return null;

0 commit comments

Comments
 (0)