jvz commented on code in PR #804:
URL: https://github.com/apache/logging-log4j2/pull/804#discussion_r846847224


##########
log4j-api-java9/src/test/java/org/apache/logging/log4j/util/java9/ServiceLoaderUtilTest.java:
##########
@@ -0,0 +1,77 @@
+/*
+ * 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.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.fail;
+
+import java.lang.invoke.MethodHandles;
+import java.util.Collections;
+import java.util.List;
+import java.util.ServiceConfigurationError;
+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() {
+        // Run only if we are a module
+        if (ServiceLoaderUtil.class.getModule().isNamed()) {

Review Comment:
   You can use `assumeTrue()` from JUnit here.



##########
log4j-api-java9/src/test/java/org/apache/logging/log4j/util/java9/ServiceLoaderUtilTest.java:
##########
@@ -0,0 +1,77 @@
+/*
+ * 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.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.fail;
+
+import java.lang.invoke.MethodHandles;
+import java.util.Collections;
+import java.util.List;
+import java.util.ServiceConfigurationError;
+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() {
+        // Run only if we are a module
+        if (ServiceLoaderUtil.class.getModule().isNamed()) {
+            List<Object> services = Collections.emptyList();
+            // Service from test module
+            try {
+                services = ServiceLoaderUtil.loadServices(Service.class, 
MethodHandles.lookup())
+                        .collect(Collectors.toList());
+            } catch (ServiceConfigurationError e) {

Review Comment:
   Nit: these types of tests can use `assertDoesNotThrow(() -> ...)`



##########
log4j-api-java9/src/test/java/org/apache/logging/log4j/util/java9/ServiceLoaderUtilTest.java:
##########
@@ -0,0 +1,77 @@
+/*
+ * 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.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.fail;
+
+import java.lang.invoke.MethodHandles;
+import java.util.Collections;
+import java.util.List;
+import java.util.ServiceConfigurationError;
+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() {
+        // Run only if we are a module
+        if (ServiceLoaderUtil.class.getModule().isNamed()) {
+            List<Object> services = Collections.emptyList();
+            // Service from test module
+            try {
+                services = ServiceLoaderUtil.loadServices(Service.class, 
MethodHandles.lookup())
+                        .collect(Collectors.toList());
+            } catch (ServiceConfigurationError e) {
+                fail(e);
+            }
+            assertEquals(2, services.size(), "Service services");

Review Comment:
   If you're going to add assertion messages, I'd suggest just using 
`assertThat()` from AssertJ to get a nice output message when assertions fail. 
For example, this could be `assertThat(services).hasSize(2)`.



##########
log4j-api/src/main/java/org/apache/logging/log4j/util/OsgiServiceLocator.java:
##########
@@ -0,0 +1,70 @@
+/*
+ * 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.MethodHandles.Lookup;
+import java.util.Arrays;
+import java.util.stream.Stream;
+
+import org.apache.logging.log4j.status.StatusLogger;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.FrameworkUtil;
+import org.osgi.framework.ServiceReference;
+
+public class OsgiServiceLocator {
+
+    private static final boolean OSGI_AVAILABLE = checkOsgiAvailable();
+
+    private static boolean checkOsgiAvailable() {
+        try {
+            Class.forName("org.osgi.framework.Bundle");
+            return true;
+        } catch (final ClassNotFoundException | LinkageError e) {
+            return false;
+        } catch (final Throwable e) {
+            LowLevelLogUtil.logException("Unknown error checking for existence 
of class: org.osgi.framework.Bundle", e);
+            return false;
+        }
+    }
+
+    public static boolean isAvailable() {
+        return OSGI_AVAILABLE;
+    }
+
+    public static <T> Stream<T> loadServices(final Class<T> serviceType, final 
Lookup lookup) {
+        return loadServices(serviceType, lookup, true);
+    }
+
+    public static <T> Stream<T> loadServices(final Class<T> serviceType, final 
Lookup lookup, final boolean verbose) {
+        final Bundle bundle = FrameworkUtil.getBundle(lookup.lookupClass());
+        if (bundle != null) {
+            final BundleContext ctx = bundle.getBundleContext();
+            try {
+                @SuppressWarnings("unchecked")
+                final ServiceReference<T>[] serviceRefs = 
(ServiceReference<T>[]) ctx
+                        .getAllServiceReferences(serviceType.getName(), null);

Review Comment:
   Can't we use the typed APIs here rather than the old untyped ones?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscr...@logging.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to