|
@@ -0,0 +1,46 @@
|
|
1
|
+package com.uqac.rthoni.java_rmi.server.executors;
|
|
2
|
+
|
|
3
|
+import com.uqac.rthoni.java_rmi.common.Command;
|
|
4
|
+import com.uqac.rthoni.java_rmi.server.ServerApplication;
|
|
5
|
+
|
|
6
|
+import java.lang.reflect.Field;
|
|
7
|
+import java.lang.reflect.Method;
|
|
8
|
+
|
|
9
|
+/**
|
|
10
|
+ * Created by robin on 9/16/16.
|
|
11
|
+ */
|
|
12
|
+public class ReadExecutor extends AbstractCommandExecutor {
|
|
13
|
+ @Override
|
|
14
|
+ public String getCommandName() {
|
|
15
|
+ return "lecture";
|
|
16
|
+ }
|
|
17
|
+
|
|
18
|
+ @Override
|
|
19
|
+ public String run(Command command, ServerApplication server) throws Exception {
|
|
20
|
+ String id = command.getArgument(0);
|
|
21
|
+ String fieldName = command.getArgument(1);
|
|
22
|
+ String methodName = String.format("get%s%s", fieldName.substring(0, 1).toUpperCase(), fieldName.substring(1));
|
|
23
|
+
|
|
24
|
+ Object obj = server.getObject(id);
|
|
25
|
+ Class objClass = obj.getClass();
|
|
26
|
+ Field field = objClass.getDeclaredField(fieldName);
|
|
27
|
+ Object result;
|
|
28
|
+ String stringResult;
|
|
29
|
+
|
|
30
|
+ try {
|
|
31
|
+ result = field.get(obj);
|
|
32
|
+ } catch (IllegalAccessException e) {
|
|
33
|
+ Method method = objClass.getDeclaredMethod(methodName);
|
|
34
|
+ result = method.invoke(obj);
|
|
35
|
+ }
|
|
36
|
+
|
|
37
|
+ if (result != null) {
|
|
38
|
+ stringResult = result.toString();
|
|
39
|
+ }
|
|
40
|
+ else {
|
|
41
|
+ stringResult = "NULL";
|
|
42
|
+ }
|
|
43
|
+
|
|
44
|
+ return stringResult;
|
|
45
|
+ }
|
|
46
|
+}
|