Skip to content

Commit af5e705

Browse files
author
zhourenjian
committed
Refactor codes.
Upgrade SimpleRPC from 1.0.0 to 1.0.1, the format of SimpleSerializable is modified a little. Attention: v 1.0.1 is not compatible with v 1.0.0
1 parent b5b1679 commit af5e705

File tree

5 files changed

+226
-145
lines changed

5 files changed

+226
-145
lines changed

sources/net.sf.j2s.ajax/ajaxrpc/net/sf/j2s/ajax/SimpleFieldFilter.java renamed to sources/net.sf.j2s.ajax/ajaxrpc/net/sf/j2s/ajax/SimpleFilter.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,28 @@
1212
package net.sf.j2s.ajax;
1313

1414
/**
15+
* This interface is used in the Java side to decide whether a given field
16+
* should be serialized or not.
17+
*
18+
* Usually, fields with modifier of public or protected among types of number
19+
* type, string type, array type will be serialized by default. But when a
20+
* SimpleFieldFilter is specified, some of the fields may not be serialized
21+
* again. For example, when a SimpleRPCRunnable instance is passed in through
22+
* HTTP connection, and some fields are modified before this instance is
23+
* back to client side. But it is no need to serialize all fields but only
24+
* those modified fields.
25+
*
1526
* @author zhou renjian
1627
*
1728
* 2007-06-10
1829
*/
19-
public interface SimpleFieldFilter {
30+
public interface SimpleFilter {
2031

2132
/**
2233
* Filter the fields to be serialized or not.
2334
* @param field
2435
* @return true for to be ignored and false for to be serialzed
2536
*/
26-
public boolean filter(String field);
37+
public boolean accept(String field);
2738

2839
}

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

Lines changed: 22 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
import java.io.IOException;
1616
import java.io.InputStream;
1717
import java.io.PrintWriter;
18-
import java.lang.reflect.Constructor;
1918
import java.lang.reflect.Field;
20-
import java.lang.reflect.InvocationTargetException;
2119
import java.lang.reflect.Modifier;
2220
import java.net.URLDecoder;
2321
import java.util.Arrays;
@@ -102,48 +100,13 @@ protected long maxXSSRequestLatency() {
102100
* specified class name is invalid, null will be returned.
103101
*/
104102
protected SimpleRPCRunnable getRunnableByRequest(String request) {
105-
if (request == null) return null;
106-
int length = request.length();
107-
if (length <= 7 || !request.startsWith("WLL")) return null;
108-
int index = request.indexOf('#');
109-
if (index == -1) return null;
110-
String clazzName = request.substring(6, index);
111-
if (!validateRunnable(clazzName)) return null;
112-
return createRunnableInstance(clazzName);
113-
}
114-
115-
/**
116-
* Create SimpleRPCRunnable instance by given class name. May be inherited to
117-
* do more jobs before starting to deserialize request and running the main job.
118-
*
119-
* @param clazzName full class name of inherited SimpleRPCRunnable class
120-
* @return instance of given class name, null may be returned.
121-
*/
122-
protected SimpleRPCRunnable createRunnableInstance(String clazzName) {
123-
try {
124-
Class runnableClass = Class.forName(clazzName);
125-
if (runnableClass != null) {
126-
// SimpleRPCRunnale should always has default constructor
127-
Constructor constructor = runnableClass.getConstructor(new Class[0]);
128-
Object obj = constructor.newInstance(new Object[0]);
129-
if (obj != null && obj instanceof SimpleRPCRunnable) {
130-
return (SimpleRPCRunnable) obj;
131-
}
103+
SimpleSerializable instance = SimpleSerializable.parseInstance(request, new SimpleFilter() {
104+
public boolean accept(String clazzName) {
105+
return validateRunnable(clazzName);
132106
}
133-
} catch (SecurityException e) {
134-
e.printStackTrace();
135-
} catch (IllegalArgumentException e) {
136-
e.printStackTrace();
137-
} catch (ClassNotFoundException e) {
138-
e.printStackTrace();
139-
} catch (NoSuchMethodException e) {
140-
e.printStackTrace();
141-
} catch (InstantiationException e) {
142-
e.printStackTrace();
143-
} catch (IllegalAccessException e) {
144-
e.printStackTrace();
145-
} catch (InvocationTargetException e) {
146-
e.printStackTrace();
107+
});
108+
if (instance instanceof SimpleRPCRunnable) {
109+
return (SimpleRPCRunnable) instance;
147110
}
148111
return null;
149112
}
@@ -264,7 +227,10 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp)
264227
resp.sendError(HttpServletResponse.SC_NOT_FOUND);
265228
return;
266229
}
267-
resp.setContentType("text/plain");
230+
resp.setHeader("Pragma", "no-cache");
231+
resp.setHeader("Cache-Control", "no-cache");
232+
resp.setDateHeader("Expires", 0);
233+
resp.setContentType("text/plain;charset=utf-8");
268234
//resp.setCharacterEncoding("utf-8");
269235
PrintWriter writer = resp.getWriter();
270236
runnable.deserialize(request);
@@ -276,16 +242,16 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp)
276242
}
277243
runnable.ajaxRun();
278244
final String[] diffs = compareDiffs(runnable, clonedRunnable);
279-
String serialize = runnable.serialize(new SimpleFieldFilter() {
245+
String serialize = runnable.serialize(new SimpleFilter() {
280246

281-
public boolean filter(String field) {
247+
public boolean accept(String field) {
282248
if (diffs == null || diffs.length == 0) return true;
283249
for (int i = 0; i < diffs.length; i++) {
284250
if (diffs[i].equals(field)) {
285-
return false;
251+
return true;
286252
}
287253
}
288-
return true;
254+
return false;
289255
}
290256

291257
});
@@ -343,20 +309,24 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp)
343309
}
344310
runnable.ajaxRun();
345311
final String[] diffs = compareDiffs(runnable, clonedRunnable);
346-
String serialize = runnable.serialize(new SimpleFieldFilter() {
312+
String serialize = runnable.serialize(new SimpleFilter() {
347313

348-
public boolean filter(String field) {
314+
public boolean accept(String field) {
349315
if (diffs == null || diffs.length == 0) return true;
350316
for (int i = 0; i < diffs.length; i++) {
351317
if (diffs[i].equals(field)) {
352-
return false;
318+
return true;
353319
}
354320
}
355-
return true;
321+
return false;
356322
}
357323

358324
});
359325

326+
resp.setHeader("Pragma", "no-cache");
327+
resp.setHeader("Cache-Control", "no-cache");
328+
resp.setDateHeader("Expires", 0);
329+
360330
if (isScriptReuest) { // cross site script response
361331
resp.setContentType("text/javascript");
362332
//resp.setCharacterEncoding("utf-8");
@@ -539,7 +509,6 @@ protected String[] compareDiffs(SimpleRPCRunnable runnable1, SimpleRPCRunnable r
539509
} catch (IllegalAccessException e) {
540510
//e.printStackTrace();
541511
}
542-
System.out.println(field1.getClass().getName());
543512
if (field1 == null) {
544513
if (field2 != null) {
545514
diffSet.add(name);

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,14 @@
2121
*/
2222
public class SimpleRPCRequest {
2323

24-
public static int MODE_AJAX = 1;
25-
public static int MODE_LOCAL_JAVA_THREAD = 2;
24+
public static final int MODE_AJAX = 1;
25+
public static final int MODE_LOCAL_JAVA_THREAD = 2;
2626

27-
static int runningMode = MODE_LOCAL_JAVA_THREAD;
27+
private static int runningMode = MODE_LOCAL_JAVA_THREAD;
28+
29+
public static int getRequstMode() {
30+
return runningMode;
31+
}
2832

2933
public static void switchToAJAXMode() {
3034
runningMode = MODE_AJAX;

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class SimpleRPCSWTRequest extends SimpleRPCRequest {
2929
*/
3030
public static void swtRequest(final SimpleRPCRunnable runnable) {
3131
runnable.ajaxIn();
32-
if (runningMode == MODE_LOCAL_JAVA_THREAD) {
32+
if (getRequstMode() == MODE_LOCAL_JAVA_THREAD) {
3333
new Thread(new Runnable(){
3434
public void run() {
3535
try {
@@ -43,7 +43,7 @@ public void run() {
4343
runnable.ajaxFail();
4444
}
4545
});
46-
}
46+
} // else ?
4747
return;
4848
}
4949
Display disp = Display.getDefault();
@@ -53,7 +53,7 @@ public void run() {
5353
runnable.ajaxOut();
5454
}
5555
});
56-
}
56+
} // else ?
5757
}
5858
}).start();
5959
} else {

0 commit comments

Comments
 (0)