Search in sources :

Example 31 with BeansXml

use of org.jboss.weld.bootstrap.spi.BeansXml in project Payara by payara.

the class BeanDeploymentArchiveImpl method populate.

private void populate(Collection<com.sun.enterprise.deployment.EjbDescriptor> ejbs, Application app) {
    try {
        boolean webinfbda = false;
        boolean hasBeansXml = false;
        String beansXMLURL = null;
        if (archive.exists(WEB_INF_BEANS_XML)) {
            beansXMLURL = WEB_INF_BEANS_XML;
        }
        if (beansXMLURL == null && archive.exists(WEB_INF_CLASSES_META_INF_BEANS_XML)) {
            beansXMLURL = WEB_INF_CLASSES_META_INF_BEANS_XML;
        }
        if (beansXMLURL != null) {
            // Parse the descriptor to determine if CDI is disabled
            BeansXml beansXML = parseBeansXML(archive, beansXMLURL);
            BeanDiscoveryMode bdMode = beansXML.getBeanDiscoveryMode();
            if (!bdMode.equals(BeanDiscoveryMode.NONE)) {
                webinfbda = true;
                // If the mode is explicitly set to "annotated", then pretend there is no beans.xml
                // to force the implicit behavior
                hasBeansXml = !bdMode.equals(BeanDiscoveryMode.ANNOTATED);
                if (logger.isLoggable(FINE)) {
                    logger.log(FINE, CDILoggerInfo.PROCESSING_BEANS_XML, new Object[] { archive.getURI(), WEB_INF_BEANS_XML, WEB_INF_CLASSES_META_INF_BEANS_XML });
                }
            } else {
                addBeansXMLURL(archive, beansXMLURL);
            }
        } else if (archive.exists(WEB_INF_CLASSES)) {
            // If WEB-INF/classes exists, check for CDI beans there
            // Check WEB-INF/classes for CDI-enabling annotations
            URI webinfclasses = new File(context.getSourceDir().getAbsolutePath(), WEB_INF_CLASSES).toURI();
            if (WeldUtils.isImplicitBeanArchive(context, webinfclasses)) {
                webinfbda = true;
                if (logger.isLoggable(FINE)) {
                    logger.log(FINE, CDILoggerInfo.PROCESSING_CDI_ENABLED_ARCHIVE, new Object[] { archive.getURI() });
                }
            }
        }
        if (webinfbda) {
            bdaType = BDAType.WAR;
            Enumeration<String> entries = archive.entries();
            while (entries.hasMoreElements()) {
                String entry = entries.nextElement();
                if (legalClassName(entry)) {
                    if (entry.contains(WEB_INF_CLASSES)) {
                        // Workaround for incorrect WARs that bundle classes above WEB-INF/classes
                        // [See. GLASSFISH-16706]
                        entry = entry.substring(WEB_INF_CLASSES.length() + 1);
                    }
                    String className = filenameToClassname(entry);
                    try {
                        if (hasBeansXml || isCDIAnnotatedClass(className)) {
                            beanClassNames.add(className);
                            beanClasses.add(getClassLoader().loadClass(className));
                        }
                        moduleClassNames.add(className);
                    } catch (Throwable t) {
                        if (logger.isLoggable(Level.WARNING)) {
                            logger.log(Level.WARNING, CDILoggerInfo.ERROR_LOADING_BEAN_CLASS, new Object[] { className, t.toString() });
                        }
                    }
                } else if (entry.endsWith(BEANS_XML_FILENAME)) {
                    addBeansXMLURL(archive, entry);
                }
            }
            archive.close();
        }
        if (archive.exists(WEB_INF_LIB)) {
            if (logger.isLoggable(FINE)) {
                logger.log(FINE, CDILoggerInfo.PROCESSING_WEB_INF_LIB, new Object[] { archive.getURI() });
            }
            bdaType = BDAType.WAR;
            Enumeration<String> entries = archive.entries(WEB_INF_LIB);
            List<ReadableArchive> weblibJarsThatAreBeanArchives = new ArrayList<ReadableArchive>();
            while (entries.hasMoreElements()) {
                String entry = (String) entries.nextElement();
                // if directly under WEB-INF/lib
                if (entry.endsWith(JAR_SUFFIX) && entry.indexOf(SEPARATOR_CHAR, WEB_INF_LIB.length() + 1) == -1 && (app == null || DOLUtils.isScanningAllowed(app, entry))) {
                    ReadableArchive weblibJarArchive = archive.getSubArchive(entry);
                    if (weblibJarArchive != null && weblibJarArchive.exists(META_INF_BEANS_XML)) {
                        // Parse the descriptor to determine if CDI is disabled
                        BeansXml beansXML = parseBeansXML(weblibJarArchive, META_INF_BEANS_XML);
                        BeanDiscoveryMode bdMode = beansXML.getBeanDiscoveryMode();
                        if (!bdMode.equals(BeanDiscoveryMode.NONE)) {
                            if (logger.isLoggable(FINE)) {
                                logger.log(FINE, CDILoggerInfo.WEB_INF_LIB_CONSIDERING_BEAN_ARCHIVE, new Object[] { entry });
                            }
                            weblibJarsThatAreBeanArchives.add(weblibJarArchive);
                        }
                    } else {
                        // Check for classes annotated with qualified annotations
                        if (WeldUtils.isImplicitBeanArchive(context, weblibJarArchive)) {
                            if (logger.isLoggable(FINE)) {
                                logger.log(FINE, CDILoggerInfo.WEB_INF_LIB_CONSIDERING_BEAN_ARCHIVE, new Object[] { entry });
                            }
                            weblibJarsThatAreBeanArchives.add(weblibJarArchive);
                        } else {
                            if (logger.isLoggable(FINE)) {
                                logger.log(FINE, CDILoggerInfo.WEB_INF_LIB_SKIPPING_BEAN_ARCHIVE, new Object[] { archive.getName() });
                            }
                        }
                    }
                }
            }
            // process all web-inf lib JARs and create BDAs for them
            List<BeanDeploymentArchiveImpl> webLibBDAs = new ArrayList<BeanDeploymentArchiveImpl>();
            if (weblibJarsThatAreBeanArchives.size() > 0) {
                ListIterator<ReadableArchive> libJarIterator = weblibJarsThatAreBeanArchives.listIterator();
                while (libJarIterator.hasNext()) {
                    ReadableArchive libJarArchive = (ReadableArchive) libJarIterator.next();
                    BeanDeploymentArchiveImpl wlbda = new BeanDeploymentArchiveImpl(libJarArchive, ejbs, context, makeBdaId(friendlyId, bdaType, libJarArchive.getName()));
                    // add to list of BDAs for this WAR
                    this.beanDeploymentArchives.add(wlbda);
                    webLibBDAs.add(wlbda);
                }
            }
            ensureWebLibJarVisibility(webLibBDAs);
        } else if (archive.getName().endsWith(RAR_SUFFIX) || archive.getName().endsWith(EXPANDED_RAR_SUFFIX)) {
            // Handle RARs. RARs are packaged differently from EJB-JARs or WARs.
            // see 20.2 of Connectors 1.6 specification
            // The resource adapter classes are in a jar file within the
            // RAR archive
            bdaType = BDAType.RAR;
            collectRarInfo(archive);
        } else if (archive.exists(META_INF_BEANS_XML)) {
            // Parse the descriptor to determine if CDI is disabled
            BeansXml beansXML = parseBeansXML(archive, META_INF_BEANS_XML);
            BeanDiscoveryMode bdMode = beansXML.getBeanDiscoveryMode();
            if (!bdMode.equals(BeanDiscoveryMode.NONE)) {
                if (logger.isLoggable(FINE)) {
                    logger.log(FINE, CDILoggerInfo.PROCESSING_BDA_JAR, new Object[] { archive.getURI() });
                }
                bdaType = BDAType.JAR;
                collectJarInfo(archive, true, !bdMode.equals(BeanDiscoveryMode.ANNOTATED));
            } else {
                addBeansXMLURL(archive, META_INF_BEANS_XML);
            }
        } else if (WeldUtils.isImplicitBeanArchive(context, archive)) {
            if (logger.isLoggable(FINE)) {
                logger.log(FINE, CDILoggerInfo.PROCESSING_BECAUSE_SCOPE_ANNOTATION, new Object[] { archive.getURI() });
            }
            bdaType = BDAType.JAR;
            collectJarInfo(archive, true, false);
        }
    // This is causing tck failures, specifically
    // MultiModuleProcessingTest.testProcessedModulesCount
    // creating a bda for an extionsion that does not include a beans.xml is handled later
    // when annotated types are created by that extension.  This is done in
    // DeploymentImpl.loadBeanDeploymentArchive(Class<?> beanClass)
    // if (archive.exists(META_INF_SERVICES_EXTENSION)){
    // if ( logger.isLoggable( FINE ) ) {
    // logger.log(FINE, "-JAR processing: " + archive.getURI()
    // + " as an extensions jar since it has META-INF/services extension");
    // }
    // bdaType = BDAType.UNKNOWN;
    // collectJarInfo(archive, false);
    // }
    } catch (IOException e) {
        logger.log(SEVERE, e.getLocalizedMessage(), e);
    } catch (ClassNotFoundException cne) {
        logger.log(SEVERE, cne.getLocalizedMessage(), cne);
    }
}
Also used : CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) IOException(java.io.IOException) URI(java.net.URI) BeanDiscoveryMode(org.jboss.weld.bootstrap.spi.BeanDiscoveryMode) BeansXml(org.jboss.weld.bootstrap.spi.BeansXml) ReadableArchive(org.glassfish.api.deployment.archive.ReadableArchive) File(java.io.File)

Example 32 with BeansXml

use of org.jboss.weld.bootstrap.spi.BeansXml in project Payara by payara.

the class DeploymentImpl method createLibJarBda.

private void createLibJarBda(RootBeanDeploymentArchive rootLibBda) {
    BeanDeploymentArchive libModuleBda = rootLibBda.getModuleBda();
    BeansXml moduleBeansXml = libModuleBda.getBeansXml();
    if (moduleBeansXml == null || !moduleBeansXml.getBeanDiscoveryMode().equals(BeanDiscoveryMode.NONE)) {
        addBdaToDeploymentBdas(rootLibBda);
        addBdaToDeploymentBdas(libModuleBda);
        if (libJarRootBdas == null) {
            libJarRootBdas = new ArrayList<>();
        }
        for (RootBeanDeploymentArchive existingLibJarRootBda : libJarRootBdas) {
            rootLibBda.getBeanDeploymentArchives().add(existingLibJarRootBda);
            rootLibBda.getBeanDeploymentArchives().add(existingLibJarRootBda.getModuleBda());
            rootLibBda.getModuleBda().getBeanDeploymentArchives().add(existingLibJarRootBda);
            rootLibBda.getModuleBda().getBeanDeploymentArchives().add(existingLibJarRootBda.getModuleBda());
            existingLibJarRootBda.getBeanDeploymentArchives().add(rootLibBda);
            existingLibJarRootBda.getBeanDeploymentArchives().add(rootLibBda.getModuleBda());
            existingLibJarRootBda.getModuleBda().getBeanDeploymentArchives().add(rootLibBda);
            existingLibJarRootBda.getModuleBda().getBeanDeploymentArchives().add(rootLibBda.getModuleBda());
        }
        libJarRootBdas.add(rootLibBda);
    }
}
Also used : BeansXml(org.jboss.weld.bootstrap.spi.BeansXml) BeanDeploymentArchive(org.jboss.weld.bootstrap.spi.BeanDeploymentArchive)

Example 33 with BeansXml

use of org.jboss.weld.bootstrap.spi.BeansXml in project Payara by payara.

the class DeploymentImpl method loadBeanDeploymentArchive.

@Override
public BeanDeploymentArchive loadBeanDeploymentArchive(Class<?> beanClass) {
    if (logger.isLoggable(FINE)) {
        logger.log(FINE, CDILoggerInfo.LOAD_BEAN_DEPLOYMENT_ARCHIVE, new Object[] { beanClass });
    }
    List<BeanDeploymentArchive> beanDeploymentArchives = getBeanDeploymentArchives();
    ListIterator<BeanDeploymentArchive> lIter = beanDeploymentArchives.listIterator();
    while (lIter.hasNext()) {
        BeanDeploymentArchive bda = lIter.next();
        if (logger.isLoggable(FINE)) {
            logger.log(FINE, CDILoggerInfo.LOAD_BEAN_DEPLOYMENT_ARCHIVE_CHECKING, new Object[] { beanClass, bda.getId() });
        }
        if (((BeanDeploymentArchiveImpl) bda).getModuleBeanClasses().contains(beanClass.getName())) {
            // as Weld automatically add theses classes to the BDA's bean Classes
            if (logger.isLoggable(FINE)) {
                logger.log(FINE, CDILoggerInfo.LOAD_BEAN_DEPLOYMENT_ARCHIVE_ADD_TO_EXISTING, new Object[] { beanClass.getName(), bda });
            }
            return bda;
        }
        // and get the right BDA for the beanClass
        if (!bda.getBeanDeploymentArchives().isEmpty()) {
            for (BeanDeploymentArchive subBda : bda.getBeanDeploymentArchives()) {
                Collection<String> moduleBeanClassNames = ((BeanDeploymentArchiveImpl) subBda).getModuleBeanClasses();
                if (logger.isLoggable(FINE)) {
                    logger.log(FINE, CDILoggerInfo.LOAD_BEAN_DEPLOYMENT_ARCHIVE_CHECKING_SUBBDA, new Object[] { beanClass, subBda.getId() });
                }
                boolean match = moduleBeanClassNames.contains(beanClass.getName());
                if (match) {
                    // as Weld automatically add theses classes to the BDA's bean Classes
                    if (logger.isLoggable(FINE)) {
                        logger.log(FINE, CDILoggerInfo.LOAD_BEAN_DEPLOYMENT_ARCHIVE_ADD_TO_EXISTING, new Object[] { beanClass.getName(), subBda });
                    }
                    return subBda;
                }
            }
        }
    }
    BeanDeploymentArchive extensionBDA = extensionBDAMap.get(beanClass.getClassLoader());
    if (extensionBDA != null) {
        return extensionBDA;
    }
    // If the BDA was not found for the Class, create one and add it
    if (logger.isLoggable(FINE)) {
        logger.log(FINE, CDILoggerInfo.LOAD_BEAN_DEPLOYMENT_ARCHIVE_CREATE_NEW_BDA, new Object[] { beanClass });
    }
    List<Class<?>> beanClasses = new ArrayList<>();
    List<URL> beanXMLUrls = new CopyOnWriteArrayList<>();
    Set<EjbDescriptor> ejbs = new HashSet<>();
    beanClasses.add(beanClass);
    BeanDeploymentArchive newBda = new BeanDeploymentArchiveImpl(beanClass.getName(), beanClasses, beanXMLUrls, ejbs, context);
    // have to create new InjectionServicesImpl for each new BDA so injection context is propagated for the correct bundle
    newBda.getServices().add(InjectionServices.class, new InjectionServicesImpl(injectionManager, DOLUtils.getCurrentBundleForContext(context), this));
    newBda.getServices().add(ResourceInjectionServices.class, new ResourceInjectionServicesImpl());
    BeansXml beansXml = newBda.getBeansXml();
    if (beansXml == null || !beansXml.getBeanDiscoveryMode().equals(BeanDiscoveryMode.NONE)) {
        if (logger.isLoggable(FINE)) {
            logger.log(FINE, CDILoggerInfo.LOAD_BEAN_DEPLOYMENT_ARCHIVE_ADD_NEW_BDA_TO_ROOTS, new Object[] {});
        }
        lIter = beanDeploymentArchives.listIterator();
        while (lIter.hasNext()) {
            BeanDeploymentArchive bda = lIter.next();
            bda.getBeanDeploymentArchives().add(newBda);
        }
        if (logger.isLoggable(FINE)) {
            logger.log(FINE, CDILoggerInfo.LOAD_BEAN_DEPLOYMENT_ARCHIVE_RETURNING_NEWLY_CREATED_BDA, new Object[] { beanClass, newBda });
        }
        beanDeploymentArchives.add(newBda);
        // Make all previously found extension BDAs visible to this extension BDA
        newBda.getBeanDeploymentArchives().addAll(extensionBDAMap.values());
        idToBeanDeploymentArchive.put(newBda.getId(), newBda);
        extensionBDAMap.put(beanClass.getClassLoader(), newBda);
        return newBda;
    }
    return null;
}
Also used : CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) URL(java.net.URL) EjbDescriptor(com.sun.enterprise.deployment.EjbDescriptor) ResourceInjectionServicesImpl(org.glassfish.weld.services.ResourceInjectionServicesImpl) BeansXml(org.jboss.weld.bootstrap.spi.BeansXml) BeanDeploymentArchive(org.jboss.weld.bootstrap.spi.BeanDeploymentArchive) InjectionServicesImpl(org.glassfish.weld.services.InjectionServicesImpl) ResourceInjectionServicesImpl(org.glassfish.weld.services.ResourceInjectionServicesImpl) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 34 with BeansXml

use of org.jboss.weld.bootstrap.spi.BeansXml in project HotswapAgent by HotswapProjects.

the class BeanClassRefreshAgent method registerArchive.

/**
 * Register bean archive into BdaAgentRegistry and into WeldPlugin. Current classLoader is  set to
 * beanArchive classLoader.
 *
 * @param appClassLoader the class loader - container or application class loader.
 * @param beanArchive the bean archive to be registered
 * @param beanArchiveType the bean archive type
 */
public static void registerArchive(ClassLoader appClassLoader, BeanDeploymentArchive beanArchive, String beanArchiveType) {
    BeansXml beansXml = beanArchive.getBeansXml();
    if (beansXml != null && beansXml.getUrl() != null && (beanArchiveType == null || "EXPLICIT".equals(beanArchiveType) || "IMPLICIT".equals(beanArchiveType))) {
        String archivePath = null;
        String beansXmlPath = beansXml.getUrl().getPath();
        if (beansXmlPath.endsWith("META-INF/beans.xml")) {
            archivePath = beansXmlPath.substring(0, beansXmlPath.length() - "META-INF/beans.xml".length());
        } else if (beansXmlPath.endsWith("WEB-INF/beans.xml")) {
            archivePath = beansXmlPath.substring(0, beansXmlPath.length() - "beans.xml".length()) + "classes";
        }
        if (archivePath.endsWith(".jar!/")) {
            archivePath = archivePath.substring(0, archivePath.length() - "!/".length());
        }
        BeanClassRefreshAgent bdaAgent = null;
        try {
            LOGGER.debug("BeanClassRefreshAgent registerArchive bdaId='{}' archivePath='{}'.", beanArchive.getId(), archivePath);
            // check that it is regular file
            // toString() is weird and solves HiearchicalUriException for URI like "file:./src/resources/file.txt".
            @SuppressWarnings("unused") File path = new File(archivePath);
            Class<?> registryClass = Class.forName(BdaAgentRegistry.class.getName(), true, appClassLoader);
            boolean contain = (boolean) ReflectionHelper.invoke(null, registryClass, "contains", new Class[] { String.class }, archivePath);
            if (!contain) {
                bdaAgent = new BeanClassRefreshAgent(beanArchive, archivePath);
                ReflectionHelper.invoke(null, registryClass, "put", new Class[] { String.class, BeanClassRefreshAgent.class }, archivePath, bdaAgent);
                bdaAgent.register();
            }
        } catch (IllegalArgumentException e) {
            LOGGER.warning("Unable to watch BeanDeploymentArchive with id={}", beanArchive.getId());
        } catch (Exception e) {
            LOGGER.error("Register archive failed.", e.getMessage());
        }
    } else {
    // TODO:
    }
}
Also used : BeansXml(org.jboss.weld.bootstrap.spi.BeansXml) File(java.io.File) IOException(java.io.IOException)

Aggregations

BeansXml (org.jboss.weld.bootstrap.spi.BeansXml)34 Test (org.testng.annotations.Test)12 URL (java.net.URL)9 BeanDeploymentArchive (org.jboss.weld.bootstrap.spi.BeanDeploymentArchive)6 ArrayList (java.util.ArrayList)5 File (java.io.File)4 Test (org.junit.Test)4 HashSet (java.util.HashSet)3 Metadata (org.jboss.weld.bootstrap.spi.Metadata)3 EjbDescriptor (com.sun.enterprise.deployment.EjbDescriptor)2 IOException (java.io.IOException)2 MalformedURLException (java.net.MalformedURLException)2 URI (java.net.URI)2 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)2 ZipFile (java.util.zip.ZipFile)2 Bean (javax.enterprise.inject.spi.Bean)2 BeanManager (javax.enterprise.inject.spi.BeanManager)2 DeploymentContext (org.glassfish.api.deployment.DeploymentContext)2 ReadableArchive (org.glassfish.api.deployment.archive.ReadableArchive)2 BeanDeploymentArchiveImpl (org.jboss.arquillian.container.weld.embedded.mock.BeanDeploymentArchiveImpl)2