2 * Copyright 2011-2012 GRNET S.A. All rights reserved.
4 * Redistribution and use in source and binary forms, with or
5 * without modification, are permitted provided that the following
8 * 1. Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the following
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
30 * The views and conclusions contained in the software and
31 * documentation are those of the authors and should not be
32 * interpreted as representing official policies, either expressed
33 * or implied, of GRNET S.A.
36 package gr.grnet.aquarium
38 import com.ckkloverdos.resource.{StreamResource, CompositeStreamResourceContext, ClassLoaderStreamResourceContext, FileStreamResourceContext}
39 import com.ckkloverdos.maybe.{Failed, Just, Maybe, NoVal}
40 import com.ckkloverdos.sys.{SysEnv, SysProp}
43 import gr.grnet.aquarium.util.justForSure
46 * Used to locate configuration files.
48 * This code was initially in [[gr.grnet.aquarium.Configurator]].
50 * @author Christos KK Loverdos <loverdos@gmail.com>
53 object ResourceLocator {
55 final val LOGBACK_XML = "logback.xml"
58 final val AKKA_HOME = SysEnv("AKKA_HOME")
61 * This is normally exported from the shell script that starts Aquarium.
63 * TODO: Make this searchable for resources (ie put it in the resource context)
65 final val AQUARIUM_HOME = SysEnv("AQUARIUM_HOME")
67 final lazy val AQUARIUM_HOME_FOLDER: File = {
68 AQUARIUM_HOME.value match {
70 val file = new File(home)
71 if(!file.isDirectory) {
72 throw new Exception("%s (%s) is not a folder".format(AQUARIUM_HOME.name, home))
74 file.getCanonicalFile()
76 throw new Exception("%s is not set".format(AQUARIUM_HOME.name))
80 final lazy val AQUARIUM_HOME_CONF_FOLDER = new File(AQUARIUM_HOME_FOLDER, "conf")
82 final lazy val LOGBACK_XML_FILE = new File(AQUARIUM_HOME_CONF_FOLDER, Names.LOGBACK_XML)
85 * This exists in order to have a feeling of where we are.
87 final lazy val HERE = justForSure(getResource(".")).get.url.toExternalForm
90 * AQUARIUM_HOME/conf resource context.
92 private[this] final lazy val HomeConfResourceContext = new FileStreamResourceContext(AQUARIUM_HOME_CONF_FOLDER)
95 * The venerable /etc resource context. Applicable in Unix environments
97 private[this] final lazy val SlashEtcResourceContext = new FileStreamResourceContext("/etc/aquarium")
100 * Class loader resource context.
101 * This has the lowest priority.
103 private[this] final lazy val ClasspathBaseResourceContext = new ClassLoaderStreamResourceContext(Thread
104 .currentThread().getContextClassLoader)
107 * Use this property to override the place where aquarium configuration resides.
109 * The value of this property is a folder that defines the highest-priority resource context.
111 private[this] final lazy val ConfBaseFolderSysProp = SysProp("aquarium.conf.base.folder")
113 private[this] final lazy val BasicResourceContext = new CompositeStreamResourceContext(
115 SlashEtcResourceContext,
116 HomeConfResourceContext,
117 ClasspathBaseResourceContext)
120 * The resource context used in the application.
122 private[this] final lazy val MasterResourceContext = {
123 ConfBaseFolderSysProp.value match {
125 // We have a system override for the configuration location
126 new CompositeStreamResourceContext(Just(BasicResourceContext), new FileStreamResourceContext(value))
130 throw new AquariumException(m , e)
134 def getResource(what: String): Maybe[StreamResource] =
135 MasterResourceContext.getResource(what)