alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Play Framework/Scala example source code file (ServerProcess.scala)

This example Play Framework source code file (ServerProcess.scala) is included in my "Source Code Warehouse" project. The intent of this project is to help you more easily find Play Framework (and Scala) source code examples by using tags.

All credit for the original source code belongs to Play Framework; I'm just trying to make examples easier to find. (For my Scala work, see my Scala examples and tutorials.)

Play Framework tags/keywords

classloader, core, int, none, nothing, option, play, play framework, properties, seq, server, serverprocess, string, unit

The ServerProcess.scala Play Framework example source code

/*
 * Copyright (C) 2009-2014 Typesafe Inc. <http://www.typesafe.com>
 */
package play.core.server

import java.lang.management.ManagementFactory
import java.util.Properties

/**
 * Abstracts a JVM process so it can be mocked for testing or to
 * isolate pseudo-processes within a VM. Code using this class
 * should use the methods in this class instead of methods like
 * `System.getProperties()`, `System.exit()`, etc.
 */
trait ServerProcess {
  /** The ClassLoader that should be used */
  def classLoader: ClassLoader
  /** The command line arguments the process as invoked with */
  def args: Seq[String]
  /** The process's system properties */
  def properties: Properties
  /** Helper for getting properties */
  final def prop(name: String): Option[String] = Option(properties.getProperty(name))
  /** The process's id */
  def pid: Option[String]
  /** Add a hook to run when the process shuts down */
  def addShutdownHook(hook: => Unit): Unit
  /** Exit the process with a message and optional cause and return code */
  def exit(message: String, cause: Option[Throwable] = None, returnCode: Int = -1): Nothing
}

/**
 * A ServerProcess that wraps a real JVM process. Calls have a real
 * effect on the JVM, e.g. `exit` calls `System.exit.`
 */
class RealServerProcess(val args: Seq[String]) extends ServerProcess {
  def classLoader: ClassLoader = Thread.currentThread.getContextClassLoader
  def properties: Properties = System.getProperties
  def pid: Option[String] = {
    ManagementFactory.getRuntimeMXBean.getName.split('@').headOption
  }
  def addShutdownHook(hook: => Unit): Unit = {
    Runtime.getRuntime.addShutdownHook(new Thread {
      override def run() = hook
    })
  }
  def exit(message: String, cause: Option[Throwable] = None, returnCode: Int = -1): Nothing = {
    System.err.println(message)
    cause.foreach(_.printStackTrace())
    System.exit(returnCode)
    // Code never reached, but throw an exception to give a type of Nothing
    throw new Exception("SystemProcess.exit called")
  }
}

Other Play Framework source code examples

Here is a short list of links related to this Play Framework ServerProcess.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.