All Downloads are FREE. Search and download functionalities are using the official Maven repository.

au.net.causal.maven.plugins.boxdb.db.Db2Factory Maven / Gradle / Ivy

There is a newer version: 3.3
Show newest version
package au.net.causal.maven.plugins.boxdb.db;

import java.io.IOException;
import java.util.List;
import java.util.regex.Pattern;

@BoxDbBundled
public class Db2Factory extends DockerDatabaseFactory
{
    public static final String DB2_TERRITORY_PROPERTY = "db2Territory";
    public static final String DB2_COLLATION_PROPERTY = "db2Collation";

    static final String DB2_DOCKER_REPOSITORY = "ibmcom/db2";

    public Db2Factory()
    {
        super("db2");
    }

    @Override
    protected void initializeDefaults(BoxConfiguration boxConfiguration)
    {
        if (boxConfiguration.getAdminUser() == null)
            boxConfiguration.setAdminUser("db2inst1");
        if (boxConfiguration.getAdminPassword() == null)
            boxConfiguration.setAdminPassword("db2box");
        if (boxConfiguration.getDatabaseName() == null)
            boxConfiguration.setDatabaseName("app");
        if (boxConfiguration.getDatabaseVersion() == null)
            boxConfiguration.setDatabaseVersion("11.5.0.0a");
        if (boxConfiguration.getDatabasePort() <= 0)
            boxConfiguration.setDatabasePort(50000);
        if (boxConfiguration.getDatabaseUser() == null)
            boxConfiguration.setDatabaseUser(boxConfiguration.getDatabaseName());
        if (boxConfiguration.getDatabasePassword() == null)
            boxConfiguration.setDatabasePassword(boxConfiguration.getDatabaseUser());
        
        EncodingTranslator encodingTranslator = new EncodingTranslator("UTF-8", this::mapCommonEncoding);
        encodingTranslator.processEncoding(boxConfiguration);
        CollationTranslator collationTranslator = new CollationTranslator("US;identity", this::mapCommonCollation);
        collationTranslator.processCollation(boxConfiguration);
        
        //Configure DB2-specific elements from the parsed collation
        configureDb2CollationProperties(boxConfiguration);
        
        super.initializeDefaults(boxConfiguration);
    }
    
    private void configureDb2CollationProperties(BoxConfiguration boxConfiguration)
    {
        String collation = boxConfiguration.getDatabaseCollation();
        if (collation == null)
            return;
        
        String[] colSplit = collation.split(Pattern.quote(";"), 2);
        String territory = colSplit[0];
        String db2Collation = "identity";
        if (colSplit.length > 1)
            db2Collation = colSplit[1];
        
        boxConfiguration.getConfiguration().putIfAbsent(DB2_TERRITORY_PROPERTY, territory);
        boxConfiguration.getConfiguration().putIfAbsent(DB2_COLLATION_PROPERTY, db2Collation);
    }

    private String mapCommonEncoding(CommonEncoding encoding)
    {
        switch (encoding)
        {
            case UNICODE:
                return "UTF-8";
            case ASCII:
                return "ISO8859-1";
            default:
                throw new Error("Unknown common encoding: " + encoding);
        }
    }

    private String mapCommonCollation(CommonCollation collation)
    {
        switch (collation)
        {
            case BINARY:
                return "US;identity";
            case CASE_INSENSITIVE:
                return "US;UCA500R1_LEN_S2";
            default:
                throw new Error("Unknown common collation: " + collation);
        }
    }

    @Override
    protected Db2Database createDockerDatabase(BoxConfiguration boxConfiguration, ProjectConfiguration projectConfiguration, BoxContext context)
    throws BoxDatabaseException
    {
        try 
        {
            return new Db2Database(boxConfiguration, projectConfiguration, context, dockerRegistry());
        }
        catch (IOException e)
        {
            throw new BoxDatabaseException("I/O error creating database: " + e, e);
        }
    }

    @Override
    public List availableVersions(ProjectConfiguration projectConfiguration, BoxContext context)
    throws BoxDatabaseException
    {
        try
        {
            return dockerRegistry().readTags(DB2_DOCKER_REPOSITORY);
        }
        catch (IOException e)
        {
            throw new BoxDatabaseException("Error reading tags: " + e.getMessage(), e);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy