<commit_before>package com.atlassian.maven.plugins.amps.product;

import com.atlassian.maven.plugins.amps.MavenGoals;
import com.atlassian.maven.plugins.amps.Product;
import com.atlassian.maven.plugins.amps.ProductArtifact;
import com.atlassian.maven.plugins.amps.util.ConfigFileUtils;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

public class JiraProductHandler extends AbstractWebappProductHandler
{
    public JiraProductHandler(final MavenProject project, final MavenGoals goals)
    {
        super(project, goals, new JiraPluginProvider());
    }

    public String getId()
    {
        return "jira";
    }

    @Override
    public ProductArtifact getArtifact()
    {
        return new ProductArtifact("com.atlassian.jira", "atlassian-jira-webapp", "RELEASE");
    }

    @Override
    public ProductArtifact getTestResourcesArtifact()
    {
        return new ProductArtifact("com.atlassian.jira.plugins", "jira-plugin-test-resources", "LATEST");
    }

    public int getDefaultHttpPort()
    {
        return 2990;
    }

    protected static File getHsqlDatabaseFile(final File homeDirectory)
    {
        return new File(homeDirectory, "database");
    }
    
    @Override
    public Map<String, String> getSystemProperties(final Product ctx)
    {
        return new HashMap<String, String>()
        {
            {
                put("jira.home", fixSlashes(getHomeDirectory(ctx).getPath()));
                put("cargo.datasource.datasource", "cargo.datasource.url=jdbc:hsqldb:"
                        + fixSlashes(getHsqlDatabaseFile(getHomeDirectory(ctx)).getAbsolutePath()) + "|"
                        + "cargo.datasource.driver=org.hsqldb.jdbcDriver|" + "cargo.datasource.username=sa|"
                        + "cargo.datasource.password=|" + "cargo.datasource.type=javax.sql.DataSource|"
                        + "cargo.datasource.jndi=jdbc/JiraDS");
            }
        };
    }

    private static String fixSlashes(final String path)
    {
        return path.replaceAll("\\\\", "/");
    }

    @Override
    public File getUserInstalledPluginsDirectory(final File webappDir, final File homeDir)
    {
        return new File(new File(homeDir, "plugins"), "installed-plugins");
    }

    @Override
    public List<ProductArtifact> getExtraContainerDependencies()
    {
        return Arrays.asList(
                new ProductArtifact("hsqldb", "hsqldb", "1.8.0.5"),
                new ProductArtifact("jta", "jta", "1.0.1"),
                new ProductArtifact("ots-jts", "ots-jts", "1.0"),

                // for data source and transaction manager providers
                new ProductArtifact("jotm", "jotm", "1.4.3"),
                new ProductArtifact("jotm", "jotm-jrmp_stubs", "1.4.3"),
                new ProductArtifact("jotm", "jotm-iiop_stubs", "1.4.3"),
                new ProductArtifact("jotm", "jonas_timer", "1.4.3"),
                new ProductArtifact("jotm", "objectweb-datasource", "1.4.3"),
                new ProductArtifact("carol", "carol", "1.5.2"),
                new ProductArtifact("carol", "carol-properties", "1.0"),
                new ProductArtifact("xapool", "xapool", "1.3.1"),
                new ProductArtifact("commons-logging", "commons-logging", "1.1.1")
        );
    }

    @Override
    public String getBundledPluginPath(Product ctx)
    {
    	String[] version = ctx.getVersion().split("-", 2)[0].split("\\.");
    	long major = Long.parseLong(version[0]);
    	long minor = Long.parseLong(version[1]);
    	
    	if (major < 4 || major == 4 && minor == 0)
    	{
    		return "WEB-INF/classes/com/atlassian/jira/plugin/atlassian-bundled-plugins.zip";
    	}
    	else
    	{
    		return "WEB-INF/classes/atlassian-bundled-plugins.zip";
    	}
    }

    @Override
    public void processHomeDirectory(final Product ctx, final File homeDir) throws MojoExecutionException
    {
        String contextPath = ctx.getContextPath();
        if (!contextPath.startsWith("/"))
        {
            contextPath = "/" + contextPath;
        }
        ConfigFileUtils.replace(new File(homeDir, "database.script"), "@project-dir@", homeDir.getParent());
        ConfigFileUtils.replace(new File(homeDir, "database.script"), "/jira-home/", "/home/");
        ConfigFileUtils.replace(new File(homeDir, "database.script"), "@base-url@",
                "http://" + ctx.getServer() + ":" + ctx.getHttpPort() + contextPath);
        createDbConfigXmlIfNecessary(homeDir);
    }

    static void createDbConfigXmlIfNecessary(File homeDir) throws MojoExecutionException
    {
        File dbConfigXml = new File(homeDir, "dbconfig.xml");
        if (dbConfigXml.exists())
        {
            return;
        }

        InputStream templateIn = JiraProductHandler.class.getResourceAsStream("jira-dbconfig-template.xml");
        if (templateIn == null)
        {
            throw new MojoExecutionException("Missing internal resource: jira-dbconfig-template.xml");
        }
        
        try
        {
            String template = IOUtils.toString(templateIn, "utf-8");
            
            File dbFile = getHsqlDatabaseFile(homeDir);<commit_msg>added some system properties to allow an alternate data source when using jira:run<commit_after>package com.atlassian.maven.plugins.amps.product;

import com.atlassian.maven.plugins.amps.MavenGoals;
import com.atlassian.maven.plugins.amps.Product;
import com.atlassian.maven.plugins.amps.ProductArtifact;
import com.atlassian.maven.plugins.amps.util.ConfigFileUtils;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

import static java.lang.String.format;

public class JiraProductHandler extends AbstractWebappProductHandler
{
    public JiraProductHandler(final MavenProject project, final MavenGoals goals)
    {
        super(project, goals, new JiraPluginProvider());
    }

    public String getId()
    {
        return "jira";
    }

    @Override
    public ProductArtifact getArtifact()
    {
        return new ProductArtifact("com.atlassian.jira", "atlassian-jira-webapp", "RELEASE");
    }

    @Override
    public ProductArtifact getTestResourcesArtifact()
    {
        return new ProductArtifact("com.atlassian.jira.plugins", "jira-plugin-test-resources", "LATEST");
    }

    public int getDefaultHttpPort()
    {
        return 2990;
    }

    protected static File getHsqlDatabaseFile(final File homeDirectory)
    {
        return new File(homeDirectory, "database");
    }

    @Override
    public Map<String, String> getSystemProperties(final Product ctx)
    {
        return new HashMap<String, String>()
        {
            {
                final String dburl = System.getProperty("amps.datasource.url", format("jdbc:hsqldb:%s/database", fixSlashes(getHomeDirectory(ctx).getAbsolutePath())));
                final String driverClass = System.getProperty("amps.datasource.driver", "org.hsqldb.jdbcDriver");
                final String username = System.getProperty("amps.datasource.username", "sa");
                final String password = System.getProperty("amps.datasource.password", "");
                final String datasourceTypeClass = "javax.sql.DataSource";

                final String datasource = format("cargo.datasource.url=%s", dburl);
                final String driver = format("cargo.datasource.driver=%s", driverClass);
                final String datasourceUsername = format("cargo.datasource.username=%s", username);
                final String datasourcePassword = format("cargo.datasource.password=%s", password);
                final String datasourceType = "cargo.datasource.type=" + datasourceTypeClass;
                final String jndi = "cargo.datasource.jndi=jdbc/JiraDS";

                put("jira.home", fixSlashes(getHomeDirectory(ctx).getPath()));
                put("cargo.datasource.datasource", format("%s|%s|%s|%s|%s|%s", datasource, driver, datasourceUsername, datasourcePassword, datasourceType, jndi));
            }
        };
    }

    private static String fixSlashes(final String path)
    {
        return path.replaceAll("\\\\", "/");
    }

    @Override
    public File getUserInstalledPluginsDirectory(final File webappDir, final File homeDir)
    {
        return new File(new File(homeDir, "plugins"), "installed-plugins");
    }

    @Override
    public List<ProductArtifact> getExtraContainerDependencies()
    {
        return Arrays.asList(
                new ProductArtifact("hsqldb", "hsqldb", "1.8.0.5"),
                new ProductArtifact("jta", "jta", "1.0.1"),
                new ProductArtifact("ots-jts", "ots-jts", "1.0"),

                // for data source and transaction manager providers
                new ProductArtifact("jotm", "jotm", "1.4.3"),
                new ProductArtifact("jotm", "jotm-jrmp_stubs", "1.4.3"),
                new ProductArtifact("jotm", "jotm-iiop_stubs", "1.4.3"),
                new ProductArtifact("jotm", "jonas_timer", "1.4.3"),
                new ProductArtifact("jotm", "objectweb-datasource", "1.4.3"),
                new ProductArtifact("carol", "carol", "1.5.2"),
                new ProductArtifact("carol", "carol-properties", "1.0"),
                new ProductArtifact("xapool", "xapool", "1.3.1"),
                new ProductArtifact("commons-logging", "commons-logging", "1.1.1")
        );
    }

    @Override
    public String getBundledPluginPath(Product ctx)
    {
    	String[] version = ctx.getVersion().split("-", 2)[0].split("\\.");
    	long major = Long.parseLong(version[0]);
    	long minor = Long.parseLong(version[1]);

    	if (major < 4 || major == 4 && minor == 0)
    	{
    		return "WEB-INF/classes/com/atlassian/jira/plugin/atlassian-bundled-plugins.zip";
    	}
    	else
    	{
    		return "WEB-INF/classes/atlassian-bundled-plugins.zip";
    	}
    }

    @Override
    public void processHomeDirectory(final Product ctx, final File homeDir) throws MojoExecutionException
    {
        String contextPath = ctx.getContextPath();
        if (!contextPath.startsWith("/"))
        {
            contextPath = "/" + contextPath;
        }
        ConfigFileUtils.replace(new File(homeDir, "database.script"), "@project-dir@", homeDir.getParent());
        ConfigFileUtils.replace(new File(homeDir, "database.script"), "/jira-home/", "/home/");
        ConfigFileUtils.replace(new File(homeDir, "database.script"), "@base-url@",
                "http://" + ctx.getServer() + ":" + ctx.getHttpPort() + contextPath);
        createDbConfigXmlIfNecessary(homeDir);
    }

    static void createDbConfigXmlIfNecessary(File homeDir) throws MojoExecutionException
    {
        File dbConfigXml = new File(homeDir, "dbconfig.xml");
        if (dbConfigXml.exists())
        {
            return;
        }

        InputStream templateIn = JiraProductHandler.class.getResourceAsStream("jira-dbconfig-template.xml");
        if (templateIn == null)
        {
            throw new MojoExecutionException("Missing internal resource: jira-dbconfig-template.xml");
        }

        try
        {
            String template = IOUtils.toString(templateIn, "utf-8");

            File dbFile = getHsqlDatabaseFile(homeDir);
{'fraction_non_alphanumeric': 0.12917133741484868, 'fraction_numerical': 0.008105544537380356, 'mean_word_length': 2.667931688804554, 'pattern_counts': {'":': 2, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, 'pii_count': 2, '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': '1700021', 'n_tokens_mistral': 3566, 'n_tokens_neox': 3429, 'n_words': 633}