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.env.Env
39 import com.ckkloverdos.key.{IntKey, StringKey, LongKey, TypedKeySkeleton, TypedKey, BooleanKey}
40 import com.ckkloverdos.props.Props
41 import gr.grnet.aquarium.store.{PolicyStore, UserStateStore, IMEventStore, ResourceEventStore, StoreProvider}
43 import gr.grnet.aquarium.util.{Loggable, Lifecycle}
44 import gr.grnet.aquarium.service.{StoreWatcherService, RabbitMQService, TimerService, EventBusService, AkkaService}
45 import com.ckkloverdos.convert.Converters
46 import java.util.concurrent.atomic.AtomicBoolean
47 import org.slf4j.{LoggerFactory, Logger}
48 import gr.grnet.aquarium.computation.UserStateComputations
49 import com.ckkloverdos.maybe._
50 import gr.grnet.aquarium.ResourceLocator._
51 import com.ckkloverdos.sys.SysProp
52 import gr.grnet.aquarium.service.event.AquariumCreatedEvent
53 import gr.grnet.aquarium.policy.{PolicyDefinedFullPriceTableRef, StdUserAgreement, UserAgreementModel, ResourceType}
57 * @author Christos KK Loverdos <loverdos@gmail.com>
60 final class Aquarium(env: Env) extends Lifecycle with Loggable {
61 import Aquarium.EnvKeys
63 private[this] val _isStopping = new AtomicBoolean(false)
65 override def toString = "%s/v%s".format(getClass.getName, version)
67 def isStopping() = _isStopping.get()
70 def getClientLogger(client: AnyRef): Logger = {
76 LoggerFactory.getLogger(client.getClass)
80 def debug(client: AnyRef, fmt: String, args: Any*) = {
81 getClientLogger(client).debug(fmt.format(args: _*))
84 def info(client: AnyRef, fmt: String, args: Any*) = {
85 getClientLogger(client).info(fmt.format(args: _*))
88 def warn(client: AnyRef, fmt: String, args: Any*) = {
89 getClientLogger(client).warn(fmt.format(args: _*))
92 @throws(classOf[AquariumInternalError])
93 def apply[T: Manifest](key: TypedKey[T]): T = {
98 throw new AquariumInternalError("Could not locate %s in Aquarium environment".format(key))
102 private[this] lazy val _allServices = Aquarium.ServiceKeys.map(this(_))
104 private[this] def startServices(): Unit = {
105 for(service ← _allServices) {
106 logStartingF(service.toString) {
112 private[this] def stopServices(): Unit = {
113 val services = _allServices.reverse
115 for(service ← services) {
116 logStoppingF(service.toString) {
117 safeUnit(service.stop())
122 private[this] def showBasicConfiguration(): Unit = {
123 logger.info("Aquarium Home = %s".format(
124 if(Homes.Folders.AquariumHome.isAbsolute)
125 Homes.Folders.AquariumHome
127 "%s [=%s]".format(Homes.Folders.AquariumHome, Homes.Folders.AquariumHome.getCanonicalPath)
130 for(folder ← this.eventsStoreFolder) {
131 logger.info("{} = {}", EnvKeys.eventsStoreFolder.name, folder)
133 this.eventsStoreFolder.throwMe // on error
135 for(prop ← Aquarium.PropsToShow) {
136 logger.info("{} = {}", prop.name, prop.rawValue)
139 logger.info("CONF_HERE = {}", HERE)
140 logger.info("{} = {}", ResourceLocator.ResourceNames.AQUARIUM_PROPERTIES, ResourceLocator.Resources.AquariumPropertiesResource)
141 logger.info("{} = {}", ResourceLocator.ResourceNames.LOGBACK_XML, ResourceLocator.Resources.LogbackXMLResource)
142 logger.info("{} = {}", ResourceLocator.ResourceNames.POLICY_YAML, ResourceLocator.Resources.PolicyYAMLResource)
144 logger.info("Runtime.getRuntime.availableProcessors() => {}", Runtime.getRuntime.availableProcessors())
147 private[this] def addShutdownHooks(): Unit = {
148 Runtime.getRuntime.addShutdownHook(new Thread(new Runnable {
150 if(!_isStopping.get()) {
151 logStoppingF("Aquarium") {
159 def start(): Unit = {
160 this._isStopping.set(false)
161 showBasicConfiguration()
167 this._isStopping.set(true)
172 * Stops Aquarium after the given millis. Used during testing.
174 def stopAfterMillis(millis: Long) {
180 * Reflectively provide a new instance of a class and configure it appropriately.
182 def newInstance[C <: AnyRef](_class: Class[C], className: String): C = {
183 val originalProps = apply(EnvKeys.originalProps)
185 val instanceM = MaybeEither(defaultClassLoader.loadClass(className).newInstance().asInstanceOf[C])
187 case Just(instance) ⇒
188 // eventBus.addSubscriber[C](instance)
190 case aquariumAware: AquariumAware ⇒
191 aquariumAware.awareOfAquarium(AquariumCreatedEvent(this))
197 case configurable: Configurable if (originalProps ne null) ⇒
198 val localProps = configurable.propertyPrefix match {
199 case somePrefix @ Some(prefix) ⇒
200 if(prefix.length == 0) {
202 "Property prefix for %s is %s. Consider using None".format(instance, somePrefix))
205 originalProps.subsetForKeyPrefix(prefix)
211 logger.debug("Configuring {} with props (prefix={})", configurable.getClass.getName, configurable.propertyPrefix)
212 MaybeEither(configurable configure localProps) match {
214 logger.info("Configured {} with props (prefix={})", configurable.getClass.getName, configurable.propertyPrefix)
217 throw new AquariumInternalError("Could not configure instance of %s".format(className), e)
226 throw new AquariumInternalError("Could not instantiate %s".format(className), e)
231 def currentResourceTypesMap: Map[String, ResourceType] = {
236 def initialUserAgreementForRole(role: String, referenceTimeMillis: Long): UserAgreementModel = {
237 // FIXME: Where is the mapping?
238 StdUserAgreement("", None, Timespan(0L), defaultInitialUserRole, PolicyDefinedFullPriceTableRef)
241 def initialBalanceForRole(role: String, referenceTimeMillis: Long): Double = {
242 // FIXME: Where is the mapping?
246 def defaultInitialUserRole: String = {
247 // FIXME: Read from properties?
251 def defaultClassLoader = apply(EnvKeys.defaultClassLoader)
253 def resourceEventStore = apply(EnvKeys.resourceEventStore)
255 def imEventStore = apply(EnvKeys.imEventStore)
257 def userStateStore = apply(EnvKeys.userStateStore)
259 def policyStore = apply(EnvKeys.policyStore)
261 def eventsStoreFolder = apply(EnvKeys.eventsStoreFolder)
263 def eventBus = apply(EnvKeys.eventBus)
265 def userStateComputations = apply(EnvKeys.userStateComputations)
267 def userStateTimestampThreshold = apply(EnvKeys.userStateTimestampThreshold)
269 def adminCookie = apply(EnvKeys.adminCookie)
271 def converters = apply(EnvKeys.converters)
273 def saveResourceEventsToEventsStoreFolder = apply(EnvKeys.eventsStoreSaveRCEvents)
275 def saveIMEventsToEventsStoreFolder = apply(EnvKeys.eventsStoreSaveIMEvents)
277 def timerService = apply(EnvKeys.timerService)
279 def restPort = apply(EnvKeys.restPort)
281 def akkaService = apply(EnvKeys.akkaService)
283 def version = apply(EnvKeys.version)
287 final val PropsToShow = List(
291 SysProp.JavaClassVersion,
292 SysProp.JavaLibraryPath,
293 SysProp.JavaClassPath,
294 SysProp.JavaIOTmpDir,
302 final val RESTAdminHeaderName = "X-Aquarium-Admin-Cookie"
303 final val RESTAdminHeaderNameLowerCase = RESTAdminHeaderName.toLowerCase
306 final class AquariumEnvKey[T: Manifest](override val name: String) extends TypedKeySkeleton[T](name) {
307 override def toString = name
310 final val ServiceKeys: List[TypedKey[_ <: Lifecycle]] = List(
311 EnvKeys.timerService,
315 EnvKeys.rabbitMQService,
316 EnvKeys.storeWatcherService
321 * The Aquarium version. Will be reported in any due occasion.
323 final val version = StringKey("version")
325 final val originalProps: TypedKey[Props] =
326 new AquariumEnvKey[Props]("originalProps")
329 * The fully qualified name of the class that implements the `StoreProvider`.
330 * Will be instantiated reflectively and should have a public default constructor.
332 final val storeProvider: TypedKey[StoreProvider] =
333 new AquariumEnvKey[StoreProvider]("store.provider.class")
336 * If a value is given to this property, then it represents a folder where all events coming to aquarium are
339 * This is for debugging purposes.
341 final val eventsStoreFolder: TypedKey[Option[File]] =
342 new AquariumEnvKey[Option[File]]("events.store.folder")
345 * If this is `true` and `events.store.folder` is defined, then all resource events are
346 * also stored in `events.store.folder`.
348 * This is for debugging purposes.
351 final val eventsStoreSaveRCEvents = BooleanKey("events.store.save.rc.events")
354 * If this is `true` and `events.store.folder` is defined, then all IM events are
355 * also stored in `events.store.folder`.
357 * This is for debugging purposes.
359 final val eventsStoreSaveIMEvents = BooleanKey("events.store.save.im.events")
362 * A time period in milliseconds for which we can tolerate stale parts regarding user state.
364 * The smaller the value, the more accurate the user credits and other state parts are.
366 * If a request for user state (e.g. balance) is received and the request timestamp exceeds
367 * the timestamp of the last known balance amount by this value, then a re-computation for
368 * the balance is triggered.
370 final val userStateTimestampThreshold = LongKey("user.state.timestamp.threshold")
373 * REST service listening port.
375 final val restPort = IntKey("rest.port")
377 final val restShutdownTimeoutMillis = LongKey("rest.shutdown.timeout.millis")
380 * A cookie used in every administrative REST API call, so that Aquarium knows it comes from
381 * an authorised client.
383 final val adminCookie: TypedKey[Option[String]] =
384 new AquariumEnvKey[Option[String]]("admin.cookie")
386 final val resourceEventStore: TypedKey[ResourceEventStore] =
387 new AquariumEnvKey[ResourceEventStore]("resource.event.store.class")
389 final val imEventStore: TypedKey[IMEventStore] =
390 new AquariumEnvKey[IMEventStore]("im.event.store.class")
392 final val userStateStore: TypedKey[UserStateStore] =
393 new AquariumEnvKey[UserStateStore]("user.state.store.class")
395 final val policyStore: TypedKey[PolicyStore] =
396 new AquariumEnvKey[PolicyStore]("policy.store.class")
399 * The class that initializes the REST service
401 final val restService: TypedKey[Lifecycle] =
402 new AquariumEnvKey[Lifecycle]("rest.service.class")
404 final val akkaService: TypedKey[AkkaService] =
405 new AquariumEnvKey[AkkaService]("akka.service")
407 final val eventBus: TypedKey[EventBusService] =
408 new AquariumEnvKey[EventBusService]("event.bus.service")
410 final val timerService: TypedKey[TimerService] =
411 new AquariumEnvKey[TimerService]("timer.service")
413 final val rabbitMQService: TypedKey[RabbitMQService] =
414 new AquariumEnvKey[RabbitMQService]("rabbitmq.service")
416 final val storeWatcherService: TypedKey[StoreWatcherService] =
417 new AquariumEnvKey[StoreWatcherService]("store.watcher.service")
419 final val converters: TypedKey[Converters] =
420 new AquariumEnvKey[Converters]("converters")
422 final val userStateComputations: TypedKey[UserStateComputations] =
423 new AquariumEnvKey[UserStateComputations]("user.state.computations")
425 final val defaultClassLoader: TypedKey[ClassLoader] =
426 new AquariumEnvKey[ClassLoader]("default.class.loader")