Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package javax.enterprise.inject.spi;
  
  import java.net.URL;
 import java.util.Set;
 
Provides access to the current container.

Author(s):
Pete Muir
Since:
1.1
 
 public abstract class CDI<T> implements Instance<T> {
 
     protected static volatile Set<CDIProviderdiscoveredProviders = 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 java.lang.IllegalStateException is thrown, otherwise the first provider which can access the container is used.

Throws:
java.lang.IllegalStateException if no CDI provider is available
 
     public static CDI<Objectcurrent() {
         if ( != null) {
             return .getCDI();
         } else {
 
             // Discover providers and cache
             if ( == null) {
                 synchronized () {
                     if ( == null) {
                         findAllProviders();
                     }
                 }
             }
 
             CDI<Objectcdi = null;
             for (CDIProvider provider : ) {
                 cdi = provider.getCDI();
                 if (cdi != null)
                     break;
             }
             if (cdi == null) {
                 throw new IllegalStateException("Unable to access CDI");
             }
             return cdi;
         }
     }

    

Set the CDIProvider to use.

If a CDIProvider is set using this method, any provider specified as a service provider will not be used.

Parameters:
provider the provider to use
Throws:
java.lang.IllegalStateException if the CDIProvider is already set
 
     public static void setCDIProvider(CDIProvider provider) {
         if ( == null) {
              = provider;
         } else {
             throw new IllegalStateException("CDIProvider has already been set");
         }
     }
 
     // Helper methods
 
     private static void findAllProviders() {
         Set<CDIProviderproviders = new LinkedHashSet<CDIProvider>();
         try {
             final ClassLoader loader = CDI.class.getClassLoader();
             final Enumeration<URLresources;
             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<Stringnames = new HashSet<String>();
            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<CDIProviderproviderClass = (Class<CDIProvider>) Class.forName(strueloader);
                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);
        }
        . = Collections.unmodifiableSet(providers);
    }
    private static final Pattern nonCommentPattern = Pattern.compile("^([^#]+)");
    private static Set<StringproviderNamesFromReader(BufferedReader readerthrows IOException {
        Set<Stringnames = new HashSet<String>();
        String line;
        while ((line = reader.readLine()) != null) {
            line = line.trim();
            Matcher m = .matcher(line);
            if (m.find()) {
                names.add(m.group().trim());
            }
        }
        return names;
    }

    
Get the CDI BeanManager for the current context
    public abstract BeanManager getBeanManager();
New to GrepCode? Check out our FAQ X