Skip to content
This repository
Browse code

use dynamic "safe_#" instead of random long

Closes #1196

Remove instance field  which stored random long in Mapper and Record
in favor of pseudo unique System.identityHashCode at call time for
purpose of runSafe{} blocks
  • Loading branch information...
commit c006de920eee45cc4522ecd9c1559c629dfd4eb0 1 parent a566382
Jon Hoffman authored February 09, 2012
14  core/util/src/main/scala/net/liftweb/util/Safe.scala
@@ -17,30 +17,22 @@
17 17
 package net.liftweb
18 18
 package util
19 19
 
20  
-import java.security.{SecureRandom, MessageDigest}
21  
-import org.apache.commons.codec.binary.Base64
22  
-
23 20
 /**
24 21
  * Manage the current "safety" state of the stack
25 22
  */
26 23
 object Safe {
27  
-  private val rand = new SecureRandom
28  
-  /**
29  
-   * Get the next "safe" number
30  
-   */
31  
-  def next = rand.synchronized{rand.nextLong}
32  
-  private val threadLocal = new ThreadGlobal[Long]
  24
+  private val threadLocal = new ThreadGlobal[Int]
33 25
 
34 26
   /**
35 27
    * Is the current context "safe" for the object with the
36 28
    * given safety code?
37 29
    */
38  
-  def safe_?(test : Long) : Boolean = test == threadLocal.value
  30
+  def safe_?(test : Int) : Boolean = test == threadLocal.value
39 31
 
40 32
   /**
41 33
    * Marks access to a given object as safe for the duration of the function
42 34
    */
43  
-  def runSafe[T](x : Long)(f : => T) : T = {
  35
+  def runSafe[T](x : Int)(f : => T) : T = {
44 36
      threadLocal.doWith(x)(f)
45 37
   }
46 38
 
5  persistence/mapper/src/main/scala/net/liftweb/mapper/Mapper.scala
@@ -37,7 +37,6 @@ trait Mapper[A<:Mapper[A]] extends BaseMapper {
37 37
   self: A =>
38 38
   type MapperType = A
39 39
 
40  
-  private val secure_# = Safe.next
41 40
   private var was_deleted_? = false
42 41
   private var dbConnectionIdentifier: Box[ConnectionIdentifier] = Empty
43 42
   private[mapper] var addedPostCommit = false
@@ -45,7 +44,7 @@ trait Mapper[A<:Mapper[A]] extends BaseMapper {
45 44
 
46 45
   def getSingleton : MetaMapper[A];
47 46
   final def safe_? : Boolean = {
48  
-    Safe.safe_?(secure_#)
  47
+    Safe.safe_?(System.identityHashCode(this))
49 48
   }
50 49
 
51 50
   def dbName:String = getSingleton.dbName
@@ -53,7 +52,7 @@ trait Mapper[A<:Mapper[A]] extends BaseMapper {
53 52
   implicit def thisToMappee(in: Mapper[A]): A = this.asInstanceOf[A]
54 53
 
55 54
   def runSafe[T](f : => T) : T = {
56  
-    Safe.runSafe(secure_#)(f)
  55
+    Safe.runSafe(System.identityHashCode(this))(f)
57 56
   }
58 57
 
59 58
   def connectionIdentifier(id: ConnectionIdentifier): A = {
9  persistence/record/src/main/scala/net/liftweb/record/Record.scala
@@ -31,11 +31,6 @@ trait Record[MyType <: Record[MyType]] extends FieldContainer {
31 31
   self: MyType =>
32 32
 
33 33
   /**
34  
-   * A unique identifier for this record... used for access control
35  
-   */
36  
-  private val secure_# = Safe.next
37  
-
38  
-  /**
39 34
    * Get the fields defined on the meta object for this record instance
40 35
    */
41 36
   def fields() = meta.fields(this)
@@ -51,11 +46,11 @@ trait Record[MyType <: Record[MyType]] extends FieldContainer {
51 46
    * Is it safe to make changes to the record (or should we check access control?)
52 47
    */
53 48
   final def safe_? : Boolean = {
54  
-    Safe.safe_?(secure_#)
  49
+    Safe.safe_?(System.identityHashCode(this))
55 50
   }
56 51
 
57 52
   def runSafe[T](f : => T) : T = {
58  
-    Safe.runSafe(secure_#)(f)
  53
+    Safe.runSafe(System.identityHashCode(this))(f)
59 54
   }
60 55
 
61 56
   /**

0 notes on commit c006de9

Please sign in to comment.
Something went wrong with that request. Please try again.