Fixing Jersey classpath resource scanning

This commit is contained in:
Viktor Klang 2010-06-05 15:38:43 +02:00
parent 5694fde51c
commit be159c9fa1

View file

@ -7,10 +7,45 @@ package se.scalablesolutions.akka.actor
import java.io.File
import java.net.{URL, URLClassLoader}
import java.util.jar.JarFile
import java.util.Enumeration
import se.scalablesolutions.akka.util.{Bootable, Logging}
import se.scalablesolutions.akka.config.Config._
class AkkaDeployClassLoader(urls : List[URL], parent : ClassLoader) extends URLClassLoader(urls.toArray.asInstanceOf[Array[URL]],parent) with Logging
{
override def findResources(resource : String) = {
val normalResult = super.findResources(resource)
if(normalResult.hasMoreElements) normalResult else findDeployed(resource)
}
def findDeployed(resource : String) = new Enumeration[URL]{
private val classes = getURLs.flatMap( listClassesInPackage(_,resource) )
log.debug("When looking for %s we found %s",resource,classes.mkString(", "))
private val it = classes.iterator
def hasMoreElements = it.hasNext
def nextElement = it.next
}
def listClassesInPackage(jar : URL, pkg : String) = {
val f = new File(jar.getFile)
val jf = new JarFile(f)
try {
val es = jf.entries
var result = List[URL]()
while(es.hasMoreElements)
{
val e = es.nextElement
if(!e.isDirectory && e.getName.startsWith(pkg) && e.getName.endsWith(".class"))
result ::= new URL("jar:" + f.toURI.toURL + "!/" + e)
}
result
} finally {
jf.close
}
}
}
/**
* Handles all modules in the deploy directory (load and unload)
*/
@ -46,10 +81,7 @@ trait BootableActorLoaderService extends Bootable with Logging {
log.debug("Loading dependencies [%s]", dependencyJars)
val allJars = toDeploy ::: dependencyJars
URLClassLoader.newInstance(
allJars.toArray.asInstanceOf[Array[URL]],
Thread.currentThread.getContextClassLoader)
//parentClassLoader)
new AkkaDeployClassLoader(allJars,Thread.currentThread.getContextClassLoader)
} else Thread.currentThread.getContextClassLoader)
}