Skip to content

Commit

Permalink
Improve errors when #cache is used but can't work
Browse files Browse the repository at this point in the history
  • Loading branch information
geoand committed Sep 8, 2023
1 parent 8ffe29d commit 9298ac6
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,35 @@
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.qute.cache.QuteCache;
import io.quarkus.qute.runtime.cache.CacheConfigurator;
import io.quarkus.qute.runtime.cache.MissingCacheConfigurator;
import io.quarkus.qute.runtime.cache.UnsupportedRemoteCacheConfigurator;

public class CacheProcessor {

@BuildStep
void initialize(Optional<CacheTypeBuildItem> cacheTypeBuildItem,
BuildProducer<AdditionalBeanBuildItem> beans,
BuildProducer<AdditionalCacheNameBuildItem> cacheNames) {
Class configuratorClass;
boolean supported = false;
if (cacheTypeBuildItem.isEmpty()) { // no caching enabled
return;
configuratorClass = MissingCacheConfigurator.class;
} else {
CacheTypeBuildItem.Type type = cacheTypeBuildItem.get().getType();
if (type != CacheTypeBuildItem.Type.LOCAL) { // it does not make sense to use a remote cache for Qute
configuratorClass = UnsupportedRemoteCacheConfigurator.class;
} else {
configuratorClass = CacheConfigurator.class;
supported = true;
}
}
CacheTypeBuildItem.Type type = cacheTypeBuildItem.get().getType();
if (type != CacheTypeBuildItem.Type.LOCAL) { // it does not make sense to use a remote cache for Qute
return;
}
beans.produce(new AdditionalBeanBuildItem("io.quarkus.qute.runtime.cache.CacheConfigurator"));

beans.produce(new AdditionalBeanBuildItem(configuratorClass.getName()));
// We need to produce additional cache name because quarkus-cache only considers the CombinedIndexBuildItem and not the bean archive index
cacheNames.produce(new AdditionalCacheNameBuildItem(QuteCache.NAME));
if (supported) {
cacheNames.produce(new AdditionalCacheNameBuildItem(QuteCache.NAME));
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.quarkus.qute.runtime.cache;

import java.util.concurrent.CompletionStage;
import java.util.function.Function;

import jakarta.enterprise.event.Observes;

import io.quarkus.qute.CacheSectionHelper;
import io.quarkus.qute.EngineBuilder;
import io.quarkus.qute.ResultNode;

public class MissingCacheConfigurator {

void configureEngine(@Observes EngineBuilder builder) {
builder.addSectionHelper(new CacheSectionHelper.Factory(new CacheSectionHelper.Cache() {

@Override
public CompletionStage<ResultNode> getValue(String key, Function<String, CompletionStage<ResultNode>> loader) {
throw new IllegalStateException("#cache cannot be used without the 'quarkus-cache' extension");
}
}));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.quarkus.qute.runtime.cache;

import java.util.concurrent.CompletionStage;
import java.util.function.Function;

import jakarta.enterprise.event.Observes;

import io.quarkus.qute.CacheSectionHelper;
import io.quarkus.qute.EngineBuilder;
import io.quarkus.qute.ResultNode;

public class UnsupportedRemoteCacheConfigurator {

void configureEngine(@Observes EngineBuilder builder) {
builder.addSectionHelper(new CacheSectionHelper.Factory(new CacheSectionHelper.Cache() {

@Override
public CompletionStage<ResultNode> getValue(String key, Function<String, CompletionStage<ResultNode>> loader) {
throw new IllegalStateException("#cache is not supported for remote caches");
}
}));
}

}

0 comments on commit 9298ac6

Please sign in to comment.