Explorar el Código

added read executor

develop
Robin Thoni hace 8 años
padre
commit
2a14b3de79

+ 18
- 0
server/src/main/java/com/uqac/rthoni/java_rmi/server/TestDbo.java Ver fichero

@@ -0,0 +1,18 @@
1
+package com.uqac.rthoni.java_rmi.server;
2
+
3
+/**
4
+ * Created by robin on 9/16/16.
5
+ */
6
+public class TestDbo {
7
+    private String privateString = "default_value";
8
+
9
+    public int publicInt = 42;
10
+
11
+    public String getPrivateString() {
12
+        return privateString;
13
+    }
14
+
15
+    public void setPrivateString(String privateString) {
16
+        this.privateString = privateString;
17
+    }
18
+}

+ 46
- 0
server/src/main/java/com/uqac/rthoni/java_rmi/server/executors/ReadExecutor.java Ver fichero

@@ -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
+}

Loading…
Cancelar
Guardar