1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 */ 19 package org.apache.shiro.mgt; 20 21 import org.apache.shiro.cache.CacheManager; 22 import org.apache.shiro.cache.CacheManagerAware; 23 import org.apache.shiro.event.EventBus; 24 import org.apache.shiro.event.EventBusAware; 25 import org.apache.shiro.event.support.DefaultEventBus; 26 import org.apache.shiro.util.Destroyable; 27 import org.apache.shiro.util.LifecycleUtils; 28 29 30 /** 31 * A very basic starting point for the SecurityManager interface that merely provides logging and caching 32 * support. All actual {@code SecurityManager} method implementations are left to subclasses. 33 * <p/> 34 * <b>Change in 1.0</b> - a default {@code CacheManager} instance is <em>not</em> created by default during 35 * instantiation. As caching strategies can vary greatly depending on an application's needs, a {@code CacheManager} 36 * instance must be explicitly configured if caching across the framework is to be enabled. 37 * 38 * @since 0.9 39 */ 40 public abstract class CachingSecurityManager implements SecurityManager, Destroyable, CacheManagerAware, EventBusAware { 41 42 /** 43 * The CacheManager to use to perform caching operations to enhance performance. Can be null. 44 */ 45 private CacheManager cacheManager; 46 47 /** 48 * The EventBus to use to use to publish and receive events of interest during Shiro's lifecycle. 49 * @since 1.3 50 */ 51 private EventBus eventBus; 52 53 /** 54 * Default no-arg constructor that will automatically attempt to initialize a default cacheManager 55 */ 56 public CachingSecurityManager() { 57 //use a default event bus: 58 setEventBus(new DefaultEventBus()); 59 } 60 61 /** 62 * Returns the CacheManager used by this SecurityManager. 63 * 64 * @return the cacheManager used by this SecurityManager 65 */ 66 public CacheManager getCacheManager() { 67 return cacheManager; 68 } 69 70 /** 71 * Sets the CacheManager used by this {@code SecurityManager} and potentially any of its 72 * children components. 73 * <p/> 74 * After the cacheManager attribute has been set, the template method 75 * {@link #afterCacheManagerSet afterCacheManagerSet()} is executed to allow subclasses to adjust when a 76 * cacheManager is available. 77 * 78 * @param cacheManager the CacheManager used by this {@code SecurityManager} and potentially any of its 79 * children components. 80 */ 81 public void setCacheManager(CacheManager cacheManager) { 82 this.cacheManager = cacheManager; 83 afterCacheManagerSet(); 84 } 85 86 /** 87 * Template callback to notify subclasses that a 88 * {@link org.apache.shiro.cache.CacheManager CacheManager} has been set and is available for use via the 89 * {@link #getCacheManager getCacheManager()} method. 90 */ 91 protected void afterCacheManagerSet() { 92 applyEventBusToCacheManager(); 93 } 94 95 /** 96 * Returns the {@code EventBus} used by this SecurityManager and potentially any of its children components. 97 * 98 * @return the {@code EventBus} used by this SecurityManager and potentially any of its children components. 99 * @since 1.3 100 */ 101 public EventBus getEventBus() { 102 return eventBus; 103 } 104 105 /** 106 * Sets the EventBus used by this {@code SecurityManager} and potentially any of its 107 * children components. 108 * <p/> 109 * After the eventBus attribute has been set, the template method 110 * {@link #afterEventBusSet() afterEventBusSet()} is executed to allow subclasses to adjust when a 111 * eventBus is available. 112 * 113 * @param eventBus the EventBus used by this {@code SecurityManager} and potentially any of its 114 * children components. 115 * @since 1.3 116 */ 117 public void setEventBus(EventBus eventBus) { 118 this.eventBus = eventBus; 119 afterEventBusSet(); 120 } 121 122 /** 123 * @since 1.3 124 */ 125 protected void applyEventBusToCacheManager() { 126 if (this.eventBus != null && this.cacheManager != null && this.cacheManager instanceof EventBusAware) { 127 ((EventBusAware)this.cacheManager).setEventBus(this.eventBus); 128 } 129 } 130 131 /** 132 * Template callback to notify subclasses that an {@link EventBus EventBus} has been set and is available for use 133 * via the {@link #getEventBus() getEventBus()} method. 134 * 135 * @since 1.3 136 */ 137 protected void afterEventBusSet() { 138 applyEventBusToCacheManager(); 139 } 140 141 /** 142 * Destroys the {@link #getCacheManager() cacheManager} via {@link LifecycleUtils#destroy LifecycleUtils.destroy}. 143 */ 144 public void destroy() { 145 LifecycleUtils.destroy(getCacheManager()); 146 this.cacheManager = null; 147 LifecycleUtils.destroy(getEventBus()); 148 this.eventBus = new DefaultEventBus(); 149 } 150 151 }