Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1457a73

Browse files
committed
Support using different types of storages
1 parent 333c6b3 commit 1457a73

File tree

1 file changed

+58
-7
lines changed

1 file changed

+58
-7
lines changed

sources/net.sf.j2s.ajax/store/net/sf/j2s/store/SimpleStore.java

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,17 @@ public class SimpleStore implements IStore {
99

1010
private static SimpleStore singleton;
1111

12+
private static SimpleStore[] extras = new SimpleStore[3];
13+
14+
public static final String TYPE_LOCALSTORAGE = "local";
15+
public static final String TYPE_COOKIE = "cookie";
16+
public static final String TYPE_XSS_COOKIE = "xss";
17+
public static final String TYPE_FILE = "file";
18+
1219
private IStore store;
20+
private String type;
1321

14-
private SimpleStore() {
22+
private SimpleStore(String type) {
1523
/**
1624
* @j2sNative
1725
var ua = navigator.userAgent.toLowerCase ();
@@ -21,11 +29,13 @@ private SimpleStore() {
2129
} catch (e) {
2230
isLocalFile = true;
2331
}
24-
if (window["j2s.html5.store"] && window["localStorage"] != null && (ua.indexOf ("gecko/") == -1 || !isLocalFile)) {
32+
if ((type == null || "local" == type) && window["j2s.html5.store"] && window["localStorage"] != null
33+
&& (ua.indexOf ("gecko/") == -1 || !isLocalFile)) {
2534
try {
2635
localStorage.setItem('net.sf.j2s.test', '1');
2736
localStorage.removeItem('net.sf.j2s.test');
2837
this.store = new net.sf.j2s.store.HTML5LocalStorage ();
38+
this.type = "local";
2939
return;
3040
} catch (error) {
3141
}
@@ -39,23 +49,64 @@ private SimpleStore() {
3949
}
4050
var isOldIE = ua.indexOf ("msie 5.5") != -1 || ua.indexOf ("msie 5.0") != -1;
4151
var cookieURL = window["j2s.xss.cookie.url"];
42-
if (!isLocal && cookieURL != null && !isOldIE) {
52+
if (!isLocal && cookieURL != null && !isOldIE && (type == null || "xss" == type)) {
4353
this.store = new net.sf.j2s.store.XSSCookieStore(cookieURL);
54+
this.type = "xss";
4455
} else {
45-
this.store = new net.sf.j2s.store.CookieStore();
56+
if (type == null || "cookie" == type) {
57+
this.store = new net.sf.j2s.store.CookieStore();
58+
this.type = "cookie";
59+
} else {
60+
throw new RuntimeException("Not supporting SimpleStore for given type \"" + type + "\"!");
61+
}
4662
}
4763
*/ {
48-
File storeFile = new File(System.getProperty("user.home"), ".java2script.store");
49-
this.store = new INIFileStore(storeFile.getAbsolutePath());
64+
if (type == null || "file".equals(type)) {
65+
File storeFile = new File(System.getProperty("user.home"), ".java2script.store");
66+
this.store = new INIFileStore(storeFile.getAbsolutePath());
67+
this.type = "file";
68+
} else {
69+
throw new RuntimeException("Not supporting SimpleStore for given type \"" + type + "\"!");
70+
}
5071
}
5172
}
5273

5374
public static SimpleStore getDefault() {
5475
if (singleton == null) {
55-
singleton = new SimpleStore();
76+
singleton = new SimpleStore(null);
5677
}
5778
return singleton;
5879
}
80+
81+
public static SimpleStore getTypedStore(String type) {
82+
if (singleton == null) {
83+
singleton = new SimpleStore(null);
84+
}
85+
if (type == null || type.length() == 0 || type.equals(singleton.type)) {
86+
return singleton;
87+
}
88+
SimpleStore[] es = extras;
89+
for (int i = 0; i < es.length; i++) {
90+
SimpleStore ss = es[i];
91+
if (ss == null) continue;
92+
if (type.equals(ss.type)) return ss;
93+
}
94+
SimpleStore typedStore = null;
95+
try {
96+
typedStore = new SimpleStore(type);
97+
} catch (Throwable e) {
98+
//e.printStackTrace();
99+
return null;
100+
}
101+
for (int i = 0; i < es.length; i++) {
102+
SimpleStore ss = es[i];
103+
if (ss == null) {
104+
es[i] = typedStore;
105+
break;
106+
}
107+
}
108+
return typedStore;
109+
}
59110

60111
public String getProperty(String name) {
61112
return store.getProperty(name);

0 commit comments

Comments
 (0)