All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.beangle.security.context.holder.scala Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (C) 2005, The Beangle Software.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see .
 */

package org.beangle.security.context

import org.beangle.commons.lang.Throwables
import org.beangle.commons.security.Request
import org.beangle.security.session.Session

object SecurityContext {
  val Anonymous = "anonymous"

  private val holder = buildHolder(System.getProperty("beangle.security.holder", "threadLocal"))

  /**
    * 
    *
  • threadLocal *
  • inheritableThreadLocal *
  • global *
*/ private def buildHolder(strategyName: String): SecurityContextHolder = { strategyName match { case "threadLocal" => new ThreadLocalHolder(false) case "inheritableThreadLocal" => new ThreadLocalHolder(true) case "global" => GlobalHolder case _ => try { val clazz = Class.forName(strategyName).asInstanceOf[Class[SecurityContextHolder]] val customStrategy = clazz.getConstructor() customStrategy.newInstance() } catch { case ex: Exception => throw Throwables.propagate(ex) } } } def clear(): Unit = { holder.set(null) } def set(ctx: SecurityContext): Unit = { holder.set(ctx) } def get: SecurityContext = { holder.get } } class SecurityContext(val session: Option[Session], val request: Request, val root: Boolean, val runAs: Option[String]) { def isValid: Boolean = { session.isDefined } } /** * A holder for storing security context information against a thread. *

* The preferred holder is loaded by ContextHolder *

* */ trait SecurityContextHolder { def get: SecurityContext def set(context: SecurityContext): Unit } /** * A static field-based implementation of ContextHolder. */ object GlobalHolder extends SecurityContextHolder { var context: SecurityContext = _ def get: SecurityContext = { context } def set(context: SecurityContext): Unit = { this.context = context } } /** * A ThreadLocal-based implementation of ContextHolder. */ class ThreadLocalHolder(inheritable: Boolean) extends SecurityContextHolder { private val context = if (inheritable) { new ThreadLocal[SecurityContext] } else { new InheritableThreadLocal[SecurityContext] } def get: SecurityContext = { context.get } def set(ctx: SecurityContext): Unit = { context.set(ctx) } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy