1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.struts2.compiler;
22
23 import java.util.Map;
24 import java.util.concurrent.ConcurrentHashMap;
25
26 /***
27 * Keeps a cache of class name -> MemoryJavaFileObject. If the requested class name is in the cache
28 * a new class is defined for it, otherwise findClass delegates to the parent class loader
29 */
30 public class MemoryClassLoader extends ClassLoader {
31 private Map<String, MemoryJavaFileObject> cachedObjects = new ConcurrentHashMap<String, MemoryJavaFileObject>();
32
33 public MemoryClassLoader() {
34
35
36
37 super(Thread.currentThread().getContextClassLoader());
38 }
39
40 @Override
41 protected Class<?> findClass(String name) throws
42 ClassNotFoundException {
43 MemoryJavaFileObject fileObject = cachedObjects.get(name);
44 if (fileObject != null) {
45 byte[] bytes = fileObject.toByteArray();
46 return defineClass(name, bytes, 0, bytes.length);
47 }
48 return super.findClass(name);
49 }
50
51 public void addMemoryJavaFileObject(String jsp, MemoryJavaFileObject memoryJavaFileObject) {
52 cachedObjects.put(jsp, memoryJavaFileObject);
53 }
54 }