1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package com.macvu.tiles.cache;
20
21 import com.macvu.tiles.CacheInformation;
22 import org.apache.struts.util.RequestUtils;
23
24 import javax.servlet.ServletContext;
25 import javax.servlet.http.HttpServletRequest;
26 import java.util.List;
27 import java.io.Serializable;
28
29 /***
30 * User: MVu
31 * Provide a convenient wrapper to perform caching for the CacheInformation.
32 */
33 public class CacheObjectWrapper {
34 public static final String CACHE_CONTROLLER_REGISTRY = "CACHE_CONTROLLER_REGISTRY";
35 public static final String TILE_CACHE_STATUS = "TILE_CACHE_STATUS";
36
37 public static final String DEFINITIONS_CONFIG_PARAMETER_NAME_LIST = "definitions-config-list";
38
39 boolean cacheEnabled = false;
40 boolean statusInitiated = false;
41
42 CacheService cacheService;
43 CacheKeyFactory cacheKeyFactory;
44
45 CacheInformation info;
46
47 public CacheObjectWrapper() {
48 }
49
50 public void initiateCacheInformation(ServletContext servletContext, CacheInformation info) {
51 if (statusInitiated) {
52 throw new IllegalStateException("Cache Informationn already been initiated, call reset() before calling initiateCacheInformation() again");
53 }
54
55 statusInitiated = true;
56
57 if (info.isCacheEnabled() && !info.getCacheAttributes().isEmpty()) {
58 this.info = info;
59 cacheEnabled = true;
60 CacheFactoryManager manager = getCacheFactoryManager(servletContext);
61
62 TilesCacheStatus tileCacheStatus = getTilesCacheStatus(servletContext);
63 CacheStatus status = tileCacheStatus.getTileCacheStatus(info.getRepositoryName());
64 if (status == null) {
65 status = new CacheStatus(info.getRepositoryName());
66 }
67
68 try {
69 cacheKeyFactory = manager.getCacheKeyFactory(info.getKeyFactory());
70 } catch (ClassNotFoundException e) {
71 setStatusError(status, "KeyFactory is not defined: " + info.getKeyFactory() + " " + e.getMessage());
72 } catch (InstantiationException e) {
73 setStatusError(status, "Unable to instantiate class: " + info.getKeyFactory() + " " + e.getMessage());
74 } catch (IllegalAccessException e) {
75 setStatusError(status, "Unable to Access thread to create: " + info.getKeyFactory() + " " + e.getMessage());
76 }
77
78 try {
79 CacheServiceFactory factory = manager.getCacheServiceFactory(info.getRepositoryFactory());
80 cacheService = factory.getCacheService(info);
81 } catch (ClassNotFoundException e) {
82 setStatusError(status, "CacheServiceFactory is not defined: " + info.getRepositoryFactory() + " " + e.getMessage());
83 } catch (InstantiationException e) {
84 setStatusError(status, "Unable to instantiate class: " + info.getRepositoryFactory() + " " + e.getMessage());
85 } catch (IllegalAccessException e) {
86 setStatusError(status, "Unable to Access thread to create: " + info.getRepositoryFactory() + " " + e.getMessage());
87 }
88 }
89 }
90
91 private void setStatusError(CacheStatus status, String errorMessage) {
92 status.setStatus(CacheStatus.STATUS_ERROR);
93 status.setError(errorMessage);
94 cacheEnabled = false;
95 }
96
97 public void reset() {
98 cacheEnabled = false;
99 statusInitiated = false;
100 cacheService = null;
101 cacheKeyFactory = null;
102 }
103
104 public boolean isCacheEnabled() {
105 return cacheEnabled;
106 }
107
108 public String getCacheKey(HttpServletRequest request) {
109 if (!statusInitiated) {
110 throw new IllegalStateException("CacheObjectWrapper must be initialized first.");
111 }
112
113
114 if (!isCacheEnabled()) {
115 return null;
116 }
117
118 return cacheKeyFactory.getCacheKey(request, info);
119 }
120
121 public Object getCache(String key) {
122 if (!statusInitiated) {
123 throw new IllegalStateException("CacheObjectWrapper must be initialized first.");
124 }
125
126
127 if (!isCacheEnabled()) {
128 return null;
129 }
130
131 return cacheService.retrieve(key);
132 }
133
134 public void putCache(String key, Serializable cache) {
135 if (!statusInitiated) {
136 throw new IllegalStateException("CacheObjectWrapper must be initialized first.");
137 }
138
139
140 if (!isCacheEnabled()) {
141 }
142
143 cacheService.store(key, cache);
144 }
145
146 public static CacheFactoryManager getCacheFactoryManager(ServletContext servletContext) {
147 CacheFactoryManager manager = (CacheFactoryManager) servletContext.getAttribute(CACHE_CONTROLLER_REGISTRY);
148 if (manager == null) {
149 manager = new CacheFactoryManager();
150 servletContext.setAttribute(CACHE_CONTROLLER_REGISTRY, manager);
151 }
152 return manager;
153 }
154
155 public static TilesCacheStatus getTilesCacheStatus(ServletContext servletContext) {
156 TilesCacheStatus tileCacheStatus = (TilesCacheStatus) servletContext.getAttribute(TILE_CACHE_STATUS);
157 if (tileCacheStatus == null) {
158 tileCacheStatus = new TilesCacheStatus();
159 servletContext.setAttribute(TILE_CACHE_STATUS, tileCacheStatus);
160 }
161 return tileCacheStatus;
162 }
163
164 public static Class applicationClass(String className) throws ClassNotFoundException {
165
166 ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
167 if (classLoader == null) {
168 classLoader = RequestUtils.class.getClassLoader();
169 }
170
171
172 return (classLoader.loadClass(className));
173 }
174
175 public static List getTileDefinitionFileList(ServletContext servletContext) {
176 List fileList = (List) servletContext.getAttribute(DEFINITIONS_CONFIG_PARAMETER_NAME_LIST);
177 return fileList;
178 }
179
180 public static void setTileDefinitionFileList(ServletContext servletContext, List fileList) {
181 servletContext.setAttribute(DEFINITIONS_CONFIG_PARAMETER_NAME_LIST, fileList);
182 }
183 }
184