Browse Source

finished loader executor; fixed method executor bugs

develop
Robin Thoni 7 years ago
parent
commit
65de509d6c

+ 11
- 6
client/src/main/java/com/uqac/rthoni/java_rmi/client/ClientApplication.java View File

45
     {
45
     {
46
         System.out.format("Reading input file %s...\n", inputFile);
46
         System.out.format("Reading input file %s...\n", inputFile);
47
         Vector<Command> commands = readInputFile(inputFile);
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
         for (Command command : commands) {
60
         for (Command command : commands) {
56
             runCommand(command, host, port, outputStream);
61
             runCommand(command, host, port, outputStream);

+ 13
- 1
common/src/main/java/com/uqac/rthoni/java_rmi/common/ReflectionUtil.java View File

100
         return value;
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
         if (className.equals("boolean")) {
104
         if (className.equals("boolean")) {
105
             return boolean.class;
105
             return boolean.class;
106
         }
106
         }
122
         if (className.equals("double")) {
122
         if (className.equals("double")) {
123
             return double.class;
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
         return Class.forName(className);
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 View File

11
 import java.net.ServerSocket;
11
 import java.net.ServerSocket;
12
 import java.net.Socket;
12
 import java.net.Socket;
13
 import java.util.HashMap;
13
 import java.util.HashMap;
14
+import java.util.List;
14
 import java.util.Vector;
15
 import java.util.Vector;
15
 
16
 
16
 /**
17
 /**
18
  */
19
  */
19
 public class ServerApplication {
20
 public class ServerApplication {
20
 
21
 
21
-    private Vector<AbstractCommandExecutor> _executors = null;
22
+    private Vector<AbstractCommandExecutor> _executors = new Vector<>();
22
 
23
 
23
     private HashMap<String, Object> _objects = new HashMap<>();
24
     private HashMap<String, Object> _objects = new HashMap<>();
24
 
25
 
26
+    private Vector<ClassLoader> _loaders = new Vector<>();
27
+
25
     private String _sourceDir;
28
     private String _sourceDir;
26
 
29
 
27
     private String _classDir;
30
     private String _classDir;
56
         return _objects.get(id);
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
     public String getSourceDir() {
72
     public String getSourceDir() {
60
         return _sourceDir;
73
         return _sourceDir;
61
     }
74
     }
105
 
118
 
106
     public void loadExecutors() throws ClassNotFoundException
119
     public void loadExecutors() throws ClassNotFoundException
107
     {
120
     {
108
-        _executors = new Vector<>();
121
+        _executors.clear();
109
         Vector<Class> classes = AbstractCommandExecutor.getAllExecutors();
122
         Vector<Class> classes = AbstractCommandExecutor.getAllExecutors();
110
         for (Class c : classes) {
123
         for (Class c : classes) {
111
             try {
124
             try {

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

23
 
23
 
24
         File root = new File(server.getClassDir(), classDir);
24
         File root = new File(server.getClassDir(), classDir);
25
         URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] { root.toURI().toURL() });
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
         return null;
31
         return null;
31
     }
32
     }

+ 2
- 2
server/src/main/java/com/uqac/rthoni/java_rmi/server/executors/MethodExecutor.java View File

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

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

1
 package com.uqac.rthoni.java_rmi.server.executors;
1
 package com.uqac.rthoni.java_rmi.server.executors;
2
 
2
 
3
 import com.uqac.rthoni.java_rmi.common.Command;
3
 import com.uqac.rthoni.java_rmi.common.Command;
4
+import com.uqac.rthoni.java_rmi.common.ReflectionUtil;
4
 import com.uqac.rthoni.java_rmi.server.ServerApplication;
5
 import com.uqac.rthoni.java_rmi.server.ServerApplication;
5
 
6
 
6
 /**
7
 /**
16
     public String run(Command command, ServerApplication server) throws Exception {
17
     public String run(Command command, ServerApplication server) throws Exception {
17
         String className = command.getArgument(0, false);
18
         String className = command.getArgument(0, false);
18
         String id = command.getArgument(1, false);
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
         server.addObject(id, obj);
23
         server.addObject(id, obj);
22
         return null;
24
         return null;
23
     }
25
     }

Loading…
Cancel
Save