summaryrefslogtreecommitdiff
path: root/test/files/detach-run/actor/ServerClassLoader.scala
blob: 3f5d96a1a18c8da4c26ee89ddf161292f073f0fd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import java.io._
import java.net.{JarURLConnection, URL, URLClassLoader}

import scala.remoting.Debug

  private class ServerObjectInputStream(in: InputStream, cl: ClassLoader)
  extends ObjectInputStream(in) {
    override def resolveClass(cd: ObjectStreamClass): Class[_] = {
      println("[ServerObjectInputStream] resolveClass "+cd.getName)
      try {
        Debug.info("load   class "+cd.getName+" from "+cl)
        val c = cl.loadClass(cd.getName)
        Debug.info("loaded class "+c.getName)
        c
      } catch {
        case cnf: ClassNotFoundException =>
          Debug.info("resolve class (this) "+cd.getName)
          val c = super.resolveClass(cd)
          Debug.info("resolve class (super) "+c.getName)
          c
      }
    }
    override def resolveProxyClass(interfaces: Array[String]): Class[_] = {
      println("[ServerObjectInputStream] resolveProxyClass "+interfaces.toList)
      try {
        val c = cl.loadClass(interfaces.last)
        Debug.info("loaded class "+c.getName)
        c
      } catch {
        case cnf: ClassNotFoundException =>
          Debug.info("resolve proxy class (this) "+interfaces.last)
          val c = super.resolveProxyClass(interfaces)
          Debug.info("resolve proxy class (super) "+c.getName)
          c
      }
    }
  }
/*
  // VARIANT 1
  class ServerClassLoader extends URLClassLoader(urls) {
    import scala.reflect.Manifest
    def load[A](a: Array[Byte])(implicit expected: Manifest[A]): A = {
      val in = new ServerObjectInputStream(new ByteArrayInputStream(a), this)
      val found = in.readObject.asInstanceOf[Manifest[_]]
      if (! (found <:< expected))
        throw new ClassCastException("type mismatch;"+
          "\n found   : "+found+
          "\n required: "+expected)
      val o = in.readObject.asInstanceOf[A]
      in.close()
      o
    }
    override def findClass(name: String): Class[_] = {
      println("[ServerClassLoader] findClass "+name)
      val b = loadClassData(name)
      if (b != null) defineClass(name, b, 0, b.length)
      else super.findClass(name)
    }
    private def loadClassData(name: String): Array[Byte] = {
      println("[ServerClassLoader] loadClassData "+name)
      null
    }
  }
  val serverClassLoader = new ServerClassLoader
*/

/*
  class ServerClassLoader(parent: ClassLoader) extends URLClassLoader(urls, parent) {
    import scala.reflect.Manifest
    def load[A](a: Array[Byte])(implicit expected: Manifest[A]): A = {
      val in = new ServerObjectInputStream(new ByteArrayInputStream(a), this)
      val found = in.readObject.asInstanceOf[Manifest[_]]
      if (! (found <:< expected))
        throw new ClassCastException("type mismatch;"+
          "\n found   : "+found+
          "\n required: "+expected)
      val o = in.readObject.asInstanceOf[A]
      in.close()
      o
    }
    override def findClass(name: String): Class[_] = {
      println("[ServerClassLoader] findClass "+name)
      val b = loadClassData(name)
      if (b != null) defineClass(name, b, 0, b.length)
      else super.findClass(name)
    }
    private def loadClassData(name: String): Array[Byte] = {
      println("[ServerClassLoader] loadClassData "+name)
      null
    }
  }
*/
class ServerClassLoader(urls: Array[URL], parent: ClassLoader)
extends URLClassLoader(urls, parent) {

  private val cache = new collection.mutable.HashMap[String, Class[_]]

  for (url <- urls) {
    val jarurl = new URL("jar:"+url+"!/")
    val con = jarurl.openConnection().asInstanceOf[JarURLConnection]
    val jar = con.getJarFile
    val e = jar.entries
    while (e.hasMoreElements) {
      val ze = e.nextElement
      val path = ze.getName
      if (path endsWith ".class") {
        val size = ze.getSize
        val name = path.replace("/", ".").substring(0, path.length - 6)
        cache += name -> this.loadClass(name)
        println("[ServerClassLoader] added "+name+" ("+size+")")
      }
    };
    //jar.close()
  }

  override def findClass(name: String): Class[_] = {
    println("[ServerClassLoader] findClass: name="+name)
    cache get name match {
      case Some(cl) =>
        println(name+" cached"); cl
      case None =>
        println(name+" not cached"); super.findClass(name)
    }
  }

}

/*
try {
    JarFile jarFile = new JarFile(srcPath);
    Enumeration<JarEntry> entries = jarFile.entries();
	String url = "file:" + srcPath;
	System.out.println(url);
	URLClassLoader classLoader = new URLClassLoader(
			new URL[] { new URL(url) });
	while (entries.hasMoreElements()) {
		JarEntry jarEntry = (JarEntry) entries
				.nextElement();
		String classPath = jarEntry.getName();
		if (classPath.endsWith(".class")) {
			String className = classPath.replace("/", ".")
					.substring(0, classPath.length() - 6);
			try {
				Class clazz = classLoader
						.loadClass(className);
				//Et là, tu fais ce que tu vexu avec la classe
			} catch (ClassNotFoundException e1) {
				e1.printStackTrace();
			}
		}
	}
} catch (IOException e1) {
	e1.printStackTrace();
}
*/