本文整理汇总了Scala中java.net.URLClassLoader类的典型用法代码示例。如果您正苦于以下问题:Scala URLClassLoader类的具体用法?Scala URLClassLoader怎么用?Scala URLClassLoader使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了URLClassLoader类的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: ClassPathExistsException
//设置package包名称以及导入依赖的类
package com.galacticfog.gestalt.lambda.impl
import java.io.File
import java.io.IOException
import java.lang.reflect.Method
import java.net.URL
import java.net.URLClassLoader
import java.util.Iterator
import java.util.ServiceLoader
case class ClassPathExistsException( msg : String ) extends Exception
object DynLoader {
def extendClasspath( dir : File, inLoader : ClassLoader ) = {
try {
val sysLoader : URLClassLoader = inLoader match {
case u : URLClassLoader => u
case _ => throw new Exception( "Cast Exception" )
}
val urls : Seq[URL] = sysLoader.getURLs()
val udir = dir.toURI().toURL()
val udirs = udir.toString()
urls.find( u => u.toString().equalsIgnoreCase(udirs) ) match {
case Some(s) => throw new ClassPathExistsException( "class path exists" )
case None => {}
}
val sysClass = classOf[URLClassLoader]
val method : Method = sysClass.getDeclaredMethod("addURL", classOf[URL] )
method.setAccessible(true)
val udirObj = udir match {
case o : Object => o
case _ => throw new Exception( "impossible" )
}
method.invoke(sysLoader, udirObj )
println( "Loaded " + udirs + " dynamically...")
}
catch {
case cpe : ClassPathExistsException => {
println( "class path exists, ignoring" )
}
case t : Throwable => {
t.printStackTrace();
}
}
}
}
示例2: JarClassSource
//设置package包名称以及导入依赖的类
package eu.tznvy.jancy.transpiler.discovery
import java.io.File
import java.net.URLClassLoader
import java.util.zip.ZipFile
import java.io.Closeable
import scala.collection.JavaConverters._
class JarClassSource(file: File) extends Closeable {
lazy val zipFile = new ZipFile(file.getPath)
override def close(): Unit = {
zipFile.close()
}
def iterate: Iterator[Class[_]] = {
val classLoader = new URLClassLoader(Array(file.toURI.toURL))
zipFile
.stream
.iterator
.asScala
.map(_.getName)
.filter(_.endsWith(".class"))
.map(_.replace(".class", "").replace('/', '.'))
.map(classLoader.loadClass)
}
}
示例3: JVMUtil
//设置package包名称以及导入依赖的类
package org.argus.jawa.core.util
import java.io.{BufferedReader, InputStreamReader}
import java.net.URLClassLoader
import java.text.NumberFormat
object JVMUtil {
def startSecondJVM[C](clazz: Class[C], jvmArgs: List[String], args: List[String], redirectStream: Boolean): Int = {
val separator = System.getProperty("file.separator")
val classpath = Thread.currentThread().getContextClassLoader.asInstanceOf[URLClassLoader].getURLs.map(_.getPath()).reduce((c1, c2) => c1 + java.io.File.pathSeparator + c2)
val path = System.getProperty("java.home") + separator + "bin" + separator + "java"
val commands: IList[String] = List(path) ::: jvmArgs ::: List("-cp", classpath, clazz.getCanonicalName.stripSuffix("$")) ::: args
import scala.collection.JavaConverters._
val processBuilder = new ProcessBuilder(commands.asJava)
processBuilder.redirectErrorStream(redirectStream)
val process = processBuilder.start()
val is = process.getInputStream
val isr = new InputStreamReader(is)
val br = new BufferedReader(isr)
var line = br.readLine()
while (line != null) {
println(line)
line = br.readLine()
}
process.waitFor()
}
def showMemoryUsage(): Unit = {
val runtime = Runtime.getRuntime
val format = NumberFormat.getInstance()
val sb = new StringBuilder()
val maxMemory = runtime.maxMemory()
val allocatedMemory = runtime.totalMemory()
val freeMemory = runtime.freeMemory()
sb.append("free memory: " + format.format(freeMemory / 1024 / 1024) + " ")
sb.append("allocated memory: " + format.format(allocatedMemory / 1024 / 1024) + " ")
sb.append("max memory: " + format.format(maxMemory / 1024 / 1024) + " ")
sb.append("total free memory: " + format.format((freeMemory + (maxMemory - allocatedMemory)) / 1024 / 1024) + " ")
println(sb.toString())
}
}
示例4: Utils
//设置package包名称以及导入依赖的类
package helpers
import java.io.File
import java.net.URLClassLoader
import cell.HandlerPool
object Utils {
val TestExpectedMessagePropertyStr = "plugin.test.expected.message"
val IsMutable = "mutable"
val IsShallowImmutable = "shallow immutable"
val IsDeeplyImmutable = "deep immutable"
val IsConditionallyImmutable = "conditionally immutable"
// Treat private var as "val"
val AllowPrivateVar = false
// Assume that certain types are immutable/mutable e.g., "scala.collection.immutable.list" is immutable.
val MakeAssumptionAboutTypes = true
private val TheScalaTestProject = false
// TODO: Temporary fix, for when analyzing Scala test project itself.
private val ScalaTestPattern = "scalatest"
private val ScalaTestClassPathFound = !TheScalaTestProject && getClass.getClassLoader.asInstanceOf[URLClassLoader].getURLs.map(_.getFile).mkString(File.pathSeparator).contains(ScalaTestPattern)
private val LoggingEnabled = !isScalaTest
private var pool: HandlerPool = _
def log(msg: => String): Unit = {
if (LoggingEnabled) {
println(s"[log] ${msg}")
}
}
def mutabilityMessage(className: String, message: String): String = s"class $className is $message"
def getCurrentTestMessage: String = System.getProperty(TestExpectedMessagePropertyStr)
def setCurrentTestMessage(mutabilityMessage: String): Unit = System.setProperty(Utils.TestExpectedMessagePropertyStr, mutabilityMessage)
def isScalaTest = ScalaTestClassPathFound
def getPool: HandlerPool = {
if (pool == null) {
newPool
}
pool
}
def newPool = {
pool = new HandlerPool()
}
}
示例5: TestUtils
//设置package包名称以及导入依赖的类
package utils
import java.io.File
import java.net.URLClassLoader
import helpers.Utils
import org.scalatest.{FlatSpec, Matchers}
import scala.reflect.runtime.{universe => ru}
import scala.tools.reflect.{ToolBox, ToolBoxError};
object TestUtils extends FlatSpec with Matchers {
val cl = getClass.getClassLoader.asInstanceOf[URLClassLoader]
val cp = cl.getURLs.map(_.getFile).mkString(File.pathSeparator)
val pluginPath = cp
val mirror = ru.runtimeMirror(cl)
val tb = mirror.mkToolBox(options = s"-Dmsg=hello -cp $cp -Xplugin:$cp") // TODO: Might have to extract plugin path instead of passing in all class paths
def expectMutability(klassesToImmutability: Map[List[String], String])(code: String): Unit = {
for ((klasses, immutabilityMessage) <- klassesToImmutability) {
expectMutability(klasses, immutabilityMessage)(code)
}
}
def expectMutability(klasses: List[String], immutabilityMessage: String)(code: String) {
for (klass <- klasses) {
expectMutability(klass, immutabilityMessage)(code)
}
}
def expectMutability(klass: String, immutabilityMessage: String)(code: String) {
val mutabilityMessage = Utils.mutabilityMessage(klass, immutabilityMessage)
Utils.setCurrentTestMessage(mutabilityMessage)
val e = intercept[ToolBoxError] {
// Intercept a false negative error to notify that the test was successful
tb.eval(tb.parse(code))
}
e.getMessage should include(mutabilityMessage)
}
}
示例6: apply
//设置package包名称以及导入依赖的类
package org.dama.datasynth.runtime.spark.operators
import java.net.{URL, URLClassLoader}
import org.apache.spark.sql.SparkSession
import org.dama.datasynth.common.generators.property.PropertyGenerator
import org.dama.datasynth.executionplan.ExecutionPlan
import org.dama.datasynth.runtime.spark.SparkRuntime
import org.dama.datasynth.runtime.spark.utils.PropertyGeneratorWrapper
import scala.util.{Failure, Success}
def apply[T](propertyTableName : String, info : ExecutionPlan.PropertyGenerator[T]) : PropertyGeneratorWrapper[T] = {
val initParameters : Seq[Object] = info.initParameters.map( x => SparkRuntime.evalValueOperator(x))
.map( ref => ref.asInstanceOf[Object])
val jarUrl = SparkRuntime.getConfig().masterWorkspaceDir+"/temp.jar"
val urlCL = new URLClassLoader( Array[URL]( new URL(jarUrl)),getClass.getClassLoader());
val constructor = urlCL.loadClass(info.className).getConstructors()(0)
val generator = constructor.newInstance(initParameters : _*).asInstanceOf[PropertyGenerator[T]]
val rndGen = SparkRuntime.fetchRndGeneratorOperator(propertyTableName)
val dependentPGs = info.dependentPropertyTables.map( pt => SparkRuntime.instantiatePropertyGeneratorOperator(pt.name, pt.generator))
new PropertyGeneratorWrapper[T](generator,rndGen,dependentPGs)
}
}
示例7: DependencyAnalyzer
//设置package包名称以及导入依赖的类
package com.kakao.cuesheet.deps
import java.net.{URL, URLClassLoader}
import com.kakao.mango.concurrent.KeyedSingletons
import com.kakao.mango.logging.Logging
import com.kakao.mango.reflect.Accessible
import scala.reflect.io.AbstractFile
class DependencyAnalyzer(loader: ClassLoader = getClass.getClassLoader) extends Logging {
val chain: List[ClassLoader] = DependencyAnalyzer.classLoaderChain(loader)
lazy val graph = {
// collect all nodes
val nodes = for (
loader <- chain;
url <- DependencyAnalyzer.components(loader)
) yield {
DependencyNode.resolve(url)
}
new DependencyGraph(nodes)
}
}
object DependencyAnalyzer extends KeyedSingletons[ClassLoader, DependencyAnalyzer] with Logging {
def components(loader: ClassLoader): Seq[URL] = {
loader match {
case _ if loader.getClass.getName.startsWith("sun.misc.Launcher$ExtClassLoader") =>
Nil // ignore extension class loader
case loader: URLClassLoader =>
loader.getURLs
case _ if loader.getClass.getName == "scala.tools.nsc.interpreter.AbstractFileClassLoader" =>
val root = Accessible.field(loader.getClass, "root")
Seq(root.get(loader).asInstanceOf[AbstractFile].toURL)
case _ if loader.getClass.getName == "scala.reflect.internal.util.AbstractFileClassLoader" =>
val root = Accessible.field(loader.getClass, "root")
Seq(root.get(loader).asInstanceOf[AbstractFile].toURL)
case _ if Seq(loader.getClass.getName).exists(c => c.startsWith("xsbt.") || c.startsWith("sbt.")) =>
Nil // ignore SBT's internal loader
case _ =>
throw new RuntimeException("Unknown ClassLoader Type: " + loader.getClass.getName)
}
}
override def newInstance(loader: ClassLoader): DependencyAnalyzer = {
new DependencyAnalyzer(loader)
}
def apply(): DependencyAnalyzer = apply(getClass.getClassLoader)
}
示例8: NamedURLClassLoader
//设置package包名称以及导入依赖的类
package com.lightbend.lagom.dev
import java.net.{ URL, URLClassLoader }
import java.util
class NamedURLClassLoader(name: String, urls: Array[URL], parent: ClassLoader) extends URLClassLoader(urls, parent) {
override def toString = name + "{" + getURLs.map(_.toString).mkString(", ") + "}"
}
class DelegatingClassLoader(commonLoader: ClassLoader, sharedClasses: Set[String], buildLoader: ClassLoader,
applicationClassLoader: () => Option[ClassLoader]) extends ClassLoader(commonLoader) {
lazy val findResourceMethod = {
val method = classOf[ClassLoader].getDeclaredMethod("findResource", classOf[String])
method.setAccessible(true)
method
}
lazy val findResourcesMethod = {
val method = classOf[ClassLoader].getDeclaredMethod("findResources", classOf[String])
method.setAccessible(true)
method
}
override def loadClass(name: String, resolve: Boolean) = {
if (sharedClasses(name)) {
buildLoader.loadClass(name)
} else {
super.loadClass(name, resolve)
}
}
override def getResource(name: String) = {
applicationClassLoader()
.flatMap(cl => Option(findResourceMethod.invoke(cl, name).asInstanceOf[URL]))
.getOrElse(super.getResource(name))
}
override def getResources(name: String) = {
val appResources = applicationClassLoader().fold(new util.Vector[URL]().elements) { cl =>
findResourcesMethod.invoke(cl, name).asInstanceOf[util.Enumeration[URL]]
}
val superResources = super.getResources(name)
val resources = new util.Vector[URL]()
while (appResources.hasMoreElements) resources.add(appResources.nextElement())
while (superResources.hasMoreElements) resources.add(superResources.nextElement())
resources.elements()
}
override def toString = "DelegatingClassLoader, using parent: " + getParent
}
示例9: extractScalaClassLoader
//设置package包名称以及导入依赖的类
package com.lightbend.lagom.maven
import java.net.URLClassLoader
import javax.inject.{ Inject, Singleton }
import org.eclipse.aether.artifact.Artifact
def extractScalaClassLoader(artifacts: Seq[Artifact]): ClassLoader = synchronized {
val scalaArtifacts = artifacts.filter { artifact =>
ScalaLibs.contains(artifact.getGroupId -> stripScalaVersion(artifact.getArtifactId))
}
val cacheKey = createCacheKey(scalaArtifacts)
cache.get(cacheKey) match {
case Some(classLoader) =>
logger.debug(s"ScalaClassLoader cache hit - $cacheKey")
classLoader
case None =>
logger.debug(s"ScalaClassLoader cache miss - $cacheKey")
val classLoader = new URLClassLoader(scalaArtifacts.map(_.getFile.toURI.toURL).toArray, null)
cache += (cacheKey -> classLoader)
classLoader
}
}
}
示例10: ContextURLClassLoader
//设置package包名称以及导入依赖的类
package spark.jobserver.util
import java.net.{URLClassLoader, URL}
import org.slf4j.LoggerFactory
class ContextURLClassLoader(urls: Array[URL], parent: ClassLoader)
extends URLClassLoader(urls, parent) {
val logger = LoggerFactory.getLogger(getClass)
override def addURL(url: URL) {
if (!getURLs.contains(url)) {
super.addURL(url)
logger.info("Added URL " + url + " to ContextURLClassLoader")
}
}
}
示例11: TestProject
//设置package包名称以及导入依赖的类
import sbt._
import java.net.{URL, URLClassLoader}
class TestProject(info: ProjectInfo) extends DefaultProject(info)
{
override def useMavenConfigurations = true
val direct = "slinky" % "slinky" % "2.1" % "test->default" from "http://slinky2.googlecode.com/svn/artifacts/2.1/slinky.jar"
lazy val checkInTest = checkClasspath(testClasspath)
lazy val checkInCompile = checkClasspath(compileClasspath)
private def checkClasspath(cp: PathFinder) =
task
{
try
{
Class.forName("slinky.http.Application", false, new URLClassLoader(cp.get.map(_.asURL).toList.toArray))
None
}
catch
{
case _: ClassNotFoundException =>
Some("Dependency not downloaded.")
}
}
}
示例12: classpath
//设置package包名称以及导入依赖的类
package com.hindog.grid.hadoop
import java.io.StringWriter
import java.net.{URL, URLClassLoader}
import java.util.concurrent.TimeUnit
import com.hindog.grid.ClasspathUtils
import org.apache.commons.io.IOUtils
import org.apache.hadoop.conf.Configuration
def classpath: Array[URL] = {
val cpProcess = new ProcessBuilder("/bin/bash", "hadoop", "classpath").start()
val stringWriter = new StringWriter()
cpProcess.waitFor(10, TimeUnit.SECONDS)
IOUtils.copy(cpProcess.getInputStream, stringWriter)
ClasspathUtils.explodeClasspath(stringWriter.toString)
}
def withHadoopClassloader[T](thunk: => T): T = {
val parent = Thread.currentThread().getContextClassLoader
try {
Thread.currentThread().setContextClassLoader(new URLClassLoader(classpath))
thunk
} finally {
Thread.currentThread().setContextClassLoader(parent)
}
}
}