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);
}
}
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);
}
}
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);
}
}
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);
}
Aggregations