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

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

This example Play Framework source code file (PlayRunHooks.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

boolean, collection, inetsocketaddress, linkedhashmap, mutable, nonfatal, onstartedplayrunhook, onstoppedplayrunhook, play, play framework, playrunhook, runhooksrunner, seq, unit, utilities

The PlayRunHooks.scala Play Framework example source code

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

import scala.util.control.NonFatal
import scala.collection.mutable.LinkedHashMap
import java.net.InetSocketAddress

/**
 * The represents an object which "hooks into" play run, and is used to
 *  apply startup/cleanup actions around a play application.
 */
trait PlayRunHook {
  /** Called before the play application is started, but after all "before run" tasks have been completed. */
  def beforeStarted(): Unit = ()
  /**
   * Called after the play application has been started.
   *  @param addr  The address/socket that play is listening to.
   */
  def afterStarted(addr: InetSocketAddress): Unit = ()
  /**
   * Called after the play process has been stopped.
   */
  def afterStopped(): Unit = ()

  /**
   * Called if there was any exception thrown during play run.
   * Useful to implement to clean up any open resources for this hook.
   */
  def onError(): Unit = ()
}

case class RunHookCompositeThrowable(val throwables: Set[Throwable]) extends Exception(
  "Multiple exceptions thrown during PlayRunHook run: " +
    throwables.map(t => t + "\n" + t.getStackTrace.take(10).++("...").mkString("\n")).mkString("\n\n")
)

object PlayRunHook {

  def makeRunHookFromOnStarted(f: (java.net.InetSocketAddress) => Unit): PlayRunHook = {
    // We create an object for a named class...
    object OnStartedPlayRunHook extends PlayRunHook {
      override def afterStarted(addr: InetSocketAddress): Unit = f(addr)
    }
    OnStartedPlayRunHook
  }

  def makeRunHookFromOnStopped(f: () => Unit): PlayRunHook = {
    object OnStoppedPlayRunHook extends PlayRunHook {
      override def afterStopped(): Unit = f()
    }
    OnStoppedPlayRunHook
  }

  // A bit of a magic hack to clean up the PlayRun file
  implicit class RunHooksRunner(val hooks: Seq[PlayRunHook]) extends AnyVal {
    /** Runs all the hooks in the sequence of hooks.  reports last failure if any have failure. */
    def run(f: PlayRunHook => Unit, suppressFailure: Boolean = false): Unit = try {

      val failures: LinkedHashMap[PlayRunHook, Throwable] = LinkedHashMap.empty

      hooks foreach { hook =>
        try {
          f(hook)
        } catch {
          case NonFatal(e) =>
            failures += hook -> e
        }
      }

      // Throw failure if it occurred....
      if (!suppressFailure && failures.nonEmpty) {
        if (failures.size == 1) {
          throw failures.values.head
        } else {
          throw RunHookCompositeThrowable(failures.values.toSet)
        }
      }
    } catch {
      case NonFatal(e) if suppressFailure =>
      // Ignoring failure in running hooks... (CCE thrown here)
    }
  }
}

Other Play Framework source code examples

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

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

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 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.