本文整理汇总了Scala中java.util.ServiceLoader类的典型用法代码示例。如果您正苦于以下问题:Scala ServiceLoader类的具体用法?Scala ServiceLoader怎么用?Scala ServiceLoader使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ServiceLoader类的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: ServiceLoaderBackedExtensionProvider
//设置package包名称以及导入依赖的类
package com.atomist.util
import java.util.ServiceLoader
import com.atomist.rug.RugRuntimeException
import com.typesafe.scalalogging.LazyLogging
import scala.collection.JavaConverters._
import scala.reflect.{ClassTag, _}
class ServiceLoaderBackedExtensionProvider[T: ClassTag](val keyProvider: T => String)
extends LazyLogging {
// The following can be cached as it creates issues in shared class loader hierarchies
def providerMap: Map[String, T] = {
logger.debug(s"Loading providers of type ${classTag[T].runtimeClass.getName} and class loader ${Thread.currentThread().getContextClassLoader}")
ServiceLoader.load(classTag[T].runtimeClass).asScala.map {
case t: T =>
val key = keyProvider.apply(t)
logger.debug(s"Registered provider '$key' with class '${t.getClass}'")
key -> t
case wtf =>
throw new RugRuntimeException("Extension", s"Provider class ${wtf.getClass} must implement ${classTag[T].runtimeClass.getName} interface", null)
}.toMap
}
}
示例2: CLI
//设置package包名称以及导入依赖的类
package org.tomahna.scalaresume.cli
import java.io.File
import java.util.ServiceLoader
import scala.io.Source
import org.tomahna.scalaresume.resume.Resume
import org.tomahna.scalaresume.theme.ThemeManager
import com.google.inject.Guice
import com.google.inject.Module
import play.api.libs.json.Json
object CLI {
private val parser = new scopt.OptionParser[Configuration](BuildInfo.name) {
head(BuildInfo.name, BuildInfo.version)
help("help").text("prints this usage text")
arg[File]("inputResume").minOccurs(1).maxOccurs(1)
.action { (x, c) => c.copy(inputResume = x) }
.text("input File")
arg[File]("outputResume").minOccurs(1).maxOccurs(1)
.action { (x, c) => c.copy(outputResume = x) }
.text("output File")
opt[String]('t', "theme")
.action { (x, c) => c.copy(theme = x) }
.text("Theme to use for printing")
}
def main(args: Array[String]): Unit = {
parser.parse(args, Configuration()) match {
case Some(config) => run(config)
case None =>
}
}
def run(configuration: Configuration): Unit = {
val injector = Guice.createInjector(ServiceLoader.load(classOf[Module]))
val themeManager = injector.getInstance(classOf[ThemeManager])
val theme = themeManager.getTheme(configuration.theme)
val input = Source.fromFile(configuration.inputResume, "utf-8").mkString
val resume = Json.parse(input).as[Resume]
theme.outputTex(resume, configuration.outputResume)
}
}