Skip to content

Commit cedc732

Browse files
author
zhourenjian
committed
Support WLL202 for class name mappings
Fixed bug on sub SimpleSerializable object with bytes field
1 parent dc5b0e1 commit cedc732

File tree

5 files changed

+104
-14
lines changed

5 files changed

+104
-14
lines changed

sources/net.sf.j2s.ajax/ajaxpipe/net/sf/j2s/ajax/SimplePipeHelper.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,14 @@ public static void pipeIn(String key, SimpleSerializable[] ss) {
200200
System.out.println("There are no pipe listening?!!!!");
201201
return; // throw exception?
202202
}
203+
int v = pipe.getSimpleVersion();
203204
synchronized (list) {
204205
boolean hasNewPriority = false;
205206
for (int i = 0; i < ss.length; i++) {
206207
SimpleSerializable s = ss[i];
208+
if (v >= 202) {
209+
s.setSimpleVersion(v);
210+
}
207211
if (s instanceof ISimpleCacheable) {
208212
ISimpleCacheable c = (ISimpleCacheable) s;
209213
if (c.isCached()) {

sources/net.sf.j2s.ajax/ajaxpipe/net/sf/j2s/ajax/SimplePipeRequest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ public static interface IHttpPipeRequestFactory extends IHttpRequestFactory {
3939

4040
}
4141

42+
/**
43+
* @j2sNative
44+
*/
45+
static {
46+
SimpleSerializable.registerShortClassName(SimplePipeSequence.class.getName(), "SPS");
47+
}
48+
4249
@J2SIgnore
4350
private static Object notifyingMutex = new Object();
4451

sources/net.sf.j2s.ajax/ajaxpipe/net/sf/j2s/ajax/SimplePipeRunnable.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ public SimpleSerializable[] through(Object ... args) {
416416
*/
417417
public boolean deal(SimpleSerializable ss) {
418418
try {
419-
Class<? extends SimpleSerializable> clazz = ss.getClass();
419+
Class<?> clazz = ss.getClass();
420420
if ("net.sf.j2s.ajax.SimpleSerializable".equals(clazz.getName())) {
421421
return true; // seldom or never reach this branch, just ignore
422422
}
@@ -439,16 +439,22 @@ public boolean deal(SimpleSerializable ss) {
439439
clazzName = clzz.getName();
440440
}
441441
if (clzz != null) {
442-
method = clzz.getMethod("deal", clazz);
443-
if (method != null) {
444-
Class<?> returnType = method.getReturnType();
445-
if (returnType == boolean.class) {
446-
Object result = method.invoke(this, ss);
447-
return ((Boolean) result).booleanValue();
442+
do {
443+
try {
444+
method = clzz.getMethod("deal", clazz);
445+
} catch (Exception e) {
448446
}
449-
}
447+
if (method != null) {
448+
Class<?> returnType = method.getReturnType();
449+
if (returnType == boolean.class) {
450+
Object result = method.invoke(this, ss);
451+
return ((Boolean) result).booleanValue();
452+
}
453+
}
454+
clazz = clazz.getSuperclass();
455+
} while (clazz != null);
450456
}
451-
} catch (Exception e) {
457+
} catch (Throwable e) {
452458
e.printStackTrace();
453459
}
454460
return false; // unknown object

sources/net.sf.j2s.ajax/ajaxpipe/net/sf/j2s/ajax/SimplePipeSequence.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@
22

33
public final class SimplePipeSequence extends SimpleSerializable {
44

5+
private static String[] mappings = new String[] {
6+
"sequence", "s"
7+
};
8+
59
public long sequence;
610

11+
@Override
12+
protected String[] fieldMapping() {
13+
if (getSimpleVersion() >= 202) {
14+
return mappings;
15+
}
16+
return null;
17+
}
18+
719
}

sources/net.sf.j2s.ajax/ajaxrpc/net/sf/j2s/ajax/SimpleSerializable.java

Lines changed: 66 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,40 @@ public class SimpleSerializable implements Cloneable {
4646

4747
public static boolean JSON_EXPAND_MODE = true;
4848

49+
public static int LATEST_SIMPLE_VERSION = 202;
50+
4951
@J2SIgnore
5052
private static Object mutex = new Object();
5153
@J2SIgnore
5254
private static Map<String, Map<String, Field>> quickFields = new HashMap<String, Map<String, Field>>();
5355
@J2SIgnore
5456
private static Set<String> expiredClasses = new HashSet<String>();
5557

58+
@J2SIgnore
59+
private static Object classMutex = new Object();
60+
@J2SIgnore
61+
private static Map<String, String> classMappings = new HashMap<String, String>();
62+
@J2SIgnore
63+
private static Map<String, String> rClassMappings = new HashMap<String, String>();
64+
65+
private int simpleVersion;
66+
67+
public int getSimpleVersion() {
68+
if (simpleVersion <= 0) {
69+
return 201;
70+
}
71+
return simpleVersion;
72+
}
73+
74+
public void setSimpleVersion(int ver) {
75+
simpleVersion = ver;
76+
if (ver < 100) {
77+
simpleVersion = 201;
78+
} else if (simpleVersion >= 1000) {
79+
simpleVersion = 201;
80+
}
81+
}
82+
5683
@J2SIgnore
5784
private static class DeserializeObject {
5885
Object object;
@@ -68,6 +95,14 @@ public DeserializeObject(Object object, int index, boolean error) {
6895

6996
};
7097

98+
@J2SIgnore
99+
public static void registerShortClassName(String clazzName, String shortName) {
100+
synchronized (classMutex) {
101+
classMappings.put(clazzName, shortName);
102+
rClassMappings.put(shortName, clazzName);
103+
}
104+
}
105+
71106
@J2SIgnore
72107
static Map<String, Field> getSerializableFields(String clazzName, Class<?> clazz, boolean forceUpdate) {
73108
Map<String, Field> fields = forceUpdate ? null : quickFields.get(clazzName);
@@ -371,7 +406,8 @@ private String serialize(SimpleFilter filter, List<SimpleSerializable> ssObjs) {
371406
* "WLL" is used to mark Simple RPC, 100 is version 1.0.0,
372407
* # is used to mark the the beginning of serialized data
373408
*/
374-
buffer.append("WLL201");
409+
buffer.append("WLL");
410+
buffer.append(getSimpleVersion());
375411
Class<?> clazz = this.getClass();
376412
String clazzName = clazz.getName();
377413
int idx = -1;
@@ -388,7 +424,16 @@ private String serialize(SimpleFilter filter, List<SimpleSerializable> ssObjs) {
388424
}
389425
clazzName = clazz.getName();
390426
}
391-
buffer.append(clazzName);
427+
if (getSimpleVersion() >= 202) {
428+
String shortClazzName = classMappings.get(clazzName);
429+
if (shortClazzName != null) {
430+
buffer.append(shortClazzName);
431+
} else {
432+
buffer.append(clazzName);
433+
}
434+
} else {
435+
buffer.append(clazzName);
436+
}
392437
buffer.append("#00000000$"); // later the number of size will be updated!
393438
int headSize = buffer.length();
394439

@@ -1933,6 +1978,10 @@ private boolean deserialize(final String str, int start, List<SimpleSerializable
19331978
int end = str.length();
19341979
int length = end - start;
19351980
if (length <= 7 || !("WLL".equals(str.substring(start, start + 3)))) return false; // Should throw exception!
1981+
try {
1982+
setSimpleVersion(Integer.parseInt(str.substring(start + 3, start + 6)));
1983+
} catch (NumberFormatException e1) {
1984+
}
19361985
int index = str.indexOf('#', start);
19371986
if (index == -1) return false; // Should throw exception!
19381987
index++;
@@ -2105,7 +2154,7 @@ private boolean deserialize(final String str, int start, List<SimpleSerializable
21052154
ss[i] = new String(Base64.base64ToByteArray(ss[i]), "utf-8");
21062155
} else if (c4 == 'U') {
21072156
ss[i] = new String(ss[i].getBytes("iso-8859-1"), "utf-8");
2108-
} else {
2157+
} else if (c4 != 'O') {
21092158
ss[i] = new String(ss[i].getBytes("iso-8859-1"), "utf-8");
21102159
}
21112160
}
@@ -2434,7 +2483,7 @@ private DeserializeObject deserializeArrayItem(String str, int index, int end, L
24342483
ss[i] = new String(Base64.base64ToByteArray(ss[i]), "utf-8");
24352484
} else if (c4 == 'U') {
24362485
ss[i] = new String(ss[i].getBytes("iso-8859-1"), "utf-8");
2437-
} else {
2486+
} else if (c4 != 'O') {
24382487
ss[i] = new String(ss[i].getBytes("iso-8859-1"), "utf-8");
24392488
}
24402489
}
@@ -2867,7 +2916,8 @@ protected boolean bytesCompactMode() {
28672916
*/
28682917
@J2SIgnore
28692918
public Object clone() throws CloneNotSupportedException {
2870-
Object clone = super.clone();
2919+
SimpleSerializable clone = (SimpleSerializable) super.clone();
2920+
clone.simpleVersion = simpleVersion;
28712921

28722922
Class<? extends SimpleSerializable> clazz = this.getClass();
28732923
Map<String, Field> fields = getSerializableFields(clazz.getName(), clazz, false);
@@ -2991,9 +3041,20 @@ public static SimpleSerializable parseInstance(String str, int start, SimpleFilt
29913041
if (str == null || start < 0) return null;
29923042
int length = str.length() - start;
29933043
if (length <= 7 || !("WLL".equals(str.substring(start, start + 3)))) return null;
3044+
int v = 0;
3045+
try {
3046+
v = Integer.parseInt(str.substring(start + 3, start + 6));
3047+
} catch (NumberFormatException e1) {
3048+
}
29943049
int index = str.indexOf('#', start);
29953050
if (index == -1) return null;
29963051
String clazzName = str.substring(start + 6, index);
3052+
if (v >= 202) {
3053+
String longClazzName = rClassMappings.get(clazzName);
3054+
if (longClazzName != null) {
3055+
clazzName = longClazzName;
3056+
}
3057+
}
29973058
if (filter != null) {
29983059
if (!filter.accept(clazzName)) return null;
29993060
}

0 commit comments

Comments
 (0)