Skip to content

Add answer to exercise 8 chapter 3 #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 14, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Handle 'System.exit' in exercise 8 chapter3
  • Loading branch information
ssmylh committed Dec 11, 2015
commit f6c6810f40c570539c58b3636b1a3225fbb9709f
11 changes: 11 additions & 0 deletions src/main/scala/org/learningconcurrency/exercises/ch3/ex8.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ object Ex8 extends App {

// This method's preconditions are the following:
// - In case of executing in sbt, set `fork` setting to `true` (set fork := true ).
//
// If passed block which contains `System.exit`, this method throws `SecurityException`.
def spawn[T](block: => T): T = {
val className = Ex8_EvaluationApp.getClass().getName().split((Pattern.quote("$")))(0)
val tmp = File.createTempFile("concurrent-programming-in-scala", null)
Expand Down Expand Up @@ -62,4 +64,13 @@ object Ex8 extends App {
case e: NumberFormatException =>
case _: Throwable => assert(false)
}

try {
spawn({
System.exit(0)
})
} catch {
case e: SecurityException =>
case _: Throwable => assert(false)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,20 @@ import java.io.FileInputStream
import java.io.FileOutputStream
import java.io.ObjectInputStream
import java.io.ObjectOutputStream
import java.security.Permission

// This application receives the file path in which the serialized `Function0` object has been written.
// Then, it reads and evaluates serialized `Function0` object, finally overwrites its result to the same file.
object Ex8_EvaluationApp extends App {
System.setSecurityManager(new SecurityManager() {
// allows access to file.
override def checkPermission(perm: Permission): Unit = {}
// detects `System.exit` in order not to be halted by the caller.
override def checkExit(status: Int): Unit = {
throw new SecurityException("not allowed to pass a block which contains System.exit(int) !")
}
});

val path = args(0)

val in = new ObjectInputStream(new FileInputStream(path))
Expand Down