summaryrefslogtreecommitdiff
path: root/test/disabled/presentation/akka/src/akka/config/Config.scala
blob: 6578c66f775d61bee6b21fda079b9cef1f24023a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/**
 * Copyright (C) 2009-2011 Scalable Solutions AB <http://scalablesolutions.se>
 */

package akka.config

import akka.AkkaException

class ConfigurationException(message: String, cause: Throwable = null) extends AkkaException(message, cause)
class ModuleNotAvailableException(message: String, cause: Throwable = null) extends AkkaException(message, cause)

/**
 * Loads up the configuration (from the akka.conf file).
 *
 * @author <a href="http://jonasboner.com">Jonas Bon&#233;r</a>
 */
object Config {
  val VERSION = "1.1.3"

  val HOME = {
    val envHome = System.getenv("AKKA_HOME") match {
      case null | "" | "." => None
      case value           => Some(value)
    }

    val systemHome = System.getProperty("akka.home") match {
      case null | "" => None
      case value     => Some(value)
    }

    envHome orElse systemHome
  }

  val config: Configuration = try {
    val confName = {
      val envConf = System.getenv("AKKA_MODE") match {
        case null | "" => None
        case value     => Some(value)
      }

      val systemConf = System.getProperty("akka.mode") match {
        case null | "" => None
        case value     => Some(value)
      }

      (envConf orElse systemConf).map("akka." + _ + ".conf").getOrElse("akka.conf")
    }

    val newInstance =
      if (System.getProperty("akka.config", "") != "") {
        val configFile = System.getProperty("akka.config", "")
        println("Loading config from -Dakka.config=" + configFile)
        Configuration.fromFile(configFile)
      } else if (getClass.getClassLoader.getResource(confName) ne null) {
        println("Loading config [" + confName + "] from the application classpath.")
        Configuration.fromResource(confName, getClass.getClassLoader)
      } else if (HOME.isDefined) {
        val configFile = HOME.get + "/config/" + confName
        println("AKKA_HOME is defined as [" + HOME.get + "], loading config from [" + configFile + "].")
        Configuration.fromFile(configFile)
      } else {
        println(
          "\nCan't load '" + confName + "'." +
            "\nOne of the three ways of locating the '" + confName + "' file needs to be defined:" +
            "\n\t1. Define the '-Dakka.config=...' system property option." +
            "\n\t2. Put the '" + confName + "' file on the classpath." +
            "\n\t3. Define 'AKKA_HOME' environment variable pointing to the root of the Akka distribution." +
            "\nI have no way of finding the '" + confName + "' configuration file." +
            "\nUsing default values everywhere.")
        Configuration.fromString("akka {}") // default empty config
      }

    val configVersion = newInstance.getString("akka.version", VERSION)
    if (configVersion != VERSION)
      throw new ConfigurationException(
        "Akka JAR version [" + VERSION + "] is different than the provided config version [" + configVersion + "]")

    newInstance
  } catch {
    case e =>
      System.err.println("Couldn't parse config, fatal error.")
      e.printStackTrace(System.err)
      System.exit(-1)
      throw e
  }

  val CONFIG_VERSION = config.getString("akka.version", VERSION)

  val TIME_UNIT = config.getString("akka.time-unit", "seconds")

  val startTime = System.currentTimeMillis
  def uptime = (System.currentTimeMillis - startTime) / 1000
}