diff options
author | Christopher Vogt <oss.nsp@cvogt.org> | 2016-03-14 23:03:37 -0400 |
---|---|---|
committer | Christopher Vogt <oss.nsp@cvogt.org> | 2016-03-19 21:13:49 -0400 |
commit | 51073a1e327c0f0663c18e0be7e9c43f62daea54 (patch) | |
tree | c96386cb5e6e99da9fb085d55bd66cf776cd3cd7 /stage1 | |
parent | 957ac4a3080383135738a9ce90790ef7692bb3c0 (diff) | |
download | cbt-51073a1e327c0f0663c18e0be7e9c43f62daea54.tar.gz cbt-51073a1e327c0f0663c18e0be7e9c43f62daea54.tar.bz2 cbt-51073a1e327c0f0663c18e0be7e9c43f62daea54.zip |
Add CachingClassLoader to cache loadClass of URLClassLoader
Diffstat (limited to 'stage1')
-rw-r--r-- | stage1/CachingClassLoader.scala | 12 | ||||
-rw-r--r-- | stage1/URLClassLoader.scala | 37 |
2 files changed, 40 insertions, 9 deletions
diff --git a/stage1/CachingClassLoader.scala b/stage1/CachingClassLoader.scala new file mode 100644 index 0000000..e75f14c --- /dev/null +++ b/stage1/CachingClassLoader.scala @@ -0,0 +1,12 @@ +package cbt +import java.net._ +import java.util.concurrent.ConcurrentHashMap +import scala.util.Try + +trait CachingClassLoader extends ClassLoader{ + def logger: Logger + val cache = new KeyLockedLazyCache[String,Try[Class[_]]]( new ConcurrentHashMap, new ConcurrentHashMap, Some(logger) ) + override def loadClass(name: String, resolve: Boolean) = { + cache.get( name, Try(super.loadClass(name, resolve)) ).get + } +} diff --git a/stage1/URLClassLoader.scala b/stage1/URLClassLoader.scala index fbe7ac8..20e2d5d 100644 --- a/stage1/URLClassLoader.scala +++ b/stage1/URLClassLoader.scala @@ -1,14 +1,13 @@ package cbt import java.net._ +import scala.util.Try -case class URLClassLoader(classPath: ClassPath, parent: ClassLoader) +case class URLClassLoader( classPath: ClassPath, parent: ClassLoader )( implicit val logger: Logger ) extends java.net.URLClassLoader( - classPath.strings.map( - path => new URL("file:"++path) - ).toArray, + classPath.strings.map( p => new URL("file:" ++ p) ).toArray, parent - ){ + ) with CachingClassLoader{ override def toString = ( scala.Console.BLUE ++ super.toString @@ -16,12 +15,32 @@ case class URLClassLoader(classPath: ClassPath, parent: ClassLoader) ++ "(\n" ++ ( getURLs.map(_.toString).sorted.mkString(",\n") - ++ ( - if(getParent() != ClassLoader.getSystemClassLoader()) + ++ ( + if(getParent() != ClassLoader.getSystemClassLoader()) ",\n" ++ getParent().toString - else "" - ) + else "" + ) ).split("\n").map(" "++_).mkString("\n") ++ "\n)" ) } + +/* +trait ClassLoaderLogging extends ClassLoader{ + def logger: Logger + val prefix = s"[${getClass.getSimpleName}] " + val postfix = " in \name" ++ this.toString + override def loadClass(name: String, resolve: Boolean): Class[_] = { + //logger.resolver(prefix ++ s"loadClass($name, $resolve)" ++ postfix ) + super.loadClass(name, resolve) + } + override def loadClass(name: String): Class[_] = { + //logger.resolver(prefix ++ s"loadClass($name)" ++ postfix ) + super.loadClass(name) + } + override def findClass(name: String): Class[_] = { + //logger.resolver(prefix ++ s"findClass($name)" ++ postfix ) + super.findClass(name) + } +} +*/ |