001package com.nimbusds.infinispan.persistence.common;
002
003
004import java.util.Collections;
005import java.util.Hashtable;
006import java.util.Map;
007
008import com.nimbusds.infinispan.persistence.common.query.QueryExecutor;
009import net.jcip.annotations.ThreadSafe;
010import org.infinispan.persistence.spi.AdvancedLoadWriteStore;
011import org.infinispan.persistence.spi.InitializationContext;
012
013
014/**
015 * Abstract persisting store for Infinispan 8.2+ caches and maps.
016 */
017@ThreadSafe
018public abstract class InfinispanStore<K,V> implements AdvancedLoadWriteStore<K,V> {
019        
020        
021        /**
022         * The instances of this class, keyed by Infinispan cache name.
023         */
024        private static Map<String,InfinispanStore> instances = new Hashtable<>();
025        
026        
027        /**
028         * Returns the {@link #init initialised} instances of this class.
029         * Intended for testing and debugging purposes.
030         *
031         * @return The instances of this class, keyed by Infinispan cache name.
032         */
033        public static Map<String,InfinispanStore> getInstances() {
034                return Collections.unmodifiableMap(instances);
035        }
036
037
038        /**
039         * The initialisation context for the associated Infinispan cache /
040         * map.
041         */
042        private InitializationContext ctx;
043        
044        
045        /**
046         * The name of the associated Infinispan cache / map.
047         */
048        private String cacheName;
049        
050        
051        /**
052         * Returns the initialisation context for the associated Infinispan
053         * cache / map.
054         *
055         * @return The initialisation context for the associated Infinispan
056         *         cache / map.
057         */
058        public InitializationContext getInitContext() {
059                
060                return ctx;
061        }
062        
063        
064        /**
065         * Returns the name of the associated Infinispan cache / map.
066         *
067         * @return The name of the associated Infinispan cache / map,
068         *         {@code null} if not available.
069         */
070        public String getCacheName() {
071                
072                return cacheName;
073        }
074        
075
076        @Override
077        public void init(final InitializationContext ctx) {
078
079                // This method will be invoked by the PersistenceManager during initialization. The InitializationContext
080                // contains:
081                // - this CacheLoader's configuration
082                // - the cache to which this loader is applied. Your loader might want to use the cache's name to construct
083                //   cache-specific identifiers
084                // - the StreamingMarshaller that needs to be used to marshall/unmarshall the entries
085                // - a TimeService which the loader can use to determine expired entries
086                // - a ByteBufferFactory which needs to be used to construct ByteBuffers
087                // - a MarshalledEntryFactory which needs to be used to construct entries from the data retrieved by the loader
088
089                this.ctx = ctx;
090                
091                // Store cache name
092                cacheName = ctx.getCache().getName();
093                
094                // Register store
095                InfinispanStore.instances.put(cacheName, this);
096        }
097        
098        
099        /**
100         * Returns an query executor against the underlying Infinispan cache
101         * store.
102         *
103         * @return The query executor, {@code null} if not supported or not
104         *         enabled.
105         */
106        public QueryExecutor<K,V> getQueryExecutor() {
107                
108                return null;
109        }
110        
111        
112        @Override
113        public void stop() {
114                
115                // Unregister store
116                if (cacheName != null) {
117                        InfinispanStore.instances.remove(cacheName);
118                }
119        }
120}