This is an automated email from the ASF dual-hosted git repository.
rmannibucau pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/openwebbeans.git
The following commit(s) were added to refs/heads/master by this push:
new f4e1305 [OWB-1359] ensure default loader service is more easily
extensible
f4e1305 is described below
commit f4e13057ea9c8d6f076535ada10beaf406a2fd2d
Author: Romain Manni-Bucau <[email protected]>
AuthorDate: Wed Dec 16 15:02:50 2020 +0100
[OWB-1359] ensure default loader service is more easily extensible
---
.../webbeans/service/DefaultLoaderService.java | 64 +++---
.../service/ManualImplementationLoaderService.java | 228 ---------------------
2 files changed, 24 insertions(+), 268 deletions(-)
diff --git
a/webbeans-impl/src/main/java/org/apache/webbeans/service/DefaultLoaderService.java
b/webbeans-impl/src/main/java/org/apache/webbeans/service/DefaultLoaderService.java
index 353d47b..4a7c882 100644
---
a/webbeans-impl/src/main/java/org/apache/webbeans/service/DefaultLoaderService.java
+++
b/webbeans-impl/src/main/java/org/apache/webbeans/service/DefaultLoaderService.java
@@ -23,22 +23,20 @@ import org.apache.webbeans.logger.WebBeansLoggerFacade;
import org.apache.webbeans.spi.LoaderService;
import org.apache.webbeans.util.WebBeansUtil;
-import java.util.ArrayList;
+import javax.enterprise.inject.spi.Extension;
import java.util.List;
import java.util.ServiceLoader;
import java.util.logging.Level;
-import java.util.logging.Logger;
+import java.util.stream.Stream;
+import java.util.stream.StreamSupport;
+
+import static java.util.stream.Collectors.toList;
/**
- * Default implementation which delegates to the s{@link ServiceLoader} of
Java 1.6 and
- * uses a fallback for Java 1.5
+ * Default implementation which delegates to the s{@link ServiceLoader}.
*/
public class DefaultLoaderService implements LoaderService
{
- private static final Logger logger =
WebBeansLoggerFacade.getLogger(DefaultLoaderService.class);
-
- private static final boolean JAVA_6_AVAILABLE = isJava6();
-
@Override
public <T> List<T> load(Class<T> serviceType)
{
@@ -48,44 +46,30 @@ public class DefaultLoaderService implements LoaderService
@Override
public <T> List<T> load(Class<T> serviceType, ClassLoader classLoader)
{
- if(JAVA_6_AVAILABLE)
+ try
{
- List<T> result = new ArrayList<>();
- try
+ Stream<T> stream =
StreamSupport.stream(ServiceLoader.load(serviceType,
classLoader).spliterator(), false);
+ if (Extension.class == serviceType)
{
- ServiceLoader<T> services;
- services = ServiceLoader.load(serviceType, classLoader);
-
- for (T service : services)
- {
- result.add(service);
- }
+ return mapExtensions(stream).collect(toList());
}
- catch (Error error)
- {
- // WTF! ServiceLoader is cool, but THAT is utter crap: it
throws some Errors!
- logger.log(Level.SEVERE, "Problem while loading CDI
Extensions", error);
- throw new WebBeansConfigurationException("Problem while
loading CDI Extensions", error);
- }
-
-
- return result;
+ // OWBPlugin
+ return stream.collect(toList());
+ }
+ catch (Error error)
+ {
+ // WTF! ServiceLoader is cool, but THAT is utter crap: it throws
some Errors!
+ WebBeansLoggerFacade.getLogger(DefaultLoaderService.class)
+ .log(Level.SEVERE, "Problem while loading CDI Extensions",
error);
+ throw new WebBeansConfigurationException("Problem while loading
CDI Extensions", error);
}
-
- return new ManualImplementationLoaderService<>(serviceType,
classLoader).loadServiceImplementations();
}
- private static boolean isJava6()
+ // enables to easily extend the loader to customize extensions:
+ // 1. filter some undesired extensions programmatically
+ // 2. remap some extensions (to drop some events or wrap them for ex)
+ protected <T> Stream<T> mapExtensions(final Stream<T> stream)
{
- try
- {
- ServiceLoader.class.getName();
- return true;
- }
- catch (NoClassDefFoundError error)
- {
- logger.info("Using Java 5 compatibility mode, because didn't find
ServiceLoader: " + error);
- return false;
- }
+ return stream;
}
}
diff --git
a/webbeans-impl/src/main/java/org/apache/webbeans/service/ManualImplementationLoaderService.java
b/webbeans-impl/src/main/java/org/apache/webbeans/service/ManualImplementationLoaderService.java
deleted file mode 100644
index 9335c21..0000000
---
a/webbeans-impl/src/main/java/org/apache/webbeans/service/ManualImplementationLoaderService.java
+++ /dev/null
@@ -1,228 +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.webbeans.service;
-
-import org.apache.webbeans.util.ClassUtil;
-
-import java.io.BufferedReader;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.lang.reflect.Constructor;
-import java.net.URL;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.Enumeration;
-import java.util.List;
-
-/**
- * Manual service loader as fallback for Java 1.5
- */
-class ManualImplementationLoaderService<T>
-{
- private static final String SERVICE_CONFIG = "META-INF/services/";
- private static final String FILE_ENCODING = "UTF-8";
-
- protected List<Class<?>> foundServiceClasses = new ArrayList<>();
- private Class<T> serviceType;
- private ClassLoader currentClassLoader;
-
- ManualImplementationLoaderService(Class<T> serviceType, ClassLoader
currentClassLoader)
- {
- this.serviceType = serviceType;
- this.currentClassLoader = currentClassLoader;
- }
-
- List<T> loadServiceImplementations()
- {
- List<Class<?>> result = resolveServiceImplementations();
-
- if (result == null)
- {
- return Collections.emptyList();
- }
-
- List<T> foundServices = new ArrayList<>();
-
- for (Class<?> serviceClass : result)
- {
- foundServices.add(createInstance(serviceClass));
- }
-
- return foundServices;
- }
-
- private List<Class<?>> resolveServiceImplementations()
- {
- for (URL configFile : getConfigFileList())
- {
- loadConfiguredServices(configFile);
- }
-
- return foundServiceClasses;
- }
-
- private List<URL> getConfigFileList()
- {
- List<URL> serviceFiles = new ArrayList<>();
-
- try
- {
- Enumeration<URL> serviceFileEnumerator =
currentClassLoader.getResources(getConfigFileLocation());
-
- while (serviceFileEnumerator.hasMoreElements())
- {
- serviceFiles.add(serviceFileEnumerator.nextElement());
- }
- }
- catch (Exception e)
- {
- throw new IllegalStateException(
- "Failed to load " + serviceType.getName() + " configured
in " + getConfigFileLocation(), e);
- }
- return serviceFiles;
- }
-
- private String getConfigFileLocation()
- {
- return SERVICE_CONFIG + serviceType.getName();
- }
-
- private void loadConfiguredServices(URL serviceFile)
- {
- InputStream inputStream = null;
-
- try
- {
- String serviceClassName;
- inputStream = serviceFile.openStream();
- BufferedReader bufferedReader = new BufferedReader(new
InputStreamReader(inputStream, FILE_ENCODING));
-
- while ((serviceClassName = bufferedReader.readLine()) != null)
- {
- serviceClassName =
extractConfiguredServiceClassName(serviceClassName);
- if (!"".equals(serviceClassName))
- {
- loadService(serviceClassName);
- }
- }
- }
- catch (Exception e)
- {
- throw new IllegalStateException("Failed to process service-config:
" + serviceFile, e);
- }
- finally
- {
- if (inputStream != null)
- {
- try
- {
- inputStream.close();
- }
- catch (Exception e)
- {
- throw new IllegalStateException("Failed to close " +
serviceFile, e);
- }
- }
- }
- }
-
- private String extractConfiguredServiceClassName(String currentConfigLine)
- {
- int startOfComment = currentConfigLine.indexOf('#');
-
- if (startOfComment > -1)
- {
- currentConfigLine = currentConfigLine.substring(0, startOfComment);
- }
- return currentConfigLine.trim();
- }
-
- private void loadService(String serviceClassName)
- {
- Class<T> serviceClass = (Class<T>) loadClass(serviceClassName);
-
- if (serviceClass != null &&
!foundServiceClasses.contains(serviceClass))
- {
- foundServiceClasses.add(serviceClass);
- }
- else if (serviceClass == null)
- {
- throw new IllegalStateException(serviceClassName + " couldn't be
loaded. " +
- "Please ensure that this class is in the classpath or
remove the entry from "
- + getConfigFileLocation() + ".");
- }
- }
-
- private Class<? extends T> loadClass(String serviceClassName)
- {
- Class<?> targetClass = ClassUtil.getClassFromName(serviceClassName);
-
- if (targetClass == null)
- {
- targetClass = loadClassForName(serviceClassName,
currentClassLoader);
-
- if (targetClass == null)
- {
- return null;
- }
- }
-
- return targetClass.asSubclass(serviceType);
- }
-
- private static Class<?> loadClassForName(String serviceClassName,
ClassLoader classLoader)
- {
- if (classLoader == null)
- {
- return null;
- }
-
- try
- {
- return classLoader.loadClass(serviceClassName);
- }
- catch (Exception e)
- {
- return loadClassForName(serviceClassName, classLoader.getParent());
- }
- }
-
- private T createInstance(Class<?> serviceClass)
- {
- try
- {
- Constructor<?> constructor = serviceClass.getDeclaredConstructor();
- constructor.setAccessible(true);
- return (T) constructor.newInstance();
- }
- catch (Exception e)
- {
- return null;
- }
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public String toString()
- {
- return "Config file: " + getConfigFileLocation();
- }
-}