1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
package org.apache.tapestry.record; |
16 |
|
|
17 |
|
import org.apache.commons.codec.binary.Base64; |
18 |
|
import org.apache.hivemind.ApplicationRuntimeException; |
19 |
|
import org.apache.hivemind.ClassResolver; |
20 |
|
import org.apache.hivemind.HiveMind; |
21 |
|
import org.apache.hivemind.util.Defense; |
22 |
|
import org.apache.tapestry.util.io.ResolvingObjectInputStream; |
23 |
|
import org.apache.tapestry.util.io.TeeOutputStream; |
24 |
|
|
25 |
|
import java.io.*; |
26 |
|
import java.util.ArrayList; |
27 |
|
import java.util.Collections; |
28 |
|
import java.util.Iterator; |
29 |
|
import java.util.List; |
30 |
|
import java.util.zip.GZIPInputStream; |
31 |
|
import java.util.zip.GZIPOutputStream; |
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
11 |
public class PersistentPropertyDataEncoderImpl implements PersistentPropertyDataEncoder { |
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
public static final String BYTESTREAM_PREFIX = "B"; |
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
public static final String GZIP_BYTESTREAM_PREFIX = "Z"; |
54 |
|
|
55 |
|
protected ClassResolver _classResolver; |
56 |
|
|
57 |
|
public String encodePageChanges(List changes) |
58 |
|
{ |
59 |
5 |
Defense.notNull(changes, "changes"); |
60 |
|
|
61 |
5 |
if (changes.isEmpty()) |
62 |
1 |
return ""; |
63 |
|
|
64 |
|
try { |
65 |
4 |
ByteArrayOutputStream bosPlain = new ByteArrayOutputStream(); |
66 |
4 |
ByteArrayOutputStream bosCompressed = new ByteArrayOutputStream(); |
67 |
|
|
68 |
4 |
GZIPOutputStream gos = new GZIPOutputStream(bosCompressed); |
69 |
|
|
70 |
4 |
TeeOutputStream tos = new TeeOutputStream(bosPlain, gos); |
71 |
|
|
72 |
4 |
ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(tos)); |
73 |
|
|
74 |
4 |
writeChangesToStream(changes, oos); |
75 |
|
|
76 |
3 |
oos.close(); |
77 |
|
|
78 |
3 |
boolean useCompressed = bosCompressed.size() < bosPlain.size(); |
79 |
|
|
80 |
3 |
byte[] data = useCompressed ? bosCompressed.toByteArray() : bosPlain.toByteArray(); |
81 |
|
|
82 |
3 |
byte[] encoded = Base64.encodeBase64(data); |
83 |
|
|
84 |
3 |
String prefix = useCompressed ? GZIP_BYTESTREAM_PREFIX : BYTESTREAM_PREFIX; |
85 |
|
|
86 |
3 |
return prefix + new String(encoded); |
87 |
|
} |
88 |
1 |
catch (Exception ex) { |
89 |
1 |
throw new ApplicationRuntimeException(RecordMessages.encodeFailure(ex), ex); |
90 |
|
} |
91 |
|
} |
92 |
|
|
93 |
|
public List decodePageChanges(String encoded) |
94 |
|
{ |
95 |
6 |
if (HiveMind.isBlank(encoded)) |
96 |
1 |
return Collections.EMPTY_LIST; |
97 |
|
|
98 |
5 |
String prefix = encoded.substring(0, 1); |
99 |
|
|
100 |
5 |
if (!(prefix.equals(BYTESTREAM_PREFIX) || prefix.equals(GZIP_BYTESTREAM_PREFIX))) |
101 |
1 |
throw new ApplicationRuntimeException(RecordMessages.unknownPrefix(prefix)); |
102 |
|
|
103 |
|
try { |
104 |
|
|
105 |
|
|
106 |
4 |
byte[] decoded = Base64.decodeBase64(encoded.substring(1).getBytes()); |
107 |
|
|
108 |
4 |
InputStream is = new ByteArrayInputStream(decoded); |
109 |
|
|
110 |
4 |
if (prefix.equals(GZIP_BYTESTREAM_PREFIX)) |
111 |
3 |
is = new GZIPInputStream(is); |
112 |
|
|
113 |
|
|
114 |
|
|
115 |
|
|
116 |
|
|
117 |
|
|
118 |
|
|
119 |
3 |
ObjectInputStream ois = new ResolvingObjectInputStream(_classResolver, new BufferedInputStream(is)); |
120 |
|
|
121 |
3 |
List result = readChangesFromStream(ois); |
122 |
|
|
123 |
3 |
ois.close(); |
124 |
|
|
125 |
3 |
return result; |
126 |
|
} |
127 |
1 |
catch (Exception ex) { |
128 |
1 |
throw new ApplicationRuntimeException(RecordMessages.decodeFailure(ex), ex); |
129 |
|
} |
130 |
|
} |
131 |
|
|
132 |
|
protected void writeChangesToStream(List changes, ObjectOutputStream oos) |
133 |
|
throws IOException |
134 |
|
{ |
135 |
4 |
oos.writeInt(changes.size()); |
136 |
|
|
137 |
4 |
Iterator i = changes.iterator(); |
138 |
26 |
while (i.hasNext()) { |
139 |
23 |
PropertyChange pc = (PropertyChange) i.next(); |
140 |
|
|
141 |
23 |
String componentPath = pc.getComponentPath(); |
142 |
23 |
String propertyName = pc.getPropertyName(); |
143 |
23 |
Object value = pc.getNewValue(); |
144 |
|
|
145 |
23 |
oos.writeBoolean(componentPath != null); |
146 |
|
|
147 |
23 |
if (componentPath != null) |
148 |
11 |
oos.writeUTF(componentPath); |
149 |
|
|
150 |
23 |
oos.writeUTF(propertyName); |
151 |
|
|
152 |
23 |
oos.writeObject(value); |
153 |
22 |
} |
154 |
3 |
} |
155 |
|
|
156 |
|
protected List readChangesFromStream(ObjectInputStream ois) |
157 |
|
throws IOException, ClassNotFoundException |
158 |
|
{ |
159 |
3 |
List result = new ArrayList(); |
160 |
|
|
161 |
3 |
int count = ois.readInt(); |
162 |
|
|
163 |
25 |
for (int i = 0; i < count; i++) { |
164 |
22 |
boolean hasPath = ois.readBoolean(); |
165 |
22 |
String componentPath = hasPath ? ois.readUTF() : null; |
166 |
22 |
String propertyName = ois.readUTF(); |
167 |
22 |
Object value = ois.readObject(); |
168 |
|
|
169 |
22 |
PropertyChangeImpl pc = new PropertyChangeImpl(componentPath, propertyName, value); |
170 |
|
|
171 |
22 |
result.add(pc); |
172 |
|
} |
173 |
|
|
174 |
3 |
return result; |
175 |
|
} |
176 |
|
|
177 |
|
public void setClassResolver(ClassResolver resolver) |
178 |
|
{ |
179 |
11 |
_classResolver = resolver; |
180 |
11 |
} |
181 |
|
} |