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 module hunt.shiro.mgt.CachingSecurityManager;
20 
21 import hunt.shiro.mgt.SecurityManager;
22 
23 import hunt.shiro.cache.CacheManager;
24 import hunt.shiro.cache.CacheManagerAware;
25 import hunt.shiro.event.EventBus;
26 import hunt.shiro.event.EventBusAware;
27 import hunt.shiro.event.support.DefaultEventBus;
28 import hunt.shiro.util.Common;
29 import hunt.shiro.util.LifecycleUtils;
30 
31 import hunt.Exceptions;
32 
33 /**
34  * A very basic starting point for the SecurityManager interface that merely provides logging and caching
35  * support.  All actual {@code SecurityManager} method implementations are left to subclasses.
36  * <p/>
37  * <b>Change in 1.0</b> - a default {@code CacheManager} instance is <em>not</em> created by default during
38  * instantiation.  As caching strategies can vary greatly depending on an application's needs, a {@code CacheManager}
39  * instance must be explicitly configured if caching across the framework is to be enabled.
40  *
41  */
42 abstract class CachingSecurityManager : SecurityManager, 
43     Destroyable, CacheManagerAware, EventBusAware {
44 
45     /**
46      * The CacheManager to use to perform caching operations to enhance performance.  Can be null.
47      */
48     private CacheManager cacheManager;
49 
50     /**
51      * The EventBus to use to use to publish and receive events of interest during Shiro's lifecycle.
52      */
53     private EventBus eventBus;
54 
55     /**
56      * Default no-arg constructor that will automatically attempt to initialize a default cacheManager
57      */
58     this() {
59         //use a default event bus:
60         setEventBus(new DefaultEventBus());
61     }
62 
63     /**
64      * Returns the CacheManager used by this SecurityManager.
65      *
66      * @return the cacheManager used by this SecurityManager
67      */
68      CacheManager getCacheManager() {
69         return cacheManager;
70     }
71 
72     /**
73      * Sets the CacheManager used by this {@code SecurityManager} and potentially any of its
74      * children components.
75      * <p/>
76      * After the cacheManager attribute has been set, the template method
77      * {@link #afterCacheManagerSet afterCacheManagerSet()} is executed to allow subclasses to adjust when a
78      * cacheManager is available.
79      *
80      * @param cacheManager the CacheManager used by this {@code SecurityManager} and potentially any of its
81      *                     children components.
82      */
83      void setCacheManager(CacheManager cacheManager) {
84         this.cacheManager = cacheManager;
85         afterCacheManagerSet();
86     }
87 
88     /**
89      * Template callback to notify subclasses that a
90      * {@link hunt.shiro.cache.CacheManager CacheManager} has been set and is available for use via the
91      * {@link #getCacheManager getCacheManager()} method.
92      */
93     protected void afterCacheManagerSet() {
94         applyEventBusToCacheManager();
95     }
96 
97     /**
98      * Returns the {@code EventBus} used by this SecurityManager and potentially any of its children components.
99      *
100      * @return the {@code EventBus} used by this SecurityManager and potentially any of its children components.
101      */
102      EventBus getEventBus() {
103         return eventBus;
104     }
105 
106     /**
107      * Sets the EventBus used by this {@code SecurityManager} and potentially any of its
108      * children components.
109      * <p/>
110      * After the eventBus attribute has been set, the template method
111      * {@link #afterEventBusSet() afterEventBusSet()} is executed to allow subclasses to adjust when a
112      * eventBus is available.
113      *
114      * @param eventBus the EventBus used by this {@code SecurityManager} and potentially any of its
115      *                     children components.
116      */
117      void setEventBus(EventBus eventBus) {
118         this.eventBus = eventBus;
119         afterEventBusSet();
120     }
121 
122     /**
123      */
124     protected void applyEventBusToCacheManager() {
125         auto cacheManagerCast = cast(EventBusAware)this.cacheManager;
126         if (this.eventBus !is null && this.cacheManager !is null && cacheManagerCast !is null) {
127             cacheManagerCast.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      */
136     protected void afterEventBusSet() {
137         applyEventBusToCacheManager();
138     }
139 
140     /**
141      * Destroys the {@link #getCacheManager() cacheManager} via {@link LifecycleUtils#destroy LifecycleUtils.destroy}.
142      */
143      void destroy() {
144         LifecycleUtils.destroy(cast(Object)getCacheManager());
145         this.cacheManager = null;
146         LifecycleUtils.destroy(cast(Object)getEventBus());
147         this.eventBus = new DefaultEventBus();
148     }
149 
150 }