View Javadoc

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.util.Destroyable;
24  import org.apache.shiro.util.LifecycleUtils;
25  
26  
27  /**
28   * A very basic starting point for the SecurityManager interface that merely provides logging and caching
29   * support.  All actual {@code SecurityManager} method implementations are left to subclasses.
30   * <p/>
31   * <b>Change in 1.0</b> - a default {@code CacheManager} instance is <em>not</em> created by default during
32   * instantiation.  As caching strategies can vary greatly depending on an application's needs, a {@code CacheManager}
33   * instance must be explicitly configured if caching across the framework is to be enabled.
34   *
35   * @since 0.9
36   */
37  public abstract class CachingSecurityManager implements SecurityManager, Destroyable, CacheManagerAware {
38  
39      /**
40       * The CacheManager to use to perform caching operations to enhance performance.  Can be null.
41       */
42      private CacheManager cacheManager;
43  
44      /**
45       * Default no-arg constructor that will automatically attempt to initialize a default cacheManager
46       */
47      public CachingSecurityManager() {
48      }
49  
50      /**
51       * Returns the CacheManager used by this SecurityManager.
52       *
53       * @return the cacheManager used by this SecurityManager
54       */
55      public CacheManager getCacheManager() {
56          return cacheManager;
57      }
58  
59      /**
60       * Sets the CacheManager used by this {@code SecurityManager} and potentially any of its
61       * children components.
62       * <p/>
63       * After the cacheManager attribute has been set, the template method
64       * {@link #afterCacheManagerSet afterCacheManagerSet()} is executed to allow subclasses to adjust when a
65       * cacheManager is available.
66       *
67       * @param cacheManager the CacheManager used by this {@code SecurityManager} and potentially any of its
68       *                     children components.
69       */
70      public void setCacheManager(CacheManager cacheManager) {
71          this.cacheManager = cacheManager;
72          afterCacheManagerSet();
73      }
74  
75      /**
76       * Template callback to notify subclasses that a
77       * {@link org.apache.shiro.cache.CacheManager CacheManager} has been set and is available for use via the
78       * {@link #getCacheManager getCacheManager()} method.
79       */
80      protected void afterCacheManagerSet() {
81      }
82  
83      /**
84       * Destroys the {@link #getCacheManager() cacheManager} via {@link LifecycleUtils#destroy LifecycleUtils.destroy}.
85       */
86      public void destroy() {
87          LifecycleUtils.destroy(getCacheManager());
88          this.cacheManager = null;
89      }
90  
91  }