2 * ================================================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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 * ================================================================================
20 package org.openecomp.portalsdk.core.objectcache.jcs;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.util.Properties;
25 import java.util.Vector;
27 import javax.annotation.PostConstruct;
28 import javax.servlet.ServletContext;
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;
41 public abstract class JCSCacheManager extends AbstractCacheManager implements CacheConstants, ServletContextAware {
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";
51 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(JCSCacheManager.class);
53 private static JCS lookUpCache;
54 private ServletContext servletContext;
56 private Properties cacheConfigProperties = null;
57 private final Vector<String> jscManagedCacheList = new Vector<String>();
59 private DataAccessService dataAccessService;
61 public JCSCacheManager() {
63 jscManagedCacheList.add(LOOKUP_OBJECT_CACHE_NAME);
67 public void configure() throws IOException {
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();
84 CompositeCacheManager ccm = CompositeCacheManager.getUnconfiguredInstance();
86 setCacheConfigProperties(p);
89 initializeLookUpCache();
90 } catch (CacheException ce) {
91 throw new IOException("configure: failed to initialize lookup cache", ce);
96 private void initializeLookUpCache() throws CacheException {
97 lookUpCache = JCS.getInstance(LOOKUP_OBJECT_CACHE_NAME);
99 JCSCacheEventHandler eventHandler = new JCSCacheEventHandler();
100 IElementAttributes elementAttributes = lookUpCache.getDefaultElementAttributes();
102 elementAttributes.addElementEventHandler(eventHandler);
104 lookUpCache.setDefaultElementAttributes(elementAttributes);
106 if (CACHE_PROPERTY_VALUE_TRUE.equalsIgnoreCase(SystemProperties.getProperty(CACHE_LOAD_ON_STARTUP))) {
111 public Object getObject(String key) {
112 if (CACHE_CONTROL_SWITCH_ON.equalsIgnoreCase(SystemProperties.getProperty(CACHE_CONTROL_SWITCH))) {
113 if (lookUpCache == null)
116 return lookUpCache.get(key);
121 public void putObject(String key, Object objectToCache) {
123 if (CACHE_CONTROL_SWITCH_ON.equalsIgnoreCase(SystemProperties.getProperty(CACHE_CONTROL_SWITCH))) {
124 if (lookUpCache != null) {
125 lookUpCache.put(key, objectToCache);
128 } catch (CacheException ce) {
129 logger.error(EELFLoggerDelegate.errorLogger, "putObject: failed to put the object with key " + key, ce);
133 public void clearCache(String region) {
135 if (region.equals(LOOKUP_OBJECT_CACHE_NAME))
137 } catch (CacheException ce) {
138 logger.error(EELFLoggerDelegate.errorLogger,
139 "clearCache: failed to clear the cache for the region " + region, ce);
143 public void clearCache() {
144 clearCache(LOOKUP_OBJECT_CACHE_NAME);
147 private void loadDataOnStartUp() {
151 public abstract void loadLookUpCache();
153 public void refreshLookUpCache() {
154 clearCache(LOOKUP_OBJECT_CACHE_NAME);
158 public Properties getCacheConfigProperties() {
159 return cacheConfigProperties;
162 public void setCacheConfigProperties(Properties cacheConfigProperties) {
163 this.cacheConfigProperties = cacheConfigProperties;
166 public Vector<String> getJscManagedCacheList() {
167 return jscManagedCacheList;
170 public DataAccessService getDataAccessService() {
171 return dataAccessService;
174 public void setDataAccessService(DataAccessService dataAccessService) {
175 this.dataAccessService = dataAccessService;
178 public ServletContext getServletContext() {
179 return servletContext;
182 public void setServletContext(ServletContext servletContext) {
183 this.servletContext = servletContext;