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

com.alachisoft.ncache.spring.NCacheCacheManager Maven / Gradle / Ivy

/*
 * 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.ncache.spring;

import com.alachisoft.ncache.spring.configuration.SpringConfigurationManager;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
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 NCacheCacheManager implements CacheManager {

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

    public NCacheCacheManager() {
    }

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

    private void configureLogger() {
            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");
                } else {
                    logFilePath = logFilePath + File.separator + "log-files";
                }
            }

            logFilePath = logFilePath + File.separator + "springCacheLogs";
            if (!(new File(logFilePath)).exists()) {
                (new File(logFilePath)).mkdirs();
            }

            DateFormat df = new SimpleDateFormat("dd-MM-yy-HH-mm-ss");
            Date dateobj = new Date();
            String localHostAddress ="";
            try{
                localHostAddress = InetAddress.getLocalHost().getHostAddress();
                FileHandler fileTxt = new FileHandler(logFilePath + File.separator + "ncache-spring_" + df.format(dateobj)+
                        "_"+localHostAddress + ".txt");
                SimpleFormatter formatterTxt = new SimpleFormatter();
                fileTxt.setFormatter(formatterTxt);
                logger.addHandler(fileTxt);

        } catch (SecurityException ex) {
        } catch (IOException 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 NCacheCache(cacheName, configurationManager.getCacheConfiguration(cacheName), logger);
                caches.put(cacheName, cache);

                logger.info(cacheName + " is initialized successfully!");
            } catch (Exception ex) {
                logger.log(Level.SEVERE, ex.getMessage());
            }
        }
        return cache;
    }

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

    @Override
    protected void finalize() throws Throwable {

        super.finalize();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy