Skip to content

Commit 7e15912

Browse files
committed
Refactor to resolve remaining issues found in Pro version
1 parent 6b4909f commit 7e15912

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

common/src/main/java/org/red5/server/service/ReflectionUtils.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public static Object[] findMethod(Object service, String methodName, List<?> lis
9292
try {
9393
Object[] convertedArgs = ConversionUtils.convertParams(args, paramTypes);
9494
if (isTrace) {
95-
log.trace("Method {} matched - parameters: {}", methodName, paramTypes);
95+
log.trace("Found method {} {} - parameters: {}", methodName, method, paramTypes);
9696
}
9797
methodResult = new Object[] { method, convertedArgs };
9898
break;
@@ -101,6 +101,9 @@ public static Object[] findMethod(Object service, String methodName, List<?> lis
101101
}
102102
}
103103
}
104+
if (isTrace) {
105+
log.trace("Method name: {} result: {}", methodName, methodResult[0]);
106+
}
104107
}
105108
return methodResult;
106109
}
@@ -118,6 +121,14 @@ public static Object[] findMethod(IConnection conn, IServiceCall call, Object se
118121
if (isDebug) {
119122
log.debug("Find method: {} in service: {} for call: {} and connection: {}", methodName, service, call, conn);
120123
}
124+
// return value(s)
125+
Object[] methodResult = NULL_RETURN;
126+
// clear any previous exception from the call as it may be reused
127+
if (call.getException() != null) {
128+
log.debug("Clearing status and exception from call: {}", call);
129+
call.setStatus(Call.STATUS_PENDING);
130+
call.setException(null);
131+
}
121132
// get the arguments
122133
final Object[] args = call.getArguments();
123134
// convert the args to their class types
@@ -138,8 +149,6 @@ public static Object[] findMethod(IConnection conn, IServiceCall call, Object se
138149
} else {
139150
argsWithConnection = conn != null ? new Object[] { conn } : new Object[0];
140151
}
141-
// return value(s)
142-
Object[] methodResult = NULL_RETURN;
143152
// get all the name matched methods once, then filter out the ones that contain a $
144153
final Set<Method> methods = Arrays.stream(service.getClass().getMethods()).filter(m -> (m.getName().equals(methodName) && !m.getName().contains("$"))).filter(m -> m.getParameterCount() == 1 || m.getParameterCount() == callParams.length || m.getParameterCount() == (callParams.length + 1)).collect(Collectors.toUnmodifiableSet());
145154
if (methods.isEmpty()) {
@@ -182,7 +191,7 @@ public static Object[] findMethod(IConnection conn, IServiceCall call, Object se
182191
try {
183192
Object[] convertedArgs = ConversionUtils.convertParams(args, paramTypes);
184193
if (isTrace) {
185-
log.trace("Method {} matched - parameters: {}", methodName, paramTypes);
194+
log.trace("Found method {} {} - parameters: {}", methodName, method, paramTypes);
186195
}
187196
methodResult = new Object[] { method, convertedArgs };
188197
break;
@@ -196,7 +205,7 @@ public static Object[] findMethod(IConnection conn, IServiceCall call, Object se
196205
try {
197206
Object[] convertedArgs = ConversionUtils.convertParams(argsWithConnection, paramTypes);
198207
if (isTrace) {
199-
log.trace("Method {} matched - parameters: {}", methodName, paramTypes);
208+
log.trace("Found method {} {} - parameters: {}", methodName, method, paramTypes);
200209
}
201210
methodResult = new Object[] { method, convertedArgs };
202211
break;
@@ -205,6 +214,9 @@ public static Object[] findMethod(IConnection conn, IServiceCall call, Object se
205214
}
206215
}
207216
}
217+
if (isTrace) {
218+
log.trace("Method name: {} result: {}", methodName, methodResult[0]);
219+
}
208220
if (methodResult[0] == null) {
209221
log.warn("Method {} not found in {} with parameters {}", methodName, service, Arrays.asList(callParams));
210222
call.setStatus(Call.STATUS_METHOD_NOT_FOUND);

common/src/main/java/org/red5/server/service/ServiceInvoker.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,20 @@ public boolean invoke(IServiceCall call, Object service) {
110110
log.debug("Method name contained an illegal prefix, it will be removed: {}", methodName);
111111
methodName = methodName.substring(1);
112112
}
113+
if (log.isTraceEnabled()) {
114+
log.trace("Method: {} call exception: ", methodName, call.getException());
115+
}
113116
// look up the method with provided matching arguments
114117
Object[] methodResult = ReflectionUtils.findMethod(conn, call, service, methodName);
115-
// get the method
116-
Method method = (methodResult != null ? (Method) methodResult[0] : null);
117-
if (method == null || call.getException() != null) {
118+
// get the method from the result, methodResult itself cannot be null!
119+
Method method = (Method) methodResult[0];
120+
// checking "|| call.getException() != null" here causes a reused call to fail if a previous attempt failed
121+
if (method == null) {
118122
log.warn("Method not found: {}", methodName);
119123
} else {
120124
log.debug("Method found: {}", methodName);
121125
// get the parameters; the value at index 1 can be null, but the methodResult array will never be null
126+
@SuppressWarnings("null")
122127
Object[] params = (Object[]) methodResult[1];
123128
try {
124129
/* XXX(paul) legacy flash logic for restricting access to methods
@@ -137,13 +142,13 @@ public boolean invoke(IServiceCall call, Object service) {
137142
Object result = null;
138143
log.debug("Invoking method: {}", method.toString());
139144
if (method.getReturnType().equals(Void.TYPE)) {
140-
log.debug("result: void");
141145
method.invoke(service, params);
142146
call.setStatus(Call.STATUS_SUCCESS_VOID);
147+
log.debug("result: void");
143148
} else {
144149
result = method.invoke(service, params);
145-
log.debug("result: {}", result);
146150
call.setStatus(result == null ? Call.STATUS_SUCCESS_NULL : Call.STATUS_SUCCESS_RESULT);
151+
log.debug("result: {}", result);
147152
}
148153
if (call instanceof IPendingServiceCall) {
149154
((IPendingServiceCall) call).setResult(result);

0 commit comments

Comments
 (0)