<commit_before>/**
 *  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.geronimo.console.configcreator;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.jar.JarFile;

import javax.enterprise.deploy.model.DDBean;
import javax.enterprise.deploy.model.DDBeanRoot;
import javax.enterprise.deploy.model.exceptions.DDBeanCreateException;
import javax.enterprise.deploy.shared.factories.DeploymentFactoryManager;
import javax.enterprise.deploy.spi.DeploymentConfiguration;
import javax.enterprise.deploy.spi.DeploymentManager;
import javax.enterprise.deploy.spi.Target;
import javax.enterprise.deploy.spi.exceptions.ConfigurationException;
import javax.enterprise.deploy.spi.exceptions.DeploymentManagerCreationException;
import javax.enterprise.deploy.spi.exceptions.InvalidModuleException;
import javax.enterprise.deploy.spi.factories.DeploymentFactory;
import javax.enterprise.deploy.spi.status.ProgressObject;
import javax.management.ObjectName;
import javax.portlet.PortletException;
import javax.portlet.PortletRequest;

import org.apache.geronimo.common.DeploymentException;
import org.apache.geronimo.connector.deployment.AdminObjectRefBuilder.AdminObjectRefProcessor;
import org.apache.geronimo.connector.deployment.ResourceRefBuilder.ResourceRefProcessor;
import org.apache.geronimo.console.configcreator.AbstractHandler.ReferenceData;
import org.apache.geronimo.console.configcreator.AbstractHandler.WARConfigData;
import org.apache.geronimo.console.configcreator.JSR77_Util.ReferredData;
import org.apache.geronimo.console.util.PortletManager;
import org.apache.geronimo.deployment.ConfigurationBuilder;
import org.apache.geronimo.deployment.ModuleIDBuilder;
import org.apache.geronimo.deployment.plugin.factories.DeploymentFactoryWithKernel;
import org.apache.geronimo.deployment.plugin.jmx.JMXDeploymentManager;
import org.apache.geronimo.deployment.service.jsr88.EnvironmentData;
import org.apache.geronimo.deployment.tools.loader.WebDeployable;
import org.apache.geronimo.j2ee.ApplicationInfo;
import org.apache.geronimo.j2ee.deployment.Module;
import org.apache.geronimo.j2ee.deployment.annotation.AnnotatedApp;
import org.apache.geronimo.j2ee.deployment.annotation.AnnotatedWebApp;
import org.apache.geronimo.j2ee.deployment.annotation.EJBAnnotationHelper;
import org.apache.geronimo.j2ee.deployment.annotation.HandlerChainAnnotationHelper;
import org.apache.geronimo.j2ee.deployment.annotation.PersistenceContextAnnotationHelper;
import org.apache.geronimo.j2ee.deployment.annotation.PersistenceUnitAnnotationHelper;
import org.apache.geronimo.j2ee.deployment.annotation.ResourceAnnotationHelper;
import org.apache.geronimo.j2ee.deployment.annotation.SecurityAnnotationHelper;
import org.apache.geronimo.j2ee.deployment.annotation.WebServiceRefAnnotationHelper;
import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
import org.apache.geronimo.kernel.Kernel;
import org.apache.geronimo.kernel.config.ConfigurationManager;
import org.apache.geronimo.kernel.repository.Artifact;
import org.apache.geronimo.naming.deployment.EnvironmentEntryBuilder.EnvEntryRefProcessor;
import org.apache.geronimo.naming.deployment.SwitchingServiceRefBuilder.ServiceRefProcessor;
import org.apache.geronimo.naming.deployment.jsr88.EjbLocalRef;
import org.apache.geronimo.naming.deployment.jsr88.EjbRef;
import org.apache.geronimo.naming.deployment.jsr88.MessageDestination;
import org.apache.geronimo.naming.deployment.jsr88.Pattern;
import org.apache.geronimo.naming.deployment.jsr88.ResourceEnvRef;
import org.apache.geronimo.naming.deployment.jsr88.ResourceRef;
import org.apache.geronimo.web.deployment.WebAppDConfigBean;
import org.apache.geronimo.web.deployment.WebAppDConfigRoot;
import org.apache.geronimo.web25.deployment.AbstractWebModuleBuilder;
import org.apache.geronimo.xbeans.geronimo.naming.GerServiceRefType;
import org.apache.geronimo.xbeans.geronimo.security.GerRoleType;
import org.apache.geronimo.xbeans.geronimo.security.GerSecurityType;
import org.apache.geronimo.xbeans.javaee.EjbLocalRefType;
import org.apache.geronimo.xbeans.javaee.EjbRefType;
import org.apache.geronimo.xbeans.javaee.MessageDestinationType;
import org.apache.geronimo.xbeans.javaee.ResourceEnvRefType;
import org.apache.geronimo.xbeans.javaee.ResourceRefType;
import org.apache.geronimo.xbeans.javaee.SecurityRoleType;
import org.apache.geronimo.xbeans.javaee.ServiceRefType;
import org.apache.geronimo.xbeans.javaee.WebAppType;
import org.apache.xbean.finder.ClassFinder;

/**
 * Util class for JSR-88 related functions
 * 
 * @version $Rev$ $Date$
 */
public class JSR88_Util {

    private static void parseAnnotations(AnnotatedApp annotatedApp, ClassFinder classFinder) throws Exception {
        ResourceAnnotationHelper.processAnnotations(annotatedApp, classFinder, EnvEntryRefProcessor.INSTANCE);
        WebServiceRefAnnotationHelper.processAnnotations(annotatedApp, classFinder);
        HandlerChainAnnotationHelper.processAnnotations(annotatedApp, classFinder);
        ResourceAnnotationHelper.processAnnotations(annotatedApp, classFinder, ServiceRefProcessor.INSTANCE);
        ResourceAnnotationHelper.processAnnotations(annotatedApp, classFinder, AdminObjectRefProcessor.INSTANCE);
        ResourceAnnotationHelper.processAnnotations(annotatedApp, classFinder, ResourceRefProcessor.INSTANCE);
        PersistenceContextAnnotationHelper.processAnnotations(annotatedApp, classFinder);
        PersistenceUnitAnnotationHelper.processAnnotations(annotatedApp, classFinder);
        EJBAnnotationHelper.processAnnotations(annotatedApp, classFinder);
        if (annotatedApp instanceof AnnotatedWebApp) {
            SecurityAnnotationHelper.processAnnotations(((AnnotatedWebApp) annotatedApp).getWebApp(), classFinder);
        }
    }

    private static ConfigurationBuilder getConfigurationBuilder(PortletRequest request) {
        Object[] builders = PortletManager.getGBeansImplementing(request, ConfigurationBuilder.class);
        ConfigurationBuilder configurationBuilder = null;
        for (int i = 0; i < builders.length; i++) {
            ObjectName objectName = PortletManager.getNameFor(request, builders[i]).getObjectName();
            if ("EARBuilder".equalsIgnoreCase(objectName.getKeyProperty(NameFactory.J2EE_NAME))) {
                configurationBuilder = (ConfigurationBuilder) builders[i];
                break;
            }
        }
        return configurationBuilder;
    }

    private static ApplicationInfo createApplicationInfo(PortletRequest request, URL moduleUrl) throws Exception {
        ConfigurationBuilder configurationBuilderBuilder = getConfigurationBuilder(request);
        File moduleFile = new File(moduleUrl.toURI());
        JarFile moduleJar = new JarFile(moduleFile);
        ApplicationInfo applicationInfo = (ApplicationInfo) configurationBuilderBuilder.getDeploymentPlan(null, moduleJar,
                new ModuleIDBuilder());
        return applicationInfo;
    }

    public static void parseWarReferences(PortletRequest request, WARConfigData data, URL warUrl)
            throws Exception {
        WebDeployable webDeployable = new WebDeployable(warUrl);
        ClassLoader classLoader = webDeployable.getModuleLoader();

        ApplicationInfo applicationInfo = createApplicationInfo(request, warUrl);
        Module module = (Module) (applicationInfo.getModules().toArray()[0]);
        WebAppType webApp = (WebAppType) module.getSpecDD();

        ClassFinder classFinder = null;
        try {
            classFinder = AbstractWebModuleBuilder.createWebAppClassFinder(webApp, classLoader);
            // classFinder = new ClassFinder(classLoader);
        } catch (DeploymentException e1) {
            // Some of the class types referred in the WAR cannot be resolved.
            // A typical case would be references to EJBs already deployed into the system, and
            // hence not packaged inside WEB-INF/lib directory of WAR.
            // try adding all EJBs deployed in the system as parents of this WAR, and
            // see if referred classes can now be successfully resolved

            classLoader = new WebDeployable(warUrl, getEjbClassLoaders(request)).getModuleLoader();
            try {
                classFinder = AbstractWebModuleBuilder.createWebAppClassFinder(webApp, classLoader);
            } catch (DeploymentException e2) {
                return;
            }
        }
        AnnotatedApp annotatedApp = module.getAnnotatedApp();
        parseAnnotations(annotatedApp, classFinder);

        //DDBeanRoot ddBeanRoot = webDeployable.getDDBeanRoot();
        //DDBean ddBean = ddBeanRoot.getChildBean("web-app")[0];

        EjbRefType[] ejbRefs = annotatedApp.getEjbRefArray();
        for (int i = 0; i < ejbRefs.length; i++) {
            String refName = ejbRefs[i].getEjbRefName().getStringValue();
            data.getEjbRefs().add(new ReferenceData(refName));
        }
        /*DDBean[] ddBeans = ddBean.getChildBean("ejb-ref");
        for (int i = 0; ddBeans != null && i < ddBeans.length; i++) {
            String refName = ddBeans[i].getChildBean("ejb-ref-name")[0].getText();
            data.getEjbRefs().add(new ReferenceData(refName));
        }*/

        EjbLocalRefType[] ejbLocalRefs = annotatedApp.getEjbLocalRefArray();
        for (int i = 0; i < ejbLocalRefs.length; i++) {
            String refName = ejbLocalRefs[i].getEjbRefName().getStringValue();
            data.getEjbLocalRefs().add(new ReferenceData(refName));
        }
        /*ddBeans = ddBean.getChildBean("ejb-local-ref");
        for (int i = 0; ddBeans != null && i < ddBeans.length; i++) {
            String refName = ddBeans[i].getChildBean("ejb-ref-name")[0].getText();
            data.getEjbLocalRefs().add(new ReferenceData(refName));
        }*/

        ResourceRefType[] resourceRefs = annotatedApp.getResourceRefArray();<commit_msg>GERONIMO-3850 Plan Creator suppresses exceptions rather than showing it to user<commit_after>/**
 *  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.geronimo.console.configcreator;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.jar.JarFile;

import javax.enterprise.deploy.model.DDBean;
import javax.enterprise.deploy.model.DDBeanRoot;
import javax.enterprise.deploy.model.exceptions.DDBeanCreateException;
import javax.enterprise.deploy.shared.factories.DeploymentFactoryManager;
import javax.enterprise.deploy.spi.DeploymentConfiguration;
import javax.enterprise.deploy.spi.DeploymentManager;
import javax.enterprise.deploy.spi.Target;
import javax.enterprise.deploy.spi.exceptions.ConfigurationException;
import javax.enterprise.deploy.spi.exceptions.DeploymentManagerCreationException;
import javax.enterprise.deploy.spi.exceptions.InvalidModuleException;
import javax.enterprise.deploy.spi.factories.DeploymentFactory;
import javax.enterprise.deploy.spi.status.ProgressObject;
import javax.management.ObjectName;
import javax.portlet.PortletException;
import javax.portlet.PortletRequest;

import org.apache.geronimo.common.DeploymentException;
import org.apache.geronimo.connector.deployment.AdminObjectRefBuilder.AdminObjectRefProcessor;
import org.apache.geronimo.connector.deployment.ResourceRefBuilder.ResourceRefProcessor;
import org.apache.geronimo.console.configcreator.AbstractHandler.ReferenceData;
import org.apache.geronimo.console.configcreator.AbstractHandler.WARConfigData;
import org.apache.geronimo.console.configcreator.JSR77_Util.ReferredData;
import org.apache.geronimo.console.util.PortletManager;
import org.apache.geronimo.deployment.ConfigurationBuilder;
import org.apache.geronimo.deployment.ModuleIDBuilder;
import org.apache.geronimo.deployment.plugin.factories.DeploymentFactoryWithKernel;
import org.apache.geronimo.deployment.plugin.jmx.JMXDeploymentManager;
import org.apache.geronimo.deployment.service.jsr88.EnvironmentData;
import org.apache.geronimo.deployment.tools.loader.WebDeployable;
import org.apache.geronimo.j2ee.ApplicationInfo;
import org.apache.geronimo.j2ee.deployment.Module;
import org.apache.geronimo.j2ee.deployment.annotation.AnnotatedApp;
import org.apache.geronimo.j2ee.deployment.annotation.AnnotatedWebApp;
import org.apache.geronimo.j2ee.deployment.annotation.EJBAnnotationHelper;
import org.apache.geronimo.j2ee.deployment.annotation.HandlerChainAnnotationHelper;
import org.apache.geronimo.j2ee.deployment.annotation.PersistenceContextAnnotationHelper;
import org.apache.geronimo.j2ee.deployment.annotation.PersistenceUnitAnnotationHelper;
import org.apache.geronimo.j2ee.deployment.annotation.ResourceAnnotationHelper;
import org.apache.geronimo.j2ee.deployment.annotation.SecurityAnnotationHelper;
import org.apache.geronimo.j2ee.deployment.annotation.WebServiceRefAnnotationHelper;
import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
import org.apache.geronimo.kernel.Kernel;
import org.apache.geronimo.kernel.config.ConfigurationManager;
import org.apache.geronimo.kernel.repository.Artifact;
import org.apache.geronimo.naming.deployment.EnvironmentEntryBuilder.EnvEntryRefProcessor;
import org.apache.geronimo.naming.deployment.SwitchingServiceRefBuilder.ServiceRefProcessor;
import org.apache.geronimo.naming.deployment.jsr88.EjbLocalRef;
import org.apache.geronimo.naming.deployment.jsr88.EjbRef;
import org.apache.geronimo.naming.deployment.jsr88.MessageDestination;
import org.apache.geronimo.naming.deployment.jsr88.Pattern;
import org.apache.geronimo.naming.deployment.jsr88.ResourceEnvRef;
import org.apache.geronimo.naming.deployment.jsr88.ResourceRef;
import org.apache.geronimo.web.deployment.WebAppDConfigBean;
import org.apache.geronimo.web.deployment.WebAppDConfigRoot;
import org.apache.geronimo.web25.deployment.AbstractWebModuleBuilder;
import org.apache.geronimo.xbeans.geronimo.naming.GerServiceRefType;
import org.apache.geronimo.xbeans.geronimo.security.GerRoleType;
import org.apache.geronimo.xbeans.geronimo.security.GerSecurityType;
import org.apache.geronimo.xbeans.javaee.EjbLocalRefType;
import org.apache.geronimo.xbeans.javaee.EjbRefType;
import org.apache.geronimo.xbeans.javaee.MessageDestinationType;
import org.apache.geronimo.xbeans.javaee.ResourceEnvRefType;
import org.apache.geronimo.xbeans.javaee.ResourceRefType;
import org.apache.geronimo.xbeans.javaee.SecurityRoleType;
import org.apache.geronimo.xbeans.javaee.ServiceRefType;
import org.apache.geronimo.xbeans.javaee.WebAppType;
import org.apache.xbean.finder.ClassFinder;

/**
 * Util class for JSR-88 related functions
 * 
 * @version $Rev$ $Date$
 */
public class JSR88_Util {

    private static void parseAnnotations(AnnotatedApp annotatedApp, ClassFinder classFinder) throws Exception {
        ResourceAnnotationHelper.processAnnotations(annotatedApp, classFinder, EnvEntryRefProcessor.INSTANCE);
        WebServiceRefAnnotationHelper.processAnnotations(annotatedApp, classFinder);
        HandlerChainAnnotationHelper.processAnnotations(annotatedApp, classFinder);
        ResourceAnnotationHelper.processAnnotations(annotatedApp, classFinder, ServiceRefProcessor.INSTANCE);
        ResourceAnnotationHelper.processAnnotations(annotatedApp, classFinder, AdminObjectRefProcessor.INSTANCE);
        ResourceAnnotationHelper.processAnnotations(annotatedApp, classFinder, ResourceRefProcessor.INSTANCE);
        PersistenceContextAnnotationHelper.processAnnotations(annotatedApp, classFinder);
        PersistenceUnitAnnotationHelper.processAnnotations(annotatedApp, classFinder);
        EJBAnnotationHelper.processAnnotations(annotatedApp, classFinder);
        if (annotatedApp instanceof AnnotatedWebApp) {
            SecurityAnnotationHelper.processAnnotations(((AnnotatedWebApp) annotatedApp).getWebApp(), classFinder);
        }
    }

    private static ConfigurationBuilder getConfigurationBuilder(PortletRequest request) {
        Object[] builders = PortletManager.getGBeansImplementing(request, ConfigurationBuilder.class);
        ConfigurationBuilder configurationBuilder = null;
        for (int i = 0; i < builders.length; i++) {
            ObjectName objectName = PortletManager.getNameFor(request, builders[i]).getObjectName();
            if ("EARBuilder".equalsIgnoreCase(objectName.getKeyProperty(NameFactory.J2EE_NAME))) {
                configurationBuilder = (ConfigurationBuilder) builders[i];
                break;
            }
        }
        return configurationBuilder;
    }

    private static ApplicationInfo createApplicationInfo(PortletRequest request, URL moduleUrl) throws Exception {
        ConfigurationBuilder configurationBuilderBuilder = getConfigurationBuilder(request);
        File moduleFile = new File(moduleUrl.toURI());
        JarFile moduleJar = new JarFile(moduleFile);
        ApplicationInfo applicationInfo = (ApplicationInfo) configurationBuilderBuilder.getDeploymentPlan(null, moduleJar,
                new ModuleIDBuilder());
        return applicationInfo;
    }

    public static void parseWarReferences(PortletRequest request, WARConfigData data, URL warUrl)
            throws Exception {
        WebDeployable webDeployable = new WebDeployable(warUrl);
        ClassLoader classLoader = webDeployable.getModuleLoader();

        ApplicationInfo applicationInfo = createApplicationInfo(request, warUrl);
        Module module = (Module) (applicationInfo.getModules().toArray()[0]);
        WebAppType webApp = (WebAppType) module.getSpecDD();

        ClassFinder classFinder = null;
        try {
            classFinder = AbstractWebModuleBuilder.createWebAppClassFinder(webApp, classLoader);
            // classFinder = new ClassFinder(classLoader);
        } catch (DeploymentException e1) {
            // Some of the class types referred in the WAR cannot be resolved.
            // A typical case would be references to EJBs already deployed into the system, and
            // hence not packaged inside WEB-INF/lib directory of WAR.
            // try adding all EJBs deployed in the system as parents of this WAR, and
            // see if referred classes can now be successfully resolved

            classLoader = new WebDeployable(warUrl, getEjbClassLoaders(request)).getModuleLoader();
            try {
                classFinder = AbstractWebModuleBuilder.createWebAppClassFinder(webApp, classLoader);
            } catch (DeploymentException e2) {
                throw e2;
            }
        }
        AnnotatedApp annotatedApp = module.getAnnotatedApp();
        parseAnnotations(annotatedApp, classFinder);

        //DDBeanRoot ddBeanRoot = webDeployable.getDDBeanRoot();
        //DDBean ddBean = ddBeanRoot.getChildBean("web-app")[0];

        EjbRefType[] ejbRefs = annotatedApp.getEjbRefArray();
        for (int i = 0; i < ejbRefs.length; i++) {
            String refName = ejbRefs[i].getEjbRefName().getStringValue();
            data.getEjbRefs().add(new ReferenceData(refName));
        }
        /*DDBean[] ddBeans = ddBean.getChildBean("ejb-ref");
        for (int i = 0; ddBeans != null && i < ddBeans.length; i++) {
            String refName = ddBeans[i].getChildBean("ejb-ref-name")[0].getText();
            data.getEjbRefs().add(new ReferenceData(refName));
        }*/

        EjbLocalRefType[] ejbLocalRefs = annotatedApp.getEjbLocalRefArray();
        for (int i = 0; i < ejbLocalRefs.length; i++) {
            String refName = ejbLocalRefs[i].getEjbRefName().getStringValue();
            data.getEjbLocalRefs().add(new ReferenceData(refName));
        }
        /*ddBeans = ddBean.getChildBean("ejb-local-ref");
        for (int i = 0; ddBeans != null && i < ddBeans.length; i++) {
            String refName = ddBeans[i].getChildBean("ejb-ref-name")[0].getText();
            data.getEjbLocalRefs().add(new ReferenceData(refName));
        }*/

        ResourceRefType[] resourceRefs = annotatedApp.getResourceRefArray();
{'fraction_non_alphanumeric': 0.09050046339202966, 'fraction_numerical': 0.004958294717330862, 'mean_word_length': 4.509573653306101, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1122532', 'n_tokens_mistral': 6175, 'n_tokens_neox': 6053, 'n_words': 1132}