39b3f0cb979280cb9a7af938fea95101b72cbfe4
[portal/sdk.git] /
1 /*-
2  * ================================================================================
3  * eCOMP Portal SDK
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ================================================================================
19  */
20 package org.openecomp.portalsdk.core.objectcache.jcs;
21
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.util.Properties;
25 import java.util.Vector;
26
27 import javax.annotation.PostConstruct;
28 import javax.servlet.ServletContext;
29
30 import org.apache.jcs.JCS;
31 import org.apache.jcs.access.exception.CacheException;
32 import org.apache.jcs.engine.CacheConstants;
33 import org.apache.jcs.engine.behavior.IElementAttributes;
34 import org.apache.jcs.engine.control.CompositeCacheManager;
35 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
36 import org.openecomp.portalsdk.core.objectcache.AbstractCacheManager;
37 import org.openecomp.portalsdk.core.service.DataAccessService;
38 import org.openecomp.portalsdk.core.util.SystemProperties;
39 import org.springframework.web.context.ServletContextAware;
40
41 public abstract class JCSCacheManager extends AbstractCacheManager implements CacheConstants, ServletContextAware {
42
43         public static String LOOKUP_OBJECT_CACHE_NAME = "lookUpObjectCache";
44         public static String JCS_CONFIG_FILE_PATH = "cache_config_file_path";
45         public static String CACHE_LOAD_ON_STARTUP = "cache_load_on_startup";
46         public static String CACHE_PROPERTY_VALUE_TRUE = "true";
47         public static String CACHE_CONTROL_SWITCH_ON = "1";
48         public static String CACHE_CONTROL_SWITCH_OFF = "0";
49         public static String CACHE_CONTROL_SWITCH = "cache_switch";
50
51         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(JCSCacheManager.class);
52
53         private static JCS lookUpCache;
54         private ServletContext servletContext;
55
56         private Properties cacheConfigProperties = null;
57         private final Vector<String> jscManagedCacheList = new Vector<String>();
58
59         private DataAccessService dataAccessService;
60
61         public JCSCacheManager() {
62                 super();
63                 jscManagedCacheList.add(LOOKUP_OBJECT_CACHE_NAME);
64         }
65
66         @PostConstruct
67         public void configure() throws IOException {
68                 super.configure();
69
70                 String jcsConfigFilePath = SystemProperties.getProperty(JCS_CONFIG_FILE_PATH);
71                 // getProperty throws if the key is missing; but check anyhow.
72                 if (jcsConfigFilePath == null || jcsConfigFilePath.length() == 0)
73                         throw new IOException("configure: failed to get value for config property " + JCS_CONFIG_FILE_PATH);
74                 InputStream jcsConfigInputStream = getServletContext().getResourceAsStream(jcsConfigFilePath);
75                 if (jcsConfigInputStream == null)
76                         throw new IOException("configure: failed to open stream for config property " + JCS_CONFIG_FILE_PATH
77                                         + " with name " + jcsConfigFilePath);
78                 logger.debug(EELFLoggerDelegate.debugLogger,
79                                 "configure: loading cache properties from classpath resource {} ", jcsConfigFilePath);
80                 Properties p = new Properties();
81                 p.load(jcsConfigInputStream);
82                 jcsConfigInputStream.close();
83
84                 CompositeCacheManager ccm = CompositeCacheManager.getUnconfiguredInstance();
85                 ccm.configure(p);
86                 setCacheConfigProperties(p);
87
88                 try {
89                         initializeLookUpCache();
90                 } catch (CacheException ce) {
91                         throw new IOException("configure: failed to initialize lookup cache", ce);
92                 }
93
94         }
95
96         private void initializeLookUpCache() throws CacheException {
97                 lookUpCache = JCS.getInstance(LOOKUP_OBJECT_CACHE_NAME);
98
99                 JCSCacheEventHandler eventHandler = new JCSCacheEventHandler();
100                 IElementAttributes elementAttributes = lookUpCache.getDefaultElementAttributes();
101
102                 elementAttributes.addElementEventHandler(eventHandler);
103
104                 lookUpCache.setDefaultElementAttributes(elementAttributes);
105
106                 if (CACHE_PROPERTY_VALUE_TRUE.equalsIgnoreCase(SystemProperties.getProperty(CACHE_LOAD_ON_STARTUP))) {
107                         loadDataOnStartUp();
108                 }
109         }
110
111         public Object getObject(String key) {
112                 if (CACHE_CONTROL_SWITCH_ON.equalsIgnoreCase(SystemProperties.getProperty(CACHE_CONTROL_SWITCH))) {
113                         if (lookUpCache == null)
114                                 return null;
115                         else
116                                 return lookUpCache.get(key);
117                 } else
118                         return null;
119         }
120
121         public void putObject(String key, Object objectToCache) {
122                 try {
123                         if (CACHE_CONTROL_SWITCH_ON.equalsIgnoreCase(SystemProperties.getProperty(CACHE_CONTROL_SWITCH))) {
124                                 if (lookUpCache != null) {
125                                         lookUpCache.put(key, objectToCache);
126                                 }
127                         }
128                 } catch (CacheException ce) {
129                         logger.error(EELFLoggerDelegate.errorLogger, "putObject: failed to put the object with key " + key, ce);
130                 }
131         }
132
133         public void clearCache(String region) {
134                 try {
135                         if (region.equals(LOOKUP_OBJECT_CACHE_NAME))
136                                 lookUpCache.clear();
137                 } catch (CacheException ce) {
138                         logger.error(EELFLoggerDelegate.errorLogger,
139                                         "clearCache: failed to clear the cache for the region " + region, ce);
140                 }
141         }
142
143         public void clearCache() {
144                 clearCache(LOOKUP_OBJECT_CACHE_NAME);
145         }
146
147         private void loadDataOnStartUp() {
148                 loadLookUpCache();
149         }
150
151         public abstract void loadLookUpCache();
152
153         public void refreshLookUpCache() {
154                 clearCache(LOOKUP_OBJECT_CACHE_NAME);
155                 loadLookUpCache();
156         }
157
158         public Properties getCacheConfigProperties() {
159                 return cacheConfigProperties;
160         }
161
162         public void setCacheConfigProperties(Properties cacheConfigProperties) {
163                 this.cacheConfigProperties = cacheConfigProperties;
164         }
165
166         public Vector<String> getJscManagedCacheList() {
167                 return jscManagedCacheList;
168         }
169
170         public DataAccessService getDataAccessService() {
171                 return dataAccessService;
172         }
173
174         public void setDataAccessService(DataAccessService dataAccessService) {
175                 this.dataAccessService = dataAccessService;
176         }
177
178         public ServletContext getServletContext() {
179                 return servletContext;
180         }
181
182         public void setServletContext(ServletContext servletContext) {
183                 this.servletContext = servletContext;
184         }
185
186 }