java.security.AccessController Scala Examples

The following examples show how to use java.security.AccessController. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
Example 1
Source File: OSUtil.scala    From tsec   with MIT License 5 votes vote down vote up
package tsec.common

import java.security.{AccessController, PrivilegedAction}
import java.util.Locale

object OSUtil {

  lazy val isWindows: Boolean = {
    OSUtil.getSystemProperty("os.name", "").toLowerCase(Locale.US).contains("win")
  }

  lazy val isOsx: Boolean = {
    val osName = OSUtil
      .getSystemProperty("os.name", "")
      .toLowerCase(Locale.US)
      .replaceAll("[^a-z0-9]+", "")

    osName.startsWith("macosx") || osName.startsWith("osx")
  }

  def getSystemProperty(key: String, default: String): String = {
    if (key == null) throw new NullPointerException("key")
    if (key.isEmpty) throw new IllegalArgumentException("key must not be empty.")
    try if (System.getSecurityManager == null) System.getProperty(key)
    else
      AccessController.doPrivileged(new PrivilegedAction[String]() {
        override def run: String = System.getProperty(key)
      })
    catch {
      case e: SecurityException =>
        default
    }
  }

} 
Example 2
Source File: Disposer.scala    From tensorflow_scala   with Apache License 2.0 5 votes vote down vote up
package org.platanios.tensorflow.api.utilities

import java.lang.Thread.currentThread
import java.lang.ref.{PhantomReference, Reference, ReferenceQueue}
import java.security.{AccessController, PrivilegedAction}
import java.util
import java.util.concurrent.ConcurrentHashMap

import scala.annotation.tailrec


  def add(target: Any, disposer: () => Unit ): Reference[Any] = {
    val reference = new PhantomReference(target, queue)
    records.put(reference, disposer)
    // TODO: make sure reference isn't GC'd before this point (e.g., with org.openjdk.jmh.infra.Blackhole::consume).
    reference
  }

  AccessController.doPrivileged(new PrivilegedAction[Unit] {
    override def run(): Unit = {
      // The thread must be a member of a thread group which will not get GCed before the VM exit. For this reason, we
      // make its parent the top-level thread group.
      @tailrec def rootThreadGroup(group: ThreadGroup = currentThread.getThreadGroup): ThreadGroup = {
        group.getParent match {
          case null => group
          case parent => rootThreadGroup(parent)
        }
      }
     
      new Thread(rootThreadGroup(), "TensorFlow Scala API Disposer") {
        override def run = while (true) {
          // Blocks until there is a reference in the queue.
          val referenceToDispose = queue.remove
          records.remove(referenceToDispose).apply()
          referenceToDispose.clear()
        }
        setContextClassLoader(null)
        setDaemon(true)
        setPriority(Thread.MAX_PRIORITY)
        start()
      }
    }
  })
} 
Example 3
Source File: PathUtil.scala    From seed   with Apache License 2.0 5 votes vote down vote up
package seed.generation.util

import java.nio.file.{Files, Path, Paths}
import java.security.AccessController

import seed.Log
import seed.cli.util.Ansi

object PathUtil {
  val TemporaryFolder = Paths.get(System.getProperty("java.io.tmpdir"))

  def tmpfsPath(projectPath: Path, log: Log): Path = {
    val name = projectPath.toAbsolutePath.getFileName.toString
    log.info("Build path set to tmpfs")
    log.warn(
      s"Please ensure that no other project with the name ${Ansi.italic(name)} compiles to tmpfs"
    )
    Paths.get("/tmp").resolve("build-" + name)
  }

  def buildPath(projectPath: Path, tmpfs: Boolean, log: Log): Path =
    if (tmpfs) tmpfsPath(projectPath, log) else projectPath.resolve("build")

  def normalisePath(pathVariable: String, root: Path)(path: Path): String = {
    val absoluteRoot = root.toFile.getAbsolutePath
    val absolutePath = path.toFile.getAbsolutePath

    val rootElems = absoluteRoot.split("/").toList
    val pathElems = absolutePath.split("/").toList
    val common    = pathElems.zip(rootElems).takeWhile { case (a, b) => a == b }

    if (common.length == 1) absolutePath
    else {
      val levels       = rootElems.length - common.length
      val relativePath = (0 until levels).map(_ => "../").mkString
      pathVariable + "/" + relativePath + pathElems
        .drop(common.length)
        .mkString("/")
    }
  }

  def buildFilePath(userPath: Path): Path =
    if (!Files.isDirectory(userPath)) userPath
    else userPath.resolve("build.toml")
}