@@ -9,9 +9,17 @@ public class SimpleStore implements IStore {
9
9
10
10
private static SimpleStore singleton ;
11
11
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
+
12
19
private IStore store ;
20
+ private String type ;
13
21
14
- private SimpleStore () {
22
+ private SimpleStore (String type ) {
15
23
/**
16
24
* @j2sNative
17
25
var ua = navigator.userAgent.toLowerCase ();
@@ -21,11 +29,13 @@ private SimpleStore() {
21
29
} catch (e) {
22
30
isLocalFile = true;
23
31
}
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)) {
25
34
try {
26
35
localStorage.setItem('net.sf.j2s.test', '1');
27
36
localStorage.removeItem('net.sf.j2s.test');
28
37
this.store = new net.sf.j2s.store.HTML5LocalStorage ();
38
+ this.type = "local";
29
39
return;
30
40
} catch (error) {
31
41
}
@@ -39,23 +49,64 @@ private SimpleStore() {
39
49
}
40
50
var isOldIE = ua.indexOf ("msie 5.5") != -1 || ua.indexOf ("msie 5.0") != -1;
41
51
var cookieURL = window["j2s.xss.cookie.url"];
42
- if (!isLocal && cookieURL != null && !isOldIE) {
52
+ if (!isLocal && cookieURL != null && !isOldIE && (type == null || "xss" == type) ) {
43
53
this.store = new net.sf.j2s.store.XSSCookieStore(cookieURL);
54
+ this.type = "xss";
44
55
} 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
+ }
46
62
}
47
63
*/ {
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
+ }
50
71
}
51
72
}
52
73
53
74
public static SimpleStore getDefault () {
54
75
if (singleton == null ) {
55
- singleton = new SimpleStore ();
76
+ singleton = new SimpleStore (null );
56
77
}
57
78
return singleton ;
58
79
}
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
+ }
59
110
60
111
public String getProperty (String name ) {
61
112
return store .getProperty (name );
0 commit comments