TransientJcrPropertyMap.java
14.3 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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.granite.asset.api.AssetException
* javax.jcr.Binary
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.PropertyIterator
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.jcr.Value
* javax.jcr.ValueFormatException
* javax.jcr.nodetype.NodeType
* org.apache.jackrabbit.util.ISO9075
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ValueMap
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.granite.asset.core.impl;
import com.adobe.granite.asset.api.AssetException;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectStreamClass;
import java.io.Serializable;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.jcr.Binary;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.PropertyIterator;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Value;
import javax.jcr.ValueFormatException;
import javax.jcr.nodetype.NodeType;
import org.apache.jackrabbit.util.ISO9075;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.jcr.resource.internal.helper.JcrPropertyMapCacheEntry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/*
* This class specifies class file version 49.0 but uses Java 6 signatures. Assumed Java 6.
*/
public class TransientJcrPropertyMap
implements ValueMap {
private static final Logger log = LoggerFactory.getLogger(TransientJcrPropertyMap.class);
private Node node_;
private final ClassLoader dynamicClassLoader;
public TransientJcrPropertyMap(Resource resource) {
this(resource, null);
}
public TransientJcrPropertyMap(Resource resource, ClassLoader dynamicCL) {
this.node_ = (Node)resource.adaptTo(Node.class);
this.dynamicClassLoader = dynamicCL;
}
public void clear() {
throw new UnsupportedOperationException();
}
public boolean containsKey(Object key) {
try {
return this.node_.hasProperty(this.checkKey(key));
}
catch (RepositoryException e) {
throw new AssetException((Throwable)e);
}
}
public boolean containsValue(Object value) {
try {
PropertyIterator it = this.node_.getProperties();
while (it.hasNext()) {
Property prop = it.nextProperty();
if (!value.equals(this.getValue(prop))) continue;
return true;
}
return false;
}
catch (RepositoryException re) {
throw new AssetException((Throwable)re);
}
}
public Set<Map.Entry<String, Object>> entrySet() {
return Collections.unmodifiableSet(this.getPropertyMap().entrySet());
}
private Map<String, Object> getPropertyMap() {
HashMap<String, Object> propMap = new HashMap<String, Object>();
try {
PropertyIterator it = this.node_.getProperties();
while (it.hasNext()) {
Property prop = it.nextProperty();
propMap.put(prop.getName(), this.getValue(prop));
}
return propMap;
}
catch (RepositoryException re) {
throw new AssetException((Throwable)re);
}
}
private Object getValue(Property prop) throws RepositoryException {
if (prop == null) {
return null;
}
JcrPropertyMapCacheEntry entry = new JcrPropertyMapCacheEntry(prop);
return entry.getDefaultValueOrNull();
}
public Object get(Object key) {
if (!this.containsKey(key)) {
return null;
}
try {
return this.getValue(this.node_.getProperty(this.checkKey(key)));
}
catch (RepositoryException re) {
throw new AssetException((Throwable)re);
}
}
public boolean isEmpty() {
try {
return !this.node_.hasProperties();
}
catch (RepositoryException e) {
throw new AssetException((Throwable)e);
}
}
public Set<String> keySet() {
return Collections.unmodifiableSet(this.getPropertyMap().keySet());
}
private int count(Iterator it) {
int cnt = 0;
while (it.hasNext()) {
it.next();
++cnt;
}
return cnt;
}
public int size() {
try {
return this.count((Iterator)this.node_.getProperties());
}
catch (RepositoryException e) {
throw new AssetException((Throwable)e);
}
}
public Collection<Object> values() {
return Collections.unmodifiableCollection(this.getPropertyMap().values());
}
public <T> T get(String name, Class<T> type) {
if (!this.containsKey(name)) {
return null;
}
String key = this.checkKey(name);
if (type == null) {
return (T)this.get(key);
}
try {
Property p = this.node_.getProperty(key);
JcrPropertyMapCacheEntry entry = new JcrPropertyMapCacheEntry(p);
return this.convertToType(entry, type);
}
catch (RepositoryException re) {
throw new AssetException((Throwable)re);
}
}
public Object put(String obj, Object value) {
String name = this.checkKey(obj);
try {
Session session = this.node_.getSession();
JcrPropertyMapCacheEntry oldEntry = null;
if (this.node_.hasProperty(name)) {
oldEntry = new JcrPropertyMapCacheEntry(this.node_.getProperty(name));
}
JcrPropertyMapCacheEntry entry = new JcrPropertyMapCacheEntry(value, session);
String mixinTypesKey = ISO9075.decode((String)"jcr:mixinTypes");
if (mixinTypesKey.equals(name)) {
this.handleMixinTypes(this.node_, entry.values);
} else if (entry.isMulti) {
this.node_.setProperty(name, entry.values);
} else {
this.node_.setProperty(name, entry.values[0]);
}
return oldEntry == null ? null : oldEntry.getDefaultValueOrNull();
}
catch (RepositoryException re) {
throw new AssetException((Throwable)re);
}
}
private void handleMixinTypes(Node node, Value[] mixinTypes) throws RepositoryException {
HashSet<String> newTypes = new HashSet<String>();
if (mixinTypes != null) {
for (Value value : mixinTypes) {
newTypes.add(value.getString());
}
}
HashSet<String> oldTypes = new HashSet<String>();
for (NodeType mixinType : node.getMixinNodeTypes()) {
oldTypes.add(mixinType.getName());
}
for (String name : oldTypes) {
if (!newTypes.contains(name)) {
node.removeMixin(name);
continue;
}
newTypes.remove(name);
}
for (String name2 : newTypes) {
node.addMixin(name2);
}
}
public void putAll(Map<? extends String, ? extends Object> map) {
if (map != null) {
for (Map.Entry<? extends String, ? extends Object> entry : map.entrySet()) {
this.put(entry.getKey(), entry.getValue());
}
}
}
public Object remove(Object obj) {
String name = this.checkKey(obj);
String mixinTypesKey = ISO9075.decode((String)"jcr:mixinTypes");
try {
JcrPropertyMapCacheEntry oldEntry = null;
if (this.node_.hasProperty(name)) {
oldEntry = new JcrPropertyMapCacheEntry(this.node_.getProperty(name));
}
if (mixinTypesKey.equals(name)) {
this.handleMixinTypes(this.node_, null);
} else {
this.node_.setProperty(name, (String)null);
}
return oldEntry == null ? null : oldEntry.getDefaultValueOrNull();
}
catch (RepositoryException re) {
throw new AssetException((Throwable)re);
}
}
public <T> T get(String name, T defaultValue) {
String key = this.checkKey(name);
if (defaultValue == null) {
return (T)this.get(key);
}
Class type = this.normalizeClass(defaultValue.getClass());
Class value = this.get(key, (T)type);
if (value == null) {
value = defaultValue;
}
return (T)value;
}
String checkKey(Object key) {
if (key == null) {
throw new NullPointerException("Key must not be null.");
}
String name = key.toString();
if (name.startsWith("./")) {
return name.substring(2);
}
return name;
}
private <T> T convertToType(JcrPropertyMapCacheEntry entry, Class<T> type) {
?[] result = null;
try {
boolean array = type.isArray();
if (entry.isMulti) {
if (array) {
result = this.convertToArray(entry, type.getComponentType());
} else if (entry.values.length > 0) {
result = this.convertToType(entry, -1, entry.values[0], type);
}
} else {
result = array ? this.convertToArray(entry, type.getComponentType()) : this.convertToType(entry, -1, entry.values[0], type);
}
}
catch (ValueFormatException vfe) {
log.info("converToType: Cannot convert value of " + entry.getDefaultValueOrNull() + " to " + type, (Throwable)vfe);
}
catch (RepositoryException re) {
log.info("converToType: Cannot get value of " + entry.getDefaultValueOrNull(), (Throwable)re);
}
return (T)result;
}
private <T> T[] convertToArray(JcrPropertyMapCacheEntry entry, Class<T> type) throws ValueFormatException, RepositoryException {
ArrayList<T> values = new ArrayList<T>();
for (int i = 0; i < entry.values.length; ++i) {
T value = this.convertToType(entry, i, entry.values[i], type);
if (value == null) continue;
values.add(value);
}
Object[] result = (Object[])Array.newInstance(type, values.size());
return values.toArray(result);
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
private <T> T convertToType(JcrPropertyMapCacheEntry entry, int index, Value jcrValue, Class<T> type) throws ValueFormatException, RepositoryException {
Object defaultValue = entry.getDefaultValue();
if (type.isInstance(defaultValue)) {
return (T)defaultValue;
}
if (String.class == type) {
return (T)jcrValue.getString();
}
if (Byte.class == type) {
return (T)Byte.valueOf((byte)jcrValue.getLong());
}
if (Short.class == type) {
return (short)jcrValue.getLong();
}
if (Integer.class == type) {
return (int)jcrValue.getLong();
}
if (Long.class == type) {
if (jcrValue.getType() == 2) {
if (index == -1) {
return entry.property.getLength();
}
return entry.property.getLengths()[index];
}
return jcrValue.getLong();
}
if (Float.class == type) {
return (T)Float.valueOf((float)jcrValue.getDouble());
}
if (Double.class == type) {
return jcrValue.getDouble();
}
if (Boolean.class == type) {
return jcrValue.getBoolean();
}
if (Date.class == type) {
return (T)jcrValue.getDate().getTime();
}
if (Calendar.class == type) {
return (T)jcrValue.getDate();
}
if (Value.class == type) {
return (T)jcrValue;
}
if (Property.class == type) {
return (T)entry.property;
}
if (Serializable.class.isAssignableFrom(type) && jcrValue.getType() == 2) {
Binary binary = null;
java.io.ObjectInputStream ois = null;
try {
binary = jcrValue.getBinary();
ois = new ObjectInputStream(binary.getStream(), this.dynamicClassLoader);
Object obj = ois.readObject();
if (type.isInstance(obj)) {
Object object = obj;
return (T)object;
}
}
catch (ClassNotFoundException cnfe) {}
catch (IOException ioe) {}
finally {
if (ois != null) {
try {
ois.close();
}
catch (IOException ignore) {
// empty catch block
}
}
if (binary != null) {
binary.dispose();
}
}
}
return null;
}
private Class<?> normalizeClass(Class<?> type) {
if (Calendar.class.isAssignableFrom(type)) {
type = Calendar.class;
} else if (Date.class.isAssignableFrom(type)) {
type = Date.class;
} else if (Value.class.isAssignableFrom(type)) {
type = Value.class;
} else if (Property.class.isAssignableFrom(type)) {
type = Property.class;
}
return type;
}
/*
* This class specifies class file version 49.0 but uses Java 6 signatures. Assumed Java 6.
*/
private static class ObjectInputStream
extends java.io.ObjectInputStream {
private ClassLoader classloader;
public ObjectInputStream(InputStream in, ClassLoader classLoader) throws IOException {
super(in);
this.classloader = classLoader;
}
@Override
protected Class<?> resolveClass(ObjectStreamClass classDesc) throws IOException, ClassNotFoundException {
if (this.classloader != null) {
return this.classloader.loadClass(classDesc.getName());
}
return super.resolveClass(classDesc);
}
}
}