1 /**
2 * Copyright 2005-2006 the original author or authors.
3 *
4 * Licensed under the Gnu General Pubic License, Version 2.0 (the
5 * "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 *
8 * http://www.opensource.org/licenses/gpl-license.php
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the Gnu General Public License for more details.
14 */
15 package org.figure8.join.services.cache;
16
17 import org.figure8.join.util.LogUtil;
18
19 import org.apache.commons.logging.Log;
20
21 import java.util.List;
22 import java.util.Collections;
23 /**
24 * The goal of cache accessor is to provide an easy access to cached data
25 * from web-tier layers. As a convenience, it provides a static key for being
26 * registered within a ServletContext or some other application context.
27 * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
28 * @version $Revision: 1.2 $
29 */
30 public class EternalCacheAccessor{
31
32
33
34 /** Get a commons logger ... */
35 private static Log log = LogUtil.getLog(EternalCacheAccessor.class);
36
37 /** An EternalCacheAccessor instance is registered within the web-tier context using this CONTEXT_KEY */
38 public static final String CONTEXT_KEY = "cacheAccessor";
39
40
41
42
43 /** The CacheLoader instance for loading eternal data */
44 private CacheLoader cacheLoader = null;
45 /** The CacheManager instance for retrieving cached data */
46 private CacheManager cacheManager = null;
47
48
49
50
51 /**
52 * Creates a new instance of EternaleCacheAccessor with initiliazed {@link CacheManager}
53 * and loaded eternal caches. The instance of {@link CacheLoader} is also passed as a
54 * parameter because cache may have a lazy loading policy. Thus, this accessor will
55 * check for loading to occur.
56 * @param cacheManager The initiliazed manager to access caches from
57 * @param cacheLoader The loader that should have loaded eternal caches.
58 */
59 public EternalCacheAccessor(CacheManager cacheManager, CacheLoader cacheLoader){
60 this.cacheManager = cacheManager;
61 this.cacheLoader = cacheLoader;
62 }
63
64
65
66
67 /** @return The CacheManager instance used by this accessor */
68 public CacheManager getCacheManager(){
69 return cacheManager;
70 }
71
72 /**
73 * Retrieve an arbitrary {@link Cache} using its key identifier
74 * @param cacheKey The key of the cache to retrieve
75 * @return The Cache object corresponding or null if not found
76 */
77 public Cache getCache(String cacheKey){
78 Cache cache = cacheManager.getCache(cacheKey);
79 return cache;
80 }
81
82 /**
83 * Retrieve all the Step domain objects present in cache
84 * @return The list of cached {@link org.figure8.join.businessobjects.commons.Step}
85 */
86 public List getCachedSteps(){
87
88 if (!cacheLoader.hasLoadedCaches()){
89 try {cacheLoader.loadCaches();}
90 catch (Exception e){
91
92 log.error("CacheLoader throws an exception while lazy-loading caches: " + e.getMessage());
93 return Collections.EMPTY_LIST;
94 }
95 }
96
97 Cache cache = cacheManager.getCache(EternalCacheKeys.STEP_ID_TO_STEP_KEY);
98 if (cache != null){
99
100 List steps = cache.getElements();
101 Collections.sort(steps);
102 if (log.isTraceEnabled())
103 log.trace("Found " + steps.size() + " cached Steps");
104 return steps;
105 }
106 else{
107 log.trace("The cached Step list is empty");
108 return Collections.EMPTY_LIST;
109 }
110 }
111
112 /**
113 * Retrieve all the Role domain objects present in cache
114 * @return The list of cached {@link org.figure8.join.businessobjects.security.Role}
115 */
116 public List getCachedRoles(){
117
118 if (!cacheLoader.hasLoadedCaches()){
119 try {cacheLoader.loadCaches();}
120 catch (Exception e){
121
122 log.error("CacheLoader throws an exception while lazy-loading caches: " + e.getMessage());
123 return Collections.EMPTY_LIST;
124 }
125 }
126
127 Cache cache = cacheManager.getCache(EternalCacheKeys.ROLE_NAME_TO_ROLE_KEY);
128 if (cache != null){
129
130 List roles = cache.getElements();
131 Collections.sort(roles);
132 if (log.isTraceEnabled())
133 log.trace("Found " + roles.size() + " cached Roles");
134 return roles;
135 }
136 else{
137 log.trace("The cached Role list is empty");
138 return Collections.EMPTY_LIST;
139 }
140 }
141
142 /**
143 * Retrieve all the Release domain objects present in cache
144 * @return The list of cached {@link org.figure8.join.businessobjects.commons.Release}
145 */
146 public List getCachedReleases(){
147
148 if (!cacheLoader.hasLoadedCaches()){
149 try {cacheLoader.loadCaches();}
150 catch (Exception e){
151
152 log.error("CacheLoader throws an exception while lazy-loading caches: " + e.getMessage());
153 return Collections.EMPTY_LIST;
154 }
155 }
156
157 Cache cache = cacheManager.getCache(EternalCacheKeys.RELEASE_NAME_TO_RELEASE_KEY);
158 if (cache != null){
159
160 List releases = cache.getElements();
161 Collections.sort(releases, Collections.reverseOrder());
162 if (log.isTraceEnabled())
163 log.trace("Found " + releases.size() + " cached Releases");
164 return releases;
165 }
166 else{
167 log.trace("The cached Release list is empty");
168 return Collections.EMPTY_LIST;
169 }
170 }
171
172 /**
173 * Retrieve all the DeliverableType domain objects present in cache
174 * @return The list of cached {@link org.figure8.join.businessobjects.artifact.DeliverableType}
175 */
176 public List getCachedDeliverableTypes(){
177
178 if (!cacheLoader.hasLoadedCaches()){
179 try {cacheLoader.loadCaches();}
180 catch (Exception e){
181
182 log.error("CacheLoader throws an exception while lazy-loading caches: " + e.getMessage());
183 return Collections.EMPTY_LIST;
184 }
185 }
186
187 Cache cache = cacheManager.getCache(EternalCacheKeys.DELTYPE_KEY_TO_DELTYPE_KEY);
188 if (cache != null){
189
190 List types = cache.getElements();
191 Collections.sort(types);
192 if (log.isTraceEnabled())
193 log.trace("Found " + types.size() + " cached DeliverableTypes");
194 return types;
195 }
196 else{
197 log.trace("The cached DeliverableType list is empty");
198 return Collections.EMPTY_LIST;
199 }
200 }
201 }