|
@@ -1,23 +1,20 @@ |
1 |
1 |
|
// This Source Code is in the Public Domain per: http://unlicense.org |
2 |
2 |
|
package org.litesoft.GWT.client.nonpublic; |
3 |
3 |
|
|
4 |
|
- |
import org.litesoft.commonfoundation.typeutils.*; |
5 |
|
- |
|
6 |
|
- |
import java.util.*; |
7 |
|
- |
|
8 |
4 |
|
import org.litesoft.GWT.client.*; |
|
5 |
+ |
import org.litesoft.commonfoundation.typeutils.*; |
9 |
6 |
|
import org.litesoft.core.delayed.*; |
10 |
7 |
|
import org.litesoft.logger.*; |
11 |
8 |
|
|
12 |
9 |
|
import com.google.gwt.event.logical.shared.*; |
13 |
10 |
|
import com.google.gwt.user.client.*; |
14 |
11 |
|
|
15 |
|
- |
public class CookieNet |
16 |
|
- |
{ |
|
12 |
+ |
import java.util.*; |
|
13 |
+ |
|
|
14 |
+ |
public class CookieNet { |
17 |
15 |
|
public static final String NO_GRAND_POOBAH_PREFIX = "noGP_"; |
18 |
16 |
|
|
19 |
|
- |
public interface Listener |
20 |
|
- |
{ |
|
17 |
+ |
public interface Listener { |
21 |
18 |
|
public void changes( CookieNetDelta pDelta ); |
22 |
19 |
|
} |
23 |
20 |
|
|
|
@@ -34,47 +31,36 @@ |
34 |
31 |
|
|
35 |
32 |
|
private static boolean mAllowUsToBeGrandPoobah = false; |
36 |
33 |
|
|
37 |
|
- |
public static void initialize( boolean pAllowUsToBeGrandPoobah ) |
38 |
|
- |
{ |
|
34 |
+ |
public static void initialize( boolean pAllowUsToBeGrandPoobah ) { |
39 |
35 |
|
mAllowUsToBeGrandPoobah = pAllowUsToBeGrandPoobah; |
40 |
36 |
|
String zName = WindowName.get(); |
41 |
|
- |
if ( zName == null ) |
42 |
|
- |
{ |
|
37 |
+ |
if ( zName == null ) { |
43 |
38 |
|
WindowName.add( new MyListener() ); |
44 |
|
- |
} |
45 |
|
- |
else |
46 |
|
- |
{ |
|
39 |
+ |
} else { |
47 |
40 |
|
Window.addCloseHandler( new MyListener() ); |
48 |
41 |
|
windowOpenedWithName(); |
49 |
42 |
|
} |
50 |
43 |
|
} |
51 |
44 |
|
|
52 |
|
- |
public static void add( Listener pListener ) |
53 |
|
- |
{ |
54 |
|
- |
if ( (pListener != null) && (-1 == indexOf( pListener )) ) |
55 |
|
- |
{ |
|
45 |
+ |
public static void add( Listener pListener ) { |
|
46 |
+ |
if ( (pListener != null) && (-1 == indexOf( pListener )) ) { |
56 |
47 |
|
sListeners = append( sListeners, pListener ); |
57 |
48 |
|
} |
58 |
49 |
|
} |
59 |
50 |
|
|
60 |
|
- |
public static void remove( Listener pListener ) |
61 |
|
- |
{ |
62 |
|
- |
if ( pListener != null ) |
63 |
|
- |
{ |
|
51 |
+ |
public static void remove( Listener pListener ) { |
|
52 |
+ |
if ( pListener != null ) { |
64 |
53 |
|
int zAt = indexOf( pListener ); |
65 |
|
- |
if ( zAt != -1 ) |
66 |
|
- |
{ |
|
54 |
+ |
if ( zAt != -1 ) { |
67 |
55 |
|
sListeners = removeAt( sListeners, zAt ); |
68 |
56 |
|
} |
69 |
57 |
|
} |
70 |
58 |
|
} |
71 |
59 |
|
|
72 |
|
- |
public static Map<String, String> dumpCookies() |
73 |
|
- |
{ |
|
60 |
+ |
public static Map<String, String> dumpCookies() { |
74 |
61 |
|
Collection<String> zNames = Cookies.getCookieNames(); |
75 |
62 |
|
Map<String, String> zCookies = new HashMap<String, String>(); |
76 |
|
- |
for ( String zName : zNames ) |
77 |
|
- |
{ |
|
63 |
+ |
for ( String zName : zNames ) { |
78 |
64 |
|
zCookies.put( zName, Cookies.getCookie( zName ) ); |
79 |
65 |
|
} |
80 |
66 |
|
return zCookies; |
|
@@ -82,30 +68,24 @@ |
82 |
68 |
|
|
83 |
69 |
|
private static int sLastNumber = 0; |
84 |
70 |
|
|
85 |
|
- |
public static int getNextNumber() |
86 |
|
- |
{ |
|
71 |
+ |
public static int getNextNumber() { |
87 |
72 |
|
int zNextNumber = --sLastNumber; |
88 |
73 |
|
return zNextNumber < -99999 ? (sLastNumber = -1) : zNextNumber; |
89 |
74 |
|
} |
90 |
75 |
|
|
91 |
|
- |
private static void windowClosedWithName() |
92 |
|
- |
{ |
|
76 |
+ |
private static void windowClosedWithName() { |
93 |
77 |
|
sClosing = true; |
94 |
78 |
|
setOurCookie( MSG_DEAD ); |
95 |
79 |
|
} |
96 |
80 |
|
|
97 |
|
- |
private static void windowOpenedWithName() |
98 |
|
- |
{ |
|
81 |
+ |
private static void windowOpenedWithName() { |
99 |
82 |
|
setOurCookie( MSG_NEW ); |
100 |
83 |
|
Long zPollAgainIn = checkForCookieChanges(); |
101 |
84 |
|
|
102 |
|
- |
if ( zPollAgainIn != null ) |
103 |
|
- |
{ |
104 |
|
- |
TimedRunnableManager.INSTANCE.runIn( new TimedRunnable() |
105 |
|
- |
{ |
|
85 |
+ |
if ( zPollAgainIn != null ) { |
|
86 |
+ |
TimedRunnableManager.INSTANCE.runIn( new TimedRunnable() { |
106 |
87 |
|
@Override |
107 |
|
- |
public Again runOnce() |
108 |
|
- |
{ |
|
88 |
+ |
public Again runOnce() { |
109 |
89 |
|
Long zPollAgainIn = checkForCookieChanges(); |
110 |
90 |
|
return (zPollAgainIn != null) ? new RunAgainIn( zPollAgainIn ) : null; |
111 |
91 |
|
} |
|
@@ -113,19 +93,16 @@ |
113 |
93 |
|
} |
114 |
94 |
|
} |
115 |
95 |
|
|
116 |
|
- |
private static void setOurCookie( String pMsg ) |
117 |
|
- |
{ |
|
96 |
+ |
private static void setOurCookie( String pMsg ) { |
118 |
97 |
|
String zName = WindowName.get(); |
119 |
98 |
|
setCookie( PREFIX_MEMBER + zName, sMyLastValue = pMsg ); |
120 |
99 |
|
} |
121 |
100 |
|
|
122 |
|
- |
private static void setCookie( String pCookieName, String pValue ) |
123 |
|
- |
{ |
|
101 |
+ |
private static void setCookie( String pCookieName, String pValue ) { |
124 |
102 |
|
Cookies.setCookie( pCookieName, pValue ); |
125 |
103 |
|
} |
126 |
104 |
|
|
127 |
|
- |
private static void removeCookie( String pCookieName ) |
128 |
|
- |
{ |
|
105 |
+ |
private static void removeCookie( String pCookieName ) { |
129 |
106 |
|
Cookies.removeCookie( pCookieName ); |
130 |
107 |
|
} |
131 |
108 |
|
|
|
@@ -136,8 +113,7 @@ |
136 |
113 |
|
private static String sMyLastValue = "?"; |
137 |
114 |
|
private static Set<String> sMembers = new HashSet<String>(); |
138 |
115 |
|
|
139 |
|
- |
private static CookieNetDelta createDelta( CurrentMemberState pNewState, String pWindowName ) |
140 |
|
- |
{ |
|
116 |
+ |
private static CookieNetDelta createDelta( CurrentMemberState pNewState, String pWindowName ) { |
141 |
117 |
|
CookieNetDelta zDelta = new CookieNetDelta(); |
142 |
118 |
|
sIamGrandPoobah = zDelta.setNowGrandPoobah( sIamGrandPoobah, pNewState.isGrandPoobah( pWindowName ) ); |
143 |
119 |
|
sMyLastValue = zDelta.setWasPoked( sMyLastValue, pNewState.getMemberValue( pWindowName ) ); |
|
@@ -150,33 +126,26 @@ |
150 |
126 |
|
/** |
151 |
127 |
|
* @return RunAgainIn |
152 |
128 |
|
*/ |
153 |
|
- |
private static Long checkForCookieChanges() |
154 |
|
- |
{ |
|
129 |
+ |
private static Long checkForCookieChanges() { |
155 |
130 |
|
CurrentMemberState zNewState = null; |
156 |
131 |
|
for ( CookieSnapshot zSnapshot = new CookieSnapshot( GRAND_POOBAH ); // |
157 |
132 |
|
!sClosing && !zSnapshot.getRawDocCookies().equals( sLastRawDocCookies ); // |
158 |
|
- |
zSnapshot = new CookieSnapshot( GRAND_POOBAH ) ) |
159 |
|
- |
{ |
|
133 |
+ |
zSnapshot = new CookieSnapshot( GRAND_POOBAH ) ) { |
160 |
134 |
|
sLastRawDocCookies = zSnapshot.getRawDocCookies(); |
161 |
135 |
|
zNewState = createNewState( zSnapshot ); |
162 |
136 |
|
} |
163 |
|
- |
if ( sClosing ) |
164 |
|
- |
{ |
|
137 |
+ |
if ( sClosing ) { |
165 |
138 |
|
return null; |
166 |
139 |
|
} |
167 |
|
- |
if ( zNewState != null ) |
168 |
|
- |
{ |
|
140 |
+ |
if ( zNewState != null ) { |
169 |
141 |
|
String zName = WindowName.get(); |
170 |
|
- |
if ( mAllowUsToBeGrandPoobah && !zNewState.hasGrandPoobah() ) |
171 |
|
- |
{ |
|
142 |
+ |
if ( mAllowUsToBeGrandPoobah && !zNewState.hasGrandPoobah() ) { |
172 |
143 |
|
String zCandidate = zNewState.getGrandPoobahCandidate(); |
173 |
|
- |
if ( zName.equals( zCandidate ) ) |
174 |
|
- |
{ |
|
144 |
+ |
if ( zName.equals( zCandidate ) ) { |
175 |
145 |
|
setCookie( GRAND_POOBAH, zName ); |
176 |
146 |
|
return checkForCookieChanges(); |
177 |
147 |
|
} |
178 |
|
- |
if ( zCandidate == null ) |
179 |
|
- |
{ |
|
148 |
+ |
if ( zCandidate == null ) { |
180 |
149 |
|
String zError = "No Members? from: " + zName; |
181 |
150 |
|
LOGGER.error.log( zError ); |
182 |
151 |
|
} |
|
@@ -184,35 +153,27 @@ |
184 |
153 |
|
} |
185 |
154 |
|
CookieNetDelta zDelta = createDelta( zNewState, zName ); |
186 |
155 |
|
Listener[] zListeners = sListeners; // Snag the List |
187 |
|
- |
for ( Listener zListener : zListeners ) |
188 |
|
- |
{ |
|
156 |
+ |
for ( Listener zListener : zListeners ) { |
189 |
157 |
|
zListener.changes( zDelta ); |
190 |
158 |
|
} |
191 |
159 |
|
} |
192 |
160 |
|
return NORMAL_POLL_TIME; |
193 |
161 |
|
} |
194 |
162 |
|
|
195 |
|
- |
private static CurrentMemberState createNewState( CookieSnapshot pSnapshot ) |
196 |
|
- |
{ |
|
163 |
+ |
private static CurrentMemberState createNewState( CookieSnapshot pSnapshot ) { |
197 |
164 |
|
String zGrandPoobah = pSnapshot.getPrimaryCookieValue(); |
198 |
165 |
|
CurrentMemberState rv = new CurrentMemberState(); |
199 |
|
- |
for ( String zName : Cookies.getCookieNames() ) |
200 |
|
- |
{ |
201 |
|
- |
if ( zName.startsWith( PREFIX_MEMBER ) ) |
202 |
|
- |
{ |
|
166 |
+ |
for ( String zName : Cookies.getCookieNames() ) { |
|
167 |
+ |
if ( zName.startsWith( PREFIX_MEMBER ) ) { |
203 |
168 |
|
String zWindowName = zName.substring( PREFIX_MEMBER.length() ); |
204 |
169 |
|
String zValue = Strings.deNull( Cookies.getCookie( zName ) ); |
205 |
|
- |
if ( (zValue.length() == 0) || MSG_DEAD.equals( zValue ) ) |
206 |
|
- |
{ |
|
170 |
+ |
if ( (zValue.length() == 0) || MSG_DEAD.equals( zValue ) ) { |
207 |
171 |
|
removeCookie( zName ); |
208 |
|
- |
if ( zWindowName.equals( zGrandPoobah ) ) |
209 |
|
- |
{ |
|
172 |
+ |
if ( zWindowName.equals( zGrandPoobah ) ) { |
210 |
173 |
|
removeCookie( GRAND_POOBAH ); |
211 |
174 |
|
zGrandPoobah = null; |
212 |
175 |
|
} |
213 |
|
- |
} |
214 |
|
- |
else |
215 |
|
- |
{ |
|
176 |
+ |
} else { |
216 |
177 |
|
rv.addMember( zWindowName, zValue ); |
217 |
178 |
|
} |
218 |
179 |
|
} |
|
@@ -223,10 +184,8 @@ |
223 |
184 |
|
|
224 |
185 |
|
private static Listener[] sListeners = new Listener[0]; |
225 |
186 |
|
|
226 |
|
- |
private static int indexOf( Listener pListener ) |
227 |
|
- |
{ |
228 |
|
- |
for ( int i = 0; i < sListeners.length; i++ ) |
229 |
|
- |
{ |
|
187 |
+ |
private static int indexOf( Listener pListener ) { |
|
188 |
+ |
for ( int i = 0; i < sListeners.length; i++ ) { |
230 |
189 |
|
Listener zListener = sListeners[i]; |
231 |
190 |
|
if ( zListener == pListener ) // Identity |
232 |
191 |
|
{ |
|
@@ -236,11 +195,9 @@ |
236 |
195 |
|
return -1; |
237 |
196 |
|
} |
238 |
197 |
|
|
239 |
|
- |
private static Listener[] append( Listener[] pArray, Listener pListener ) |
240 |
|
- |
{ |
|
198 |
+ |
private static Listener[] append( Listener[] pArray, Listener pListener ) { |
241 |
199 |
|
int zLength = pArray.length; |
242 |
|
- |
if ( zLength == 0 ) |
243 |
|
- |
{ |
|
200 |
+ |
if ( zLength == 0 ) { |
244 |
201 |
|
return new Listener[]{pListener}; |
245 |
202 |
|
} |
246 |
203 |
|
Listener[] zNew = new Listener[zLength + 1]; |
|
@@ -249,89 +206,71 @@ |
249 |
206 |
|
return zNew; |
250 |
207 |
|
} |
251 |
208 |
|
|
252 |
|
- |
private static Listener[] removeAt( Listener[] pArray, int pIndexToRemove ) |
253 |
|
- |
{ |
|
209 |
+ |
private static Listener[] removeAt( Listener[] pArray, int pIndexToRemove ) { |
254 |
210 |
|
int zNewLength = pArray.length - 1; |
255 |
211 |
|
Listener[] zNew = new Listener[zNewLength]; |
256 |
|
- |
if ( pIndexToRemove != 0 ) |
257 |
|
- |
{ |
|
212 |
+ |
if ( pIndexToRemove != 0 ) { |
258 |
213 |
|
System.arraycopy( pArray, 0, zNew, 0, pIndexToRemove ); |
259 |
214 |
|
} |
260 |
|
- |
if ( pIndexToRemove != zNewLength ) |
261 |
|
- |
{ |
|
215 |
+ |
if ( pIndexToRemove != zNewLength ) { |
262 |
216 |
|
System.arraycopy( pArray, pIndexToRemove + 1, zNew, pIndexToRemove, zNewLength - pIndexToRemove ); |
263 |
217 |
|
} |
264 |
218 |
|
return zNew; |
265 |
219 |
|
} |
266 |
220 |
|
|
267 |
221 |
|
private static class MyListener implements CloseHandler<Window>, |
268 |
|
- |
WindowName.Listener |
269 |
|
- |
{ |
|
222 |
+ |
WindowName.Listener { |
270 |
223 |
|
@Override |
271 |
|
- |
public void windowNameChanged() |
272 |
|
- |
{ |
|
224 |
+ |
public void windowNameChanged() { |
273 |
225 |
|
WindowName.remove( this ); |
274 |
226 |
|
Window.addCloseHandler( this ); |
275 |
227 |
|
windowOpenedWithName(); |
276 |
228 |
|
} |
277 |
229 |
|
|
278 |
230 |
|
@Override |
279 |
|
- |
public void onClose( CloseEvent<Window> event ) |
280 |
|
- |
{ |
|
231 |
+ |
public void onClose( CloseEvent<Window> event ) { |
281 |
232 |
|
windowClosedWithName(); |
282 |
233 |
|
} |
283 |
234 |
|
} |
284 |
235 |
|
|
285 |
|
- |
private static class CurrentMemberState |
286 |
|
- |
{ |
|
236 |
+ |
private static class CurrentMemberState { |
287 |
237 |
|
private String mGrandPoobah = null; |
288 |
238 |
|
private Map<String, String> mMembers = new HashMap<String, String>(); |
289 |
239 |
|
|
290 |
|
- |
public boolean hasGrandPoobah() |
291 |
|
- |
{ |
|
240 |
+ |
public boolean hasGrandPoobah() { |
292 |
241 |
|
return mGrandPoobah != null; |
293 |
242 |
|
} |
294 |
243 |
|
|
295 |
|
- |
public String getGrandPoobah() |
296 |
|
- |
{ |
|
244 |
+ |
public String getGrandPoobah() { |
297 |
245 |
|
return mGrandPoobah; |
298 |
246 |
|
} |
299 |
247 |
|
|
300 |
|
- |
public void setGrandPoobah( String pGrandPoobah ) |
301 |
|
- |
{ |
|
248 |
+ |
public void setGrandPoobah( String pGrandPoobah ) { |
302 |
249 |
|
mGrandPoobah = pGrandPoobah; |
303 |
250 |
|
} |
304 |
251 |
|
|
305 |
|
- |
public void addMember( String pWindowName, String pValue ) |
306 |
|
- |
{ |
|
252 |
+ |
public void addMember( String pWindowName, String pValue ) { |
307 |
253 |
|
mMembers.put( pWindowName, pValue ); |
308 |
254 |
|
} |
309 |
255 |
|
|
310 |
|
- |
public boolean isGrandPoobah( String pWindowName ) |
311 |
|
- |
{ |
|
256 |
+ |
public boolean isGrandPoobah( String pWindowName ) { |
312 |
257 |
|
return hasGrandPoobah() && mGrandPoobah.equals( pWindowName ); |
313 |
258 |
|
} |
314 |
259 |
|
|
315 |
|
- |
public String getMemberValue( String pWindowName ) |
316 |
|
- |
{ |
|
260 |
+ |
public String getMemberValue( String pWindowName ) { |
317 |
261 |
|
return mMembers.get( pWindowName ); |
318 |
262 |
|
} |
319 |
263 |
|
|
320 |
|
- |
public Set<String> getMembers() |
321 |
|
- |
{ |
|
264 |
+ |
public Set<String> getMembers() { |
322 |
265 |
|
return mMembers.keySet(); |
323 |
266 |
|
} |
324 |
267 |
|
|
325 |
|
- |
public String getGrandPoobahCandidate() |
326 |
|
- |
{ |
327 |
|
- |
if ( !hasGrandPoobah() && !mMembers.isEmpty() ) |
328 |
|
- |
{ |
|
268 |
+ |
public String getGrandPoobahCandidate() { |
|
269 |
+ |
if ( !hasGrandPoobah() && !mMembers.isEmpty() ) { |
329 |
270 |
|
ArrayList<String> zMemberWinNames = new ArrayList<String>( mMembers.keySet() ); |
330 |
271 |
|
Collections.sort( zMemberWinNames ); |
331 |
|
- |
for ( String zWinName : zMemberWinNames ) |
332 |
|
- |
{ |
333 |
|
- |
if ( !zWinName.startsWith( NO_GRAND_POOBAH_PREFIX ) ) |
334 |
|
- |
{ |
|
272 |
+ |
for ( String zWinName : zMemberWinNames ) { |
|
273 |
+ |
if ( !zWinName.startsWith( NO_GRAND_POOBAH_PREFIX ) ) { |
335 |
274 |
|
return zWinName; |
336 |
275 |
|
} |
337 |
276 |
|
} |