0

I use typesafe config library to manage my Scala project's configuration. I use the following pattern to organize and load my configurations in a type-safe manner

object AppConfig {
  private val config = ConfigFactory.load()
  lazy val host: String = config.getString("host")
  object Splunk {
    private lazy val splunkConf = config.getConfig("splunk")
    lazy val index = splunkConf.getString("index")
    lazy val token = splunkConf.getString("token")
  }
}

I need to inject some of those configs in my logback.groovy. This works perfectly when accessing direct properties of AppConfig : example AppConfig.host(), but not for nested objects: like AppConfig.Splunk.token() logback.groovy

appender("splunk", HttpEventCollectorLogbackAppender) {
    url = "http://someSplunkUrl:8088/services/collector"
    host = AppConfig.host()
    token = AppConfig.Splunk.token() 
    index = AppConfig.Splunk.index()
    batch_size_count = 1
    layout(PatternLayout) {
        pattern = "%msg"
    }
}
groovy.lang.MissingPropertyException: No such property: Splunk for class: AppConfig
    at groovy.lang.MetaClassImpl.invokeStaticMissingProperty(MetaClassImpl.java:1028)
    at groovy.lang.MetaClassImpl.getProperty(MetaClassImpl.java:1932)
    at groovy.lang.MetaClassImpl.getProperty(MetaClassImpl.java:1908)
    at groovy.lang.MetaClassImpl.getProperty(MetaClassImpl.java:3886)
    at org.codehaus.groovy.runtime.callsite.ClassMetaClassGetPropertySite.getProperty(ClassMetaClassGetPropertySite.java:50)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGetProperty(AbstractCallSite.java:298)
    at Script1$_run_closure2.doCall(Script1.groovy:18)
    at Script1$_run_closure2.doCall(Script1.groovy)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
Rafik Saad
  • 596
  • 1
  • 4
  • 9
  • Use typesafe config wrapper - https://github.com/pureconfig/pureconfig . Define config as case classes, load it and then access properties of these case classes. – Mateusz Kubuszok Sep 14 '19 at 09:26
  • [Can you try](https://stackoverflow.com/questions/35709154/access-inner-class-method-with-fine-syntax-in-java-using-scala-class-object) `AppConfig.Splunk$.MODULE$.token()` – tim_yates Sep 14 '19 at 15:34

1 Answers1

0

I ended up referencing the nested objected as below :

def splunkConf = Class.forName("com.util.AppConfig\$Splunk\$")
                      .getDeclaredConstructor().newInstance()

I could then access the fields successfully :

token = splunkConf."token"()
index = splunkConf."index"()
Rafik Saad
  • 596
  • 1
  • 4
  • 9