com.rojoma.simplearm.v2

ResourceScope

final class ResourceScope extends AnyRef

A (managable) object for managing resources with lifetimes that do not nest nicely.

using(new ResourceScope) { rs =>
val in = rs.open(new FileInputStream("a"))
val out = rs.open(new FileOutputStream("b"))
rs.close(in) // explicit close of a resource
throw new Exception // resource scope ensures "out" is closed
}

Resource scopes may themselves be owned by other resource scopes. It is possible to build an object which contains complex sub-resources in this fashion.

def allocateComplexThing() =
using(new ResourceScope("temp")) { tmpScope =>
  new ComplexThing {
    // If ComplexThing's ctor throws, tmpScope will ensure cleanup
    val myScope = tmpScope.open(new ResourceScope("complex thing"))
    val sub1 = myScope.open(subResourceOne())
    val sub2 = myScope.open(subResourceTwo())
    // The very last thing the constructor does is unmanage
    // the scope and so take ownership.
    tmpScope.unmanage(myScope)
    def close() { myScope.close() }
  }
}

All methods on this class are thread-safe up to disposal of the ResourceScope itself.

Linear Supertypes
AnyRef, Any
Ordering
  1. Alphabetic
  2. By inheritance
Inherited
  1. ResourceScope
  2. AnyRef
  3. Any
  1. Hide All
  2. Show all
Learn more about member selection
Visibility
  1. Public
  2. All

Instance Constructors

  1. new ResourceScope(name: String = ResourceScope.anonymousScopeName)

Value Members

  1. final def !=(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  2. final def !=(arg0: Any): Boolean

    Definition Classes
    Any
  3. final def ##(): Int

    Definition Classes
    AnyRef → Any
  4. final def ==(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  5. final def ==(arg0: Any): Boolean

    Definition Classes
    Any
  6. final def asInstanceOf[T0]: T0

    Definition Classes
    Any
  7. def clone(): AnyRef

    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  8. def close(): Unit

    Closes all resources contained in this scope, in the reverse order they were added.

  9. def close(value: Any): Unit

  10. final def eq(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  11. def equals(arg0: Any): Boolean

    Definition Classes
    AnyRef → Any
  12. def finalize(): Unit

    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  13. final def getClass(): Class[_]

    Definition Classes
    AnyRef → Any
  14. def hashCode(): Int

    Definition Classes
    AnyRef → Any
  15. final def isInstanceOf[T0]: Boolean

    Definition Classes
    Any
  16. def isManaged(x: Any): Boolean

  17. val name: String

  18. final def ne(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  19. final def notify(): Unit

    Definition Classes
    AnyRef
  20. final def notifyAll(): Unit

    Definition Classes
    AnyRef
  21. def open[T](value: ⇒ T, transitiveClose: Seq[Any] = Nil)(implicit res: Resource[T]): T

    Manages a resource.

    Manages a resource. Once a resource is managed, to close it explicitly use the close(Any) method on the ResourceScope, not any close on the managed object.

    Optionally, this function can take a list of values to automatically close when the current resource is closed. Those values must be resources managed by this scope. If this value is transferred to another scope, the other resources will be transferred along with it.

    Exceptions thrown
    IllegalArgumentException

    if the value is already managed by this scope, or if any value listed in transitiveClose is not.

  22. def openUnmanaged[T](value: ⇒ T, transitiveClose: Seq[Any] = Nil): T

    Registers an unmanaged value with this scope.

    Registers an unmanaged value with this scope. This isn't very useful in itself, but it can take a list of values to transitively close, just like open. This is useful for things like iterator wrappers which are not themselves closeable but which refer to closable things, and which may be transferred to other scopes.

    Exceptions thrown
    IllegalArgumentException

    if the value is already "managed" by this scope, or if any value listed in transitiveClose is not.

  23. final def synchronized[T0](arg0: ⇒ T0): T0

    Definition Classes
    AnyRef
  24. def toString(): String

    Definition Classes
    ResourceScope → AnyRef → Any
  25. def transfer[A](value: A): Transferator[A]

    Transfers a resource's ownership (and that of all its dependencies) between two scopes.

    Transfers a resource's ownership (and that of all its dependencies) between two scopes.

    rs1.transfer(something).to(rs2)
    rs2.transfer(something).from(rs1) // equivalent

    If the resource has dependencies, the order in which they are transferred to the other scope is defined only up to what can be inferred by the graph formed by those dependencies. That is:

    val d1 = rs1.open(f())
    val d2 = rs1.open(f())
    val r = rs1.open(g(), transitiveClose = List(d1, d2))
    // rs1.close() would be guaranteed to close [r, d2, d1]
    rs1.transfer(r).to(rs2)
    rs2.close() // may close either [r, d2, d1], or [r, d1, d2]
    Exceptions thrown
    IllegalArgumentException

    if the value is not managed by the source scope

    Note

    This function is easy to use incorrectly; it depends on the declared dependency graph being correct.

  26. def unmanage(value: Any): Unit

    Un-manages a resource.

    Un-manages a resource. If this succeeds, the value will no longer be managed at all.

    Exceptions thrown
    IllegalArgumentException

    if the value is not managed by this scope

  27. def unmanagedWithAssociatedScope[A](associatedScopeName: String = ResourceScope.anonymousScopeName)(f: (ResourceScope) ⇒ A): A

    Utility for creating an owned-but-unmanaged object with an associated managed scope.

    Utility for creating an owned-but-unmanaged object with an associated managed scope.

    using(new ResourceScope("scope")) { rs =>
    val lines: Iterator[Line] = rs.unmanagedWithAssociatedScope("iterator scope") { itScope =>
      val stream = itScope.open(new FileInputStream("/tmp/foo"))
      linesOfFile(stream) // if this throws, stream will be closed before unmanagedWithAssociatedScope returns
    }
    rs.close(lines) // closes the associated FileInputStream
    }
  28. final def wait(): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  29. final def wait(arg0: Long, arg1: Int): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  30. final def wait(arg0: Long): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  31. def withCleanup[T, U](value: ⇒ T, transitiveClose: Seq[Any] = Nil)(cleanup: (T) ⇒ U): T

    Registers an value with this scope with an ad-hoc cleanup operation.

    Registers an value with this scope with an ad-hoc cleanup operation.

    Exceptions thrown
    IllegalArgumentException

    if the value is already managed by this scope, or if any value listed in transitiveClose is not.

Inherited from AnyRef

Inherited from Any

Ungrouped