Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanseifert committed Apr 19, 2023
2 parents 516bd2b + e77c1c0 commit 4915fba
Show file tree
Hide file tree
Showing 12 changed files with 407 additions and 21 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ npm-debug.log
.vlt
.vlt-sync*
.brackets.json
dependency-reduced-pom.xml
6 changes: 6 additions & 0 deletions changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
xsi:schemaLocation="http://maven.apache.org/changes/1.0.0 http://maven.apache.org/plugins/maven-changes-plugin/xsd/changes-1.0.0.xsd">
<body>

<release version="1.15.6" date="2023-04-19">
<action type="update" dev="sseifert" issue="19">
Eliminate dependency to Guava. Embed Caffeine as replacement for Guava Cache.
</action>
</release>

<release version="1.15.4" date="2023-02-24">
<action type="update" dev="sseifert">
Switch to Java 11 as minimum version.
Expand Down
53 changes: 49 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

<groupId>io.wcm</groupId>
<artifactId>io.wcm.handler.media</artifactId>
<version>1.15.4</version>
<version>1.15.6</version>
<packaging>jar</packaging>

<name>Media Handler</name>
Expand All @@ -49,7 +49,7 @@
<site.url.module.prefix>handler/media</site.url.module.prefix>

<!-- Enable reproducible builds -->
<project.build.outputTimestamp>2023-02-24T15:01:16Z</project.build.outputTimestamp>
<project.build.outputTimestamp>2023-04-19T11:15:03Z</project.build.outputTimestamp>
</properties>

<dependencies>
Expand Down Expand Up @@ -98,6 +98,13 @@
<scope>compile</scope>
</dependency>

<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
<version>3.1.5</version>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.testing.hamcrest</artifactId>
Expand Down Expand Up @@ -131,7 +138,7 @@
<dependency>
<groupId>io.wcm</groupId>
<artifactId>io.wcm.testing.aem-mock.junit5</artifactId>
<version>5.1.2</version>
<version>5.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
Expand All @@ -149,7 +156,7 @@
<dependency>
<groupId>io.wcm</groupId>
<artifactId>io.wcm.testing.wcm-io-mock.sling</artifactId>
<version>1.1.0</version>
<version>1.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down Expand Up @@ -236,6 +243,8 @@
javax.annotation;version="[0.0,2)",\
<!-- Package version change between AEM 6.2 and AEM 6.3 -->\
com.day.cq.dam.api.handler;version="[1.0,3)",\
<!-- Caffeine is embedded -->\
!com.github.benmanes.caffeine.*,\
*
</bnd>
</configuration>
Expand All @@ -254,6 +263,42 @@
</configuration>
</plugin>

<!-- Embed shaded version of Caffeine to avoid classpath issues in unit tests -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<configuration>
<artifactSet>
<includes>
<include>com.github.ben-manes.caffeine:caffeine</include>
</includes>
</artifactSet>
<relocations>
<relocation>
<pattern>com.github.benmanes.caffeine</pattern>
<shadedPattern>io.wcm.handler.media.shaded.com.github.benmanes.caffeine</shadedPattern>
</relocation>
</relocations>
<filters>
<filter>
<artifact>com.github.ben-manes.caffeine:caffeine</artifact>
<excludes>
<exclude>module-info.class</exclude>
<exclude>META-INF/**</exclude>
</excludes>
</filter>
</filters>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>

<plugin>
<groupId>io.wcm.maven.plugins</groupId>
<artifactId>i18n-maven-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

Expand All @@ -41,8 +40,8 @@
import org.osgi.service.component.annotations.ReferencePolicy;
import org.osgi.service.component.annotations.ReferencePolicyOption;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;

import io.wcm.handler.media.format.MediaFormat;
import io.wcm.handler.media.format.MediaFormatProviderManager;
Expand All @@ -67,7 +66,7 @@ public final class MediaFormatProviderManagerImpl implements MediaFormatProvider
private BundleContext bundleContext;

// cache resolving of media formats per combined cache key of context-aware services
private final Cache<String, SortedSet<MediaFormat>> cache = CacheBuilder.newBuilder()
private final Cache<String, SortedSet<MediaFormat>> cache = Caffeine.newBuilder()
.expireAfterWrite(1, TimeUnit.HOURS)
.build();

Expand All @@ -80,14 +79,9 @@ private void activate(BundleContext bc) {
public SortedSet<MediaFormat> getMediaFormats(Resource contextResource) {
ResolveAllResult<MediaFormatProvider> result = serviceResolver.resolveAll(MediaFormatProvider.class, contextResource);
String key = result.getCombinedKey();
try {
return cache.get(key, () -> result.getServices()
.flatMap(provider -> provider.getMediaFormats().stream())
.collect(Collectors.toCollection(TreeSet::new)));
}
catch (ExecutionException ex) {
throw new RuntimeException("Error accessing media format provider result cache.", ex);
}
return cache.get(key, theKey -> result.getServices()
.flatMap(provider -> provider.getMediaFormats().stream())
.collect(Collectors.toCollection(TreeSet::new)));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.util.concurrent.Striped;
import io.wcm.handler.mediasource.dam.impl.metadata.concurrency.StripedLazyWeakLock;

/**
* Synchronized the generation of rendition metadata through the ways (metadata service, workflow process)
Expand All @@ -43,11 +43,11 @@ public final class AssetSynchonizationService {

private static final Logger log = LoggerFactory.getLogger(AssetSynchonizationService.class);

private Striped<Lock> lazyWeakLock;
private StripedLazyWeakLock lazyWeakLock;

@Activate
private void activate() {
lazyWeakLock = Striped.lazyWeakLock(STRIPE_COUNT);
lazyWeakLock = new StripedLazyWeakLock(STRIPE_COUNT);
}

@Deactivate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@
import com.day.cq.dam.api.DamEvent;
import com.day.cq.dam.api.DamEvent.Type;
import com.day.cq.dam.api.handler.store.AssetStore;
import com.google.common.util.concurrent.ThreadFactoryBuilder;

import io.wcm.handler.media.MediaFileType;
import io.wcm.handler.mediasource.dam.impl.metadata.concurrency.NamedThreadFactory;
import io.wcm.wcm.commons.instancetype.InstanceTypeService;
import io.wcm.wcm.commons.util.RunMode;

Expand Down Expand Up @@ -130,7 +130,7 @@ private void activate(ComponentContext componentContext, Config config) {
this.synchronousProcessing = config.threadPoolSize() <= 0;
if (this.enabled && !this.synchronousProcessing) {
this.executorService = Executors.newScheduledThreadPool(config.threadPoolSize(),
new ThreadFactoryBuilder().setNameFormat(getClass().getSimpleName() + "-%d").build());
new NamedThreadFactory(getClass().getSimpleName()));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* #%L
* wcm.io
* %%
* Copyright (C) 2023 wcm.io
* %%
* 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.
* #L%
*/
package io.wcm.handler.mediasource.dam.impl.metadata.concurrency;

import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicLong;

/**
* Creates new threads with a given formatted name, including a counter that is incremented for each new thread.
*/
public final class NamedThreadFactory implements ThreadFactory {

private final String namePrefix;
private final AtomicLong counter = new AtomicLong();

/**
* @param namePrefix Prefix for thread name, will be suffixed with "-{number}".
*/
public NamedThreadFactory(String namePrefix) {
this.namePrefix = namePrefix;
}

@Override
public Thread newThread(Runnable r) {
Thread thread = Executors.defaultThreadFactory().newThread(r);
thread.setName(namePrefix + "-" + counter.getAndIncrement());
return thread;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* #%L
* wcm.io
* %%
* Copyright (C) 2023 wcm.io
* %%
* 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.
* #L%
*/
package io.wcm.handler.mediasource.dam.impl.metadata.concurrency;

/**
* Maps keys to a striped index set. Each key is mapped to a index within the max stripe count.
* <p>
* The logic is extracted from <a href=
* "https://github.com/google/guava/blob/master/guava/src/com/google/common/util/concurrent/Striped.java">Striped</a>,
* initially written by Dimitris Andreou from the Guava team (Apache 2.0 license).
* </p>
*/
final class StripeIndex {

// Capacity (power of two) minus one, for fast mod evaluation
private final int mask;
private final int size;

// A bit mask were all bits are set.
private static final int ALL_SET = ~0;

// The largest power of two that can be represented as an {@code int}.
private static final int MAX_POWER_OF_TWO = 1 << (Integer.SIZE - 2);

/**
* @param stripes the minimum number of stripes required
*/
StripeIndex(int stripes) {
if (stripes <= 0) {
throw new IllegalArgumentException("Invalid number of stripes: " + stripes);
}
this.mask = stripes > MAX_POWER_OF_TWO ? ALL_SET : ceilToPowerOfTwo(stripes) - 1;
this.size = (mask == ALL_SET) ? Integer.MAX_VALUE : mask + 1;
}

/** Returns the total number of stripes in this instance. */
int size() {
return size;
}

/**
* Returns the index to which the given key is mapped, so that getAt(indexFor(key)) == get(key).
*/
int indexFor(Object key) {
int hash = smear(key.hashCode());
return hash & mask;
}

private static int smear(int hashCode) {
int newHashCode = hashCode;
newHashCode ^= (newHashCode >>> 20) ^ (newHashCode >>> 12);
return newHashCode ^ (newHashCode >>> 7) ^ (newHashCode >>> 4);
}

private static int ceilToPowerOfTwo(int x) {
return 1 << log2RoundCeiling(x);
}

private static int log2RoundCeiling(int x) {
return Integer.SIZE - Integer.numberOfLeadingZeros(x - 1);
}

}
Loading

0 comments on commit 4915fba

Please sign in to comment.