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

javax.enterprise.inject.spi.CDI Maven / Gradle / Ivy

There is a newer version: 3.0.0.Alpha1
Show newest version
package javax.enterprise.inject.spi;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.enterprise.inject.Instance;

/**
 * Provides access to the current container.
 * 
 * @author Pete Muir
 * @since 1.1
 */
public abstract class CDI implements Instance {

    protected static volatile Set discoveredProviders = null;
    protected static volatile CDIProvider configuredProvider = null;

    private static final Object lock = new Object();

    /**
     * 

* Get the CDI instance that provides access to the current container. *

* *

* If there are no providers available, an {@link IllegalStateException} is thrown, otherwise the first provider which can * access the container is used. *

* * @throws IllegalStateException if no CDI provider is available * */ public static CDI current() { if (configuredProvider != null) { return configuredProvider.getCDI(); } else { // Discover providers and cache if (discoveredProviders == null) { synchronized (lock) { if (discoveredProviders == null) { findAllProviders(); } } } CDI cdi = null; for (CDIProvider provider : discoveredProviders) { cdi = provider.getCDI(); if (cdi != null) break; } if (cdi == null) { throw new IllegalStateException("Unable to access CDI"); } return cdi; } } /** *

* Set the {@link CDIProvider} to use. *

* *

* If a {@link CDIProvider} is set using this method, any provider specified as a service provider will not be used. *

* * @param provider the provider to use * @throws IllegalStateException if the {@link CDIProvider} is already set */ public static void setCDIProvider(CDIProvider provider) { if (configuredProvider == null) { configuredProvider = provider; } else { throw new IllegalStateException("CDIProvider has already been set"); } } // Helper methods private static void findAllProviders() { Set providers = new LinkedHashSet(); try { final ClassLoader loader = CDI.class.getClassLoader(); final Enumeration resources; if (loader != null) { resources = loader.getResources("META-INF/services/" + CDIProvider.class.getName()); } else { // this should not happen unless the CDI api is on the boot // class path resources = ClassLoader.getSystemResources("META-INF/services/" + CDIProvider.class.getName()); } final Set names = new HashSet(); while (resources.hasMoreElements()) { URL url = resources.nextElement(); InputStream is = url.openStream(); try { names.addAll(providerNamesFromReader(new BufferedReader(new InputStreamReader(is)))); } finally { is.close(); } } for (String s : names) { final Class providerClass = (Class) Class.forName(s, true, loader); providers.add(providerClass.newInstance()); } } catch (IOException e) { throw new IllegalStateException(e); } catch (InstantiationException e) { throw new IllegalStateException(e); } catch (IllegalAccessException e) { throw new IllegalStateException(e); } catch (ClassNotFoundException e) { throw new IllegalStateException(e); } CDI.discoveredProviders = Collections.unmodifiableSet(providers); } private static final Pattern nonCommentPattern = Pattern.compile("^([^#]+)"); private static Set providerNamesFromReader(BufferedReader reader) throws IOException { Set names = new HashSet(); String line; while ((line = reader.readLine()) != null) { line = line.trim(); Matcher m = nonCommentPattern.matcher(line); if (m.find()) { names.add(m.group().trim()); } } return names; } /** * Get the CDI BeanManager for the current context * */ public abstract BeanManager getBeanManager(); }