f90e27f4d3a8ca11315807660e34dd2e4a46b822
[ccsdk/cds.git] /
1 /*
2  *  Copyright © 2019 IBM.
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.onap.ccsdk.cds.controllerblueprints.core.scripts
18
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
25 import java.net.URL
26 import java.net.URLClassLoader
27
28
29 object BluePrintCompileCache {
30     val log = logger(BluePrintCompileCache::class)
31
32     private val classLoaderCache: LoadingCache<String, URLClassLoader> = CacheBuilder.newBuilder()
33             .maximumSize(50)
34             .build(BluePrintClassLoader)
35
36     fun classLoader(key: String): URLClassLoader {
37         return classLoaderCache.get(key)
38     }
39
40     fun cleanClassLoader(key: String) {
41         if(hasClassLoader(key)){
42             // Make sure to close all classloader loaded resources before we let go of it.
43             // This fixes a Delete failure message on filesystem that keeps locks on opened jars;
44             // like Windows and NFS.
45             classLoaderCache.get(key).close()
46             classLoaderCache.invalidate(key)
47             log.info("Cleaned compiled cache($key)")
48         }else{
49             log.warn("No compiled cache($key) present to clean.")
50         }
51     }
52
53     fun hasClassLoader(key: String): Boolean {
54         return classLoaderCache.asMap().containsKey(key)
55     }
56 }
57
58 object BluePrintClassLoader : CacheLoader<String, URLClassLoader>() {
59
60     val log = logger(BluePrintClassLoader::class)
61
62     override fun load(key: String): URLClassLoader {
63         log.info("loading compiled cache($key)")
64         val keyPath = normalizedFile(key)
65         if (!keyPath.exists()) {
66             throw BluePrintException("failed to load cache($key), missing files.")
67         }
68         val urls = arrayListOf<URL>()
69         keyPath.walkTopDown()
70                 .filter { it.name.endsWith("cba-kts.jar") }
71                 .forEach {
72                     log.debug("Adding (${it.absolutePath}) to cache($key)")
73                     urls.add(it.toURI().toURL())
74                 }
75         return URLClassLoader(urls.toTypedArray(), this.javaClass.classLoader)
76     }
77 }