Created
February 9, 2012 09:21
-
-
Save patriknw/1778698 to your computer and use it in GitHub Desktop.
Illustrates a POJO API on top of Config
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package akka.config.bean | |
import java.util.{ List ⇒ JList } | |
import java.util.{ ArrayList ⇒ JArrayList } | |
import java.util.{ HashMap ⇒ JHashMap } | |
import scala.reflect.BeanProperty | |
import com.typesafe.config.Config | |
import com.typesafe.config.ConfigFactory | |
class AkkaConfigBean { | |
@BeanProperty | |
var loglevel: String = _ | |
@BeanProperty | |
var eventHandlers: JList[String] = new JArrayList | |
@BeanProperty | |
var daemonic: java.lang.Boolean = _ | |
@BeanProperty | |
var dispatchers: JList[DispatcherConfigBean] = new JArrayList | |
@BeanProperty | |
var remote: RemoteConfigBean = _ | |
def toConfig(): Config = { | |
import scala.collection.JavaConverters._ | |
val m = new JHashMap[String, Any] | |
if (loglevel ne null) m.put("akka.loglevel", loglevel) | |
if (!eventHandlers.isEmpty) m.put("akka.event-handlers", eventHandlers) | |
if (daemonic ne null) m.put("akka.daemonic", daemonic) | |
for (d ← dispatchers.asScala) d.toConfig(m) | |
if (remote ne null) remote.toConfig(m) | |
ConfigFactory.parseMap(m) | |
} | |
override def toString(): String = toConfig.root.render | |
} | |
class DispatcherConfigBean(dispatcherId: String) { | |
def this() { | |
this("akka.actor.default-dispatcher") | |
} | |
@BeanProperty | |
var dispatcherType: String = _ | |
@BeanProperty | |
var executor: ExecutorConfigBean = _ | |
def toConfig(m: JHashMap[String, Any]): Unit = { | |
if (dispatcherType ne null) m.put(dispatcherId + ".type", dispatcherType) | |
if (executor ne null) executor.toConfig(dispatcherId, m) | |
} | |
} | |
trait ExecutorConfigBean { | |
def toConfig(dispatcherId: String, m: JHashMap[String, Any]): Unit | |
} | |
class ForkJoinExecutorConfigBean extends ExecutorConfigBean { | |
@BeanProperty | |
var parallelismMin: java.lang.Integer = _ | |
@BeanProperty | |
var parallelismFactor: java.lang.Double = _ | |
@BeanProperty | |
var parallelismMax: java.lang.Integer = _ | |
def toConfig(dispatcherId: String, m: JHashMap[String, Any]): Unit = { | |
m.put(dispatcherId + ".executor", "fork-join-executor") | |
if (parallelismMin ne null) m.put(dispatcherId + ".fork-join-executor.parallelism-min", parallelismMin) | |
if (parallelismFactor ne null) m.put(dispatcherId + ".fork-join-executor.parallelism-factor", parallelismFactor) | |
if (parallelismMax ne null) m.put(dispatcherId + ".fork-join-executor.parallelism-max", parallelismMax) | |
} | |
} | |
class ThreadPoolExecutorConfigBean extends ExecutorConfigBean { | |
@BeanProperty | |
var corePoolSizeMin: java.lang.Integer = _ | |
@BeanProperty | |
var corePoolSizeFactor: java.lang.Double = _ | |
@BeanProperty | |
var corePoolSizeMax: java.lang.Integer = _ | |
@BeanProperty | |
var keepAliveTime: String = _ | |
def toConfig(dispatcherId: String, m: JHashMap[String, Any]): Unit = { | |
m.put(dispatcherId + ".executor", "thread-pool-executor") | |
if (corePoolSizeMin ne null) m.put(dispatcherId + ".thread-pool-executor.parallelism-min", corePoolSizeMin) | |
if (corePoolSizeFactor ne null) m.put(dispatcherId + ".thread-pool-executor.parallelism-factor", corePoolSizeFactor) | |
if (corePoolSizeMax ne null) m.put(dispatcherId + ".thread-pool-executor.parallelism-max", corePoolSizeMax) | |
if (keepAliveTime ne null) m.put(dispatcherId + ".thread-pool-executor.keep-alive-time", keepAliveTime) | |
} | |
} | |
class RemoteConfigBean { | |
@BeanProperty | |
var hostname: String = _ | |
@BeanProperty | |
var port: java.lang.Integer = _ | |
def toConfig(m: JHashMap[String, Any]): Unit = { | |
if (hostname ne null) m.put("akka.remote.netty.hostname", hostname) | |
if (port ne null) m.put("akka.remote.netty.port", port) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment