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

org.neogroup.warp.data.DataSources Maven / Gradle / Ivy

There is a newer version: 1.4.3
Show newest version
package org.neogroup.warp.data;

import java.util.HashMap;
import java.util.Map;

import static org.neogroup.warp.Warp.*;

public abstract class DataSources {

    private static final String DEFAULT_DATA_SOURCE_NAME_PROPERTY = "defaultDatasourceName";
    private static final String DEFAULT_DATA_SOURCE_NAME = "main";

    private static final Map dataSourcesByName;

    static {
        dataSourcesByName = new HashMap<>();
    }

    public static void registerDataSource(String dataSourceName, Class dataSourceClass) {
        try {
            DataSource dataConnection = dataSourceClass.getConstructor().newInstance();
            dataSourcesByName.put(dataSourceName, dataConnection);
            getLogger().info("Data source \"" + dataSourceClass.getName() + "\" registered !!" + (dataSourceName != null ? " [name=" + dataSourceName + "]" : ""));
        } catch (Exception ex) {
            throw new RuntimeException("Error registering data manager \"" + dataSourceClass.getName() + "\" !!", ex);
        }
    }

    public static DataConnection getConnection() {
        DataConnection connection = null;
        if (!dataSourcesByName.isEmpty()) {
            DataSource source = null;
            if (dataSourcesByName.size() == 1) {
                source = dataSourcesByName.values().iterator().next();
            }
            else {
                String dataSourceName = null;
                if (hasProperty(DEFAULT_DATA_SOURCE_NAME_PROPERTY)) {
                    dataSourceName = getProperty(DEFAULT_DATA_SOURCE_NAME_PROPERTY);
                }
                else {
                    dataSourceName = DEFAULT_DATA_SOURCE_NAME;
                }
                source = dataSourcesByName.get(dataSourceName);
                if (source == null) {
                    throw new RuntimeException("No data source with name \"" + dataSourceName + "\"");
                }
            }
            connection = source.getConnection();
        }
        return connection;
    }

    public static DataConnection getConnection(String dataSourceName) {
        DataSource source = dataSourcesByName.get(dataSourceName);
        if (source == null) {
            throw new RuntimeException("No data source with name \"" + dataSourceName + "\"");
        }
        return source.getConnection();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy