All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.alachisoft.integrations.spring.CacheManagerWrapper Maven / Gradle / Ivy

There is a newer version: 5.2-alpha-23
Show newest version
/*
 * Alachisoft (R) NCache Integrations
 * NCache Provider for Spring
 * ===============================================================================
 * Copyright © Alachisoft.  All rights reserved.
 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
 * OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
 * LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE.
 * ===============================================================================
 */
package com.alachisoft.integrations.spring;

import com.alachisoft.integrations.spring.configuration.SpringConfigurationManager;
import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;

public class CacheManagerWrapper implements CacheManager {

    private HashMap caches = new HashMap();
    private SpringConfigurationManager configurationManager = null;
    private Logger logger = null;
    private String logFilePath = null;

    public CacheManagerWrapper() {
    }

    public void setLogFilePath(String path) {
        logFilePath = path;
    }

    private void configureLogger() {
        try {
            logger = Logger.getLogger("com.alachisoft.integrations.spring");
            logger.setLevel(Level.INFO);
            if (logFilePath == null) {
                logFilePath = System.getenv("NCHOME");
                if (logFilePath == null || logFilePath.isEmpty())
                    logFilePath = System.getProperty("user.dir");
                logFilePath = logFilePath + File.separator + "log";
            }
            if (!(new File(logFilePath)).exists()) {
                (new File(logFilePath)).mkdirs();
            }
            FileHandler fileTxt = new FileHandler(logFilePath + File.separator + "ncache-spring-" + (new Date()).toString().replaceAll(":", "-") + ".txt");
            SimpleFormatter formatterTxt = new SimpleFormatter();
            fileTxt.setFormatter(formatterTxt);
            logger.addHandler(fileTxt);
        } catch (SecurityException ex) {
            Logger.getLogger(CacheManagerWrapper.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(CacheManagerWrapper.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void setSpringConfigurationManager(SpringConfigurationManager configurationManager) {
        this.configurationManager = configurationManager;
    }

    @Override
    public Cache getCache(String cacheName) {
        Cache cache = caches.get(cacheName);
        if (cache == null) {
            if (logger == null) {
                configureLogger();
            }
            try {
                cache = new CacheWrapper(cacheName, configurationManager.getCacheConfiguration(cacheName));
                caches.put(cacheName, cache);
            } catch (Exception ex) {
                Logger.getLogger(CacheManagerWrapper.class.getName()).log(Level.SEVERE, ex.getMessage());
            }
        }
        return cache;
    }

    @Override
    public Collection getCacheNames() {
        return caches.keySet();
    }

    @Override
    protected void finalize() throws Throwable {

        super.finalize();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy