Search in sources :

Example 1 with DataSource

use of org.wso2.carbon.apimgt.core.dao.impl.DataSource in project jaggery by wso2.

the class DatabaseHostObject method jsConstructor.

public static Scriptable jsConstructor(Context cx, Object[] args, Function ctorObj, boolean inNewExpr) throws ScriptException {
    int argsCount = args.length;
    DatabaseHostObject db = new DatabaseHostObject();
    //args count 1 for dataSource name
    if (argsCount != 1 && argsCount != 3 && argsCount != 4) {
        HostObjectUtil.invalidNumberOfArgs(hostObjectName, hostObjectName, argsCount, true);
    }
    if (!(args[0] instanceof String)) {
        HostObjectUtil.invalidArgsError(hostObjectName, hostObjectName, "1", "string", args[0], true);
    }
    if (argsCount == 1) {
        String dataSourceName = (String) args[0];
        DataSourceManager dataSourceManager = new DataSourceManager();
        try {
            CarbonDataSource carbonDataSource = dataSourceManager.getInstance().getDataSourceRepository().getDataSource(dataSourceName);
            DataSource dataSource = (DataSource) carbonDataSource.getDSObject();
            db.conn = dataSource.getConnection();
            db.context = cx;
            return db;
        } catch (DataSourceException e) {
            log.error("Failed to access datasource " + dataSourceName, e);
        } catch (SQLException e) {
            log.error("Failed to get connection", e);
        }
    }
    if (!(args[1] instanceof String)) {
        HostObjectUtil.invalidArgsError(hostObjectName, hostObjectName, "2", "string", args[1], true);
    }
    if (!(args[2] instanceof String) && !(args[2] instanceof Integer)) {
        HostObjectUtil.invalidArgsError(hostObjectName, hostObjectName, "3", "string", args[2], true);
    }
    NativeObject configs = null;
    if (argsCount == 4) {
        if (!(args[3] instanceof NativeObject)) {
            HostObjectUtil.invalidArgsError(hostObjectName, hostObjectName, "4", "object", args[3], true);
        }
        configs = (NativeObject) args[3];
    }
    String dbUrl = (String) args[0];
    RDBMSConfiguration rdbmsConfig = new RDBMSConfiguration();
    try {
        if (configs != null) {
            Gson gson = new Gson();
            rdbmsConfig = gson.fromJson(HostObjectUtil.serializeJSON(configs), RDBMSConfiguration.class);
        }
        if (rdbmsConfig.getDriverClassName() == null || rdbmsConfig.getDriverClassName().equals("")) {
            rdbmsConfig.setDriverClassName(getDriverClassName(dbUrl));
        }
        rdbmsConfig.setUsername((String) args[1]);
        rdbmsConfig.setPassword((String) args[2]);
        rdbmsConfig.setUrl(dbUrl);
        try {
            rdbmsDataSource = new RDBMSDataSource(rdbmsConfig);
        } catch (DataSourceException e) {
            throw new ScriptException(e);
        }
        db.conn = rdbmsDataSource.getDataSource().getConnection();
        db.context = cx;
        return db;
    } catch (SQLException e) {
        String msg = "Error connecting to the database : " + dbUrl;
        log.warn(msg, e);
        throw new ScriptException(msg, e);
    }
}
Also used : CarbonDataSource(org.wso2.carbon.ndatasource.core.CarbonDataSource) RDBMSDataSource(org.wso2.carbon.ndatasource.rdbms.RDBMSDataSource) Gson(com.google.gson.Gson) DataSourceManager(org.wso2.carbon.ndatasource.core.DataSourceManager) RDBMSConfiguration(org.wso2.carbon.ndatasource.rdbms.RDBMSConfiguration) DataSource(javax.sql.DataSource) CarbonDataSource(org.wso2.carbon.ndatasource.core.CarbonDataSource) RDBMSDataSource(org.wso2.carbon.ndatasource.rdbms.RDBMSDataSource) NativeObject(org.mozilla.javascript.NativeObject) ScriptException(org.jaggeryjs.scriptengine.exceptions.ScriptException) DataSourceException(org.wso2.carbon.ndatasource.common.DataSourceException)

Example 2 with DataSource

use of org.wso2.carbon.apimgt.core.dao.impl.DataSource in project carbon-apimgt by wso2.

the class BundleActivator method start.

@Activate
protected void start(BundleContext bundleContext) {
    try {
        // Set default timestamp to UTC
        java.util.TimeZone.setDefault(java.util.TimeZone.getTimeZone("Etc/UTC"));
        Context ctx = jndiContextManager.newInitialContext();
        DataSource dataSourceAMDB = new DataSourceImpl((HikariDataSource) ctx.lookup("java:comp/env/jdbc/WSO2AMDB"));
        DAOUtil.initialize(dataSourceAMDB);
        boolean isAnalyticsEnabled = ServiceReferenceHolder.getInstance().getAPIMConfiguration().getAnalyticsConfigurations().isEnabled();
        if (isAnalyticsEnabled) {
            DataSource dataSourceStatDB = new DataSourceImpl((HikariDataSource) ctx.lookup("java:comp/env/jdbc/WSO2AMSTATSDB"));
            DAOUtil.initializeAnalyticsDataSource(dataSourceStatDB);
        }
        WorkflowExtensionsConfigBuilder.build(configProvider);
        ServiceDiscoveryConfigBuilder.build(configProvider);
        ContainerBasedGatewayConfigBuilder.build(configProvider);
        BrokerManager.start();
        Broker broker = new BrokerImpl();
        BrokerUtil.initialize(broker);
    } catch (NamingException e) {
        log.error("Error occurred while jndi lookup", e);
    }
    // deploying default policies
    try {
        ThrottlerUtil.addDefaultAdvancedThrottlePolicies();
        if (log.isDebugEnabled()) {
            log.debug("Checked default throttle policies successfully");
        }
    } catch (APIManagementException e) {
        log.error("Error occurred while deploying default policies", e);
    }
    // securing files
    try {
        boolean fileEncryptionEnabled = ServiceReferenceHolder.getInstance().getAPIMConfiguration().getFileEncryptionConfigurations().isEnabled();
        if (fileEncryptionEnabled) {
            FileEncryptionUtility fileEncryptionUtility = FileEncryptionUtility.getInstance();
            fileEncryptionUtility.init();
            fileEncryptionUtility.encryptFiles();
        }
    } catch (APIManagementException e) {
        log.error("Error occurred while encrypting files", e);
    }
}
Also used : Context(javax.naming.Context) BundleContext(org.osgi.framework.BundleContext) BrokerImpl(org.wso2.carbon.apimgt.core.impl.BrokerImpl) Broker(org.wso2.carbon.apimgt.core.api.Broker) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) FileEncryptionUtility(org.wso2.carbon.apimgt.core.impl.FileEncryptionUtility) DataSourceImpl(org.wso2.carbon.apimgt.core.dao.impl.DataSourceImpl) NamingException(javax.naming.NamingException) DataSource(org.wso2.carbon.apimgt.core.dao.impl.DataSource) HikariDataSource(com.zaxxer.hikari.HikariDataSource) Activate(org.osgi.service.component.annotations.Activate)

Example 3 with DataSource

use of org.wso2.carbon.apimgt.core.dao.impl.DataSource in project carbon-apimgt by wso2.

the class BrokerManager method start.

/**
 * Starting the broker
 */
public static void start() {
    try {
        StartupContext startupContext = new StartupContext();
        initConfigProvider(startupContext);
        BrokerConfigProvider service = startupContext.getService(BrokerConfigProvider.class);
        BrokerConfiguration brokerConfiguration = service.getConfigurationObject(BrokerConfiguration.NAMESPACE, BrokerConfiguration.class);
        DataSource dataSource = getDataSource(brokerConfiguration.getDataSource());
        startupContext.registerService(DataSource.class, dataSource);
        restServer = new BrokerRestServer(startupContext);
        broker = new Broker(startupContext);
        broker.startMessageDelivery();
        amqpServer = new Server(startupContext);
        amqpServer.start();
        restServer.start();
        loadUsers();
    } catch (Exception e) {
        log.error("Error while starting broker", e);
    }
}
Also used : StartupContext(org.wso2.broker.common.StartupContext) BrokerRestServer(org.wso2.broker.rest.BrokerRestServer) Broker(org.wso2.broker.core.Broker) Server(org.wso2.broker.amqp.Server) BrokerRestServer(org.wso2.broker.rest.BrokerRestServer) BrokerConfiguration(org.wso2.broker.core.configuration.BrokerConfiguration) BrokerConfigProvider(org.wso2.broker.common.BrokerConfigProvider) ConfigurationException(org.wso2.carbon.config.ConfigurationException) HikariDataSource(com.zaxxer.hikari.HikariDataSource) DataSource(javax.sql.DataSource)

Example 4 with DataSource

use of org.wso2.carbon.apimgt.core.dao.impl.DataSource in project carbon-apimgt by wso2.

the class DAOIntegrationTestBase method setUp.

@BeforeMethod
public void setUp() throws Exception {
    String sqlFilePath = null;
    if (H2.equals(database)) {
        ((H2DataSource) dataSource).resetDB();
        sqlFilePath = ".." + File.separator + ".." + File.separator + ".." + File.separator + "features" + File.separator + "apimgt" + File.separator + "org.wso2.carbon.apimgt.core.feature" + File.separator + "resources" + File.separator + "dbscripts" + File.separator + "h2.sql";
    } else if (MYSQL.contains(database)) {
        ((MySQLDataSource) dataSource).resetDB();
        sqlFilePath = ".." + File.separator + ".." + File.separator + ".." + File.separator + "features" + File.separator + "apimgt" + File.separator + "org.wso2.carbon.apimgt.core.feature" + File.separator + "resources" + File.separator + "dbscripts" + File.separator + "mysql.sql";
    } else if (POSTGRES.contains(database)) {
        ((PostgreDataSource) dataSource).resetDB();
        sqlFilePath = ".." + File.separator + ".." + File.separator + ".." + File.separator + "features" + File.separator + "apimgt" + File.separator + "org.wso2.carbon.apimgt.core.feature" + File.separator + "resources" + File.separator + "dbscripts" + File.separator + "postgres.sql";
    } else if (MSSQL.contains(database)) {
        ((MSSQLDataSource) dataSource).resetDB();
        sqlFilePath = ".." + File.separator + ".." + File.separator + ".." + File.separator + "features" + File.separator + "apimgt" + File.separator + "org.wso2.carbon.apimgt.core.feature" + File.separator + "resources" + File.separator + "dbscripts" + File.separator + "mssql.sql";
    } else if (ORACLE.contains(database)) {
        ((OracleDataSource) dataSource).resetDB();
        sqlFilePath = ".." + File.separator + ".." + File.separator + ".." + File.separator + "features" + File.separator + "apimgt" + File.separator + "org.wso2.carbon.apimgt.core.feature" + File.separator + "resources" + File.separator + "dbscripts" + File.separator + "oracle.sql";
    }
    DAOUtil.clearDataSource();
    DAOUtil.initialize(dataSource);
    try (Connection connection = DAOUtil.getConnection()) {
        DBScriptRunnerUtil.executeSQLScript(sqlFilePath, connection);
    }
    PolicyDAO policyDAO = DAOFactory.getPolicyDAO();
    SampleTestObjectCreator.createDefaultPolicy(policyDAO);
}
Also used : Connection(java.sql.Connection) PolicyDAO(org.wso2.carbon.apimgt.core.dao.PolicyDAO) BeforeMethod(org.testng.annotations.BeforeMethod)

Aggregations

HikariDataSource (com.zaxxer.hikari.HikariDataSource)2 DataSource (javax.sql.DataSource)2 Gson (com.google.gson.Gson)1 Connection (java.sql.Connection)1 Context (javax.naming.Context)1 NamingException (javax.naming.NamingException)1 ScriptException (org.jaggeryjs.scriptengine.exceptions.ScriptException)1 NativeObject (org.mozilla.javascript.NativeObject)1 BundleContext (org.osgi.framework.BundleContext)1 Activate (org.osgi.service.component.annotations.Activate)1 BeforeMethod (org.testng.annotations.BeforeMethod)1 Server (org.wso2.broker.amqp.Server)1 BrokerConfigProvider (org.wso2.broker.common.BrokerConfigProvider)1 StartupContext (org.wso2.broker.common.StartupContext)1 Broker (org.wso2.broker.core.Broker)1 BrokerConfiguration (org.wso2.broker.core.configuration.BrokerConfiguration)1 BrokerRestServer (org.wso2.broker.rest.BrokerRestServer)1 Broker (org.wso2.carbon.apimgt.core.api.Broker)1 PolicyDAO (org.wso2.carbon.apimgt.core.dao.PolicyDAO)1 DataSource (org.wso2.carbon.apimgt.core.dao.impl.DataSource)1