<commit_before>/**
 *
 * Copyright 2003-2004 The Apache Software Foundation
 *
 *  Licensed 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.jetty.deployment;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.jar.JarFile;
import java.util.jar.JarInputStream;
import java.util.zip.ZipEntry;
import javax.management.AttributeNotFoundException;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.ReflectionException;
import javax.naming.NamingException;
import javax.transaction.UserTransaction;

import org.apache.geronimo.common.xml.XmlBeansUtil;
import org.apache.geronimo.deployment.DeploymentException;
import org.apache.geronimo.deployment.service.GBeanHelper;
import org.apache.geronimo.gbean.GBeanInfo;
import org.apache.geronimo.gbean.GBeanInfoFactory;
import org.apache.geronimo.gbean.jmx.GBeanMBean;
import org.apache.geronimo.j2ee.deployment.EARContext;
import org.apache.geronimo.j2ee.deployment.Module;
import org.apache.geronimo.j2ee.deployment.ModuleBuilder;
import org.apache.geronimo.j2ee.deployment.WebModule;
import org.apache.geronimo.jetty.JettyWebApplicationContext;
import org.apache.geronimo.naming.deployment.ENCConfigBuilder;
import org.apache.geronimo.naming.java.ComponentContextBuilder;
import org.apache.geronimo.naming.java.ReadOnlyContext;
import org.apache.geronimo.naming.jmx.JMXReferenceFactory;
import org.apache.geronimo.security.deploy.DefaultPrincipal;
import org.apache.geronimo.security.deploy.Principal;
import org.apache.geronimo.security.deploy.Realm;
import org.apache.geronimo.security.deploy.Role;
import org.apache.geronimo.security.deploy.Security;
import org.apache.geronimo.transaction.UserTransactionImpl;
import org.apache.geronimo.xbeans.geronimo.jetty.JettyDefaultPrincipalType;
import org.apache.geronimo.xbeans.geronimo.jetty.JettyDependencyType;
import org.apache.geronimo.xbeans.geronimo.jetty.JettyGbeanType;
import org.apache.geronimo.xbeans.geronimo.jetty.JettyLocalRefType;
import org.apache.geronimo.xbeans.geronimo.jetty.JettyPrincipalType;
import org.apache.geronimo.xbeans.geronimo.jetty.JettyRealmType;
import org.apache.geronimo.xbeans.geronimo.jetty.JettyRoleMappingsType;
import org.apache.geronimo.xbeans.geronimo.jetty.JettyRoleType;
import org.apache.geronimo.xbeans.geronimo.jetty.JettySecurityType;
import org.apache.geronimo.xbeans.geronimo.jetty.JettyWebAppDocument;
import org.apache.geronimo.xbeans.geronimo.jetty.JettyWebAppType;
import org.apache.geronimo.xbeans.j2ee.EjbLocalRefType;
import org.apache.geronimo.xbeans.j2ee.EjbRefType;
import org.apache.geronimo.xbeans.j2ee.EnvEntryType;
import org.apache.geronimo.xbeans.j2ee.ResourceRefType;
import org.apache.geronimo.xbeans.j2ee.WebAppDocument;
import org.apache.geronimo.xbeans.j2ee.WebAppType;
import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlObject;


/**
 * @version $Revision: 1.9 $ $Date: 2004/06/15 03:00:37 $
 */
public class JettyModuleBuilder implements ModuleBuilder {
    private static final String PARENT_ID = "org/apache/geronimo/Server";

    public XmlObject getDeploymentPlan(URL module) throws XmlException {
        try {
            URL moduleBase;
            if (module.toString().endsWith("/")) {
                moduleBase = module;
            } else {
                moduleBase = new URL("jar:" + module.toString() + "!/");
            }
            JettyWebAppDocument plan = (JettyWebAppDocument) XmlBeansUtil.getXmlObject(new URL(moduleBase, "WEB-INF/geronimo-jetty.xml"), JettyWebAppDocument.type);
            if (plan == null) {
                return createDefaultPlan(moduleBase);
            }
            return plan.getWebApp();
        } catch (MalformedURLException e) {
            return null;
        }
    }

    private JettyWebAppType createDefaultPlan(URL moduleBase) throws XmlException {
        // load the web.xml
        URL webXmlUrl = null;
        try {
            webXmlUrl = new URL(moduleBase, "WEB-INF/web.xml");
        } catch (MalformedURLException e) {
            return null;
        }
        WebAppDocument webAppDoc = (WebAppDocument) XmlBeansUtil.getXmlObject(webXmlUrl, WebAppDocument.type);
        if (webAppDoc == null) {
            return null;
        }

        // construct the empty geronimo-jetty.xml
        JettyWebAppType jettyWebApp = JettyWebAppType.Factory.newInstance();

        // set the parentId, configId and context root
        jettyWebApp.setParentId(PARENT_ID);
        String id = webAppDoc.getWebApp().getId();
        if (id == null) {
            id = moduleBase.getFile();
            if (id.endsWith("!/")) {
                id = id.substring(0, id.length()-2);
            }
            if (id.endsWith(".war")) {
                id = id.substring(0, id.length()-4);
            }
            id = id.substring(id.lastIndexOf('/') + 1);
        }

        jettyWebApp.setConfigId(id);
        jettyWebApp.setContextRoot(id);
        return jettyWebApp;
    }

    public boolean canHandlePlan(XmlObject plan) {
        return plan instanceof JettyWebAppType;
    }

    public URI getParentId(XmlObject plan) throws DeploymentException {
        JettyWebAppType jettyWebApp = (JettyWebAppType) plan;
        try {
            return new URI(jettyWebApp.getParentId());
        } catch (URISyntaxException e) {
            throw new DeploymentException("Invalid parentId " + jettyWebApp.getParentId(), e);
        }
    }

    public URI getConfigId(XmlObject plan) throws DeploymentException {
        JettyWebAppType jettyWebApp = (JettyWebAppType) plan;
        try {
            return new URI(jettyWebApp.getConfigId());
        } catch (URISyntaxException e) {
            throw new DeploymentException("Invalid configId " + jettyWebApp.getConfigId(), e);
        }
    }

    public Module createModule(String name, XmlObject plan) throws DeploymentException {
        JettyWebAppType jettyWebApp = (JettyWebAppType) plan;
        WebModule module = new WebModule(name, URI.create("/"), jettyWebApp.getContextRoot());
        module.setVendorDD(jettyWebApp);
        return module;
    }

    public void installModule(JarFile earFile, EARContext earContext, Module webModule) throws DeploymentException {
        try {
            URI warRoot = null;
            JarInputStream jarIS = null;
            if (!webModule.getURI().equals(URI.create("/"))) {
                ZipEntry warEntry = earFile.getEntry(webModule.getURI().toString());
                jarIS = new JarInputStream(earFile.getInputStream(warEntry));
                warRoot = URI.create(webModule.getURI() + "/");
            } else {
                jarIS = new JarInputStream(new FileInputStream(earFile.getName()));
                warRoot = URI.create("war/");
            }

            // add the warfile's content to the configuration
            ZipEntry src;
            WebAppType webApp = null;
            JettyWebAppType jettyWebApp = (JettyWebAppType) webModule.getVendorDD();
            while ((src = jarIS.getNextEntry()) != null) {
                URI target = warRoot.resolve(src.getName());
                if ("WEB-INF/web.xml".equals(src.getName())) {
                    byte[] buffer = getBytes(jarIS);
                    earContext.addFile(target, new ByteArrayInputStream(buffer));
                    try {
                        WebAppDocument doc = (WebAppDocument) XmlBeansUtil.parse(new ByteArrayInputStream(buffer), WebAppDocument.type);
                        webApp = doc.getWebApp();
                    } catch (XmlException e) {
                        throw new DeploymentException("Unable to parse web.xml", e);<commit_msg>Use new xmlbeans schema conversion tool to accept dtd based and schema based dds<commit_after>/**
 *
 * Copyright 2003-2004 The Apache Software Foundation
 *
 *  Licensed 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.jetty.deployment;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.jar.JarFile;
import java.util.jar.JarInputStream;
import java.util.zip.ZipEntry;
import javax.management.AttributeNotFoundException;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.ReflectionException;
import javax.naming.NamingException;
import javax.transaction.UserTransaction;

import org.apache.geronimo.common.xml.XmlBeansUtil;
import org.apache.geronimo.deployment.DeploymentException;
import org.apache.geronimo.deployment.service.GBeanHelper;
import org.apache.geronimo.gbean.GBeanInfo;
import org.apache.geronimo.gbean.GBeanInfoFactory;
import org.apache.geronimo.gbean.jmx.GBeanMBean;
import org.apache.geronimo.j2ee.deployment.EARContext;
import org.apache.geronimo.j2ee.deployment.Module;
import org.apache.geronimo.j2ee.deployment.ModuleBuilder;
import org.apache.geronimo.j2ee.deployment.WebModule;
import org.apache.geronimo.jetty.JettyWebApplicationContext;
import org.apache.geronimo.naming.deployment.ENCConfigBuilder;
import org.apache.geronimo.naming.java.ComponentContextBuilder;
import org.apache.geronimo.naming.java.ReadOnlyContext;
import org.apache.geronimo.naming.jmx.JMXReferenceFactory;
import org.apache.geronimo.security.deploy.DefaultPrincipal;
import org.apache.geronimo.security.deploy.Principal;
import org.apache.geronimo.security.deploy.Realm;
import org.apache.geronimo.security.deploy.Role;
import org.apache.geronimo.security.deploy.Security;
import org.apache.geronimo.transaction.UserTransactionImpl;
import org.apache.geronimo.xbeans.geronimo.jetty.JettyDefaultPrincipalType;
import org.apache.geronimo.xbeans.geronimo.jetty.JettyDependencyType;
import org.apache.geronimo.xbeans.geronimo.jetty.JettyGbeanType;
import org.apache.geronimo.xbeans.geronimo.jetty.JettyLocalRefType;
import org.apache.geronimo.xbeans.geronimo.jetty.JettyPrincipalType;
import org.apache.geronimo.xbeans.geronimo.jetty.JettyRealmType;
import org.apache.geronimo.xbeans.geronimo.jetty.JettyRoleMappingsType;
import org.apache.geronimo.xbeans.geronimo.jetty.JettyRoleType;
import org.apache.geronimo.xbeans.geronimo.jetty.JettySecurityType;
import org.apache.geronimo.xbeans.geronimo.jetty.JettyWebAppDocument;
import org.apache.geronimo.xbeans.geronimo.jetty.JettyWebAppType;
import org.apache.geronimo.xbeans.j2ee.EjbLocalRefType;
import org.apache.geronimo.xbeans.j2ee.EjbRefType;
import org.apache.geronimo.xbeans.j2ee.EnvEntryType;
import org.apache.geronimo.xbeans.j2ee.ResourceRefType;
import org.apache.geronimo.xbeans.j2ee.WebAppDocument;
import org.apache.geronimo.xbeans.j2ee.WebAppType;
import org.apache.geronimo.xbeans.j2ee.EjbJarDocument;
import org.apache.geronimo.schema.SchemaConversionUtils;
import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlObject;


/**
 * @version $Revision: 1.10 $ $Date: 2004/06/17 06:56:02 $
 */
public class JettyModuleBuilder implements ModuleBuilder {
    private static final String PARENT_ID = "org/apache/geronimo/Server";

    public XmlObject getDeploymentPlan(URL module) throws XmlException {
        try {
            URL moduleBase;
            if (module.toString().endsWith("/")) {
                moduleBase = module;
            } else {
                moduleBase = new URL("jar:" + module.toString() + "!/");
            }
            JettyWebAppDocument plan = (JettyWebAppDocument) XmlBeansUtil.getXmlObject(new URL(moduleBase, "WEB-INF/geronimo-jetty.xml"), JettyWebAppDocument.type);
            if (plan == null) {
                return createDefaultPlan(moduleBase);
            }
            return plan.getWebApp();
        } catch (MalformedURLException e) {
            return null;
        }
    }

    private JettyWebAppType createDefaultPlan(URL moduleBase) throws XmlException {
        // load the web.xml
        URL webXmlUrl = null;
        try {
            webXmlUrl = new URL(moduleBase, "WEB-INF/web.xml");
        } catch (MalformedURLException e) {
            return null;
        }
        WebAppDocument webAppDoc = (WebAppDocument) XmlBeansUtil.getXmlObject(webXmlUrl, WebAppDocument.type);
        if (webAppDoc == null) {
            return null;
        }

        // construct the empty geronimo-jetty.xml
        JettyWebAppType jettyWebApp = JettyWebAppType.Factory.newInstance();

        // set the parentId, configId and context root
        jettyWebApp.setParentId(PARENT_ID);
        String id = webAppDoc.getWebApp().getId();
        if (id == null) {
            id = moduleBase.getFile();
            if (id.endsWith("!/")) {
                id = id.substring(0, id.length()-2);
            }
            if (id.endsWith(".war")) {
                id = id.substring(0, id.length()-4);
            }
            id = id.substring(id.lastIndexOf('/') + 1);
        }

        jettyWebApp.setConfigId(id);
        jettyWebApp.setContextRoot(id);
        return jettyWebApp;
    }

    public boolean canHandlePlan(XmlObject plan) {
        return plan instanceof JettyWebAppType;
    }

    public URI getParentId(XmlObject plan) throws DeploymentException {
        JettyWebAppType jettyWebApp = (JettyWebAppType) plan;
        try {
            return new URI(jettyWebApp.getParentId());
        } catch (URISyntaxException e) {
            throw new DeploymentException("Invalid parentId " + jettyWebApp.getParentId(), e);
        }
    }

    public URI getConfigId(XmlObject plan) throws DeploymentException {
        JettyWebAppType jettyWebApp = (JettyWebAppType) plan;
        try {
            return new URI(jettyWebApp.getConfigId());
        } catch (URISyntaxException e) {
            throw new DeploymentException("Invalid configId " + jettyWebApp.getConfigId(), e);
        }
    }

    public Module createModule(String name, XmlObject plan) throws DeploymentException {
        JettyWebAppType jettyWebApp = (JettyWebAppType) plan;
        WebModule module = new WebModule(name, URI.create("/"), jettyWebApp.getContextRoot());
        module.setVendorDD(jettyWebApp);
        return module;
    }

    public void installModule(JarFile earFile, EARContext earContext, Module webModule) throws DeploymentException {
        try {
            URI warRoot = null;
            JarInputStream jarIS = null;
            if (!webModule.getURI().equals(URI.create("/"))) {
                ZipEntry warEntry = earFile.getEntry(webModule.getURI().toString());
                jarIS = new JarInputStream(earFile.getInputStream(warEntry));
                warRoot = URI.create(webModule.getURI() + "/");
            } else {
                jarIS = new JarInputStream(new FileInputStream(earFile.getName()));
                warRoot = URI.create("war/");
            }

            // add the warfile's content to the configuration
            ZipEntry src;
            WebAppType webApp = null;
            JettyWebAppType jettyWebApp = (JettyWebAppType) webModule.getVendorDD();
            while ((src = jarIS.getNextEntry()) != null) {
                URI target = warRoot.resolve(src.getName());
                if ("WEB-INF/web.xml".equals(src.getName())) {
                    byte[] buffer = getBytes(jarIS);
                    earContext.addFile(target, new ByteArrayInputStream(buffer));
                    try {
                        XmlObject dd = SchemaConversionUtils.parse(new ByteArrayInputStream(buffer));
                        WebAppDocument doc = SchemaConversionUtils.convertToServletSchema(dd);
                        webApp = doc.getWebApp();
                    } catch (XmlException e) {
                        throw new DeploymentException("Unable to parse web.xml", e);
{'fraction_non_alphanumeric': 0.10020637468470535, 'fraction_numerical': 0.005044714515019491, 'mean_word_length': 3.1095406360424027, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 19}, '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': '6276032', 'n_tokens_mistral': 5300, 'n_tokens_neox': 5117, 'n_words': 1017}