org.wso2.carbon.ndatasource.common.DataSourceException Java Examples

The following examples show how to use org.wso2.carbon.ndatasource.common.DataSourceException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DBReportingService.java    From carbon-commons with Apache License 2.0 6 votes vote down vote up
public List<String> getCarbonDataSourceNames() {
    DataSourceService dataSourceService = ReportingComponent.
            getCarbonDataSourceService();
    if (dataSourceService == null) {
        log.error("Carbon data source service is not available, returning empty list");
        return new ArrayList<String>();
    }
    try {
        ArrayList<String> dsNames = new ArrayList<String>();
        List<CarbonDataSource> dataSourceList = dataSourceService.getAllDataSources();
        for (CarbonDataSource dataSource : dataSourceList){
            dsNames.add(dataSource.getDSMInfo().getName());
        }
        return dsNames;
    } catch (DataSourceException e) {
       log.error(e.getMessage());
        return new ArrayList<String>();
    }
}
 
Example #2
Source File: DatasourceMigrator.java    From product-ei with Apache License 2.0 6 votes vote down vote up
/**
 * Migrate the password in datasource configuration
 *
 * @param tenantId
 * @param dataSources
 * @throws MigrationClientException
 */
private void updatePasswordInRegistryDataSources(int tenantId, List<Resource> dataSources)
        throws MigrationClientException {
    for (Resource dataSource : dataSources) {
        try {
            InputStream contentStream = dataSource.getContentStream();
            OMElement omElement = Utility.toOM(contentStream);
            Iterator pit = ((OMElement) ((OMElement) omElement.getChildrenWithName(Constant.DEFINITION_Q).next())
                    .getChildrenWithName(Constant.CONFIGURATION_Q).next()).getChildrenWithName(Constant.PASSWORD_Q);
            while (pit.hasNext()) {
                OMElement passwordElement = (OMElement) pit.next();
                if (Boolean.parseBoolean(passwordElement.getAttributeValue(Constant.ENCRYPTED_Q))) {
                    String password = passwordElement.getText();
                    String newEncryptedPassword = Utility.getNewEncryptedValue(password);
                    if (StringUtils.isNotEmpty(newEncryptedPassword)) {
                        passwordElement.setText(newEncryptedPassword);
                        dataSource.setContent(omElement.toString().getBytes());
                        DataSourceDAO.saveDataSource(tenantId, dataSource);
                    }
                }
            }
        } catch (XMLStreamException | CryptoException | RegistryException | DataSourceException e) {
            throw new MigrationClientException(e.getMessage());
        }
    }
}
 
Example #3
Source File: DatasourceClient.java    From carbon-commons with Apache License 2.0 6 votes vote down vote up
/**
 * get all data source names available
 *
 * @return names of the datasources available in the server
 * @throws ReportingException will occurred if no datasources found.
 */
public String[] getDataSourceNames() throws ReportingException {
    DataSourceService dataSourceService =
            ReportingTemplateComponent.getCarbonDataSourceService();
    ArrayList<String> dsnames = new ArrayList<String>();
    if (dataSourceService != null) {
        try {
            List<CarbonDataSource> dataSources = dataSourceService.getAllDataSources();
            for (CarbonDataSource aDataSource : dataSources) {
                dsnames.add(aDataSource.getDSMInfo().getName());
            }
        } catch (DataSourceException e) {
            log.error(e.getMessage(), e);
            throw new ReportingException(e.getMessage(), e);
        }
        return dsnames.toArray(new String[dsnames.size()]);
    } else {
        log.error("No datasource service found");
        throw new ReportingException("No datasource service found");
    }
}
 
Example #4
Source File: HostObjectUtilsTest.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
@Test
public void testIsUsageDataSourceSpecified() throws Exception {
    PowerMockito.mockStatic(HostObjectComponent.class);
    HostObjectComponent hostObjectComponent = Mockito.mock(HostObjectComponent.class);
    DataSourceService dataSourceService = Mockito.mock(DataSourceService.class);
    CarbonDataSource carbonDataSource = Mockito.mock(CarbonDataSource.class);

    Mockito.when(hostObjectComponent.getDataSourceService()).thenReturn(dataSourceService);
    Mockito.when(dataSourceService.getDataSource(Mockito.anyString())).thenReturn(carbonDataSource);
    Assert.assertTrue(hostObject.isUsageDataSourceSpecified());

    //when datasource is not specified
    Mockito.when(dataSourceService.getDataSource(Mockito.anyString())).thenReturn(null);
    Assert.assertFalse(hostObject.isUsageDataSourceSpecified());

    //exception path
    Mockito.when(dataSourceService.getDataSource(Mockito.anyString())).thenThrow(new DataSourceException());
    hostObject.isUsageDataSourceSpecified();
}
 
Example #5
Source File: NDataSourceAdminServiceClient.java    From carbon-commons with Apache License 2.0 6 votes vote down vote up
public boolean testDataSourceConnection(WSDataSourceMetaInfo dataSourceMetaInfo) throws RemoteException, 
		DataSourceException {
	validateDataSourceMetaInformation(dataSourceMetaInfo);
	if (log.isDebugEnabled()) {
		log.debug("Going test connection of Datasource :" + dataSourceMetaInfo.getName());
	}
	try {
		return stub.testDataSourceConnection(dataSourceMetaInfo);
	} catch (NDataSourceAdminDataSourceException e) {
		if (e.getFaultMessage().getDataSourceException().isErrorMessageSpecified()) {
			handleException(e.getFaultMessage().getDataSourceException().getErrorMessage(), e);
		}
		handleException(e.getMessage(), e);
	}
	return false;
}
 
Example #6
Source File: NDataSourceAdminServiceClient.java    From carbon-commons with Apache License 2.0 5 votes vote down vote up
public void deleteDataSource(String dsName) throws RemoteException,
		DataSourceException {
	validateName(dsName);
	if (log.isDebugEnabled()) {
		log.debug("Going to delete a Datasource with name : " + dsName);
	}
	try {
		stub.deleteDataSource(dsName);
	} catch (NDataSourceAdminDataSourceException e) {
		if (e.getFaultMessage().getDataSourceException().isErrorMessageSpecified()) {
			handleException(e.getFaultMessage().getDataSourceException().getErrorMessage(), e);
		}
		handleException(e.getMessage(), e);
	}
}
 
Example #7
Source File: NDataSourceAdminServiceClient.java    From carbon-commons with Apache License 2.0 5 votes vote down vote up
public boolean reloadDataSource(String dsName) throws RemoteException,
		DataSourceException {
	validateName(dsName);
	try {
		return stub.reloadDataSource(dsName);
	} catch (NDataSourceAdminDataSourceException e) {
		if (e.getFaultMessage().getDataSourceException().isErrorMessageSpecified()) {
			handleException(e.getFaultMessage().getDataSourceException().getErrorMessage(), e);
		}
		handleException(e.getMessage(), e);
	}
	return false;
}
 
Example #8
Source File: NDataSourceAdminServiceClient.java    From carbon-commons with Apache License 2.0 5 votes vote down vote up
public boolean reloadAllDataSources() throws RemoteException,
		DataSourceException {
	try {
		return stub.reloadAllDataSources();
	} catch (NDataSourceAdminDataSourceException e) {
		if (e.getFaultMessage().getDataSourceException().isErrorMessageSpecified()) {
			handleException(e.getFaultMessage().getDataSourceException().getErrorMessage(), e);
		}
		handleException(e.getMessage(), e);
	}
	return false;
}
 
Example #9
Source File: NDataSourceAdminServiceClient.java    From carbon-commons with Apache License 2.0 5 votes vote down vote up
public String[] getDataSourceTypes() throws RemoteException,
		DataSourceException {
	String[] dataSourceTypes = null;
	try {
		dataSourceTypes = stub.getDataSourceTypes();
	} catch (NDataSourceAdminDataSourceException e) {
		if (e.getFaultMessage().getDataSourceException().isErrorMessageSpecified()) {
			handleException(e.getFaultMessage().getDataSourceException().getErrorMessage(), e);
		}
		handleException(e.getMessage(), e);
	}
	return dataSourceTypes;
}
 
Example #10
Source File: NDataSourceAdminServiceClient.java    From carbon-commons with Apache License 2.0 5 votes vote down vote up
public WSDataSourceInfo[] getAllDataSourcesForType(String dsType) throws RemoteException,
		DataSourceException {
	validateType(dsType);
	WSDataSourceInfo[] allDataSources = null;
	try {
		allDataSources = stub.getAllDataSourcesForType(dsType);
	} catch (NDataSourceAdminDataSourceException e) {
		if (e.getFaultMessage().getDataSourceException().isErrorMessageSpecified()) {
			handleException(e.getFaultMessage().getDataSourceException().getErrorMessage(), e);
		}
		handleException(e.getMessage(), e);
	}
	return allDataSources;
}
 
Example #11
Source File: NDataSourceAdminServiceClient.java    From carbon-commons with Apache License 2.0 5 votes vote down vote up
public WSDataSourceInfo getDataSource(String dsName) throws RemoteException,
		DataSourceException {
	validateName(dsName);
	WSDataSourceInfo wsDataSourceInfo = null;
	try {
		wsDataSourceInfo = stub.getDataSource(dsName);
	} catch (NDataSourceAdminDataSourceException e) {
		if (e.getFaultMessage().getDataSourceException().isErrorMessageSpecified()) {
			handleException(e.getFaultMessage().getDataSourceException().getErrorMessage(), e);
		}
		handleException(e.getMessage(), e);
	}
	return wsDataSourceInfo;
}
 
Example #12
Source File: NDataSourceAdminServiceClient.java    From carbon-commons with Apache License 2.0 5 votes vote down vote up
public WSDataSourceInfo[] getAllDataSources() throws RemoteException,
		DataSourceException {
	WSDataSourceInfo[] allDataSources = null;
	try {
		allDataSources = stub.getAllDataSources();
	} catch (NDataSourceAdminDataSourceException e) {
		if (e.getFaultMessage().getDataSourceException().isErrorMessageSpecified()) {
			handleException(e.getFaultMessage().getDataSourceException().getErrorMessage(), e);
		}
		handleException(e.getMessage(), e);
	}
	return allDataSources;
}
 
Example #13
Source File: DataSourceDAO.java    From product-ei with Apache License 2.0 5 votes vote down vote up
/**
 * Obtain the datasource registry instance of the tenant
 *
 * @param tenantId
 * @return
 * @throws DataSourceException
 */
private static Registry getRegistry(int tenantId) throws DataSourceException {

    Registry registry = DataSourceUtils.getConfRegistryForTenant(tenantId);
    if (log.isDebugEnabled()) {
        log.debug("Retrieving the config registry for tenant: " + tenantId);
    }
    return registry;
}
 
Example #14
Source File: NDataSourceAdminServiceClient.java    From carbon-commons with Apache License 2.0 5 votes vote down vote up
public void addDataSource(WSDataSourceMetaInfo dataSourceMetaInfo) throws RemoteException,
		DataSourceException {
	validateDataSourceMetaInformation(dataSourceMetaInfo);
	if (log.isDebugEnabled()) {
		log.debug("Going to add Datasource :" + dataSourceMetaInfo.getName());
	}
	try {
		stub.addDataSource(dataSourceMetaInfo);
	} catch (NDataSourceAdminDataSourceException e) {
		if (e.getFaultMessage().getDataSourceException().isErrorMessageSpecified()) {
			handleException(e.getFaultMessage().getDataSourceException().getErrorMessage(), e);
		}
		handleException(e.getMessage(), e);
	}
}
 
Example #15
Source File: HostObjectUtils.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
protected static boolean isUsageDataSourceSpecified() {
    try {
        return (null != HostObjectComponent.getDataSourceService().
                getDataSource(APIConstants.API_USAGE_DATA_SOURCE_NAME));
    } catch (DataSourceException e) {
        return false;
    }
}
 
Example #16
Source File: DataSourceDAO.java    From product-ei with Apache License 2.0 5 votes vote down vote up
/**
 * Save the resource in registry
 *
 * @param tenantId
 * @param resource
 * @throws DataSourceException
 */
public static void saveDataSource(int tenantId, Resource resource) throws DataSourceException {
    try {
        getRegistry(tenantId).put(resource.getPath(), resource);
    } catch (RegistryException e) {
        new DataSourceException("Error while saving the datasource into the registry.", e);
    }
}
 
Example #17
Source File: NDataSourceHelper.java    From carbon-commons with Apache License 2.0 4 votes vote down vote up
private static DSXMLConfiguration createDSXMLConfiguration(String type,
		HttpServletRequest request, NDataSourceAdminServiceClient client) throws RemoteException, DataSourceException, NDataSourceAdminDataSourceException {
	if (type.equals(NDataSourceClientConstants.RDBMS_DTAASOURCE_TYPE)) {
		RDBMSDSXMLConfiguration rdbmsDSXMLConfig = null;
		try {
			rdbmsDSXMLConfig = new RDBMSDSXMLConfiguration();
		} catch (NDataSourceAdminDataSourceException e) {
			handleException(e.getMessage());
		}

		String dsProvider = sanitizeInput(request.getParameter("dsProviderType"));
		if (NDataSourceClientConstants.RDBMS_EXTERNAL_DATASOURCE_PROVIDER.equals(dsProvider)) {
			String dsclassname = sanitizeInput(request.getParameter("dsclassname"));
			if (dsclassname == null || "".equals(dsclassname)) {
				handleException(bundle.getString("ds.dsclassname.cannotfound.msg"));
			}

			// retrieve external data source properties
			String dsproviderProperties = sanitizeInput(request.getParameter("dsproviderProperties"));
			if (dsproviderProperties == null || "".equals(dsproviderProperties)) {
				handleException(bundle.getString("ds.external.datasource.property.cannotfound.msg"));
			}
			String[] propsList = dsproviderProperties.split("::");
			String[] property = null;
			List<DataSourceProperty> dataSourceProps = new ArrayList<DataSourceProperty>();

			for (int i = 0; i < propsList.length; i++) {
				RDBMSDSXMLConfiguration.DataSourceProperty dataSourceProperty = new RDBMSDSXMLConfiguration.DataSourceProperty();
				property = propsList[i].split(",");
				dataSourceProperty.setName(property[0]);
				dataSourceProperty.setValue(property[1]);
				dataSourceProps.add(dataSourceProperty);
			}
			rdbmsDSXMLConfig.setDataSourceClassName(dsclassname);
			rdbmsDSXMLConfig.setDataSourceProps(dataSourceProps);
		} else if ("default".equals(dsProvider)) {
			String driver = Encode.forHtmlContent(request.getParameter("driver"));
			if (driver == null || "".equals(driver)) {
				handleException(bundle.getString("ds.driver.cannotfound.msg"));
			}
			String url = sanitizeInput(request.getParameter("url"));
			if (url == null || "".equals(url)) {
				handleException(bundle.getString("ds.url.cannotfound.msg"));
			}
			String username = sanitizeInput(request.getParameter("username"));
			String password = null;
			
			boolean isEditMode = Boolean.parseBoolean(request.getParameter("editMode"));
			if (isEditMode){
				String changePassword = request.getParameter("changePassword");
				changePassword = (changePassword == null || changePassword.equals("false")) ? "false" : "true";
				if (Boolean.parseBoolean(changePassword)) {
					password = request.getParameter("newPassword");
				} else {
					WSDataSourceInfo dataSourceInfo = client.getDataSource(request.getParameter("dsName"));
					WSDataSourceMetaInfo_WSDataSourceDefinition dataSourceDefinition = dataSourceInfo.getDsMetaInfo().getDefinition();
					String configuration = dataSourceDefinition.getDsXMLConfiguration();
					RDBMSDSXMLConfiguration rdbmsCon = (RDBMSDSXMLConfiguration)NDataSourceHelper.unMarshal(type, configuration);
					password = rdbmsCon.getPassword().getValue();
				}
			} else {
				password = request.getParameter("password");
			}

			rdbmsDSXMLConfig.setUrl(url);
			rdbmsDSXMLConfig.setDriverClassName(driver);
			rdbmsDSXMLConfig.setUsername(username);
			RDBMSDSXMLConfiguration.Password passwordOb = new RDBMSDSXMLConfiguration.Password();
			passwordOb.setValue(password);
			rdbmsDSXMLConfig.setPassword(passwordOb);
		} else {
			throw new IllegalArgumentException("Unknown data source provider type");
		}
		// retrieve data source parameteres.
		setDatasourceProperties(rdbmsDSXMLConfig, request);
		return rdbmsDSXMLConfig;
	} else {
		throw new IllegalArgumentException("Provided Data Source type not supported");
	}
}
 
Example #18
Source File: NDataSourceHelper.java    From carbon-commons with Apache License 2.0 4 votes vote down vote up
public static WSDataSourceMetaInfo createWSDataSourceMetaInfo(HttpServletRequest request, NDataSourceAdminServiceClient client) throws RemoteException, DataSourceException, NDataSourceAdminDataSourceException {
	WSDataSourceMetaInfo_WSDataSourceDefinition dataSourceDefinition = null;
	String datasourceType = sanitizeInput(request.getParameter("dsType"));
	String datasourceCustomType = sanitizeInput(request.getParameter("customDsType"));
	boolean configView = Boolean.parseBoolean(request.getParameter("configView"));
	bundle = ResourceBundle.getBundle("org.wso2.carbon.ndatasource.ui.i18n.Resources",
			request.getLocale());
	String name = sanitizeInput(request.getParameter("dsName"));
	if (name == null || "".equals(name)) {
		name = request.getParameter("name_hidden");
		if (name == null || "".equals(name)) {
			handleException(bundle.getString("ds.name.cannotfound.msg"));
		}
	}
	
	if (configView && (datasourceCustomType == null || "".equals(datasourceCustomType))) {
		handleException(bundle.getString("custom.ds.type.name.cannotfound.msg"));
	}

	String description = sanitizeInput(request.getParameter("description"));

	WSDataSourceMetaInfo dataSourceMetaInfo = new WSDataSourceMetaInfo();
	dataSourceMetaInfo.setName(name);
	dataSourceMetaInfo.setSystem(Boolean.parseBoolean(request.getParameter("isSystem")));
	if (description != null && !("".equals(description))) {
		dataSourceMetaInfo.setDescription(description);
	}
	if (configView) {
		dataSourceDefinition = createCustomDS(request.getParameter("configContent"), datasourceCustomType);
	} else if (datasourceType.equals(NDataSourceClientConstants.RDBMS_DTAASOURCE_TYPE)) {
		if (request.getParameter("jndiname") != null && !request.getParameter("jndiname").equals("")) {
			dataSourceMetaInfo.setJndiConfig(createJNDIConfig(request));
		} else {
			if (request.getParameter("useDataSourceFactory") != null || 
					(request.getParameter("jndiProperties") != null && !request.getParameter("jndiProperties").equals(""))) {
				handleException(bundle.getString("jndi.name.cannotfound.msg"));
			}
		}

		DSXMLConfiguration dsXMLConfig = createDSXMLConfiguration(datasourceType, request, client);

		dataSourceDefinition = createWSDataSourceDefinition(
				dsXMLConfig, datasourceType);
	} else {
		throw new IllegalArgumentException("Provided Data Source type not supported");
	}
	dataSourceMetaInfo.setDefinition(dataSourceDefinition);
	return dataSourceMetaInfo;
}
 
Example #19
Source File: NDataSourceAdminServiceClient.java    From carbon-commons with Apache License 2.0 4 votes vote down vote up
private static void handleException(String msg, Exception e) throws DataSourceException{
	throw new DataSourceException(msg, e);
}