2 * Copyright 2011 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 actor.{ActorProvider}
39 import com.ckkloverdos.resource._
40 import com.ckkloverdos.sys.SysProp
41 import com.ckkloverdos.props.Props
42 import com.ckkloverdos.maybe.{Maybe, Failed, Just, NoVal}
43 import com.ckkloverdos.convert.Converters.{DefaultConverters => TheDefaultConverters}
44 import processor.actor.{UserEventProcessorService, ResourceEventProcessorService, EventProcessorService}
46 import util.{Lifecycle, Loggable}
49 * The master configurator. Responsible to load all of application configuration and provide the relevant services.
51 * @author Christos KK Loverdos <loverdos@gmail.com>.
53 class Configurator(val props: Props) extends Loggable {
54 import Configurator.Keys
57 * Reflectively provide a new instance of a class and configure it appropriately.
59 private[this] def newInstance[C : Manifest](className: String): C = {
60 val instanceM = Maybe(defaultClassLoader.loadClass(className).newInstance().asInstanceOf[C])
62 case Just(instance) ⇒ instance match {
63 case configurable: Configurable ⇒
64 Maybe(configurable configure props) match {
68 throw new Exception("Could not configure instance of %s".format(className), e)
70 throw new Exception("Could not configure instance of %s".format(className))
76 throw new Exception("Could not instantiate %s".format(className), e)
78 throw new Exception("Could not instantiate %s".format(className))
83 private[this] lazy val _actorProvider: ActorProvider = {
84 val instance = newInstance[ActorProvider](props.getEx(Keys.actor_provider_class))
85 logger.info("Loaded ActorProvider: %s".format(instance.getClass))
89 private[this] lazy val _storeProvider: StoreProvider = {
90 val instance = newInstance[StoreProvider](props.getEx(Keys.store_provider_class))
91 logger.info("Loaded StoreProvider: %s".format(instance.getClass))
95 private[this] lazy val _restService: Lifecycle = {
96 val instance = newInstance[Lifecycle](props.getEx(Keys.rest_service_class))
97 logger.info("Loaded RESTService: %s".format(instance.getClass))
101 private[this] lazy val _userStateStoreM: Maybe[UserStateStore] = {
102 // If there is a specific `UserStateStore` implementation specified in the
103 // properties, then this implementation overrides the user store given by
105 props.get(Keys.user_state_store_class) map { className ⇒
106 val instance = newInstance[UserStateStore](className)
107 logger.info("Overriding UserStateStore provisioning. Implementation given by: %s".format(instance.getClass))
112 private[this] lazy val _resourceEventStoreM: Maybe[ResourceEventStore] = {
113 // If there is a specific `EventStore` implementation specified in the
114 // properties, then this implementation overrides the event store given by
116 props.get(Keys.resource_event_store_class) map { className ⇒
117 val instance = newInstance[ResourceEventStore](className)
118 logger.info("Overriding EventStore provisioning. Implementation given by: %s".format(instance.getClass))
123 private[this] lazy val _userEventStoreM: Maybe[UserEventStore] = {
124 props.get(Keys.user_event_store_class) map { className ⇒
125 val instance = newInstance[UserEventStore](className)
126 logger.info("Overriding UserEventStore provisioning. Implementation given by: %s".format(instance.getClass))
131 private[this] lazy val _WalletEventStoreM: Maybe[WalletEntryStore] = {
132 // If there is a specific `IMStore` implementation specified in the
133 // properties, then this implementation overrides the event store given by
135 props.get(Keys.wallet_entry_store_class) map { className ⇒
136 val instance = newInstance[WalletEntryStore](className)
137 logger.info("Overriding WalletEntryStore provisioning. Implementation given by: %s".format(instance.getClass))
142 private[this] lazy val _resEventProc: ResourceEventProcessorService = new ResourceEventProcessorService
144 private[this] lazy val _imEventProc: EventProcessorService = new UserEventProcessorService
146 def get(key: String, default: String = ""): String = props.getOr(key, default)
148 def defaultClassLoader = Thread.currentThread().getContextClassLoader
150 def startServices(): Unit = {
152 _actorProvider.start()
153 _resEventProc.start()
157 def stopServices(): Unit = {
161 _actorProvider.stop()
163 // akka.actor.Actor.registry.shutdownAll()
166 def stopServicesWithDelay(millis: Long) {
171 def actorProvider = _actorProvider
173 def userStateStore = {
174 _userStateStoreM match {
176 case _ ⇒ storeProvider.userStateStore
180 def resourceEventStore = {
181 _resourceEventStoreM match {
183 case _ ⇒ storeProvider.resourceEventStore
188 _WalletEventStoreM match {
190 case _ ⇒ storeProvider.walletEntryStore
194 def userEventStore = {
195 _userEventStoreM match {
197 case _ ⇒ storeProvider.userEventStore
201 def storeProvider = _storeProvider
204 object Configurator {
205 implicit val DefaultConverters = TheDefaultConverters
207 val MasterConfName = "aquarium.properties"
210 * Current directory resource context.
211 * Normally this should be the application installation directory.
213 * It takes priority over `ClasspathBaseResourceContext`.
215 val AppBaseResourceContext = new FileStreamResourceContext(".")
218 * The venerable /etc resource context
220 val SlashEtcResourceContext = new FileStreamResourceContext("/etc")
223 * Class loader resource context.
224 * This has the lowest priority.
226 val ClasspathBaseResourceContext = new ClassLoaderStreamResourceContext(Thread.currentThread().getContextClassLoader)
229 * Use this property to override the place where aquarium configuration resides.
231 * The value of this property is a folder that defines the highest-priority resource context.
233 val ConfBaseFolderSysProp = SysProp("aquarium.conf.base.folder")
236 * The resource context used in the application.
238 lazy val MasterResourceContext = {
239 val rc0 = ClasspathBaseResourceContext
240 val rc1 = AppBaseResourceContext
241 val rc2 = SlashEtcResourceContext
242 val basicContext = new CompositeStreamResourceContext(NoVal, rc2, rc1, rc0)
244 ConfBaseFolderSysProp.value match {
246 // We have a system override for the configuration location
247 new CompositeStreamResourceContext(Just(basicContext), new FileStreamResourceContext(value))
251 throw new RuntimeException(m , e)
255 lazy val MasterConfResource = {
256 val maybeMCResource = MasterResourceContext getResource MasterConfName
257 maybeMCResource match {
258 case Just(masterConfResource) ⇒
261 throw new RuntimeException("Could not find master configuration file: %s".format(MasterConfName))
263 throw new RuntimeException(m, e)
267 lazy val MasterConfProps = {
268 val maybeProps = Props apply MasterConfResource
273 throw new RuntimeException("Could not load master configuration file: %s".format(MasterConfName))
275 throw new RuntimeException(m, e)
279 lazy val MasterConfigurator = {
280 Maybe(new Configurator(MasterConfProps)) match {
281 case Just(masterConf) ⇒
284 throw new RuntimeException("Could not initialize master configuration file: %s".format(MasterConfName))
286 throw new RuntimeException(m, e)
291 * Defines the names of all the known keys inside the master properties file.
295 * The Aquarium version. Will be reported in any due occasion.
297 final val version = "version"
300 * The fully qualified name of the class that implements the `ActorProvider`.
301 * Will be instantiated reflectively and should have a public default constructor.
303 final val actor_provider_class = "actor.provider.class"
306 * The class that initializes the REST service
308 final val rest_service_class = "rest.service.class"
311 * The fully qualified name of the class that implements the `StoreProvider`.
312 * Will be instantiated reflectively and should have a public default constructor.
314 final val store_provider_class = "store.provider.class"
317 * The class that implements the User store
319 final val user_state_store_class = "user.state.store.class"
322 * The class that implements the resource event store
324 final val resource_event_store_class = "resource.event.store.class"
327 * The class that implements the IM event store
329 final val user_event_store_class = "user.event.store.class"
332 * The class that implements the wallet entries store
334 final val wallet_entry_store_class = "wallet.entry.store.class"
336 /** The lower mark for the UserActors' LRU, managed by UserActorManager.
338 * The terminology is borrowed from the (also borrowed) Apache-lucene-solr-based implementation.
341 final val user_actors_lru_lower_mark = "user.actors.LRU.lower.mark"
344 * The upper mark for the UserActors' LRU, managed by UserActorManager.
346 * The terminology is borrowed from the (also borrowed) Apache-lucene-solr-based implementation.
348 final val user_actors_lru_upper_mark = "user.actors.LRU.upper.mark"
351 * Comma separated list of amqp servers running in active-active
354 final val amqp_servers = "amqp.servers"
357 * Comma separated list of amqp servers running in active-active
360 final val amqp_port = "amqp.port"
363 * User name for connecting with the AMQP server
365 final val amqp_username = "amqp.username"
368 * Passwd for connecting with the AMQP server
370 final val amqp_password = "amqp.passwd"
373 * Virtual host on the AMQP server
375 final val amqp_vhost = "amqp.vhost"
378 * Comma separated list of exchanges known to aquarium
380 final val amqp_exchanges = "amqp.exchanges"
383 * REST service listening port.
387 final val rest_port = "rest.port"
390 * Provider for persistence services
392 final val persistence_provider = "persistence.provider"
395 * Hostname for the persistence service
397 final val persistence_host = "persistence.host"
400 * Username for connecting to the persistence service
402 final val persistence_username = "persistence.username"
405 * Password for connecting to the persistence service
407 final val persistence_password = "persistence.password"
410 * Password for connecting to the persistence service
412 final val persistence_port = "persistence.port"
415 * The DB schema to use
417 final val persistence_db = "persistence.db"
420 * A time period in milliseconds for which we can tolerate stale data regarding user state.
422 * The smaller the value, the more accurate the user credits and other state data are.
424 * If a request for user state (e.g. balance) is received and the request timestamp exceeds
425 * the timestamp of the last known balance amount by this value, then a re-computation for
426 * the balance is triggered.
428 final val user_state_timestamp_threshold = "user.state.timestamp.threshold"