Skip to content

Commit a7d0d86

Browse files
committed
从主项目同步过来
1 parent 47ebfef commit a7d0d86

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+378
-216
lines changed

pom.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
44
<modelVersion>4.0.0</modelVersion>
55

6-
<groupId>apijson.server</groupId>
6+
<groupId>apijson.orm</groupId>
77
<artifactId>apijson-orm</artifactId>
88
<version>4.0.0</version>
99
<packaging>jar</packaging>
@@ -22,7 +22,7 @@
2222
<dependency>
2323
<groupId>com.alibaba</groupId>
2424
<artifactId>fastjson</artifactId>
25-
<version>1.2.58</version>
25+
<version>1.2.61</version>
2626
</dependency>
2727
</dependencies>
2828

src/main/java/apijson/JSONObject.java

-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
import java.util.List;
1919
import java.util.Map;
2020

21-
import apijson.server.JSONRequest;
22-
2321
/**use this class instead of com.alibaba.fastjson.JSONObject
2422
* @author Lemon
2523
* @see #put

src/main/java/apijson/server/RemoteFunction.java src/main/java/apijson/orm/AbstractFunctionParser.java

+124-35
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
See the License for the specific language governing permissions and
1313
limitations under the License.*/
1414

15-
package apijson.server;
15+
package apijson.orm;
1616

1717
import java.lang.reflect.InvocationTargetException;
1818
import java.util.Arrays;
@@ -32,66 +32,155 @@
3232
/**可远程调用的函数类
3333
* @author Lemon
3434
*/
35-
public class RemoteFunction {
36-
// private static final String TAG = "RemoteFunction";
35+
public class AbstractFunctionParser implements FunctionParser {
36+
// private static final String TAG = "AbstractFunctionParser";
3737

3838
// <methodName, JSONObject>
3939
// <isContain, <arguments:"array,key", tag:null, methods:null>>
4040
public static final Map<String, JSONObject> FUNCTION_MAP;
4141
static {
4242
FUNCTION_MAP = new HashMap<>();
4343
}
44-
45-
private final RequestMethod method;
46-
private final String tag;
47-
private final int version;
48-
public RemoteFunction(RequestMethod method, String tag, int version) {
49-
this.method = method == null ? RequestMethod.GET : method;
50-
this.tag = tag;
51-
this.version = version;
44+
45+
private RequestMethod method;
46+
private String tag;
47+
private int version;
48+
private JSONObject request;
49+
public AbstractFunctionParser() {
50+
this(null, null, 0, null);
51+
}
52+
public AbstractFunctionParser(RequestMethod method, String tag, int version, @NotNull JSONObject request) {
53+
setMethod(method == null ? RequestMethod.GET : method);
54+
setTag(tag);
55+
setVersion(version);
56+
setRequest(request);
5257
}
5358

59+
@Override
5460
public RequestMethod getMethod() {
5561
return method;
5662
}
63+
@Override
64+
public AbstractFunctionParser setMethod(RequestMethod method) {
65+
this.method = method;
66+
return this;
67+
}
68+
@Override
5769
public String getTag() {
5870
return tag;
5971
}
72+
@Override
73+
public AbstractFunctionParser setTag(String tag) {
74+
this.tag = tag;
75+
return this;
76+
}
77+
@Override
6078
public int getVersion() {
6179
return version;
6280
}
81+
@Override
82+
public AbstractFunctionParser setVersion(int version) {
83+
this.version = version;
84+
return this;
85+
}
86+
87+
private String key;
88+
@Override
89+
public String getKey() {
90+
return key;
91+
}
92+
@Override
93+
public AbstractFunctionParser setKey(String key) {
94+
this.key = key;
95+
return this;
96+
}
97+
98+
private String parentPath;
99+
@Override
100+
public String getParentPath() {
101+
return parentPath;
102+
}
103+
@Override
104+
public AbstractFunctionParser setParentPath(String parentPath) {
105+
this.parentPath = parentPath;
106+
return this;
107+
}
108+
private String currentName;
109+
@Override
110+
public String getCurrentName() {
111+
return currentName;
112+
}
113+
@Override
114+
public AbstractFunctionParser setCurrentName(String currentName) {
115+
this.currentName = currentName;
116+
return this;
117+
}
118+
119+
@NotNull
120+
@Override
121+
public JSONObject getRequest() {
122+
return request;
123+
}
124+
@Override
125+
public AbstractFunctionParser setRequest(@NotNull JSONObject request) {
126+
this.request = request;
127+
return this;
128+
}
129+
130+
private JSONObject currentObject;
131+
@NotNull
132+
@Override
133+
public JSONObject getCurrentObject() {
134+
return currentObject;
135+
}
136+
@Override
137+
public AbstractFunctionParser setCurrentObject(@NotNull JSONObject currentObject) {
138+
this.currentObject = currentObject;
139+
return this;
140+
}
141+
142+
143+
/**反射调用
144+
* @param function 例如get(object,key),参数只允许引用,不能直接传值
145+
* @param currentObject 不作为第一个参数,就不能远程调用invoke,避免死循环
146+
* @return {@link #invoke(AbstractFunctionParser, String, JSONObject)}
147+
*/
148+
@Override
149+
public Object invoke(@NotNull String function, @NotNull JSONObject currentObject) throws Exception {
150+
return invoke(this, function, currentObject);
151+
}
63152

64153
/**反射调用
65-
* @param fun
154+
* @param parser
66155
* @param request
67156
* @param function 例如get(Map:map,key),参数只允许引用,不能直接传值
68-
* @return
157+
* @return {@link #invoke(AbstractFunctionParser, String, Class[], Object[])}
69158
*/
70-
public static Object invoke(@NotNull RemoteFunction fun, @NotNull JSONObject request, @NotNull String function) throws Exception {
159+
public static Object invoke(@NotNull AbstractFunctionParser parser, @NotNull String function, @NotNull JSONObject currentObject) throws Exception {
160+
161+
FunctionBean fb = parseFunction(function, currentObject, false);
71162

72-
FunctionBean fb = parseFunction(function, request, false);
73-
74163
JSONObject row = FUNCTION_MAP.get(fb.getMethod());
75164
if (row == null) {
76165
throw new UnsupportedOperationException("不允许调用远程函数 " + fb.getMethod() + " !");
77166
}
78-
167+
79168
int v = row.getIntValue("version");
80-
if (fun.getVersion() < v) {
81-
throw new UnsupportedOperationException("不允许 version = " + fun.getVersion() + " 的请求调用远程函数 " + fb.getMethod() + " ! 必须满足 version >= " + v + " !");
169+
if (parser.getVersion() < v) {
170+
throw new UnsupportedOperationException("不允许 version = " + parser.getVersion() + " 的请求调用远程函数 " + fb.getMethod() + " ! 必须满足 version >= " + v + " !");
82171
}
83172
String t = row.getString("tag");
84-
if (t != null && t.equals(fun.getTag()) == false) {
85-
throw new UnsupportedOperationException("不允许 tag = " + fun.getTag() + " 的请求调用远程函数 " + fb.getMethod() + " ! 必须满足 tag = " + t + " !");
173+
if (t != null && t.equals(parser.getTag()) == false) {
174+
throw new UnsupportedOperationException("不允许 tag = " + parser.getTag() + " 的请求调用远程函数 " + fb.getMethod() + " ! 必须满足 tag = " + t + " !");
86175
}
87176
String[] methods = StringUtil.split(row.getString("methods"));
88177
List<String> ml = methods == null || methods.length <= 0 ? null : Arrays.asList(methods);
89-
if (ml != null && ml.contains(fun.getMethod().toString()) == false) {
90-
throw new UnsupportedOperationException("不允许 method = " + fun.getMethod() + " 的请求调用远程函数 " + fb.getMethod() + " ! 必须满足 method 在 " + methods + "内 !");
178+
if (ml != null && ml.contains(parser.getMethod().toString()) == false) {
179+
throw new UnsupportedOperationException("不允许 method = " + parser.getMethod() + " 的请求调用远程函数 " + fb.getMethod() + " ! 必须满足 method 在 " + methods + "内 !");
91180
}
92181

93182
try {
94-
return invoke(fun, fb.getMethod(), fb.getTypes(), fb.getValues());
183+
return invoke(parser, fb.getMethod(), fb.getTypes(), fb.getValues());
95184
} catch (Exception e) {
96185
if (e instanceof NoSuchMethodException) {
97186
throw new IllegalArgumentException("字符 " + function + " 对应的远程函数 " + getFunction(fb.getMethod(), fb.getKeys()) + " 不在后端工程的DemoFunction内!"
@@ -112,6 +201,16 @@ public static Object invoke(@NotNull RemoteFunction fun, @NotNull JSONObject req
112201
}
113202

114203
}
204+
205+
/**反射调用
206+
* @param methodName
207+
* @param parameterTypes
208+
* @param args
209+
* @return
210+
*/
211+
public static Object invoke(@NotNull AbstractFunctionParser parser, @NotNull String methodName, @NotNull Class<?>[] parameterTypes, @NotNull Object[] args) throws Exception {
212+
return parser.getClass().getMethod(methodName, parameterTypes).invoke(parser, args);
213+
}
115214

116215
/**解析函数
117216
* @param function
@@ -200,16 +299,6 @@ else if (v instanceof JSONArray) { // Collection) {
200299
}
201300

202301

203-
/**反射调用
204-
* @param methodName
205-
* @param parameterTypes
206-
* @param args
207-
* @return
208-
*/
209-
public static Object invoke(@NotNull RemoteFunction fun, @NotNull String methodName, @NotNull Class<?>[] parameterTypes, @NotNull Object[] args) throws Exception {
210-
return fun.getClass().getDeclaredMethod(methodName, parameterTypes).invoke(fun, args);
211-
}
212-
213302
/**
214303
* @param method
215304
* @param keys
@@ -300,7 +389,7 @@ public String toFunctionCallString(boolean useValue, String quote) {
300389
s += (i <= 0 ? "" : ",") + (arg instanceof Boolean || arg instanceof Number ? arg : quote + arg + quote);
301390
}
302391
}
303-
392+
304393
return s + ")";
305394
}
306395

src/main/java/apijson/server/AbstractObjectParser.java src/main/java/apijson/orm/AbstractObjectParser.java

+13-19
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,24 @@
1212
See the License for the specific language governing permissions and
1313
limitations under the License.*/
1414

15-
package apijson.server;
15+
package apijson.orm;
1616

1717
import static apijson.JSONObject.KEY_COMBINE;
1818
import static apijson.JSONObject.KEY_DROP;
1919
import static apijson.JSONObject.KEY_TRY;
2020
import static apijson.RequestMethod.POST;
2121
import static apijson.RequestMethod.PUT;
22-
import static apijson.server.SQLConfig.TYPE_ITEM;
22+
import static apijson.orm.SQLConfig.TYPE_ITEM;
2323

2424
import java.rmi.ServerException;
2525
import java.util.ArrayList;
2626
import java.util.Arrays;
27-
import java.util.HashMap;
2827
import java.util.LinkedHashMap;
2928
import java.util.LinkedHashSet;
3029
import java.util.List;
3130
import java.util.Map;
3231
import java.util.Map.Entry;
3332
import java.util.Set;
34-
import java.util.regex.Pattern;
3533

3634
import javax.activation.UnsupportedDataTypeException;
3735

@@ -44,9 +42,9 @@
4442
import apijson.NotNull;
4543
import apijson.RequestMethod;
4644
import apijson.StringUtil;
47-
import apijson.server.RemoteFunction.FunctionBean;
48-
import apijson.server.exception.ConflictException;
49-
import apijson.server.exception.NotExistException;
45+
import apijson.orm.AbstractFunctionParser.FunctionBean;
46+
import apijson.orm.exception.ConflictException;
47+
import apijson.orm.exception.NotExistException;
5048

5149

5250
/**简化Parser,getObject和getArray(getArrayConfig)都能用
@@ -72,6 +70,7 @@ public AbstractObjectParser setParser(AbstractParser<?> parser) {
7270
protected final List<Join> joinList;
7371
protected final boolean isTable;
7472
protected final String path;
73+
protected final String name;
7574
protected final String table;
7675
protected final String alias;
7776

@@ -99,9 +98,10 @@ public AbstractObjectParser(@NotNull JSONObject request, String parentPath, Stri
9998

10099
this.type = arrayConfig == null ? 0 : arrayConfig.getType();
101100
this.joinList = arrayConfig == null ? null : arrayConfig.getJoinList();
101+
this.name = name;
102102
this.path = AbstractParser.getAbsPath(parentPath, name);
103103

104-
apijson.server.Entry<String, String> entry = Pair.parseEntry(name, true);
104+
apijson.orm.Entry<String, String> entry = Pair.parseEntry(name, true);
105105
this.table = entry.getKey();
106106
this.alias = entry.getValue();
107107
this.isTable = apijson.JSONObject.isTableKey(table);
@@ -127,12 +127,6 @@ public AbstractObjectParser(@NotNull JSONObject request, String parentPath, Stri
127127
Log.d(TAG, "AbstractObjectParser isEmpty = " + isEmpty + "; tri = " + tri + "; drop = " + drop);
128128
}
129129

130-
public static final Map<String, Pattern> COMPILE_MAP;
131-
static {
132-
COMPILE_MAP = new HashMap<String, Pattern>();
133-
}
134-
135-
136130

137131
private boolean invalidate = false;
138132
public void invalidate() {
@@ -408,7 +402,7 @@ else if (value instanceof String) { // 引用赋值路径
408402
type = "-";
409403
k = k.substring(0, k.length() - 1);
410404

411-
parseFunction(request, k, (String) value);
405+
parseFunction(k, (String) value, parentPath, name, request);
412406
}
413407
else {
414408
if (k.endsWith("+")) {
@@ -750,22 +744,22 @@ public void onFunctionResponse(String type) throws Exception {
750744
for (Entry<String, String> entry : functionSet) {
751745

752746
// parseFunction(json, entry.getKey(), entry.getValue());
753-
parseFunction(response, entry.getKey(), entry.getValue());
747+
parseFunction(entry.getKey(), entry.getValue(), parentPath, name, response);
754748
}
755749
}
756750
}
757751

758-
public void parseFunction(JSONObject json, String key, String value) throws Exception {
752+
public void parseFunction(String key, String value, String parentPath, String currentName, JSONObject currentObject) throws Exception {
759753
Object result;
760754
if (key.startsWith("@")) { //TODO 以后这种小众功能从 ORM 移出,作为一个 plugin/APIJSONProcedure
761-
FunctionBean fb = RemoteFunction.parseFunction(value, json, true);
755+
FunctionBean fb = AbstractFunctionParser.parseFunction(value, currentObject, true);
762756

763757
SQLConfig config = newSQLConfig(true);
764758
config.setProcedure(fb.toFunctionCallString(true));
765759
result = parseResponse(config, true);
766760
}
767761
else {
768-
result = parser.onFunctionParse(json, value);
762+
result = parser.onFunctionParse(key, value, parentPath, currentName, currentObject);
769763
}
770764

771765
if (result != null) {

0 commit comments

Comments
 (0)