This is an automated email from the ASF dual-hosted git repository.
mattsicker pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
The following commit(s) were added to refs/heads/master by this push:
new bbc9e18fab Use injectable plugin lists in JsonTemplateLayout
bbc9e18fab is described below
commit bbc9e18fab9559c8f604ea312b058712e55211f6
Author: Matt Sicker <[email protected]>
AuthorDate: Sun May 15 00:40:58 2022 -0500
Use injectable plugin lists in JsonTemplateLayout
Removes some no longer needed code as well.
Signed-off-by: Matt Sicker <[email protected]>
---
.../layout/template/json/JsonTemplateLayout.java | 25 ++--
.../json/resolver/EventResolverFactories.java | 39 -------
.../json/resolver/EventResolverFactory.java | 10 --
.../json/resolver/EventResolverInterceptor.java | 10 --
.../json/resolver/EventResolverInterceptors.java | 39 -------
.../resolver/StackTraceElementResolverFactory.java | 10 --
.../json/resolver/TemplateResolverFactories.java | 127 ---------------------
.../json/resolver/TemplateResolverFactory.java | 16 ---
.../json/resolver/TemplateResolverInterceptor.java | 16 ---
.../resolver/TemplateResolverInterceptors.java | 113 ------------------
10 files changed, 18 insertions(+), 387 deletions(-)
diff --git
a/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/JsonTemplateLayout.java
b/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/JsonTemplateLayout.java
index ffe037d5f2..4c868bbcb5 100644
---
a/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/JsonTemplateLayout.java
+++
b/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/JsonTemplateLayout.java
@@ -20,7 +20,6 @@ import org.apache.logging.log4j.core.Layout;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.StringLayout;
import org.apache.logging.log4j.core.config.Configuration;
-import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;
import org.apache.logging.log4j.core.config.plugins.PluginConfiguration;
import org.apache.logging.log4j.core.layout.ByteBufferDestination;
import org.apache.logging.log4j.core.layout.Encoder;
@@ -28,10 +27,8 @@ import
org.apache.logging.log4j.core.layout.TextEncoderHelper;
import org.apache.logging.log4j.core.util.Constants;
import org.apache.logging.log4j.core.util.StringEncoder;
import
org.apache.logging.log4j.layout.template.json.resolver.EventResolverContext;
-import
org.apache.logging.log4j.layout.template.json.resolver.EventResolverFactories;
import
org.apache.logging.log4j.layout.template.json.resolver.EventResolverFactory;
import
org.apache.logging.log4j.layout.template.json.resolver.EventResolverInterceptor;
-import
org.apache.logging.log4j.layout.template.json.resolver.EventResolverInterceptors;
import
org.apache.logging.log4j.layout.template.json.resolver.EventResolverStringSubstitutor;
import org.apache.logging.log4j.layout.template.json.resolver.TemplateResolver;
import
org.apache.logging.log4j.layout.template.json.resolver.TemplateResolvers;
@@ -39,10 +36,13 @@ import
org.apache.logging.log4j.layout.template.json.util.JsonWriter;
import org.apache.logging.log4j.layout.template.json.util.Recycler;
import org.apache.logging.log4j.layout.template.json.util.RecyclerFactory;
import org.apache.logging.log4j.layout.template.json.util.Uris;
+import org.apache.logging.log4j.plugins.Category;
+import org.apache.logging.log4j.plugins.Factory;
import org.apache.logging.log4j.plugins.Node;
import org.apache.logging.log4j.plugins.Plugin;
import org.apache.logging.log4j.plugins.PluginBuilderAttribute;
import org.apache.logging.log4j.plugins.PluginElement;
+import org.apache.logging.log4j.plugins.di.Key;
import org.apache.logging.log4j.status.StatusLogger;
import org.apache.logging.log4j.util.Strings;
@@ -52,10 +52,13 @@ import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.CodingErrorAction;
import java.util.Collections;
+import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
+import java.util.function.Function;
import java.util.function.Supplier;
+import java.util.stream.Collectors;
@Plugin(name = "JsonTemplateLayout",
category = Node.CATEGORY,
@@ -121,10 +124,18 @@ public class JsonTemplateLayout implements StringLayout {
final JsonWriter jsonWriter) {
// Inject resolver factory and interceptor plugins.
+ final List<EventResolverFactory> resolverFactories =
+ configuration.getComponent(new
@Category(EventResolverFactory.CATEGORY) Key<>() {});
final Map<String, EventResolverFactory> resolverFactoryByName =
-
EventResolverFactories.populateResolverFactoryByName(configuration);
+ resolverFactories.stream().collect(
+ Collectors.toMap(EventResolverFactory::getName,
Function.identity(), (factory, conflictingFactory) -> {
+ final String message = String.format(
+ "found resolver factories with overlapping
names: %s (%s and %s)",
+ factory.getName(), conflictingFactory,
factory);
+ throw new IllegalArgumentException(message);
+ }, LinkedHashMap::new));
final List<EventResolverInterceptor> resolverInterceptors =
- EventResolverInterceptors.populateInterceptors(configuration);
+ configuration.getComponent(new
@Category(EventResolverInterceptor.CATEGORY) Key<>() {});
final EventResolverStringSubstitutor substitutor =
new
EventResolverStringSubstitutor(configuration.getStrSubstitutor());
@@ -337,7 +348,7 @@ public class JsonTemplateLayout implements StringLayout {
return CONTENT_FORMAT;
}
- @PluginBuilderFactory
+ @Factory
@SuppressWarnings("WeakerAccess")
public static Builder newBuilder() {
return new Builder();
@@ -631,7 +642,7 @@ public class JsonTemplateLayout implements StringLayout {
return String.format("%s=%s", key, formattedValue);
}
- @PluginBuilderFactory
+ @Factory
public static EventTemplateAdditionalField.Builder newBuilder() {
return new EventTemplateAdditionalField.Builder();
}
diff --git
a/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/resolver/EventResolverFactories.java
b/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/resolver/EventResolverFactories.java
deleted file mode 100644
index 35e075cbc1..0000000000
---
a/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/resolver/EventResolverFactories.java
+++ /dev/null
@@ -1,39 +0,0 @@
-/*
- * 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.logging.log4j.layout.template.json.resolver;
-
-import org.apache.logging.log4j.core.LogEvent;
-import org.apache.logging.log4j.core.config.Configuration;
-
-import java.util.Map;
-
-/**
- * Utility class for {@link EventResolverFactory}.
- */
-public final class EventResolverFactories {
-
- private EventResolverFactories() {}
-
- public static Map<String, EventResolverFactory>
populateResolverFactoryByName(
- final Configuration configuration) {
- return TemplateResolverFactories.populateFactoryByName(
- configuration,
- LogEvent.class,
- EventResolverContext.class);
- }
-
-}
diff --git
a/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/resolver/EventResolverFactory.java
b/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/resolver/EventResolverFactory.java
index 2783bfff9e..c804011c3d 100644
---
a/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/resolver/EventResolverFactory.java
+++
b/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/resolver/EventResolverFactory.java
@@ -27,14 +27,4 @@ import org.apache.logging.log4j.core.LogEvent;
public interface EventResolverFactory
extends TemplateResolverFactory<LogEvent, EventResolverContext> {
- @Override
- default Class<LogEvent> getValueClass() {
- return LogEvent.class;
- }
-
- @Override
- default Class<EventResolverContext> getContextClass() {
- return EventResolverContext.class;
- }
-
}
diff --git
a/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/resolver/EventResolverInterceptor.java
b/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/resolver/EventResolverInterceptor.java
index 126ac46cb3..89bbdd5811 100644
---
a/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/resolver/EventResolverInterceptor.java
+++
b/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/resolver/EventResolverInterceptor.java
@@ -24,14 +24,4 @@ import org.apache.logging.log4j.core.LogEvent;
public interface EventResolverInterceptor
extends TemplateResolverInterceptor<LogEvent, EventResolverContext> {
- @Override
- default Class<LogEvent> getValueClass() {
- return LogEvent.class;
- }
-
- @Override
- default Class<EventResolverContext> getContextClass() {
- return EventResolverContext.class;
- }
-
}
diff --git
a/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/resolver/EventResolverInterceptors.java
b/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/resolver/EventResolverInterceptors.java
deleted file mode 100644
index 32c10049ff..0000000000
---
a/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/resolver/EventResolverInterceptors.java
+++ /dev/null
@@ -1,39 +0,0 @@
-/*
- * 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.logging.log4j.layout.template.json.resolver;
-
-import org.apache.logging.log4j.core.LogEvent;
-import org.apache.logging.log4j.core.config.Configuration;
-
-import java.util.List;
-
-/**
- * Utility class for {@link EventResolverInterceptor}.
- */
-public final class EventResolverInterceptors {
-
- private EventResolverInterceptors() {}
-
- public static List<EventResolverInterceptor> populateInterceptors(
- final Configuration configuration) {
- return TemplateResolverInterceptors.populateInterceptors(
- configuration,
- LogEvent.class,
- EventResolverContext.class);
- }
-
-}
diff --git
a/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/resolver/StackTraceElementResolverFactory.java
b/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/resolver/StackTraceElementResolverFactory.java
index 02c96b208d..b50bcbc7b0 100644
---
a/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/resolver/StackTraceElementResolverFactory.java
+++
b/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/resolver/StackTraceElementResolverFactory.java
@@ -31,16 +31,6 @@ final class StackTraceElementResolverFactory
return INSTANCE;
}
- @Override
- public Class<StackTraceElement> getValueClass() {
- return StackTraceElement.class;
- }
-
- @Override
- public Class<StackTraceElementResolverContext> getContextClass() {
- return StackTraceElementResolverContext.class;
- }
-
@Override
public String getName() {
return StackTraceElementResolver.getName();
diff --git
a/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/resolver/TemplateResolverFactories.java
b/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/resolver/TemplateResolverFactories.java
deleted file mode 100644
index 8bd503b358..0000000000
---
a/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/resolver/TemplateResolverFactories.java
+++ /dev/null
@@ -1,127 +0,0 @@
-/*
- * 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.logging.log4j.layout.template.json.resolver;
-
-import org.apache.logging.log4j.Logger;
-import org.apache.logging.log4j.core.config.Configuration;
-import org.apache.logging.log4j.plugins.di.Key;
-import org.apache.logging.log4j.plugins.util.PluginCategory;
-import org.apache.logging.log4j.plugins.util.TypeUtil;
-import org.apache.logging.log4j.status.StatusLogger;
-
-import java.util.LinkedHashMap;
-import java.util.Map;
-
-/**
- * Utility class for {@link TemplateResolverFactory}.
- */
-public final class TemplateResolverFactories {
-
- private static final Logger LOGGER = StatusLogger.getLogger();
-
- private TemplateResolverFactories() {}
-
- /**
- * Populates plugins implementing
- * {@link TemplateResolverFactory TemplateResolverFactory<V, C>},
- * where {@code V} and {@code C} denote the value and context class types,
- * respectively.
- */
- public static <V, C extends TemplateResolverContext<V, C>, F extends
TemplateResolverFactory<V, C>> Map<String, F> populateFactoryByName(
- final Configuration configuration,
- final Class<V> valueClass,
- final Class<C> contextClass) {
-
- // Populate template resolver factories.
- final PluginCategory factoryPlugins =
-
configuration.getComponent(TemplateResolverFactory.PLUGIN_CATEGORY_KEY);
- if (LOGGER.isDebugEnabled()) {
- LOGGER.debug(
- "found {} plugins of category \"{}\": {}",
- factoryPlugins.size(),
- TemplateResolverFactory.CATEGORY,
- factoryPlugins.getPluginKeys());
- }
-
- // Filter matching resolver factories.
- final Map<String, F> factoryByName =
- populateFactoryByName(factoryPlugins, configuration,
valueClass, contextClass);
- if (LOGGER.isDebugEnabled()) {
- LOGGER.debug(
- "matched {} resolver factories out of {} for value class
{} and context class {}: {}",
- factoryByName.size(),
- factoryPlugins.size(),
- valueClass,
- contextClass,
- factoryByName.keySet());
- }
- return factoryByName;
-
- }
-
- private static <V, C extends TemplateResolverContext<V, C>, F extends
TemplateResolverFactory<V, C>> Map<String, F> populateFactoryByName(
- final PluginCategory factoryPlugins,
- final Configuration configuration,
- final Class<V> valueClass,
- final Class<C> contextClass) {
- final Map<String, F> factoryByName = new LinkedHashMap<>();
- factoryPlugins.forEachMatching(
- pluginType ->
pluginType.getImplementedInterfaces().contains(TemplateResolverFactory.class),
- pluginType -> {
- @SuppressWarnings("rawtypes")
- final Class<? extends TemplateResolverFactory>
factoryClass =
-
pluginType.getPluginClass().asSubclass(TemplateResolverFactory.class);
- final TemplateResolverFactory<?, ?> rawFactory =
-
configuration.getComponent(Key.forClass(factoryClass));
- final F factory = castFactory(valueClass, contextClass,
rawFactory);
- if (factory != null) {
- addFactory(factoryByName, factory);
- }
- });
- return factoryByName;
- }
-
- private static <V, C extends TemplateResolverContext<V, C>, F extends
TemplateResolverFactory<V, C>> F castFactory(
- final Class<V> valueClass,
- final Class<C> contextClass,
- final TemplateResolverFactory<?, ?> factory) {
- final Class<?> factoryValueClass = factory.getValueClass();
- final Class<?> factoryContextClass = factory.getContextClass();
- final boolean factoryValueClassMatched =
- valueClass.isAssignableFrom(factoryValueClass);
- final boolean factoryContextClassMatched =
- contextClass.isAssignableFrom(factoryContextClass);
- if (factoryValueClassMatched && factoryContextClassMatched) {
- return TypeUtil.cast(factory);
- }
- return null;
- }
-
- private static <V, C extends TemplateResolverContext<V, C>, F extends
TemplateResolverFactory<V, C>> void addFactory(
- final Map<String, F> factoryByName,
- final F factory) {
- final String factoryName = factory.getName();
- final F conflictingFactory = factoryByName.putIfAbsent(factoryName,
factory);
- if (conflictingFactory != null) {
- final String message = String.format(
- "found resolver factories with overlapping names: %s (%s
and %s)",
- factoryName, conflictingFactory, factory);
- throw new IllegalArgumentException(message);
- }
- }
-
-}
diff --git
a/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/resolver/TemplateResolverFactory.java
b/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/resolver/TemplateResolverFactory.java
index 170bc46378..06615df832 100644
---
a/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/resolver/TemplateResolverFactory.java
+++
b/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/resolver/TemplateResolverFactory.java
@@ -16,10 +16,6 @@
*/
package org.apache.logging.log4j.layout.template.json.resolver;
-import org.apache.logging.log4j.plugins.Named;
-import org.apache.logging.log4j.plugins.di.Key;
-import org.apache.logging.log4j.plugins.util.PluginCategory;
-
/**
* {@link TemplateResolver} factory.
*
@@ -33,18 +29,6 @@ public interface TemplateResolverFactory<V, C extends
TemplateResolverContext<V,
*/
String CATEGORY = "JsonTemplateResolverFactory";
- Key<PluginCategory> PLUGIN_CATEGORY_KEY = new @Named(CATEGORY) Key<>() {};
-
- /**
- * The targeted value class.
- */
- Class<V> getValueClass();
-
- /**
- * The targeted {@link TemplateResolverContext} class.
- */
- Class<C> getContextClass();
-
String getName();
TemplateResolver<V> create(C context, TemplateResolverConfig config);
diff --git
a/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/resolver/TemplateResolverInterceptor.java
b/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/resolver/TemplateResolverInterceptor.java
index ad30d684d4..b8920d2492 100644
---
a/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/resolver/TemplateResolverInterceptor.java
+++
b/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/resolver/TemplateResolverInterceptor.java
@@ -16,10 +16,6 @@
*/
package org.apache.logging.log4j.layout.template.json.resolver;
-import org.apache.logging.log4j.plugins.Named;
-import org.apache.logging.log4j.plugins.di.Key;
-import org.apache.logging.log4j.plugins.util.PluginCategory;
-
/**
* Main {@link TemplateResolver} compilation interception interface.
*
@@ -33,18 +29,6 @@ public interface TemplateResolverInterceptor<V, C extends
TemplateResolverContex
*/
String CATEGORY = "JsonTemplateResolverInterceptor";
- Key<PluginCategory> PLUGIN_CATEGORY_KEY = new @Named(CATEGORY) Key<>() {};
-
- /**
- * The targeted value class.
- */
- Class<V> getValueClass();
-
- /**
- * The targeted {@link TemplateResolverContext} class.
- */
- Class<C> getContextClass();
-
/**
* Intercept the read template before compiler (i.e.,
* {@link TemplateResolvers#ofTemplate(TemplateResolverContext, String)}
diff --git
a/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/resolver/TemplateResolverInterceptors.java
b/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/resolver/TemplateResolverInterceptors.java
deleted file mode 100644
index 99dc926647..0000000000
---
a/log4j-layout-template-json/src/main/java/org/apache/logging/log4j/layout/template/json/resolver/TemplateResolverInterceptors.java
+++ /dev/null
@@ -1,113 +0,0 @@
-/*
- * 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.logging.log4j.layout.template.json.resolver;
-
-import org.apache.logging.log4j.Logger;
-import org.apache.logging.log4j.core.config.Configuration;
-import org.apache.logging.log4j.plugins.di.Key;
-import org.apache.logging.log4j.plugins.util.PluginCategory;
-import org.apache.logging.log4j.plugins.util.TypeUtil;
-import org.apache.logging.log4j.status.StatusLogger;
-
-import java.util.LinkedList;
-import java.util.List;
-
-/**
- * Utility class for {@link TemplateResolverInterceptor}.
- */
-public class TemplateResolverInterceptors {
-
- private static final Logger LOGGER = StatusLogger.getLogger();
-
- private TemplateResolverInterceptors() {}
-
- /**
- * Populates plugins implementing
- * {@link TemplateResolverInterceptor TemplateResolverInterceptor<V,
C>},
- * where {@code V} and {@code C} denote the value and context class types,
- * respectively.
- */
- public static <V, C extends TemplateResolverContext<V, C>, I extends
TemplateResolverInterceptor<V, C>> List<I> populateInterceptors(
- final Configuration configuration,
- final Class<V> valueClass,
- final Class<C> contextClass) {
-
- // Populate interceptors.
- final PluginCategory interceptorPlugins =
-
configuration.getComponent(TemplateResolverInterceptor.PLUGIN_CATEGORY_KEY);
- if (LOGGER.isDebugEnabled()) {
- LOGGER.debug(
- "found {} plugins of category \"{}\": {}",
- interceptorPlugins.size(),
- TemplateResolverInterceptor.CATEGORY,
- interceptorPlugins.getPluginKeys());
- }
-
- // Filter matching interceptors.
- final List<I> interceptors =
- populateInterceptors(interceptorPlugins, configuration,
valueClass, contextClass);
- LOGGER.debug(
- "{} interceptors matched out of {} for value class {} and
context class {}",
- interceptors.size(),
- interceptorPlugins.size(),
- valueClass,
- contextClass);
- return interceptors;
-
- }
-
- private static <V, C extends TemplateResolverContext<V, C>, I extends
TemplateResolverInterceptor<V, C>> List<I> populateInterceptors(
- final PluginCategory interceptorPlugins,
- final Configuration configuration,
- final Class<V> valueClass,
- final Class<C> contextClass) {
- final List<I> interceptors = new LinkedList<>();
- interceptorPlugins.forEachMatching(
- pluginType ->
pluginType.getImplementedInterfaces().contains(TemplateResolverInterceptor.class),
- pluginType -> {
- @SuppressWarnings("rawtypes")
- final Class<? extends TemplateResolverInterceptor>
interceptorClass =
-
pluginType.getPluginClass().asSubclass(TemplateResolverInterceptor.class);
- final TemplateResolverInterceptor<?, ?> rawInterceptor =
-
configuration.getComponent(Key.forClass(interceptorClass));
- final I interceptor =
- castInterceptor(valueClass, contextClass,
rawInterceptor);
- if (interceptor != null) {
- interceptors.add(interceptor);
- }
- }
- );
- return interceptors;
- }
-
- private static <V, C extends TemplateResolverContext<V, C>, I extends
TemplateResolverInterceptor<V, C>> I castInterceptor(
- final Class<V> valueClass,
- final Class<C> contextClass,
- final TemplateResolverInterceptor<?, ?> interceptor) {
- final Class<?> interceptorValueClass = interceptor.getValueClass();
- final Class<?> interceptorContextClass = interceptor.getContextClass();
- final boolean interceptorValueClassMatched =
- valueClass.isAssignableFrom(interceptorValueClass);
- final boolean interceptorContextClassMatched =
- contextClass.isAssignableFrom(interceptorContextClass);
- if (interceptorValueClassMatched && interceptorContextClassMatched) {
- return TypeUtil.cast(interceptor);
- }
- return null;
- }
-
-}