2 * Copyright © 2017-2018 AT&T Intellectual Property.
3 * Modifications Copyright © 2018 IBM.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 package org.onap.ccsdk.apps.controllerblueprints.scripts
20 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants
21 import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintScriptsService
22 import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintContext
23 import org.springframework.stereotype.Service
26 import kotlin.script.experimental.api.ResultValue
27 import kotlin.script.experimental.api.resultOrNull
28 import kotlin.script.experimental.jvmhost.createJvmCompilationConfigurationFromTemplate
31 open class BluePrintScriptsServiceImpl : BluePrintScriptsService {
33 override fun <T> scriptInstance(blueprintContext: BluePrintContext, scriptClassName: String,
34 reCompile: Boolean): T {
36 val kotlinScriptPath = blueprintContext.rootPath.plus(File.separator)
37 .plus(BluePrintConstants.TOSCA_SCRIPTS_KOTLIN_DIR)
39 val compiledJar = kotlinScriptPath.plus(File.separator)
40 .plus(getBluePrintScriptsJarName(blueprintContext))
42 val scriptSource = BluePrintSourceCode()
44 val sources: MutableList<String> = arrayListOf()
45 sources.add(kotlinScriptPath)
46 scriptSource.blueprintKotlinSources = sources
47 scriptSource.moduleName = "${blueprintContext.name()}-${blueprintContext.version()}-cba-kts"
48 scriptSource.targetJarFile = File(compiledJar)
49 scriptSource.regenerate = reCompile
51 val compilationConfiguration = createJvmCompilationConfigurationFromTemplate<BluePrintKotlinScript>()
52 val scriptEvaluator = BluePrintScriptEvaluator(scriptClassName)
54 val compiledResponse = BlueprintScriptingHost(scriptEvaluator).eval(scriptSource, compilationConfiguration,
57 val returnValue = compiledResponse.resultOrNull()?.returnValue as? ResultValue.Value
59 return returnValue?.value!! as T
62 override fun <T> scriptInstance(scriptClassName: String): T {
63 val args = ArrayList<Any?>()
64 return Thread.currentThread().contextClassLoader.loadClass(scriptClassName).constructors
65 .single().newInstance(*args.toArray()) as T
69 fun getBluePrintScriptsJarName(blueprintContext: BluePrintContext): String {
70 return "${blueprintContext.name()}-${blueprintContext.version()}-cba-kts.jar"