Skip to content

Commit

Permalink
Merge commit '039c002437991e3a6a837bcf7bd309c21a92b8cf' into issue-2796
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisdennis committed Dec 14, 2022
2 parents 2087d6f + 039c002 commit 02256ed
Show file tree
Hide file tree
Showing 935 changed files with 36,037 additions and 27,192 deletions.
21 changes: 10 additions & 11 deletions 107/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,22 @@ sourceSets {
}

dependencies {
compile project(':impl'), project(':xml')
provided "javax.cache:cache-api:$parent.jcacheVersion"
tckTestRuntime 'javax.cache:cache-tests:1.0.1'
tckTestClasses('javax.cache:cache-tests:1.0.1:tests') {
api project(':api')
providedApi "javax.cache:cache-api:$parent.jcacheVersion"

implementation project(':impl')
implementation project(':xml')
implementation "org.terracotta:statistics:$parent.statisticVersion"

tckTestRuntime "javax.cache:cache-tests:$jcacheTckVersion"
tckTestClasses("javax.cache:cache-tests:$jcacheTckVersion:tests") {
transitive = false
}
}

tasks.withType(JavaCompile) {
options.compilerArgs += ['-Werror']
testCompile project(path: ':xml', configuration: 'testArchives')
}

javadoc {
exclude '**/tck/**'
classpath = sourceSets.main.compileClasspath + sourceSets.main.runtimeClasspath + configurations.provided
}

test {
Expand Down Expand Up @@ -78,5 +79,3 @@ task tckTest(type: Test, dependsOn: unpackTckTests) {
}

test.dependsOn tckTest


42 changes: 19 additions & 23 deletions 107/src/main/java/org/ehcache/jsr107/CacheResources.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,18 @@
*/
package org.ehcache.jsr107;

import java.io.Closeable;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;

import javax.cache.CacheException;
import javax.cache.configuration.CacheEntryListenerConfiguration;

import org.ehcache.jsr107.internal.Jsr107CacheLoaderWriter;

import static org.ehcache.jsr107.CloseUtil.closeAllAfter;

/**
* @author teck
*/
Expand Down Expand Up @@ -66,9 +68,7 @@ synchronized ListenerResources<K, V> registerCacheEntryListener(CacheEntryListen
throw new IllegalArgumentException("listener config already registered");
}

MultiCacheException mce = new MultiCacheException();
ListenerResources<K, V> rv = ListenerResources.createListenerResources(listenerConfig, mce);
mce.throwIfNotEmpty();
ListenerResources<K, V> rv = ListenerResources.createListenerResources(listenerConfig);
listenerResources.put(listenerConfig, rv);
return rv;
}
Expand All @@ -86,33 +86,29 @@ synchronized ListenerResources<K, V> deregisterCacheEntryListener(CacheEntryList
if (resources == null) {
return null;
}
MultiCacheException mce = new MultiCacheException();
close(resources, mce);
mce.throwIfNotEmpty();
try {
CloseUtil.closeAll(resources);
} catch (Throwable t) {
throw new CacheException(t);
}
return resources;
}

synchronized void closeResources(MultiCacheException mce) {
synchronized void closeResources() {
if (closed.compareAndSet(false, true)) {
close(expiryPolicy, mce);
close(cacheLoaderWriter, mce);
for (ListenerResources<K, V> lr : listenerResources.values()) {
close(lr, mce);
try {
CloseUtil.closeAll(expiryPolicy, cacheLoaderWriter, listenerResources.values());
} catch (Throwable t) {
throw new CacheException(t);
}
}
}

boolean isClosed() {
return closed.get();
}

static void close(Object obj, MultiCacheException mce) {
if (obj instanceof Closeable) {
try {
((Closeable) obj).close();
} catch (Throwable t) {
mce.addThrowable(t);
}
synchronized CacheException closeResourcesAfter(CacheException exception) {
if (closed.compareAndSet(false, true)) {
return closeAllAfter(exception, expiryPolicy, cacheLoaderWriter, listenerResources.values());
} else {
return exception;
}
}
}
86 changes: 86 additions & 0 deletions 107/src/main/java/org/ehcache/jsr107/CloseUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright Terracotta, Inc.
*
* Licensed 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.ehcache.jsr107;

import java.io.Closeable;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Optional;
import java.util.stream.Stream;

public class CloseUtil {
public static <T extends Throwable> T closeAllAfter(T failure, Object ... objects) {
Optional<Closeable> closeable = extractCloseables(Stream.of(objects)).reduce(CloseUtil::composeCloseables);
if (closeable.isPresent()) {
try {
closeable.get().close();
} catch (Throwable t) {
failure.addSuppressed(t);
}
}
return failure;
}

static void closeAll(Object ... objects) throws IOException {
closeAll(Stream.of(objects));
}

static void closeAll(Stream<Object> objects) throws IOException {
chain(extractCloseables(objects));
}

static void chain(Closeable ... objects) throws IOException {
chain(Stream.of(objects));
}

public static void chain(Stream<Closeable> objects) throws IOException {
Optional<Closeable> closeable = objects.reduce(CloseUtil::composeCloseables);
if (closeable.isPresent()) {
closeable.get().close();
}
}

private static Stream<Closeable> extractCloseables(Stream<Object> objects) {
return objects.filter(o -> o != null).flatMap(o -> {
if (o instanceof Collection<?>) {
return ((Collection<?>) o).stream();
} else if (o.getClass().isArray()) {
return Arrays.stream((Object[]) o);
} else {
return Stream.of(o);
}
}).filter(o -> o != null).filter(Closeable.class::isInstance).map(Closeable.class::cast);
}

private static Closeable composeCloseables(Closeable a, Closeable b) {
return () -> {
try {
a.close();
} catch (Throwable t1) {
try {
b.close();
} catch (Throwable t2) {
t1.addSuppressed(t2);
}
throw t1;
}
b.close();
};
}

}
47 changes: 18 additions & 29 deletions 107/src/main/java/org/ehcache/jsr107/ConfigurationMerger.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
import org.ehcache.impl.copy.SerializingCopier;
import org.ehcache.jsr107.config.ConfigurationElementState;
import org.ehcache.jsr107.config.Jsr107CacheConfiguration;
import org.ehcache.jsr107.config.Jsr107Service;
import org.ehcache.jsr107.internal.Jsr107CacheLoaderWriter;
import org.ehcache.spi.copy.Copier;
import org.ehcache.spi.loaderwriter.CacheLoaderWriterConfiguration;
import org.ehcache.xml.XmlConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -39,6 +39,7 @@
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import javax.cache.CacheException;
import javax.cache.configuration.CacheEntryListenerConfiguration;
import javax.cache.configuration.CompleteConfiguration;
import javax.cache.configuration.Configuration;
Expand All @@ -49,6 +50,7 @@
import static org.ehcache.config.builders.CacheConfigurationBuilder.newCacheConfigurationBuilder;
import static org.ehcache.config.builders.ResourcePoolsBuilder.heap;
import static org.ehcache.core.spi.service.ServiceUtils.findSingletonAmongst;
import static org.ehcache.jsr107.CloseUtil.closeAllAfter;

/**
* ConfigurationMerger
Expand Down Expand Up @@ -81,7 +83,7 @@ <K, V> ConfigHolder<K, V> mergeConfigurations(String cacheName, Configuration<K,

String templateName = jsr107Service.getTemplateNameForCache(cacheName);
if (xmlConfiguration != null && templateName != null) {
CacheConfigurationBuilder<K, V> templateBuilder = null;
CacheConfigurationBuilder<K, V> templateBuilder;
try {
templateBuilder = xmlConfiguration.newCacheConfigurationBuilderFromTemplate(templateName,
jsr107Configuration.getKeyType(), jsr107Configuration.getValueType());
Expand All @@ -106,11 +108,11 @@ <K, V> ConfigHolder<K, V> mergeConfigurations(String cacheName, Configuration<K,
}

boolean useEhcacheLoaderWriter;
DefaultCacheLoaderWriterConfiguration ehcacheLoaderWriterConfiguration = builder.getExistingServiceConfiguration(DefaultCacheLoaderWriterConfiguration.class);
CacheLoaderWriterConfiguration ehcacheLoaderWriterConfiguration = builder.getExistingServiceConfiguration(DefaultCacheLoaderWriterConfiguration.class);
if (ehcacheLoaderWriterConfiguration == null) {
useEhcacheLoaderWriter = false;
// No template loader/writer - let's activate the JSR-107 one if any
loaderWriter = initCacheLoaderWriter(jsr107Configuration, new MultiCacheException());
loaderWriter = initCacheLoaderWriter(jsr107Configuration);
if (loaderWriter != null && (jsr107Configuration.isReadThrough() || jsr107Configuration.isWriteThrough())) {
cacheLoaderWriterFactory.registerJsr107Loader(cacheName, loaderWriter);
}
Expand All @@ -127,37 +129,29 @@ <K, V> ConfigHolder<K, V> mergeConfigurations(String cacheName, Configuration<K,
setupManagementAndStatsInternal(jsr107Configuration, findSingletonAmongst(Jsr107CacheConfiguration.class, cacheConfiguration.getServiceConfigurations()));

if (hasConfiguredExpiry) {
expiryPolicy = new EhcacheExpiryWrapper<>(cacheConfiguration.getExpiry());
expiryPolicy = new EhcacheExpiryWrapper<>(cacheConfiguration.getExpiryPolicy());
}

return new ConfigHolder<>(
new CacheResources<>(cacheName, loaderWriter, expiryPolicy, initCacheEventListeners(jsr107Configuration)),
new Eh107CompleteConfiguration<>(jsr107Configuration, cacheConfiguration, hasConfiguredExpiry, useEhcacheLoaderWriter),
cacheConfiguration, useEhcacheLoaderWriter);
} catch (Throwable throwable) {
MultiCacheException mce = new MultiCacheException();
CacheResources.close(expiryPolicy, mce);
CacheResources.close(loaderWriter, mce);

if (throwable instanceof IllegalArgumentException) {
String message = throwable.getMessage();
if (mce.getMessage() != null) {
message = message + "\nSuppressed " + mce.getMessage();
}
throw new IllegalArgumentException(message, throwable);
throw closeAllAfter((IllegalArgumentException) throwable, expiryPolicy, loaderWriter);
} else {
throw closeAllAfter(new CacheException(throwable), expiryPolicy, loaderWriter);
}
mce.addFirstThrowable(throwable);
throw mce;
}
}

private <K, V> CacheConfigurationBuilder<K, V> handleStoreByValue(Eh107CompleteConfiguration<K, V> jsr107Configuration, CacheConfigurationBuilder<K, V> builder, String cacheName) {
DefaultCopierConfiguration copierConfig = builder.getExistingServiceConfiguration(DefaultCopierConfiguration.class);
DefaultCopierConfiguration<?> copierConfig = builder.getExistingServiceConfiguration(DefaultCopierConfiguration.class);
if(copierConfig == null) {
if(jsr107Configuration.isStoreByValue()) {
if (xmlConfiguration != null) {
DefaultCopyProviderConfiguration defaultCopyProviderConfiguration = findSingletonAmongst(DefaultCopyProviderConfiguration.class,
xmlConfiguration.getServiceCreationConfigurations().toArray());
xmlConfiguration.getServiceCreationConfigurations());
if (defaultCopyProviderConfiguration != null) {
Map<Class<?>, ClassInstanceConfiguration<Copier<?>>> defaults = defaultCopyProviderConfiguration.getDefaults();
handleCopierDefaultsforImmutableTypes(defaults);
Expand Down Expand Up @@ -188,8 +182,8 @@ private <K, V> CacheConfigurationBuilder<K, V> handleStoreByValue(Eh107CompleteC
}

@SuppressWarnings("unchecked")
private static <K, V> CacheConfigurationBuilder<K, V> addDefaultCopiers(CacheConfigurationBuilder<K, V> builder, Class keyType, Class valueType ) {
Set<Class> immutableTypes = new HashSet<>();
private static <K, V> CacheConfigurationBuilder<K, V> addDefaultCopiers(CacheConfigurationBuilder<K, V> builder, Class<K> keyType, Class<V> valueType ) {
Set<Class<?>> immutableTypes = new HashSet<>();
immutableTypes.add(String.class);
immutableTypes.add(Long.class);
immutableTypes.add(Float.class);
Expand Down Expand Up @@ -219,7 +213,7 @@ private static void handleCopierDefaultsforImmutableTypes(Map<Class<?>, ClassIns
addIdentityCopierIfNoneRegistered(defaults, Character.class);
}

@SuppressWarnings("unchecked")
@SuppressWarnings({"rawtypes", "unchecked"})
private static void addIdentityCopierIfNoneRegistered(Map<Class<?>, ClassInstanceConfiguration<Copier<?>>> defaults, Class<?> clazz) {
if (!defaults.containsKey(clazz)) {
defaults.put(clazz, new DefaultCopierConfiguration(Eh107IdentityCopier.class, DefaultCopierConfiguration.Type.VALUE));
Expand All @@ -228,9 +222,8 @@ private static void addIdentityCopierIfNoneRegistered(Map<Class<?>, ClassInstanc

private <K, V> Map<CacheEntryListenerConfiguration<K, V>, ListenerResources<K, V>> initCacheEventListeners(CompleteConfiguration<K, V> config) {
Map<CacheEntryListenerConfiguration<K, V>, ListenerResources<K, V>> listenerResources = new ConcurrentHashMap<>();
MultiCacheException mce = new MultiCacheException();
for (CacheEntryListenerConfiguration<K, V> listenerConfig : config.getCacheEntryListenerConfigurations()) {
listenerResources.put(listenerConfig, ListenerResources.createListenerResources(listenerConfig, mce));
listenerResources.put(listenerConfig, ListenerResources.createListenerResources(listenerConfig));
}
return listenerResources;
}
Expand All @@ -239,7 +232,7 @@ private <K, V> Eh107Expiry<K, V> initExpiryPolicy(CompleteConfiguration<K, V> co
return new ExpiryPolicyToEhcacheExpiry<>(config.getExpiryPolicyFactory().create());
}

private <K, V> Jsr107CacheLoaderWriter<K, V> initCacheLoaderWriter(CompleteConfiguration<K, V> config, MultiCacheException mce) {
private <K, V> Jsr107CacheLoaderWriter<K, V> initCacheLoaderWriter(CompleteConfiguration<K, V> config) {
Factory<CacheLoader<K, V>> cacheLoaderFactory = config.getCacheLoaderFactory();
@SuppressWarnings("unchecked")
Factory<CacheWriter<K, V>> cacheWriterFactory = (Factory<CacheWriter<K, V>>) (Object) config.getCacheWriterFactory();
Expand All @@ -256,11 +249,7 @@ private <K, V> Jsr107CacheLoaderWriter<K, V> initCacheLoaderWriter(CompleteConfi
try {
cacheWriter = cacheWriterFactory == null ? null : cacheWriterFactory.create();
} catch (Throwable t) {
if (t != mce) {
mce.addThrowable(t);
}
CacheResources.close(cacheLoader, mce);
throw mce;
throw closeAllAfter(new CacheException(t), cacheLoader);
}

if (cacheLoader == null && cacheWriter == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ class DefaultConfigurationResolver {
static final String DEFAULT_CONFIG_PROPERTY_NAME = "ehcache.jsr107.config.default";

static URI resolveConfigURI(Properties cacheManagerProperties) {
Object config = null;

config = cacheManagerProperties.get(DEFAULT_CONFIG_PROPERTY_NAME);
Object config = cacheManagerProperties.get(DEFAULT_CONFIG_PROPERTY_NAME);

if (config == null) {
config = System.getProperties().get(DEFAULT_CONFIG_PROPERTY_NAME);
Expand Down
Loading

0 comments on commit 02256ed

Please sign in to comment.