root / src / main / scala / gr / grnet / aquarium / ResourceLocator.scala @ ae9ad70f
History | View | Annotate | Download (4.5 kB)
1 |
/* |
---|---|
2 |
* Copyright 2011-2012 GRNET S.A. All rights reserved. |
3 |
* |
4 |
* Redistribution and use in source and binary forms, with or |
5 |
* without modification, are permitted provided that the following |
6 |
* conditions are met: |
7 |
* |
8 |
* 1. Redistributions of source code must retain the above |
9 |
* copyright notice, this list of conditions and the following |
10 |
* disclaimer. |
11 |
* |
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. |
16 |
* |
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. |
29 |
* |
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. |
34 |
*/ |
35 |
|
36 |
package gr.grnet.aquarium |
37 |
|
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} |
41 |
import java.io.File |
42 |
|
43 |
import gr.grnet.aquarium.util.justForSure |
44 |
|
45 |
/** |
46 |
* Used to locate configuration files. |
47 |
* |
48 |
* This code was initially in [[gr.grnet.aquarium.Configurator]]. |
49 |
* |
50 |
* @author Christos KK Loverdos <loverdos@gmail.com> |
51 |
*/ |
52 |
|
53 |
object ResourceLocator { |
54 |
/** |
55 |
* This is normally exported from the shell script that starts Aquarium. |
56 |
*/ |
57 |
final val AQUARIUM_HOME = SysEnv("AQUARIUM_HOME") |
58 |
|
59 |
lazy val AQUARIUM_HOME_FOLDER: File = { |
60 |
AQUARIUM_HOME.value match { |
61 |
case Just(home) ⇒ |
62 |
val file = new File(home) |
63 |
if(!file.isDirectory) { |
64 |
throw new Exception("%s (%s) is not a folder".format(AQUARIUM_HOME.name, home)) |
65 |
} |
66 |
file.getCanonicalFile() |
67 |
case _ ⇒ |
68 |
throw new Exception("%s is not set".format(AQUARIUM_HOME.name)) |
69 |
} |
70 |
} |
71 |
|
72 |
/** |
73 |
* This exists in order to have a feeling of where we are. |
74 |
*/ |
75 |
final val HERE = justForSure(getResource(".")).get.url.toExternalForm |
76 |
|
77 |
/** |
78 |
* Current directory resource context. |
79 |
*/ |
80 |
private[this] final val AppBaseResourceContext = new FileStreamResourceContext(".") |
81 |
|
82 |
/** |
83 |
* Default config context for Aquarium distributions |
84 |
*/ |
85 |
private[this] final val LocalConfigResourceContext = new FileStreamResourceContext("conf") |
86 |
|
87 |
/** |
88 |
* The venerable /etc resource context. Applicable in Unix environments |
89 |
*/ |
90 |
private[this] final val SlashEtcResourceContext = new FileStreamResourceContext("/etc/aquarium") |
91 |
|
92 |
/** |
93 |
* Class loader resource context. |
94 |
* This has the lowest priority. |
95 |
*/ |
96 |
private[this] final val ClasspathBaseResourceContext = new ClassLoaderStreamResourceContext(Thread.currentThread().getContextClassLoader) |
97 |
|
98 |
/** |
99 |
* Use this property to override the place where aquarium configuration resides. |
100 |
* |
101 |
* The value of this property is a folder that defines the highest-priority resource context. |
102 |
*/ |
103 |
private[this] final val ConfBaseFolderSysProp = SysProp("aquarium.conf.base.folder") |
104 |
|
105 |
private[this] final val BasicResourceContext = new CompositeStreamResourceContext( |
106 |
NoVal, |
107 |
SlashEtcResourceContext, |
108 |
LocalConfigResourceContext, |
109 |
AppBaseResourceContext, |
110 |
ClasspathBaseResourceContext) |
111 |
|
112 |
/** |
113 |
* The resource context used in the application. |
114 |
*/ |
115 |
private[this] final val MasterResourceContext = { |
116 |
ConfBaseFolderSysProp.value match { |
117 |
case Just(value) ⇒ |
118 |
// We have a system override for the configuration location |
119 |
new CompositeStreamResourceContext(Just(BasicResourceContext), new FileStreamResourceContext(value)) |
120 |
case NoVal ⇒ |
121 |
BasicResourceContext |
122 |
case Failed(e, m) ⇒ |
123 |
throw new RuntimeException(m , e) |
124 |
} |
125 |
} |
126 |
|
127 |
def getResource(what: String): Maybe[StreamResource] = |
128 |
MasterResourceContext.getResource(what) |
129 |
} |