001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.shiro.cache;
020
021import org.apache.shiro.lang.util.Destroyable;
022import org.apache.shiro.lang.util.LifecycleUtils;
023import org.apache.shiro.lang.util.StringUtils;
024
025import java.util.Collection;
026import java.util.concurrent.ConcurrentHashMap;
027import java.util.concurrent.ConcurrentMap;
028
029/**
030 * Very simple abstract {@code CacheManager} implementation that retains all created {@link Cache Cache} instances in
031 * an in-memory {@link ConcurrentMap ConcurrentMap}.  {@code Cache} instance creation is left to subclasses via
032 * the {@link #createCache createCache} method implementation.
033 *
034 * @since 1.0
035 */
036public abstract class AbstractCacheManager implements CacheManager, Destroyable {
037
038    /**
039     * Retains all Cache objects maintained by this cache manager.
040     */
041    private final ConcurrentMap<String, Cache<?, ?>> caches;
042
043    /**
044     * Default no-arg constructor that instantiates an internal name-to-cache {@code ConcurrentMap}.
045     */
046    public AbstractCacheManager() {
047        this.caches = new ConcurrentHashMap<>();
048    }
049
050    /**
051     * Returns the cache with the specified {@code name}.  If the cache instance does not yet exist, it will be lazily
052     * created, retained for further access, and then returned.
053     *
054     * @param name the name of the cache to acquire.
055     * @return the cache with the specified {@code name}.
056     * @throws IllegalArgumentException if the {@code name} argument is {@code null} or does not contain text.
057     * @throws CacheException           if there is a problem lazily creating a {@code Cache} instance.
058     */
059    @SuppressWarnings("unchecked")
060    public <K, V> Cache<K, V> getCache(String name) throws IllegalArgumentException, CacheException {
061        if (!StringUtils.hasText(name)) {
062            throw new IllegalArgumentException("Cache name cannot be null or empty.");
063        }
064
065        Cache<K, V> cache;
066
067        cache = (Cache<K, V>) caches.get(name);
068        if (cache == null) {
069            cache = createCache(name);
070            Cache<K, V> existing = (Cache<K, V>) caches.putIfAbsent(name, cache);
071            if (existing != null) {
072                cache = existing;
073            }
074        }
075
076        //noinspection unchecked
077        return cache;
078    }
079
080    /**
081     * Creates a new {@code Cache} instance associated with the specified {@code name}.
082     *
083     * @param name the name of the cache to create
084     * @return a new {@code Cache} instance associated with the specified {@code name}.
085     * @throws CacheException if the {@code Cache} instance cannot be created.
086     */
087    protected abstract <K, V> Cache<K, V> createCache(String name) throws CacheException;
088
089    /**
090     * Cleanup method that first {@link LifecycleUtils#destroy destroys} all of it's managed caches and then
091     * {@link java.util.Map#clear clears} out the internally referenced cache map.
092     *
093     * @throws Exception if any of the managed caches can't destroy properly.
094     */
095    public void destroy() throws Exception {
096        while (!caches.isEmpty()) {
097            for (Cache cache : caches.values()) {
098                LifecycleUtils.destroy(cache);
099            }
100            caches.clear();
101        }
102    }
103
104    public String toString() {
105        Collection<Cache<?, ?>> values = caches.values();
106        StringBuilder sb = new StringBuilder(getClass().getSimpleName())
107                .append(" with ")
108                .append(caches.size())
109                .append(" cache(s)): [");
110        int i = 0;
111        for (Cache cache : values) {
112            if (i > 0) {
113                sb.append(", ");
114            }
115            sb.append(cache.toString());
116            i++;
117        }
118        sb.append("]");
119        return sb.toString();
120    }
121}