001/** 002 * Copyright (C) 2006-2025 Talend Inc. - www.talend.com 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.talend.sdk.components.vault.jcache; 017 018import java.io.IOException; 019import java.io.StringReader; 020import java.net.URI; 021import java.util.Optional; 022import java.util.Properties; 023 024import javax.cache.Cache; 025import javax.cache.CacheManager; 026import javax.cache.Caching; 027import javax.cache.spi.CachingProvider; 028import javax.enterprise.context.ApplicationScoped; 029import javax.enterprise.inject.Disposes; 030import javax.enterprise.inject.Produces; 031import javax.inject.Inject; 032 033import org.eclipse.microprofile.config.inject.ConfigProperty; 034import org.talend.sdk.components.vault.client.DecryptedValue; 035import org.talend.sdk.components.vault.configuration.Documentation; 036 037@ApplicationScoped 038public class JCacheSetup { 039 040 @Inject 041 @Documentation("Configuration for JCache setup, default implementation is Geronimo Simple Cache.") 042 @ConfigProperty(name = "talend.vault.cache.jcache.manager.uri", 043 defaultValue = "geronimo://simple-jcache.properties") 044 private String configurationUri; 045 046 @Inject 047 @Documentation("JCache `CacheManager` properties used to initialized the instance.") 048 @ConfigProperty(name = "talend.vault.cache.jcache.manager.properties", defaultValue = "") 049 private String configurationProperties; 050 051 @Inject 052 private CacheConfigurationFactory cacheConfiguration; 053 054 @Produces 055 @ApplicationScoped 056 public CachingProvider cachingProvider() { 057 return Caching.getCachingProvider(Thread.currentThread().getContextClassLoader()); 058 } 059 060 public void releaseCachingProvider(@Disposes final CachingProvider provider) { 061 provider.close(); // will close manager as well 062 } 063 064 @Produces 065 @ApplicationScoped 066 public CacheManager cacheManager(final CachingProvider provider) { 067 return provider 068 .getCacheManager(URI.create(configurationUri), Thread.currentThread().getContextClassLoader(), 069 Optional.of(configurationProperties).filter(it -> !it.isEmpty()).map(it -> { 070 final Properties properties = new Properties(); 071 try (final StringReader reader = new StringReader(it)) { 072 properties.load(reader); 073 } catch (final IOException e) { 074 throw new IllegalArgumentException(e); 075 } 076 return properties; 077 }).orElseGet(provider::getDefaultProperties)); 078 } 079 080 @Produces 081 @ApplicationScoped 082 public Cache<String, DecryptedValue> cache(final CacheManager manager) { 083 final CacheSizeManager<String, DecryptedValue> listener = new CacheSizeManager<>(cacheConfiguration.maxSize()); 084 final Cache<String, DecryptedValue> cache = manager 085 .createCache("org.talend.sdk.component.runtime.server.vault.DECRYPTED_VALUES", 086 cacheConfiguration.createConfiguration(listener)); 087 listener.accept(cache); 088 return cache; 089 } 090 091 public void releaseCache(@Disposes final Cache<String, DecryptedValue> cache) { 092 cache.close(); 093 } 094}