Explorar el Código

finished loader executor; fixed method executor bugs

develop
Robin Thoni hace 8 años
padre
commit
65de509d6c

+ 11
- 6
client/src/main/java/com/uqac/rthoni/java_rmi/client/ClientApplication.java Ver fichero

@@ -45,12 +45,17 @@ public class ClientApplication {
45 45
     {
46 46
         System.out.format("Reading input file %s...\n", inputFile);
47 47
         Vector<Command> commands = readInputFile(inputFile);
48
-        FileOutputStream outputStream = null;
49
-        try {
50
-            outputStream = new FileOutputStream(outputFile);
51
-        } catch (Exception e) {
52
-            System.err.format("Failed to open output file %s: %s\n", outputFile, e.getMessage());
53
-            System.exit(3);
48
+        OutputStream outputStream = null;
49
+        if (outputFile.equals("-")) {
50
+            outputStream = System.out;
51
+        }
52
+        else {
53
+            try {
54
+                outputStream = new FileOutputStream(outputFile);
55
+            } catch (Exception e) {
56
+                System.err.format("Failed to open output file %s: %s\n", outputFile, e.getMessage());
57
+                System.exit(3);
58
+            }
54 59
         }
55 60
         for (Command command : commands) {
56 61
             runCommand(command, host, port, outputStream);

+ 13
- 1
common/src/main/java/com/uqac/rthoni/java_rmi/common/ReflectionUtil.java Ver fichero

@@ -100,7 +100,7 @@ public class ReflectionUtil {
100 100
         return value;
101 101
     }
102 102
 
103
-    public static Class getClass(String className) throws ClassNotFoundException {
103
+    public static Class getClass(String className, List<ClassLoader> loaders) throws ClassNotFoundException {
104 104
         if (className.equals("boolean")) {
105 105
             return boolean.class;
106 106
         }
@@ -122,6 +122,18 @@ public class ReflectionUtil {
122 122
         if (className.equals("double")) {
123 123
             return double.class;
124 124
         }
125
+        for (ClassLoader classLoader : loaders) {
126
+            try {
127
+                return (Class) Class.forName(className, true, classLoader);
128
+            } catch (ClassNotFoundException e) {
129
+            }
130
+        }
125 131
         return Class.forName(className);
126 132
     }
133
+
134
+    public static Object newInstance(String className, List<ClassLoader> loaders)
135
+            throws IllegalAccessException, InstantiationException, ClassNotFoundException {
136
+        Class c = getClass(className, loaders);
137
+        return c.newInstance();
138
+    }
127 139
 }

+ 15
- 2
server/src/main/java/com/uqac/rthoni/java_rmi/server/ServerApplication.java Ver fichero

@@ -11,6 +11,7 @@ import java.net.InetAddress;
11 11
 import java.net.ServerSocket;
12 12
 import java.net.Socket;
13 13
 import java.util.HashMap;
14
+import java.util.List;
14 15
 import java.util.Vector;
15 16
 
16 17
 /**
@@ -18,10 +19,12 @@ import java.util.Vector;
18 19
  */
19 20
 public class ServerApplication {
20 21
 
21
-    private Vector<AbstractCommandExecutor> _executors = null;
22
+    private Vector<AbstractCommandExecutor> _executors = new Vector<>();
22 23
 
23 24
     private HashMap<String, Object> _objects = new HashMap<>();
24 25
 
26
+    private Vector<ClassLoader> _loaders = new Vector<>();
27
+
25 28
     private String _sourceDir;
26 29
 
27 30
     private String _classDir;
@@ -56,6 +59,16 @@ public class ServerApplication {
56 59
         return _objects.get(id);
57 60
     }
58 61
 
62
+    public void addClassLoader(ClassLoader loader)
63
+    {
64
+        _loaders.add(loader);
65
+    }
66
+
67
+    public List<ClassLoader> getClassLoaders()
68
+    {
69
+        return _loaders;
70
+    }
71
+
59 72
     public String getSourceDir() {
60 73
         return _sourceDir;
61 74
     }
@@ -105,7 +118,7 @@ public class ServerApplication {
105 118
 
106 119
     public void loadExecutors() throws ClassNotFoundException
107 120
     {
108
-        _executors = new Vector<>();
121
+        _executors.clear();
109 122
         Vector<Class> classes = AbstractCommandExecutor.getAllExecutors();
110 123
         for (Class c : classes) {
111 124
             try {

+ 4
- 3
server/src/main/java/com/uqac/rthoni/java_rmi/server/executors/ClassLoaderExecutor.java Ver fichero

@@ -23,9 +23,10 @@ public class ClassLoaderExecutor extends AbstractCommandExecutor {
23 23
 
24 24
         File root = new File(server.getClassDir(), classDir);
25 25
         URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] { root.toURI().toURL() });
26
-        Class<?> cls = Class.forName(className, true, classLoader); // Should print "hello".
27
-        Object instance = cls.newInstance(); // Should print "world".
28
-        System.out.println(instance); // Should print "test.Test@hashcode".
26
+        server.addClassLoader(classLoader);
27
+//        Class<?> cls = Class.forName(className, true, classLoader); // Should print "hello".
28
+//        Object instance = cls.newInstance(); // Should print "world".
29
+//        System.out.println(instance); // Should print "test.Test@hashcode".
29 30
 
30 31
         return null;
31 32
     }

+ 2
- 2
server/src/main/java/com/uqac/rthoni/java_rmi/server/executors/MethodExecutor.java Ver fichero

@@ -26,7 +26,7 @@ public class MethodExecutor extends AbstractCommandExecutor {
26 26
         List<Pair<Class, String>> argClasses = command.getArgumentAsList(2, true).stream().map(v -> {
27 27
             String[] split = v.split(":");
28 28
             try {
29
-                return new Pair<>(ReflectionUtil.getClass(split[0]), split[1]);
29
+                return new Pair<>(ReflectionUtil.getClass(split[0], server.getClassLoaders()), split[1]);
30 30
             } catch (ClassNotFoundException e) {
31 31
                 return null;
32 32
             }
@@ -34,7 +34,7 @@ public class MethodExecutor extends AbstractCommandExecutor {
34 34
         List<Object> arguments = argClasses.stream().map(p -> {
35 35
             String value = p.getValue();
36 36
             if (value.startsWith("ID(") && value.endsWith(")")) {
37
-                value = value.substring(3, value.length() - 2);
37
+                value = value.substring(3, value.length() - 1);
38 38
                 return server.getObject(value);
39 39
             }
40 40
             return ReflectionUtil.toObject(p.getKey(), value);

+ 4
- 2
server/src/main/java/com/uqac/rthoni/java_rmi/server/executors/NewInstanceExecutor.java Ver fichero

@@ -1,6 +1,7 @@
1 1
 package com.uqac.rthoni.java_rmi.server.executors;
2 2
 
3 3
 import com.uqac.rthoni.java_rmi.common.Command;
4
+import com.uqac.rthoni.java_rmi.common.ReflectionUtil;
4 5
 import com.uqac.rthoni.java_rmi.server.ServerApplication;
5 6
 
6 7
 /**
@@ -16,8 +17,9 @@ public class NewInstanceExecutor extends AbstractCommandExecutor {
16 17
     public String run(Command command, ServerApplication server) throws Exception {
17 18
         String className = command.getArgument(0, false);
18 19
         String id = command.getArgument(1, false);
19
-        Class c = Class.forName(className);
20
-        Object obj = c.newInstance();
20
+        Object obj = ReflectionUtil.newInstance(className, server.getClassLoaders());
21
+//        Class c = Class.forName(className);
22
+//        Object obj = c.newInstance();
21 23
         server.addObject(id, obj);
22 24
         return null;
23 25
     }

Loading…
Cancelar
Guardar