| 1 | package org.jtoolkit.essence.data; |
| 2 | |
| 3 | import org.jetbrains.annotations.NotNull; |
| 4 | import org.jetbrains.annotations.Nullable; |
| 5 | import org.jtoolkit.essence.app.ComponentBuilder; |
| 6 | import org.jtoolkit.essence.app.Container; |
| 7 | import org.jtoolkit.essence.app.Runs; |
| 8 | import org.jtoolkit.essence.app.pojo.impl.DataValueClass; |
| 9 | import org.jtoolkit.essence.data.impl.CacheStore; |
| 10 | |
| 11 | import javax.cache.Cache; |
| 12 | import java.io.Closeable; |
| 13 | import java.io.IOException; |
| 14 | import java.util.LinkedHashMap; |
| 15 | import java.util.Map; |
| 16 | import java.util.concurrent.ConcurrentMap; |
| 17 | |
| 18 | public class CacheComponentBuilder implements ComponentBuilder, Runs, Closeable { |
| 19 | private final String name; |
| 20 | private final Container container; |
| 21 | private final Map<String, CacheStore> cacheDataMap = new LinkedHashMap<String, CacheStore>(); |
| 22 | |
| 23 | public CacheComponentBuilder(String name, Container container) throws InstantiationException { |
| 24 | this.name = name; |
| 25 | this.container = container; |
| 26 | Integer size = DataValueClass.cast(Integer.class, container.getProperties().getValue("caches.defaultMaxSize")); |
| 27 | int defaultCacheSize = size == null ? 1000 : size; |
| 28 | for (CacheData cd : container.removeConfigAs("Caches", CacheData.class)) |
| 29 | cacheDataMap.put(cd.name, new CacheStore(defaultCacheSize, cd)); |
| 30 | } |
| 31 | |
| 32 | // must define a constructor which takes a Container |
| 33 | // public ComponentBuilder(Container container) |
| 34 | @Nullable |
| 35 | public <T> T resolveComponent(@NotNull String componentName, @Nullable Class<T> type) throws IllegalStateException, IllegalArgumentException { |
| 36 | if (type == Store.class || type == null) |
| 37 | return (T) cacheDataMap.get(componentName); |
| 38 | if (type == Map.class || type == ConcurrentMap.class) |
| 39 | return (T) cacheDataMap.get(componentName).getMapView(); |
| 40 | if (type == Cache.class) |
| 41 | return (T) cacheDataMap.get(componentName).getCacheView(); |
| 42 | |
| 43 | throw new IllegalArgumentException(container.getName() + ": " + "Attempted to get component " + componentName + |
| 44 | " as " + type + " however CacheComponentBuilder only supports Store, Map, ConcurrentMap and Cache"); |
| 45 | } |
| 46 | |
| 47 | @NotNull public String getName() { |
| 48 | return name; |
| 49 | } |
| 50 | |
| 51 | public void start() throws IllegalStateException, IllegalArgumentException { |
| 52 | } |
| 53 | |
| 54 | public void stop() { |
| 55 | } |
| 56 | |
| 57 | public void close() throws IOException { |
| 58 | for (Store s : cacheDataMap.values()) |
| 59 | s.close(); |
| 60 | } |
| 61 | |
| 62 | public static class CacheData { |
| 63 | public final String name; |
| 64 | public final EvictionStrategy evictionStrategy; |
| 65 | public final Integer cacheSize; |
| 66 | |
| 67 | CacheData(@NotNull String name, @NotNull EvictionStrategy evictionStrategy, Integer cacheSize) { |
| 68 | this.name = name; |
| 69 | this.evictionStrategy = evictionStrategy; |
| 70 | this.cacheSize = cacheSize; |
| 71 | } |
| 72 | } |
| 73 | } |