private void updateAllUserDataSource(boolean unregister) throws DataSourceException { try { if (this.getRegistry().resourceExists(DataSourceConstants.DATASOURCES_REPOSITORY_BASE_PATH)) { Collection dsCollection = (Collection) this.getRegistry().get(DataSourceConstants.DATASOURCES_REPOSITORY_BASE_PATH); String[] dsmPaths = dsCollection.getChildren(); for (String dsmPath : dsmPaths) { try { this.updateDataSource(this.resourceNameFromPath(dsmPath), unregister); } catch (DataSourceException e) { log.error( "Error in updating data source [remove:" + unregister + "] at path '" + dsmPath + "': " + e.getMessage(), e); } } } } catch (Exception e) { throw new DataSourceException( "Error in getting all data sources from repository: " + e.getMessage(), e); } }
public static DataSource createDataSource(RDBMSConfiguration config) { try { RDBMSDataSource dataSource = new RDBMSDataSource(config); return dataSource.getDataSource(); } catch (DataSourceException e) { throw new RuntimeException("Error in creating data source: " + e.getMessage(), e); } }
/** * Tests Connection of the data source * * @param dsmInfo The meta information of the data source to be tested. */ public boolean testDataSourceConnection(DataSourceMetaInfo dsmInfo) throws DataSourceException { if (log.isDebugEnabled()) { log.debug("Testing connection of data source: " + dsmInfo.getName()); } DataSourceReader dsReader = DataSourceManager.getInstance().getDataSourceReader(dsmInfo.getDefinition().getType()); try { return dsReader.testDataSourceConnection( DataSourceUtils.elementToString( (Element) dsmInfo.getDefinition().getDsXMLConfiguration())); } catch (DataSourceException e) { log.error(e.getMessage(), e); throw e; } }
private static Connection createConnection(String dataSourceId) throws SQLException, DataServiceFault { DataSourceService dataSourceService = DataServicesDSComponent.getDataSourceService(); CarbonDataSource cds; try { cds = dataSourceService.getDataSource(dataSourceId); } catch (DataSourceException e) { throw new DataServiceFault(e, "Error in retrieving data source: " + e.getMessage()); } if (cds == null) { throw new DataServiceFault("DataSource '" + dataSourceId + "' is not available."); } Object ds = cds.getDSObject(); if (!(ds instanceof DataSource)) { throw new DataServiceFault("DataSource '" + dataSourceId + "' is not an RDBMS data source."); } return ((DataSource) ds).getConnection(); }
private static DataSourceMetaInfo.DataSourceDefinition createDSXMLDefinition( RDBMSConfiguration rdbmsConfiguration) throws RSSManagerException { ByteArrayOutputStream out = new ByteArrayOutputStream(); try { createMarshaller().marshal(rdbmsConfiguration, out); } catch (JAXBException e) { String msg = "Error occurred while marshalling datasource configuration"; throw new RSSManagerException(msg, e); } DataSourceMetaInfo.DataSourceDefinition defn = new DataSourceMetaInfo.DataSourceDefinition(); ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); defn.setType(RSSManagerConstants.RDBMS_DATA_SOURCE_TYPE); try { defn.setDsXMLConfiguration(DataSourceUtils.convertToDocument(in).getDocumentElement()); } catch (DataSourceException e) { throw new RSSManagerException(e.getMessage(), e); } return defn; }
/** * Method to read data source file and create the object from it. * * @param file - xml file * @return - dataSourceMetaInfo object which is created using the xml file. * @throws - org.apache.axis2.deployment.DeploymentException. */ private DataSourceMetaInfo readDataSourceFile(File file) throws DeploymentException { if (log.isDebugEnabled()) { log.debug("Reading data source file from car file - " + file.getName()); } try { InputStream in = new FileInputStream(file); Document doc = DataSourceUtils.convertToDocument(in); /* only super tenant will lookup secure vault information for system data sources, * others are not allowed to */ DataSourceUtils.secureResolveDocument(doc, false); // create JAXB context and initializing Marshaller JAXBContext jaxbContext = JAXBContext.newInstance(DataSourceMetaInfo.class); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); // this will create Java object - data source from xml file DataSourceMetaInfo dataSourceMetaInfo = (DataSourceMetaInfo) jaxbUnmarshaller.unmarshal(doc); return dataSourceMetaInfo; } catch (JAXBException e) { throw new DeploymentException( "DataSourceCappDeployer::readDataSourceFile --> " + "Error in reading data source file: " + e.getMessage(), e); } catch (FileNotFoundException e) { throw new DeploymentException( "DataSourceCappDeployer::readDataSourceFile --> " + "Error data source file not found: " + e.getMessage(), e); } catch (DataSourceException e) { throw new DeploymentException( "DataSourceCappDeployer::readDataSourceFile --> " + "Error in decrypting data source file: " + e.getMessage(), e); } }
public static DataSource createUserStoreDataSource(RealmConfiguration realmConfig) { String dataSourceName = realmConfig.getUserStoreProperty(JDBCRealmConstants.DATASOURCE); if (dataSourceName != null) { return lookupDataSource(dataSourceName); } RDBMSConfiguration dsConfig = new RDBMSConfiguration(); dsConfig.setDriverClassName(realmConfig.getUserStoreProperty(JDBCRealmConstants.DRIVER_NAME)); if (dsConfig.getDriverClassName() == null) { return null; } dsConfig.setUrl(realmConfig.getUserStoreProperty(JDBCRealmConstants.URL)); dsConfig.setUsername(realmConfig.getUserStoreProperty(JDBCRealmConstants.USER_NAME)); dsConfig.setPassword(realmConfig.getUserStoreProperty(JDBCRealmConstants.PASSWORD)); if (realmConfig.getUserStoreProperty(JDBCRealmConstants.MAX_ACTIVE) != null && !realmConfig.getUserStoreProperty(JDBCRealmConstants.MAX_ACTIVE).equals("")) { dsConfig.setMaxActive( Integer.parseInt(realmConfig.getUserStoreProperty(JDBCRealmConstants.MAX_ACTIVE))); } else { dsConfig.setMaxActive(DEFAULT_MAX_ACTIVE); } if (realmConfig.getUserStoreProperty(JDBCRealmConstants.MIN_IDLE) != null && !realmConfig.getUserStoreProperty(JDBCRealmConstants.MIN_IDLE).equals("")) { dsConfig.setMinIdle( Integer.parseInt(realmConfig.getUserStoreProperty(JDBCRealmConstants.MIN_IDLE))); } else { dsConfig.setMinIdle(DEFAULT_MIN_IDLE); } if (realmConfig.getUserStoreProperty(JDBCRealmConstants.MAX_IDLE) != null && !realmConfig.getUserStoreProperty(JDBCRealmConstants.MAX_IDLE).equals("")) { dsConfig.setMinIdle( Integer.parseInt(realmConfig.getUserStoreProperty(JDBCRealmConstants.MAX_IDLE))); } else { dsConfig.setMinIdle(DEFAULT_MAX_IDLE); } if (realmConfig.getUserStoreProperty(JDBCRealmConstants.MAX_WAIT) != null && !realmConfig.getUserStoreProperty(JDBCRealmConstants.MAX_WAIT).equals("")) { dsConfig.setMaxWait( Integer.parseInt(realmConfig.getUserStoreProperty(JDBCRealmConstants.MAX_WAIT))); } else { dsConfig.setMaxWait(DEFAULT_MAX_WAIT); } if (realmConfig.getUserStoreProperty(JDBCRealmConstants.TEST_WHILE_IDLE) != null && !realmConfig.getUserStoreProperty(JDBCRealmConstants.TEST_WHILE_IDLE).equals("")) { dsConfig.setTestWhileIdle( Boolean.parseBoolean( realmConfig.getUserStoreProperty(JDBCRealmConstants.TEST_WHILE_IDLE))); } if (realmConfig.getUserStoreProperty(JDBCRealmConstants.TIME_BETWEEN_EVICTION_RUNS_MILLIS) != null && !realmConfig .getUserStoreProperty(JDBCRealmConstants.TIME_BETWEEN_EVICTION_RUNS_MILLIS) .equals("")) { dsConfig.setTimeBetweenEvictionRunsMillis( Integer.parseInt( realmConfig.getUserStoreProperty( JDBCRealmConstants.TIME_BETWEEN_EVICTION_RUNS_MILLIS))); } if (realmConfig.getUserStoreProperty(JDBCRealmConstants.MIN_EVIC_TABLE_IDLE_TIME_MILLIS) != null && !realmConfig .getUserStoreProperty(JDBCRealmConstants.MIN_EVIC_TABLE_IDLE_TIME_MILLIS) .equals("")) { dsConfig.setMinEvictableIdleTimeMillis( Integer.parseInt( realmConfig.getUserStoreProperty( JDBCRealmConstants.MIN_EVIC_TABLE_IDLE_TIME_MILLIS))); } if (realmConfig.getUserStoreProperty(JDBCRealmConstants.VALIDATION_QUERY) != null) { dsConfig.setValidationQuery( realmConfig.getUserStoreProperty(JDBCRealmConstants.VALIDATION_QUERY)); } try { return new RDBMSDataSource(dsConfig).getDataSource(); } catch (DataSourceException e) { throw new RuntimeException("Error in creating data source: " + e.getMessage(), e); } }
/** * Deploy or un-deploy data sources. if deploying, adding the data source to the data sources and * if undeploying, removing the data source from data sources. there can be multiple data sources * as separate xml files. * * @param deploy - identify whether deployment process or un-deployment process. * @param artifacts - list of artifacts to be deployed. */ private void deployUnDeployDataSources(boolean deploy, List<Artifact> artifacts) throws DeploymentException { for (Artifact artifact : artifacts) { if (DATA_SOURCE_TYPE.equals(artifact.getType())) { List<CappFile> files = artifact.getFiles(); if (files == null || files.isEmpty()) { throw new DeploymentException( "DataSourceCappDeployer::deployUnDeployDataSources --> " + "Error No data sources found in the artifact to deploy"); } for (CappFile cappFile : files) { String fileName = cappFile.getName(); String dataSourceConfigPath = artifact.getExtractedPath() + File.separator + fileName; File file = new File(dataSourceConfigPath); if (!file.exists()) { throw new DeploymentException( "DataSourceCappDeployer::deployUnDeployDataSources --> " + "Error Data source file cannot be found in artifact, " + "file name - " + fileName); } DataSourceMetaInfo dataSourceMetaInfo = readDataSourceFile(file); /** * If some artifact fail to deploy, then whole car file will fail, then it will invoke * un-deploy flow for the car file so that should only un-deploy artifacts which are * already deployed. To identify that we use deployment status of the artifact */ if (deploy) { try { if (DataSourceManager.getInstance() .getDataSourceRepository() .getDataSource(dataSourceMetaInfo.getName()) != null) { artifact.setDeploymentStatus(AppDeployerConstants.DEPLOYMENT_STATUS_FAILED); throw new DeploymentException( "DataSourceCappDeployer::deployUnDeployDataSources --> " + "Error in deploying data source: data source " + "with same name already exist, " + "data source name - " + dataSourceMetaInfo.getName()); } dataSourceMetaInfo.setCarbonApplicationDeployed(true); DataSourceManager.getInstance() .getDataSourceRepository() .addDataSource(dataSourceMetaInfo); artifact.setDeploymentStatus(AppDeployerConstants.DEPLOYMENT_STATUS_DEPLOYED); } catch (DataSourceException e) { throw new DeploymentException( "DataSourceCappDeployer::deployUnDeployDataSources --> " + "Error in deploying data source: " + e.getMessage(), e); } } else { try { if (DataSourceManager.getInstance() .getDataSourceRepository() .getDataSource(dataSourceMetaInfo.getName()) != null && artifact .getDeploymentStatus() .equals(AppDeployerConstants.DEPLOYMENT_STATUS_DEPLOYED)) { DataSourceManager.getInstance() .getDataSourceRepository() .deleteDataSource(dataSourceMetaInfo.getName()); } } catch (DataSourceException e) { throw new DeploymentException( "DataSourceCappDeployer::deployUnDeployDataSources --> " + "Error in undeploying data source: " + e.getMessage(), e); } } } } } }