|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| package jtr.remote.cl; |
|
8 |
| |
|
9 |
| import java.io.File; |
|
10 |
| import java.io.FileNotFoundException; |
|
11 |
| import java.io.FileOutputStream; |
|
12 |
| import java.io.IOException; |
|
13 |
| import java.net.MalformedURLException; |
|
14 |
| import java.net.URL; |
|
15 |
| import java.rmi.RemoteException; |
|
16 |
| import java.security.SecureClassLoader; |
|
17 |
| import jtr.enterprise.LocatorException; |
|
18 |
| import jtr.remote.test.NodeInfo; |
|
19 |
| import jtr.remote.utils.RmiUtil; |
|
20 |
| import org.apache.log4j.Logger; |
|
21 |
| |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| public class JtrRmiClassLoader extends SecureClassLoader { |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
6
| public JtrRmiClassLoader(NodeInfo serverCL) {
|
|
38 |
6
| this.serverCL = serverCL;
|
|
39 |
6
| try {
|
|
40 |
6
| rcl = (JtrRmiClassLoaderServer) RmiUtil.lookupServer(serverCL);
|
|
41 |
| } catch(LocatorException e) { |
|
42 |
0
| String msg = "Unable to connect to "+serverCL;
|
|
43 |
0
| logger.fatal(msg,e);
|
|
44 |
0
| System.err.println(msg+"\n"+e);
|
|
45 |
0
| System.out.println(msg+"\n"+e);
|
|
46 |
| } |
|
47 |
| } |
|
48 |
| |
|
49 |
1717
| @Override
|
|
50 |
| public Class<?> findClass(String name) throws ClassNotFoundException { |
|
51 |
1717
| byte[] bytecode = getByteCode(name);
|
|
52 |
1717
| if(bytecode==null)
|
|
53 |
92
| throw new ClassNotFoundException("Unable to find class "+name);
|
|
54 |
1625
| return defineClass(name,bytecode,0,bytecode.length);
|
|
55 |
| } |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
1717
| private byte[] getByteCode(String name) {
|
|
63 |
1717
| Byte[] bytecode = null;
|
|
64 |
1717
| String url = serverCL.toURL();
|
|
65 |
1717
| try {
|
|
66 |
1717
| logger.debug("Downloading class "+name+"...");
|
|
67 |
1717
| bytecode = rcl.getBytecode(name);
|
|
68 |
1716
| logger.debug("Bytecode received for class "+name);
|
|
69 |
| } catch (RemoteException ex) { |
|
70 |
| |
|
71 |
| |
|
72 |
0
| logger.error("Unable to contact server class loader at "+url,ex);
|
|
73 |
| } |
|
74 |
| |
|
75 |
1717
| if(bytecode!=null) {
|
|
76 |
1624
| byte[] res = new byte[bytecode.length];
|
|
77 |
1625
| for (int i = 0; i<bytecode.length; i++) {
|
|
78 |
6267788
| res[i] = bytecode[i];
|
|
79 |
| } |
|
80 |
1625
| return res;
|
|
81 |
| } |
|
82 |
92
| return null;
|
|
83 |
| } |
|
84 |
| |
|
85 |
39
| @Override
|
|
86 |
| protected URL findResource(String name) { |
|
87 |
39
| URL res = null;
|
|
88 |
39
| File file = null;
|
|
89 |
39
| try {
|
|
90 |
39
| logger.debug("Downloading resource "+name);
|
|
91 |
39
| Byte[] resource = rcl.getResource(name);
|
|
92 |
39
| logger.debug("Got resource "+name);
|
|
93 |
| |
|
94 |
39
| if(resource!=null) {
|
|
95 |
| |
|
96 |
9
| file = prepareFile(name);
|
|
97 |
9
| FileOutputStream fos = new FileOutputStream(file);
|
|
98 |
| |
|
99 |
| |
|
100 |
9
| byte[] tmp = new byte[resource.length];
|
|
101 |
9
| for (int i = 0; i<resource.length; i++) {
|
|
102 |
171138
| tmp[i] = resource[i];
|
|
103 |
| } |
|
104 |
| |
|
105 |
| |
|
106 |
9
| fos.write(tmp);
|
|
107 |
| |
|
108 |
9
| res = file.toURL();
|
|
109 |
| |
|
110 |
9
| fos.close();
|
|
111 |
| } |
|
112 |
| } catch (RemoteException ex) { |
|
113 |
0
| logger.error("Unable to contact server class loader",ex);
|
|
114 |
| } catch (MalformedURLException ex) { |
|
115 |
0
| logger.error("The provided URL is not legal",ex);
|
|
116 |
| } catch (FileNotFoundException ex) { |
|
117 |
0
| logger.fatal("Error preparing file "+file.getAbsolutePath());
|
|
118 |
| } catch (IOException ex) { |
|
119 |
0
| logger.fatal("Error accessing file "+file.getAbsolutePath());
|
|
120 |
| } |
|
121 |
| |
|
122 |
39
| return res;
|
|
123 |
| } |
|
124 |
| |
|
125 |
| |
|
126 |
| |
|
127 |
| |
|
128 |
| |
|
129 |
9
| private File prepareFile(String name) {
|
|
130 |
9
| File res = null;
|
|
131 |
9
| String[] names = name.split("/");
|
|
132 |
9
| if(names.length==1) {
|
|
133 |
| |
|
134 |
0
| res = new File(names[0]);
|
|
135 |
| } else { |
|
136 |
| |
|
137 |
9
| int pathLength = names.length;
|
|
138 |
9
| File parent = null;
|
|
139 |
9
| for(int i=0;i<pathLength-1;i++) {
|
|
140 |
59
| parent = new File(parent,names[i]);
|
|
141 |
59
| if(!parent.exists()) {
|
|
142 |
0
| parent.mkdir();
|
|
143 |
| } |
|
144 |
| } |
|
145 |
9
| res = new File(name);
|
|
146 |
| } |
|
147 |
9
| return res;
|
|
148 |
| } |
|
149 |
| |
|
150 |
| private static Logger logger = Logger.getLogger(JtrRmiClassLoader.class); |
|
151 |
| private NodeInfo serverCL; |
|
152 |
| private static JtrRmiClassLoaderServer rcl; |
|
153 |
| } |