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.mgt;
020
021import org.apache.shiro.cache.CacheManager;
022import org.apache.shiro.cache.CacheManagerAware;
023import org.apache.shiro.util.Destroyable;
024import org.apache.shiro.util.LifecycleUtils;
025
026
027/**
028 * A very basic starting point for the SecurityManager interface that merely provides logging and caching
029 * support.  All actual {@code SecurityManager} method implementations are left to subclasses.
030 * <p/>
031 * <b>Change in 1.0</b> - a default {@code CacheManager} instance is <em>not</em> created by default during
032 * instantiation.  As caching strategies can vary greatly depending on an application's needs, a {@code CacheManager}
033 * instance must be explicitly configured if caching across the framework is to be enabled.
034 *
035 * @since 0.9
036 */
037public abstract class CachingSecurityManager implements SecurityManager, Destroyable, CacheManagerAware {
038
039    /**
040     * The CacheManager to use to perform caching operations to enhance performance.  Can be null.
041     */
042    private CacheManager cacheManager;
043
044    /**
045     * Default no-arg constructor that will automatically attempt to initialize a default cacheManager
046     */
047    public CachingSecurityManager() {
048    }
049
050    /**
051     * Returns the CacheManager used by this SecurityManager.
052     *
053     * @return the cacheManager used by this SecurityManager
054     */
055    public CacheManager getCacheManager() {
056        return cacheManager;
057    }
058
059    /**
060     * Sets the CacheManager used by this {@code SecurityManager} and potentially any of its
061     * children components.
062     * <p/>
063     * After the cacheManager attribute has been set, the template method
064     * {@link #afterCacheManagerSet afterCacheManagerSet()} is executed to allow subclasses to adjust when a
065     * cacheManager is available.
066     *
067     * @param cacheManager the CacheManager used by this {@code SecurityManager} and potentially any of its
068     *                     children components.
069     */
070    public void setCacheManager(CacheManager cacheManager) {
071        this.cacheManager = cacheManager;
072        afterCacheManagerSet();
073    }
074
075    /**
076     * Template callback to notify subclasses that a
077     * {@link org.apache.shiro.cache.CacheManager CacheManager} has been set and is available for use via the
078     * {@link #getCacheManager getCacheManager()} method.
079     */
080    protected void afterCacheManagerSet() {
081    }
082
083    /**
084     * Destroys the {@link #getCacheManager() cacheManager} via {@link LifecycleUtils#destroy LifecycleUtils.destroy}.
085     */
086    public void destroy() {
087        LifecycleUtils.destroy(getCacheManager());
088        this.cacheManager = null;
089    }
090
091}