Enabling Code Formatter
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / commons / processor-core / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / core / BluePrintCoreConfiguration.kt
index 1cb66b8..2a4d05d 100644 (file)
@@ -1,5 +1,6 @@
 /*
  * Copyright © 2017-2018 AT&T Intellectual Property.
+ * Modifications Copyright © 2019 IBM.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 
 package org.onap.ccsdk.cds.blueprintsprocessor.core
 
-import org.onap.ccsdk.cds.controllerblueprints.core.config.BluePrintPathConfiguration
+import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
+import org.onap.ccsdk.cds.controllerblueprints.core.config.BluePrintLoadConfiguration
+import org.onap.ccsdk.cds.controllerblueprints.core.logger
 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintDependencyService
 import org.slf4j.LoggerFactory
 import org.springframework.beans.factory.annotation.Autowired
 import org.springframework.boot.context.properties.bind.Bindable
 import org.springframework.boot.context.properties.bind.Binder
-import org.springframework.boot.context.properties.source.ConfigurationPropertySources
 import org.springframework.context.ApplicationContext
 import org.springframework.context.ApplicationContextAware
 import org.springframework.context.annotation.Bean
@@ -31,36 +33,45 @@ import org.springframework.core.env.Environment
 import org.springframework.stereotype.Service
 
 @Configuration
-open class BluePrintCoreConfiguration(private val bluePrintProperties: BlueprintProcessorProperties) {
+open class BluePrintCoreConfiguration(private val bluePrintPropertiesService: BluePrintPropertiesService) {
 
     companion object {
+
         const val PREFIX_BLUEPRINT_PROCESSOR = "blueprintsprocessor"
     }
 
     @Bean
-    open fun bluePrintPathConfiguration(): BluePrintPathConfiguration {
-        return bluePrintProperties
-                .propertyBeanType(PREFIX_BLUEPRINT_PROCESSOR, BluePrintPathConfiguration::class.java)
+    open fun bluePrintLoadConfiguration(): BluePrintLoadConfiguration {
+        return bluePrintPropertiesService
+            .propertyBeanType(PREFIX_BLUEPRINT_PROCESSOR, BluePrintLoadConfiguration::class.java)
     }
-
 }
 
 @Configuration
-open class BlueprintPropertyConfiguration {
+open class BluePrintPropertyConfiguration {
+
     @Autowired
     lateinit var environment: Environment
 
     @Bean
     open fun bluePrintPropertyBinder(): Binder {
-        val configurationPropertySource = ConfigurationPropertySources.get(environment)
-        return Binder(configurationPropertySource)
+        return Binder.get(environment)
     }
 }
 
 @Service
-open class BlueprintProcessorProperties(private var bluePrintPropertyBinder: Binder) {
+open class BluePrintPropertiesService(private var bluePrintPropertyConfig: BluePrintPropertyConfiguration) {
+
+    private val log = logger(BluePrintPropertiesService::class)
+
     fun <T> propertyBeanType(prefix: String, type: Class<T>): T {
-        return bluePrintPropertyBinder.bind(prefix, Bindable.of(type)).get()
+        return try {
+            bluePrintPropertyConfig.bluePrintPropertyBinder().bind(prefix, Bindable.of(type)).get()
+        } catch (e: NoSuchElementException) {
+            val errMsg = "Error: missing property \"$prefix\"... Check the application.properties file."
+            log.error(errMsg)
+            throw BluePrintProcessorException(e, errMsg)
+        }
     }
 }
 
@@ -74,5 +85,4 @@ open class BlueprintDependencyConfiguration : ApplicationContextAware {
         BluePrintDependencyService.inject(applicationContext)
         log.info("Dependency Management module created...")
     }
-
-}
\ No newline at end of file
+}