Search in sources :

Example 1 with Extension

use of javax.enterprise.inject.spi.Extension in project tomee by apache.

the class OptimizedLoaderService method isFiltered.

// mainly intended to avoid conflicts between internal and overrided spec extensions
private boolean isFiltered(final Collection<Extension> extensions, final Extension next) {
    final ClassLoader containerLoader = ParentClassLoaderFinder.Helper.get();
    final Class<? extends Extension> 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 // see org.apache.openejb.batchee.BatchEEServiceManager
        "org.apache.batchee.container.cdi.BatchCDIInjectionExtension":
            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<URL> appResources = Thread.currentThread().getContextClassLoader().getResources(spi);
                if (appResources != null && appResources.hasMoreElements()) {
                    final Collection<URL> 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;
}
Also used : Extension(javax.enterprise.inject.spi.Extension) JMS2CDIExtension(org.apache.openejb.resource.activemq.jms2.cdi.JMS2CDIExtension) SystemInstance(org.apache.openejb.loader.SystemInstance) URL(java.net.URL)

Example 2 with Extension

use of javax.enterprise.inject.spi.Extension in project tomee by apache.

the class OptimizedLoaderService method loadExtensions.

protected List<? extends Extension> loadExtensions(final ClassLoader classLoader) {
    final List<Extension> list = loaderService.load(Extension.class, classLoader);
    final Collection<String> 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<Extension> extensionCopy = new ArrayList<>(list);
    final Iterator<Extension> 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<Extension>() {

            @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;
}
Also used : Extension(javax.enterprise.inject.spi.Extension) JMS2CDIExtension(org.apache.openejb.resource.activemq.jms2.cdi.JMS2CDIExtension) ArrayList(java.util.ArrayList) JMS2CDIExtension(org.apache.openejb.resource.activemq.jms2.cdi.JMS2CDIExtension)

Example 3 with Extension

use of javax.enterprise.inject.spi.Extension in project wildfly by wildfly.

the class WeldPortableExtensionProcessor method loadAttachments.

private void loadAttachments(Module module, DeploymentUnit deploymentUnit, WeldPortableExtensions extensions) throws DeploymentUnitProcessingException {
    // now load extensions
    try {
        Enumeration<URL> resources = module.getClassLoader().getResources("META-INF/services/" + Extension.class.getName());
        final List<String> services = new ArrayList<>();
        while (resources.hasMoreElements()) {
            URL resource = resources.nextElement();
            final InputStream stream = resource.openStream();
            try {
                final BufferedReader reader = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8));
                String line;
                while ((line = reader.readLine()) != null) {
                    final int commentIdx = line.indexOf('#');
                    final String className;
                    if (commentIdx == -1) {
                        className = line.trim();
                    } else {
                        className = line.substring(0, commentIdx).trim();
                    }
                    if (className.length() == 0) {
                        continue;
                    }
                    services.add(className);
                }
            } finally {
                VFSUtils.safeClose(stream);
            }
        }
        for (String service : services) {
            final Class<Extension> extensionClass = loadExtension(service, module.getClassLoader());
            if (extensionClass == null) {
                continue;
            }
            extensions.tryRegisterExtension(extensionClass, deploymentUnit);
        }
    } catch (IOException e) {
        throw new DeploymentUnitProcessingException(e);
    }
}
Also used : DeploymentUnitProcessingException(org.jboss.as.server.deployment.DeploymentUnitProcessingException) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) URL(java.net.URL) Extension(javax.enterprise.inject.spi.Extension) BufferedReader(java.io.BufferedReader)

Example 4 with Extension

use of javax.enterprise.inject.spi.Extension in project wildfly by wildfly.

the class EnterpriseBeanDiscoveryTest method createTestArchive.

@Deployment
public static EnterpriseArchive createTestArchive() {
    // 1.1 version beans.xml with bean-discovery-mode of all
    JavaArchive alpha = ShrinkWrap.create(JavaArchive.class, ALPHA_JAR).addClasses(Alpha.class, AlphaLocal.class).addAsManifestResource(newBeans11Descriptor("all"), "beans.xml");
    // Empty beans.xml
    JavaArchive bravo = ShrinkWrap.create(JavaArchive.class, BRAVO_JAR).addClasses(Bravo.class, BravoLocal.class).addAsManifestResource(new StringAsset(""), "beans.xml");
    // No version beans.xml
    JavaArchive charlie = ShrinkWrap.create(JavaArchive.class, CHARLIE_JAR).addClasses(Charlie.class, CharlieLocal.class).addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
    // Session bean and no beans.xml
    JavaArchive delta = ShrinkWrap.create(JavaArchive.class, DELTA_JAR).addClasses(Delta.class, DeltaLocal.class);
    // Session bean and 1.1 version beans.xml with bean-discovery-mode of annotated
    JavaArchive echo = ShrinkWrap.create(JavaArchive.class, ECHO_JAR).addClasses(Echo.class, EchoLocal.class).addAsManifestResource(newBeans11Descriptor("annotated"), "beans.xml");
    // Session bean and 1.1 version beans.xml with bean-discovery-mode of none
    JavaArchive foxtrot = ShrinkWrap.create(JavaArchive.class, FOXTROT_JAR).addClasses(Foxtrot.class, FoxtrotLocal.class).addAsManifestResource(newBeans11Descriptor("none"), "beans.xml");
    // Archive which contains an extension and no beans.xml file - not a bean archive
    JavaArchive legacy = ShrinkWrap.create(JavaArchive.class, LEGACY_JAR).addClasses(LegacyExtension.class, LegacyBean.class).addAsServiceProvider(Extension.class, LegacyExtension.class);
    StringBuilder manifestBuilder = new StringBuilder("Class-Path:");
    for (String s : new String[] { ALPHA_JAR, BRAVO_JAR, CHARLIE_JAR, DELTA_JAR, ECHO_JAR, FOXTROT_JAR, LEGACY_JAR }) {
        manifestBuilder.append(" ");
        manifestBuilder.append(s);
    }
    WebArchive webArchive = ShrinkWrap.create(WebArchive.class).addClasses(EnterpriseBeanDiscoveryTest.class, VerifyingExtension.class).addAsServiceProvider(Extension.class, VerifyingExtension.class).setManifest(new StringAsset(manifestBuilder.toString()));
    return ShrinkWrap.create(EnterpriseArchive.class).addAsModules(webArchive, alpha, bravo, charlie, delta, echo, foxtrot, legacy).addAsLibrary(ShrinkWrap.create(JavaArchive.class).addClass(Ping.class));
}
Also used : StringAsset(org.jboss.shrinkwrap.api.asset.StringAsset) WebArchive(org.jboss.shrinkwrap.api.spec.WebArchive) JavaArchive(org.jboss.shrinkwrap.api.spec.JavaArchive) Extension(javax.enterprise.inject.spi.Extension) Deployment(org.jboss.arquillian.container.test.api.Deployment)

Aggregations

Extension (javax.enterprise.inject.spi.Extension)4 URL (java.net.URL)2 ArrayList (java.util.ArrayList)2 JMS2CDIExtension (org.apache.openejb.resource.activemq.jms2.cdi.JMS2CDIExtension)2 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 SystemInstance (org.apache.openejb.loader.SystemInstance)1 Deployment (org.jboss.arquillian.container.test.api.Deployment)1 DeploymentUnitProcessingException (org.jboss.as.server.deployment.DeploymentUnitProcessingException)1 StringAsset (org.jboss.shrinkwrap.api.asset.StringAsset)1 JavaArchive (org.jboss.shrinkwrap.api.spec.JavaArchive)1 WebArchive (org.jboss.shrinkwrap.api.spec.WebArchive)1