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 */
019 package org.apache.shiro.mgt;
020
021 import org.apache.shiro.cache.CacheManager;
022 import org.apache.shiro.cache.CacheManagerAware;
023 import org.apache.shiro.util.Destroyable;
024 import 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 * @author Les Hazlewood
036 * @author Jeremy Haile
037 * @since 0.9
038 */
039 public abstract class CachingSecurityManager implements SecurityManager, Destroyable, CacheManagerAware {
040
041 /**
042 * The CacheManager to use to perform caching operations to enhance performance. Can be null.
043 */
044 private CacheManager cacheManager;
045
046 /**
047 * Default no-arg constructor that will automatically attempt to initialize a default cacheManager
048 */
049 public CachingSecurityManager() {
050 }
051
052 /**
053 * Returns the CacheManager used by this SecurityManager.
054 *
055 * @return the cacheManager used by this SecurityManager
056 */
057 public CacheManager getCacheManager() {
058 return cacheManager;
059 }
060
061 /**
062 * Sets the CacheManager used by this {@code SecurityManager} and potentially any of its
063 * children components.
064 * <p/>
065 * After the cacheManager attribute has been set, the template method
066 * {@link #afterCacheManagerSet afterCacheManagerSet()} is executed to allow subclasses to adjust when a
067 * cacheManager is available.
068 *
069 * @param cacheManager the CacheManager used by this {@code SecurityManager} and potentially any of its
070 * children components.
071 */
072 public void setCacheManager(CacheManager cacheManager) {
073 this.cacheManager = cacheManager;
074 afterCacheManagerSet();
075 }
076
077 /**
078 * Template callback to notify subclasses that a
079 * {@link org.apache.shiro.cache.CacheManager CacheManager} has been set and is available for use via the
080 * {@link #getCacheManager getCacheManager()} method.
081 */
082 protected void afterCacheManagerSet() {
083 }
084
085 /**
086 * Destroys the {@link #getCacheManager() cacheManager} via {@link LifecycleUtils#destroy LifecycleUtils.destroy}.
087 */
088 public void destroy() {
089 LifecycleUtils.destroy(getCacheManager());
090 this.cacheManager = null;
091 }
092
093 }