2 * Copyright © 2019 IBM.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.ccsdk.cds.controllerblueprints.core.scripts
19 import com.google.common.cache.CacheBuilder
20 import com.google.common.cache.CacheLoader
21 import com.google.common.cache.LoadingCache
22 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException
23 import org.onap.ccsdk.cds.controllerblueprints.core.logger
24 import org.onap.ccsdk.cds.controllerblueprints.core.normalizedFile
26 import java.net.URLClassLoader
29 object BluePrintCompileCache {
30 val log = logger(BluePrintCompileCache::class)
32 private val classLoaderCache: LoadingCache<String, URLClassLoader> = CacheBuilder.newBuilder()
34 .build(BluePrintClassLoader)
36 fun classLoader(key: String): URLClassLoader {
37 return classLoaderCache.get(key)
40 fun cleanClassLoader(key: String) {
41 classLoaderCache.invalidate(key)
42 log.info("Cleaned script cache($key)")
45 fun hasClassLoader(key: String): Boolean {
46 return classLoaderCache.asMap().containsKey(key)
50 object BluePrintClassLoader : CacheLoader<String, URLClassLoader>() {
52 val log = logger(BluePrintClassLoader::class)
54 override fun load(key: String): URLClassLoader {
55 log.info("loading cache key($key)")
56 val keyPath = normalizedFile(key)
57 if (!keyPath.exists()) {
58 throw BluePrintException("failed to load cache($key), missing files.")
60 val urls = arrayListOf<URL>()
62 .filter { it.name.endsWith("cba-kts.jar") }
64 log.debug("Adding (${it.absolutePath}) to cache key($key)")
65 urls.add(it.toURI().toURL())
67 return URLClassLoader(urls.toTypedArray(), this.javaClass.classLoader)