ICUService.java
12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
* Decompiled with CFR 0_118.
*/
package com.adobe.agl.impl;
import com.adobe.agl.impl.ICUDebug;
import com.adobe.agl.impl.ICUNotifier;
import com.adobe.agl.impl.ICURWLock;
import com.adobe.agl.util.ULocale;
import java.lang.ref.SoftReference;
import java.util.*;
public class ICUService
extends ICUNotifier {
protected final String name;
private static final boolean DEBUG = ICUDebug.enabled("service");
private final ICURWLock factoryLock = new ICURWLock();
private final List factories = new ArrayList();
private int defaultSize = 0;
private SoftReference cacheref;
private SoftReference idref;
private LocaleRef dnref;
public ICUService(String name) {
this.name = name;
}
public Object getKey(Key key) {
return this.getKey(key, null);
}
public Object getKey(Key key, String[] actualReturn) {
return this.getKey(key, actualReturn, null);
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public Object getKey(Key key, String[] actualReturn, Factory factory) {
if (this.factories.size() == 0) {
return this.handleDefault(key, actualReturn);
}
if (DEBUG) {
System.out.println("Service: " + this.name + " key: " + key.canonicalID());
}
CacheEntry result = null;
if (key != null) {
try {
this.factoryLock.acquireRead();
Map<String, CacheEntry> cache = null;
SoftReference cref = this.cacheref;
if (cref != null) {
if (DEBUG) {
System.out.println("Service " + this.name + " ref exists");
}
cache = (Map<String, CacheEntry>)cref.get();
}
if (cache == null) {
if (DEBUG) {
System.out.println("Service " + this.name + " cache was empty");
}
cache = Collections.synchronizedMap(new HashMap());
cref = new SoftReference(cache);
}
String currentDescriptor = null;
ArrayList<String> cacheDescriptorList = null;
boolean putInCache = false;
int NDebug = 0;
int startIndex = 0;
int limit = this.factories.size();
boolean cacheResult = true;
if (factory != null) {
for (int i = 0; i < limit; ++i) {
if (factory != this.factories.get(i)) continue;
startIndex = i + 1;
break;
}
if (startIndex == 0) {
throw new IllegalStateException("Factory " + factory + "not registered with service: " + this);
}
cacheResult = false;
}
block4 : do {
currentDescriptor = key.currentDescriptor();
if (DEBUG) {
System.out.println(this.name + "[" + NDebug++ + "] looking for: " + currentDescriptor);
}
if ((result = (CacheEntry)cache.get(currentDescriptor)) != null) {
if (!DEBUG) break;
System.out.println(this.name + " found with descriptor: " + currentDescriptor);
break;
}
if (DEBUG) {
System.out.println("did not find: " + currentDescriptor + " in cache");
}
putInCache = cacheResult;
int index = startIndex;
while (index < limit) {
Object service;
Factory f = (Factory)this.factories.get(index++);
if (DEBUG) {
System.out.println("trying factory[" + (index - 1) + "] " + f.toString());
}
if ((service = f.create(key, this)) != null) {
result = new CacheEntry(currentDescriptor, service);
if (!DEBUG) break block4;
System.out.println(this.name + " factory supported: " + currentDescriptor + ", caching");
break block4;
}
if (!DEBUG) continue;
System.out.println("factory did not support: " + currentDescriptor);
}
if (cacheDescriptorList == null) {
cacheDescriptorList = new ArrayList<String>(5);
}
cacheDescriptorList.add(currentDescriptor);
} while (key.fallback());
if (result != null) {
if (putInCache) {
if (DEBUG) {
System.out.println("caching '" + result.actualDescriptor + "'");
}
cache.put(result.actualDescriptor, result);
if (cacheDescriptorList != null) {
Iterator iter = cacheDescriptorList.iterator();
while (iter.hasNext()) {
String desc = (String)iter.next();
if (DEBUG) {
System.out.println(this.name + " adding descriptor: '" + desc + "' for actual: '" + result.actualDescriptor + "'");
}
cache.put(desc, result);
}
}
this.cacheref = cref;
}
if (actualReturn != null) {
actualReturn[0] = result.actualDescriptor.indexOf("/") == 0 ? result.actualDescriptor.substring(1) : result.actualDescriptor;
}
if (DEBUG) {
System.out.println("found in service: " + this.name);
}
Object iter = result.service;
return iter;
}
}
finally {
this.factoryLock.releaseRead();
}
}
if (DEBUG) {
System.out.println("not found in service: " + this.name);
}
return this.handleDefault(key, actualReturn);
}
protected Object handleDefault(Key key, String[] actualIDReturn) {
return null;
}
public Set getVisibleIDs() {
return this.getVisibleIDs(null);
}
public Set getVisibleIDs(String matchID) {
Set result = this.getVisibleIDMap().keySet();
Key fallbackKey = this.createKey(matchID);
if (fallbackKey != null) {
HashSet temp = new HashSet(result.size());
Iterator iter = result.iterator();
while (iter.hasNext()) {
String id = (String)iter.next();
if (!fallbackKey.isFallbackOf(id)) continue;
temp.add((String)id);
}
result = temp;
}
return result;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
private Map getVisibleIDMap() {
Map idcache = null;
SoftReference ref = this.idref;
if (ref != null) {
idcache = (HashMap)ref.get();
}
while (idcache == null) {
ICUService iCUService = this;
synchronized (iCUService) {
if (ref == this.idref || this.idref == null) {
try {
this.factoryLock.acquireRead();
idcache = new HashMap();
ListIterator lIter = this.factories.listIterator(this.factories.size());
while (lIter.hasPrevious()) {
Factory f = (Factory)lIter.previous();
f.updateVisibleIDs(idcache);
}
idcache = Collections.unmodifiableMap(idcache);
this.idref = new SoftReference(idcache);
}
finally {
this.factoryLock.releaseRead();
}
}
ref = this.idref;
idcache = (Map)ref.get();
continue;
}
}
return idcache;
}
public String getDisplayName(String id, ULocale locale) {
Map m = this.getVisibleIDMap();
Factory f = (Factory)m.get(id);
if (f != null) {
return f.getDisplayName(id, locale);
}
Key key = this.createKey(id);
while (key.fallback()) {
f = (Factory)m.get(key.currentID());
if (f == null) continue;
return f.getDisplayName(id, locale);
}
return null;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public final Factory registerFactory(Factory factory) {
if (factory == null) {
throw new NullPointerException();
}
try {
this.factoryLock.acquireWrite();
this.factories.add(0, factory);
this.clearCaches();
}
finally {
this.factoryLock.releaseWrite();
}
this.notifyChanged();
return factory;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
public final boolean unregisterFactory(Factory factory) {
boolean result;
if (factory == null) {
throw new NullPointerException();
}
result = false;
try {
this.factoryLock.acquireWrite();
if (this.factories.remove(factory)) {
result = true;
this.clearCaches();
}
}
finally {
this.factoryLock.releaseWrite();
}
if (result) {
this.notifyChanged();
}
return result;
}
public boolean isDefault() {
return this.factories.size() == this.defaultSize;
}
protected void markDefault() {
this.defaultSize = this.factories.size();
}
public Key createKey(String id) {
return id == null ? null : new Key(id);
}
protected void clearCaches() {
this.cacheref = null;
this.idref = null;
this.dnref = null;
}
protected void clearServiceCache() {
this.cacheref = null;
}
protected void notifyListener(EventListener l) {
((ServiceListener)l).serviceChanged(this);
}
public String toString() {
return Object.super.toString() + "{" + this.name + "}";
}
public static interface ServiceListener
extends EventListener {
public void serviceChanged(ICUService var1);
}
private static class LocaleRef {
}
private static final class CacheEntry {
final String actualDescriptor;
final Object service;
CacheEntry(String actualDescriptor, Object service) {
this.actualDescriptor = actualDescriptor;
this.service = service;
}
}
public static interface Factory {
public Object create(Key var1, ICUService var2);
public void updateVisibleIDs(Map var1);
public String getDisplayName(String var1, ULocale var2);
}
public static class Key {
private final String id;
public Key(String id) {
this.id = id;
}
public String canonicalID() {
return this.id;
}
public String currentID() {
return this.canonicalID();
}
public String currentDescriptor() {
return "/" + this.currentID();
}
public boolean fallback() {
return false;
}
public boolean isFallbackOf(String idToCheck) {
return this.canonicalID().equals(idToCheck);
}
}
}