All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.openejb.cdi.OptimizedLoaderService Maven / Gradle / Ivy

There is a newer version: 10.0.0-M3
Show newest version
/*
 * 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.openejb.cdi;

import org.apache.openejb.core.ParentClassLoaderFinder;
import org.apache.openejb.loader.SystemInstance;
import org.apache.openejb.resource.activemq.jms2.cdi.JMS2CDIExtension;
import org.apache.openejb.util.LogCategory;
import org.apache.openejb.util.Logger;
import org.apache.webbeans.service.DefaultLoaderService;
import org.apache.webbeans.spi.LoaderService;
import org.apache.webbeans.spi.plugins.OpenWebBeansPlugin;

import javax.enterprise.inject.spi.Extension;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;

/**
 * @version $Rev$ $Date$
 */
public class OptimizedLoaderService implements LoaderService {

    private static final Logger log = Logger.getInstance(LogCategory.OPENEJB.createChild("cdi"), OptimizedLoaderService.class);

    public static final ThreadLocal> ADDITIONAL_EXTENSIONS = new ThreadLocal>();

    private final LoaderService loaderService;
    private final Properties config;

    public OptimizedLoaderService(final Properties appConfig) {
        this(
                is("openejb.cdi.ignore-not-loadable-extensions", appConfig, SystemInstance.get().getProperties(), "false") ?
                        new FilterableServiceLoader() : new DefaultLoaderService(),
                appConfig);
    }

    public OptimizedLoaderService(final LoaderService loaderService, final Properties appConfig) {
        this.loaderService = loaderService;
        this.config = appConfig;
    }

    @Override
    public  List load(final Class serviceType) {
        return load(serviceType, Thread.currentThread().getContextClassLoader());
    }

    @Override
    public  List load(final Class serviceType, final ClassLoader classLoader) {
        // ServiceLoader is expensive (can take up to a half second).  This is an optimization
        if (OpenWebBeansPlugin.class.equals(serviceType)) {
            return (List) loadWebBeansPlugins(classLoader);
        }

        // As far as we know, this only is reached for CDI Extension discovery
        if (Extension.class.equals(serviceType)) {
            return (List) loadExtensions(classLoader);
        }
        return loaderService.load(serviceType, classLoader);
    }

    protected List loadExtensions(final ClassLoader classLoader) {
        final List list = loaderService.load(Extension.class, classLoader);
        final Collection additional = ADDITIONAL_EXTENSIONS.get();
        if (additional != null) {
            for (final String name : additional) {
                try {
                    list.add(Extension.class.cast(classLoader.loadClass(name).newInstance()));
                } catch (final Exception ignored) {
                    // no-op
                }
            }
        }

        if (hasJms()) {
            list.add(new JMS2CDIExtension());
        }

        final Collection extensionCopy = new ArrayList<>(list);

        final Iterator it = list.iterator();
        while (it.hasNext()) {
            if (it.hasNext()) {
                if (isFiltered(extensionCopy, it.next())) {
                    it.remove();
                }
            }
        }

        if ("true".equals(OptimizedLoaderService.this.config.getProperty("openejb.cdi.extensions.sorted",
                SystemInstance.get().getProperty("openejb.cdi.extensions.sorted")))) {
            Collections.sort(list, new Comparator() {
                @Override
                public int compare(final Extension o1, final Extension o2) {
                    final int val1 = getVal(o1);
                    final int val2 = getVal(o1);
                    if (val1 == val2) {
                        return o1.getClass().getName().compareTo(o2.getClass().getName());
                    }
                    return val1 - val2;
                }

                private int getVal(final Extension o1) {
                    final String key = "openejb.cdi.extensions." + o1.getClass().getName() + ".ordinal";
                    final String config = OptimizedLoaderService.this.config.getProperty(key, SystemInstance.get().getProperty(key));
                    return config == null ? 0 : Integer.parseInt(config.trim());
                }
            });
        }

        return list;
    }

    private boolean hasJms() {
        try {
            Thread.currentThread().getContextClassLoader().loadClass("org.apache.activemq.ra.ActiveMQManagedConnectionFactory");
            return true;
        } catch (final NoClassDefFoundError | ClassNotFoundException e) {
            return false;
        }
    }

    // mainly intended to avoid conflicts between internal and overrided spec extensions
    private boolean isFiltered(final Collection extensions, final Extension next) {
        final ClassLoader containerLoader = ParentClassLoaderFinder.Helper.get();
        final Class extClass = next.getClass();
        final String name = extClass.getName();

        final String activeKey = name + ".active";
        final SystemInstance systemInstance = SystemInstance.get();
        if (!is(activeKey, config, systemInstance.getProperties(), "true")) {
            return true;
        }

        if (extClass.getClassLoader() != containerLoader) {
            return false;
        }

        switch (name) {
            case "org.apache.bval.cdi.BValExtension":
                for (final Extension e : extensions) {
                    final String en = e.getClass().getName();

                    // org.hibernate.validator.internal.cdi.ValidationExtension but allowing few evolutions of packages
                    if (en.startsWith("org.hibernate.validator.") && en.endsWith("ValidationExtension")) {
                        log.info("Skipping BVal CDI integration cause hibernate was found in the application");
                        return true;
                    }
                }
                break;
            case "org.apache.batchee.container.cdi.BatchCDIInjectionExtension": // see org.apache.openejb.batchee.BatchEEServiceManager
                return "true".equals(systemInstance.getProperty("tomee.batchee.cdi.use-extension", "false"));
            case "org.apache.commons.jcs.jcache.cdi.MakeJCacheCDIInterceptorFriendly":
                final String spi = "META-INF/services/javax.cache.spi.CachingProvider";
                try {
                    final Enumeration appResources = Thread.currentThread().getContextClassLoader().getResources(spi);
                    if (appResources != null && appResources.hasMoreElements()) {
                        final Collection containerResources = Collections.list(containerLoader.getResources(spi));
                        do {
                            if (!containerResources.contains(appResources.nextElement())) {
                                log.info("Skipping JCS CDI integration cause another provide was found in the application");
                                return true;
                            }
                        } while (appResources.hasMoreElements());
                    }
                } catch (final Exception e) {
                    // no-op
                }
                break;
            default:
        }
        return false;
    }

    private List loadWebBeansPlugins(final ClassLoader loader) {
        final List list = new ArrayList<>(2);
        list.add(new CdiPlugin());
        {
            final Class clazz;
            try {
                clazz = loader.loadClass("org.apache.geronimo.openejb.cdi.GeronimoWebBeansPlugin");
                try {
                    list.add(OpenWebBeansPlugin.class.cast(clazz.newInstance()));
                } catch (final Exception e) {
                    log.error("Unable to load OpenWebBeansPlugin: GeronimoWebBeansPlugin");
                }
            } catch (final ClassNotFoundException e) {
                // ignore
            }
        }
        return list;
    }

    private static boolean is(final String activeKey, final Properties config, final Properties fallback, final String or) {
        return Boolean.parseBoolean(config.getProperty(activeKey, fallback.getProperty(activeKey, or)));
    }

    private static class FilterableServiceLoader implements LoaderService {
        private static final String SERVICE_CONFIG = "META-INF/services/" + Extension.class.getName();
        private static final String FILE_ENCODING = "UTF-8";

        private List> foundServiceClasses = new ArrayList>();
        private ClassLoader loader;

        private List loadServiceImplementations() {
            final List> result = resolveServiceImplementations();
            if (result == null) {
                return Collections.emptyList();
            }
            final List foundServices = new ArrayList<>();
            for (final Class serviceClass : result) {
                try {
                    foundServices.add(Extension.class.cast(serviceClass.newInstance()));
                } catch (final NoClassDefFoundError | InstantiationException | IllegalAccessException e) {
                    log.error("Ignoring a CDI Extension, cause it can't be instantiated (" + serviceClass + "): " + e.getMessage());
                }
            }
            return foundServices;
        }

        private List> resolveServiceImplementations() {
            for (final URL configFile : getConfigFileList()) {
                loadConfiguredServices(configFile);
            }
            return foundServiceClasses;
        }

        private List getConfigFileList() {
            final List serviceFiles = new ArrayList<>();
            try {
                final Enumeration serviceFileEnumerator = loader.getResources(SERVICE_CONFIG);
                while (serviceFileEnumerator.hasMoreElements()) {
                    serviceFiles.add(serviceFileEnumerator.nextElement());
                }
            } catch (final Exception e) {
                throw new IllegalStateException("Failed to load Extension configured in " + SERVICE_CONFIG, e);
            }
            return serviceFiles;
        }

        private void loadConfiguredServices(final URL serviceFile) {
            try (final InputStream inputStream = serviceFile.openStream()) {
                String serviceClassName;
                final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, FILE_ENCODING));
                while ((serviceClassName = bufferedReader.readLine()) != null) {
                    serviceClassName = extractConfiguredServiceClassName(serviceClassName);
                    if (!"".equals(serviceClassName)) {
                        loadService(serviceClassName);
                    }
                }
            } catch (final Exception e) {
                throw new IllegalStateException("Failed to process service-config: " + 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(final String serviceClassName) {
            final Class serviceClass;
            try {
                serviceClass = (Class) loader.loadClass(serviceClassName);
                if (!foundServiceClasses.contains(serviceClass)) {
                    foundServiceClasses.add(serviceClass);
                }
            } catch (final NoClassDefFoundError |ClassNotFoundException e) {
                log.error("Ignoring " + serviceClassName + " cause it can't be loaded.");
            }
        }

        @Override
        public  List load(final Class serviceType) {
            throw new UnsupportedOperationException();
        }

        @Override
        public  List load(final Class serviceType, final ClassLoader classLoader) {
            loader = classLoader;
            try {
                return (List) loadServiceImplementations();
            } finally {
                loader = null;
                foundServiceClasses.clear();
            }
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy