This is an automated email from the ASF dual-hosted git repository.
pkarwasz pushed a commit to branch release-2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
The following commit(s) were added to refs/heads/release-2.x by this push:
new 9877f246a9 Remove Java 9 version of `ServiceLoaderUtil`
9877f246a9 is described below
commit 9877f246a9fabf656b5a2c0d9794e303b7bd099f
Author: Piotr P. Karwasz <[email protected]>
AuthorDate: Sat Oct 29 22:15:23 2022 +0200
Remove Java 9 version of `ServiceLoaderUtil`
Using `ServiceLoader#stream()` is less resilient to service
configuration errors than the way it is done in the Java 8 version of
`ServiceLoaderUtil`.
---
log4j-api-java9/src/assembly/java9.xml | 18 +--
.../apache/logging/log4j/status/StatusLogger.java | 38 ------
.../apache/logging/log4j/util/PropertySource.java | 7 --
.../logging/log4j/util/ServiceLoaderUtil.java | 131 ---------------------
.../log4j/util/java9/ServiceLoaderUtilTest.java | 53 ---------
.../log4j/util/java9/test/BetterService.java | 21 ----
.../logging/log4j/util/java9/test/Service.java | 21 ----
.../logging/log4j/util/java9/test/Service1.java | 21 ----
.../logging/log4j/util/java9/test/Service2.java | 21 ----
log4j-api-java9/src/test/java9/module-info.java | 35 ------
10 files changed, 5 insertions(+), 361 deletions(-)
diff --git a/log4j-api-java9/src/assembly/java9.xml
b/log4j-api-java9/src/assembly/java9.xml
index ca7c4ba142..5d333ba6b5 100644
--- a/log4j-api-java9/src/assembly/java9.xml
+++ b/log4j-api-java9/src/assembly/java9.xml
@@ -29,20 +29,12 @@
<directory>${project.build.outputDirectory}</directory>
<outputDirectory>/classes/META-INF/versions/9</outputDirectory>
<includes>
- <include>**/*.class</include>
+ <include>module-info.class</include>
+ <include>org/apache/logging/log4j/util/Base64Util.class</include>
+ <include>org/apache/logging/log4j/util/ProcessIdUtil.class</include>
+ <include>org/apache/logging/log4j/util/StackLocator.class</include>
+
<include>org/apache/logging/log4j/util/internal/DefaultObjectInputFilter.class</include>
</includes>
- <excludes>
- <exclude>**/Dummy.class</exclude>
- <exclude>**/spi/Provider.class</exclude>
- <exclude>**/status/StatusLogger.class</exclude>
- <exclude>**/util/EnvironmentPropertySource.class</exclude>
- <exclude>**/util/LoaderUtil.class</exclude>
- <exclude>**/util/PropertySource.class</exclude>
- <exclude>**/util/PrivateSecurityManagerStackTraceUtil.class</exclude>
- <exclude>**/util/SystemPropertiesPropertySource.class</exclude>
- <exclude>**/message/ThreadDumpMessage.class</exclude>
- <exclude>**/message/ThreadDumpMessage$ThreadInfoFactory.class</exclude>
- </excludes>
</fileSet>
</fileSets>
</assembly>
diff --git
a/log4j-api-java9/src/main/java/org/apache/logging/log4j/status/StatusLogger.java
b/log4j-api-java9/src/main/java/org/apache/logging/log4j/status/StatusLogger.java
deleted file mode 100644
index f139b159e1..0000000000
---
a/log4j-api-java9/src/main/java/org/apache/logging/log4j/status/StatusLogger.java
+++ /dev/null
@@ -1,38 +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.status;
-
-import java.util.concurrent.atomic.AtomicInteger;
-
-/**
- * This is a dummy class and is only here to allow module-info.java to compile.
- * It will not be copied into the log4j-api module.
- */
-public final class StatusLogger {
-
- private static final StatusLogger STATUS_LOGGER = new StatusLogger();
-
- public void error(String message, Object p0, Object p1) {
- }
-
- public void warn(String message, Object p0, Object p1) {
- }
-
- public static StatusLogger getLogger() {
- return STATUS_LOGGER;
- }
-}
diff --git
a/log4j-api-java9/src/main/java/org/apache/logging/log4j/util/PropertySource.java
b/log4j-api-java9/src/main/java/org/apache/logging/log4j/util/PropertySource.java
index b90e7d1f8f..f4d9fc30ba 100644
---
a/log4j-api-java9/src/main/java/org/apache/logging/log4j/util/PropertySource.java
+++
b/log4j-api-java9/src/main/java/org/apache/logging/log4j/util/PropertySource.java
@@ -24,11 +24,4 @@ import java.util.stream.Stream;
* be copied into the log4j-api module.
*/
public interface PropertySource {
-
- /**
- * This method's only purpose is to test {@link ServiceLoaderUtil} from
inside the module.
- */
- public static Stream<PropertySource> loadPropertySources() {
- return ServiceLoaderUtil.loadServices(PropertySource.class,
MethodHandles.lookup());
- }
}
diff --git
a/log4j-api-java9/src/main/java/org/apache/logging/log4j/util/ServiceLoaderUtil.java
b/log4j-api-java9/src/main/java/org/apache/logging/log4j/util/ServiceLoaderUtil.java
deleted file mode 100644
index 10b8cd547d..0000000000
---
a/log4j-api-java9/src/main/java/org/apache/logging/log4j/util/ServiceLoaderUtil.java
+++ /dev/null
@@ -1,131 +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.util;
-
-import java.lang.invoke.CallSite;
-import java.lang.invoke.LambdaMetafactory;
-import java.lang.invoke.MethodHandle;
-import java.lang.invoke.MethodHandles.Lookup;
-import java.lang.invoke.MethodType;
-import java.security.AccessController;
-import java.security.PrivilegedAction;
-import java.util.HashSet;
-import java.util.Objects;
-import java.util.ServiceConfigurationError;
-import java.util.ServiceLoader;
-import java.util.Set;
-import java.util.stream.Stream;
-
-import org.apache.logging.log4j.status.StatusLogger;
-
-public final class ServiceLoaderUtil {
-
- private ServiceLoaderUtil() {
- }
-
- /**
- * Retrieves the available services from the caller's classloader.
- *
- * Broken services will be ignored.
- *
- * @param <T> The service type.
- * @param serviceType The class of the service.
- * @param lookup The calling class data.
- * @return A stream of service instances.
- */
- public static <T> Stream<T> loadServices(final Class<T> serviceType,
Lookup lookup) {
- return loadServices(serviceType, lookup, false);
- }
-
- /**
- * Retrieves the available services from the caller's classloader and
possibly
- * the thread context classloader.
- *
- * Broken services will be ignored.
- *
- * @param <T> The service type.
- * @param serviceType The class of the service.
- * @param lookup The calling class data.
- * @param useTccl If true the thread context classloader will also be
used.
- * @return A stream of service instances.
- */
- public static <T> Stream<T> loadServices(final Class<T> serviceType,
Lookup lookup, boolean useTccl) {
- return loadServices(serviceType, lookup, useTccl, true);
- }
-
- static <T> Stream<T> loadServices(final Class<T> serviceType, final Lookup
lookup, final boolean useTccl,
- final boolean verbose) {
- final ClassLoader classLoader = lookup.lookupClass().getClassLoader();
- Stream<T> services = loadClassloaderServices(serviceType, lookup,
classLoader, verbose);
- if (useTccl) {
- final ClassLoader contextClassLoader =
LoaderUtil.getThreadContextClassLoader();
- if (contextClassLoader != classLoader) {
- services = Stream.concat(services,
- loadClassloaderServices(serviceType, lookup,
contextClassLoader, verbose));
- }
- }
- final Set<Class<?>> classes = new HashSet<>();
- // only the first occurrence of a class
- return services.filter(service -> classes.add(service.getClass()));
- }
-
- static <T> Stream<T> loadClassloaderServices(final Class<T> serviceType,
final Lookup lookup,
- final ClassLoader classLoader, final boolean verbose) {
- try {
- // Creates a lambda in the caller's domain that calls
`ServiceLoader`
- final MethodHandle loadHandle =
lookup.findStatic(ServiceLoader.class, "load",
- MethodType.methodType(ServiceLoader.class, Class.class,
ClassLoader.class));
- final CallSite callSite = LambdaMetafactory.metafactory(lookup,
- "run",
- MethodType.methodType(PrivilegedAction.class, Class.class,
ClassLoader.class),
- MethodType.methodType(Object.class),
- loadHandle,
- MethodType.methodType(ServiceLoader.class));
- final PrivilegedAction<ServiceLoader<T>> action =
(PrivilegedAction<ServiceLoader<T>>) callSite
- .getTarget()//
- .bindTo(serviceType)
- .bindTo(classLoader)
- .invoke();
- final ServiceLoader<T> serviceLoader;
- if (System.getSecurityManager() == null) {
- serviceLoader = action.run();
- } else {
- final MethodHandle privilegedHandle =
lookup.findStatic(AccessController.class, "doPrivileged",
- MethodType.methodType(Object.class,
PrivilegedAction.class));
- serviceLoader = (ServiceLoader<T>)
privilegedHandle.invoke(action);
- }
- return serviceLoader.stream().map(provider -> {
- try {
- return provider.get();
- } catch (ServiceConfigurationError e) {
- if (verbose) {
- StatusLogger.getLogger().warn("Unable to load service
class for service {}",
- serviceType.getClass(), e);
- }
- }
- return null;
- }).filter(Objects::nonNull);
- } catch (Throwable e) {
- if (verbose) {
- StatusLogger.getLogger().error("Unable to load services for
service {}", serviceType, e);
- }
- }
- return Stream.empty();
- }
-
-}
diff --git
a/log4j-api-java9/src/test/java/org/apache/logging/log4j/util/java9/ServiceLoaderUtilTest.java
b/log4j-api-java9/src/test/java/org/apache/logging/log4j/util/java9/ServiceLoaderUtilTest.java
deleted file mode 100644
index 9117629940..0000000000
---
a/log4j-api-java9/src/test/java/org/apache/logging/log4j/util/java9/ServiceLoaderUtilTest.java
+++ /dev/null
@@ -1,53 +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.util.java9;
-
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
-
-import java.lang.invoke.MethodHandles;
-import java.util.List;
-import java.util.stream.Collectors;
-
-import org.apache.logging.log4j.util.PropertySource;
-import org.apache.logging.log4j.util.ServiceLoaderUtil;
-import org.apache.logging.log4j.util.java9.test.BetterService;
-import org.apache.logging.log4j.util.java9.test.Service;
-import org.junit.jupiter.api.Test;
-
-public class ServiceLoaderUtilTest {
-
- @Test
- public void testServiceResolution() {
- List<Object> services;
- // Service from test module
- services = assertDoesNotThrow(() ->
ServiceLoaderUtil.loadServices(Service.class, MethodHandles.lookup())
- .collect(Collectors.toList()));
- assertThat(services).hasSize(2);
- // BetterService from test module
- services = assertDoesNotThrow(() ->
ServiceLoaderUtil.loadServices(BetterService.class, MethodHandles.lookup())
- .collect(Collectors.toList()));
- assertThat(services).hasSize(1);
- // PropertySource from org.apache.logging.log4j module from this module
- services = assertDoesNotThrow(() ->
ServiceLoaderUtil.loadServices(PropertySource.class, MethodHandles.lookup())
- .collect(Collectors.toList()));
- assertThat(services).hasSize(0);
- // PropertySource from within org.apache.logging.log4j module
- services = assertDoesNotThrow(() ->
PropertySource.loadPropertySources().collect(Collectors.toList()));
- assertThat(services).hasSize(2);
- }
-}
diff --git
a/log4j-api-java9/src/test/java/org/apache/logging/log4j/util/java9/test/BetterService.java
b/log4j-api-java9/src/test/java/org/apache/logging/log4j/util/java9/test/BetterService.java
deleted file mode 100644
index e2eaf384db..0000000000
---
a/log4j-api-java9/src/test/java/org/apache/logging/log4j/util/java9/test/BetterService.java
+++ /dev/null
@@ -1,21 +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.util.java9.test;
-
-public interface BetterService extends Service {
-}
\ No newline at end of file
diff --git
a/log4j-api-java9/src/test/java/org/apache/logging/log4j/util/java9/test/Service.java
b/log4j-api-java9/src/test/java/org/apache/logging/log4j/util/java9/test/Service.java
deleted file mode 100644
index c256fe907b..0000000000
---
a/log4j-api-java9/src/test/java/org/apache/logging/log4j/util/java9/test/Service.java
+++ /dev/null
@@ -1,21 +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.util.java9.test;
-
-public interface Service {
-}
\ No newline at end of file
diff --git
a/log4j-api-java9/src/test/java/org/apache/logging/log4j/util/java9/test/Service1.java
b/log4j-api-java9/src/test/java/org/apache/logging/log4j/util/java9/test/Service1.java
deleted file mode 100644
index 6b1498c40e..0000000000
---
a/log4j-api-java9/src/test/java/org/apache/logging/log4j/util/java9/test/Service1.java
+++ /dev/null
@@ -1,21 +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.util.java9.test;
-
-public class Service1 implements Service {
-}
\ No newline at end of file
diff --git
a/log4j-api-java9/src/test/java/org/apache/logging/log4j/util/java9/test/Service2.java
b/log4j-api-java9/src/test/java/org/apache/logging/log4j/util/java9/test/Service2.java
deleted file mode 100644
index 14b73cd034..0000000000
---
a/log4j-api-java9/src/test/java/org/apache/logging/log4j/util/java9/test/Service2.java
+++ /dev/null
@@ -1,21 +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.util.java9.test;
-
-public class Service2 implements BetterService {
-}
\ No newline at end of file
diff --git a/log4j-api-java9/src/test/java9/module-info.java
b/log4j-api-java9/src/test/java9/module-info.java
deleted file mode 100644
index cc0c24cca3..0000000000
--- a/log4j-api-java9/src/test/java9/module-info.java
+++ /dev/null
@@ -1,35 +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.
- */
-import org.apache.logging.log4j.util.java9.test.BetterService;
-import org.apache.logging.log4j.util.java9.test.Service;
-import org.apache.logging.log4j.util.java9.test.Service1;
-import org.apache.logging.log4j.util.java9.test.Service2;
-
-open module org.apache.logging.log4j.java9test {
- exports org.apache.logging.log4j.util.java9;
-
- requires org.apache.logging.log4j;
- requires transitive org.junit.jupiter.engine;
- requires transitive org.junit.jupiter.api;
- requires transitive org.assertj.core;
-
- uses Service;
- uses BetterService;
-
- provides Service with Service1, Service2;
- provides BetterService with Service2;
-}