abstractcacheadministrator.java

来自「一个不错的cache」· Java 代码 · 共 418 行 · 第 1/2 页

JAVA
418
字号
     * Retrieves the value of one of the configuration properties.     *     * @param key The key assigned to the property     * @return Property value, or <code>null</code> if the property could not be found.     */    public String getProperty(String key) {        return config.getProperty(key);    }    /**     * Indicates whether the unlimited disk cache is enabled or not.     */    public boolean isUnlimitedDiskCache() {        return unlimitedDiskCache;    }    /**     * Check if we use overflowPersistence     *     * @return Returns the overflowPersistence.     */    public boolean isOverflowPersistence() {        return this.overflowPersistence;    }    /**     * Sets the overflowPersistence flag     *     * @param overflowPersistence The overflowPersistence to set.     */    public void setOverflowPersistence(boolean overflowPersistence) {        this.overflowPersistence = overflowPersistence;    }    /**     * Retrieves an array containing instances all of the {@link CacheEventListener}     * classes that are specified in the OSCache configuration file.     */    protected CacheEventListener[] getCacheEventListeners() {        CacheEventListener[] listeners = null;        List classes = StringUtil.split(config.getProperty(CACHE_ENTRY_EVENT_LISTENERS_KEY), ',');        listeners = new CacheEventListener[classes.size()];        for (int i = 0; i < classes.size(); i++) {            String className = (String) classes.get(i);            try {                Class clazz = Class.forName(className);                if (!CacheEventListener.class.isAssignableFrom(clazz)) {                    log.error("Specified listener class '" + className + "' does not implement CacheEventListener. Ignoring this listener.");                } else {                    listeners[i] = (CacheEventListener) clazz.newInstance();                }            } catch (ClassNotFoundException e) {                log.error("CacheEventListener class '" + className + "' not found. Ignoring this listener.", e);            } catch (InstantiationException e) {                log.error("CacheEventListener class '" + className + "' could not be instantiated because it is not a concrete class. Ignoring this listener.", e);            } catch (IllegalAccessException e) {                log.error("CacheEventListener class '" + className + "' could not be instantiated because it is not public. Ignoring this listener.", e);            }        }        return listeners;    }    /**     * If there is a <code>PersistenceListener</code> in the configuration     * it will be instantiated and applied to the given cache object. If the     * <code>PersistenceListener</code> cannot be found or instantiated, an     * error will be logged but the cache will not have a persistence listener     * applied to it and no exception will be thrown.<p>     *     * A cache can only have one <code>PersistenceListener</code>.     *     * @param cache the cache to apply the <code>PersistenceListener</code> to.     *     * @return the same cache object that was passed in.     */    protected Cache setPersistenceListener(Cache cache) {        String persistenceClassname = config.getProperty(PERSISTENCE_CLASS_KEY);        try {            Class clazz = Class.forName(persistenceClassname);            PersistenceListener persistenceListener = (PersistenceListener) clazz.newInstance();            cache.setPersistenceListener(persistenceListener.configure(config));        } catch (ClassNotFoundException e) {            log.error("PersistenceListener class '" + persistenceClassname + "' not found. Check your configuration.", e);        } catch (Exception e) {            log.error("Error instantiating class '" + persistenceClassname + "'", e);        }        return cache;    }    /**     * Applies all of the recognised listener classes to the supplied     * cache object. Recognised classes are {@link CacheEntryEventListener}     * and {@link CacheMapAccessEventListener}.<p>     *     * @param cache The cache to apply the configuration to.     * @return cache The configured cache object.     */    protected Cache configureStandardListeners(Cache cache) {        if (config.getProperty(PERSISTENCE_CLASS_KEY) != null) {            cache = setPersistenceListener(cache);        }        if (config.getProperty(CACHE_ENTRY_EVENT_LISTENERS_KEY) != null) {            // Grab all the specified listeners and add them to the cache's            // listener list. Note that listeners that implement more than            // one of the event interfaces will be added multiple times.            CacheEventListener[] listeners = getCacheEventListeners();            for (int i = 0; i < listeners.length; i++) {                // Pass through the configuration to those listeners that require it                if (listeners[i] instanceof LifecycleAware) {                    try {                        ((LifecycleAware) listeners[i]).initialize(cache, config);                    } catch (InitializationException e) {                        log.error("Could not initialize listener '" + listeners[i].getClass().getName() + "'. Listener ignored.", e);                        continue;                    }                }                if (listeners[i] instanceof CacheEntryEventListener) {                    cache.addCacheEventListener(listeners[i], CacheEntryEventListener.class);                }                if (listeners[i] instanceof CacheMapAccessEventListener) {                    cache.addCacheEventListener(listeners[i], CacheMapAccessEventListener.class);                }            }        }        return cache;    }    /**     * Finalizes all the listeners that are associated with the given cache object.     * Any <code>FinalizationException</code>s that are thrown by the listeners will     * be caught and logged.     */    protected void finalizeListeners(Cache cache) {        // It's possible for cache to be null if getCache() was never called (CACHE-63)        if (cache == null) {            return;        }        Object[] listeners = cache.listenerList.getListenerList();        for (int i = listeners.length - 2; i >= 0; i -= 2) {            if (listeners[i + 1] instanceof LifecycleAware) {                try {                    ((LifecycleAware) listeners[i + 1]).finialize();                } catch (FinalizationException e) {                    log.error("Listener could not be finalized", e);                }            }        }    }    /**     * Initialize the core cache parameters from the configuration properties.     * The parameters that are initialized are:     * <ul>     * <li>the algorithm class ({@link #CACHE_ALGORITHM_KEY})</li>     * <li>the cache size ({@link #CACHE_CAPACITY_KEY})</li>     * <li>whether the cache is blocking or non-blocking ({@link #CACHE_BLOCKING_KEY})</li>     * <li>whether caching to memory is enabled ({@link #CACHE_MEMORY_KEY})</li>     * <li>whether the persistent cache is unlimited in size ({@link #CACHE_DISK_UNLIMITED_KEY})</li>     * </ul>     */    private void initCacheParameters() {        algorithmClass = getProperty(CACHE_ALGORITHM_KEY);        blocking = "true".equalsIgnoreCase(getProperty(CACHE_BLOCKING_KEY));        String cacheMemoryStr = getProperty(CACHE_MEMORY_KEY);        if ((cacheMemoryStr != null) && cacheMemoryStr.equalsIgnoreCase("false")) {            memoryCaching = false;        }        unlimitedDiskCache = Boolean.valueOf(config.getProperty(CACHE_DISK_UNLIMITED_KEY)).booleanValue();        overflowPersistence = Boolean.valueOf(config.getProperty(CACHE_PERSISTENCE_OVERFLOW_KEY)).booleanValue();        String cacheSize = getProperty(CACHE_CAPACITY_KEY);        try {            if ((cacheSize != null) && (cacheSize.length() > 0)) {                cacheCapacity = Integer.parseInt(cacheSize);            }        } catch (NumberFormatException e) {            log.error("The value supplied for the cache capacity, '" + cacheSize + "', is not a valid number. The cache capacity setting is being ignored.");        }    }    /**     * Load the properties file from the classpath.     */    private void loadProps(Properties p) {        config = new Config(p);    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?