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.realm;
020
021 import org.apache.shiro.cache.CacheManager;
022 import org.apache.shiro.cache.CacheManagerAware;
023 import org.apache.shiro.util.Nameable;
024
025 import java.util.concurrent.atomic.AtomicInteger;
026
027
028 /**
029 * A very basic abstract extension point for the {@link Realm} interface that provides caching support.
030 * <p/>
031 * All actual Realm method implementations are left to subclasses.
032 *
033 * @author Les Hazlewood
034 * @since 0.9
035 */
036 public abstract class CachingRealm implements Realm, Nameable, CacheManagerAware {
037
038 //TODO - complete JavaDoc
039
040 private static final AtomicInteger INSTANCE_COUNT = new AtomicInteger();
041
042 /*--------------------------------------------
043 | I N S T A N C E V A R I A B L E S |
044 ============================================*/
045 private String name;
046 private boolean cachingEnabled;
047 private CacheManager cacheManager;
048
049 public CachingRealm() {
050 this.cachingEnabled = true;
051 this.name = getClass().getName() + "_" + INSTANCE_COUNT.getAndIncrement();
052 }
053
054 /**
055 * Returns the <tt>CacheManager</tt> used for data caching to reduce EIS round trips, or <tt>null</tt> if
056 * caching is disabled.
057 *
058 * @return the <tt>CacheManager</tt> used for data caching to reduce EIS round trips, or <tt>null</tt> if
059 * caching is disabled.
060 */
061 public CacheManager getCacheManager() {
062 return this.cacheManager;
063 }
064
065 /**
066 * Sets the <tt>CacheManager</tt> to be used for data caching to reduce EIS round trips.
067 * <p/>
068 * <p>This property is <tt>null</tt> by default, indicating that caching is turned off.
069 *
070 * @param cacheManager the <tt>CacheManager</tt> to use for data caching, or <tt>null</tt> to disable caching.
071 */
072 public void setCacheManager(CacheManager cacheManager) {
073 this.cacheManager = cacheManager;
074 afterCacheManagerSet();
075 }
076
077 /**
078 * Returns {@code true} if caching should be used if a {@link CacheManager} has been
079 * {@link #setCacheManager(org.apache.shiro.cache.CacheManager) configured}, {@code false} otherwise.
080 * <p/>
081 * The default value is {@code true} since the large majority of Realms will benefit from caching if a CacheManager
082 * has been configured. However, memory-only realms should set this value to {@code false} since they would
083 * manage account data in memory already lookups would already be as efficient as possible.
084 *
085 * @return {@code true} if caching will be globally enabled if a {@link CacheManager} has been
086 * configured, {@code false} otherwise
087 */
088 public boolean isCachingEnabled() {
089 return cachingEnabled;
090 }
091
092 /**
093 * Sets whether or not caching should be used if a {@link CacheManager} has been
094 * {@link #setCacheManager(org.apache.shiro.cache.CacheManager) configured}.
095 *
096 * @param cachingEnabled whether or not to globally enable caching for this realm.
097 */
098 public void setCachingEnabled(boolean cachingEnabled) {
099 this.cachingEnabled = cachingEnabled;
100 }
101
102 public String getName() {
103 return name;
104 }
105
106 public void setName(String name) {
107 this.name = name;
108 }
109
110 protected void afterCacheManagerSet() {
111 }
112 }