org.apache.commons.jcs.jcache.JCSCachingManager Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.commons.jcs.jcache;
import org.apache.commons.jcs.engine.control.CompositeCacheManager;
import org.apache.commons.jcs.jcache.lang.Subsitutor;
import org.apache.commons.jcs.jcache.proxy.ClassLoaderAwareCache;
import javax.cache.Cache;
import javax.cache.CacheManager;
import javax.cache.configuration.Configuration;
import javax.cache.spi.CachingProvider;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import static org.apache.commons.jcs.jcache.Asserts.assertNotNull;
public class JCSCachingManager implements CacheManager
{
private static final Subsitutor SUBSTITUTOR = Subsitutor.Helper.INSTANCE;
private static class InternalManager extends CompositeCacheManager
{
protected static InternalManager create()
{
return new InternalManager();
}
@Override // needed to call it from JCSCachingManager
protected void initialize() {
super.initialize();
}
}
private final CachingProvider provider;
private final URI uri;
private final ClassLoader loader;
private final Properties properties;
private final ConcurrentMap> caches = new ConcurrentHashMap>();
private final Properties configProperties;
private volatile boolean closed = false;
private InternalManager delegate = InternalManager.create();
public JCSCachingManager(final CachingProvider provider, final URI uri, final ClassLoader loader, final Properties properties)
{
this.provider = provider;
this.uri = uri;
this.loader = loader;
this.properties = readConfig(uri, loader, properties);
this.configProperties = properties;
delegate.setJmxName(CompositeCacheManager.JMX_OBJECT_NAME
+ ",provider=" + provider.hashCode()
+ ",uri=" + uri.toString().replaceAll(",|:|=|\n", ".")
+ ",classloader=" + loader.hashCode()
+ ",properties=" + this.properties.hashCode());
delegate.initialize();
delegate.configure(this.properties);
}
private Properties readConfig(final URI uri, final ClassLoader loader, final Properties properties) {
final Properties props = new Properties();
InputStream inStream = null;
try
{
if (JCSCachingProvider.DEFAULT_URI.toString().equals(uri.toString()) || uri.toURL().getProtocol().equals("jcs"))
{
inStream = loader.getResourceAsStream(uri.getPath());
}
else
{
inStream = uri.toURL().openStream();
}
props.load(inStream);
}
catch (final IOException e)
{
throw new IllegalArgumentException(e);
}
finally
{
if (inStream != null)
{
try
{
inStream.close();
}
catch (final IOException e)
{
// no-op
}
}
}
if (properties != null)
{
props.putAll(properties);
}
for (final Map.Entry
© 2015 - 2025 Weber Informatics LLC | Privacy Policy