/** * Adds a new data source to the repository. * * @param dsmInfo The meta information of the data source to be added. */ public void addDataSource(DataSourceMetaInfo dsmInfo) throws DataSourceException { if (log.isDebugEnabled()) { log.debug("Adding data source: " + dsmInfo.getName()); } if (!dsmInfo.isSystem()) { this.persistDataSource(dsmInfo); } this.registerDataSource(dsmInfo); if (!dsmInfo.isSystem()) { this.notifyClusterDSChange(dsmInfo.getName()); } }
/** * 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 void registerJNDI(DataSourceMetaInfo dsmInfo, Object dsObject) throws DataSourceException { try { PrivilegedCarbonContext.startTenantFlow(); PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(this.getTenantId()); JNDIConfig jndiConfig = dsmInfo.getJndiConfig(); if (jndiConfig == null) { return; } InitialContext context; try { context = new InitialContext(jndiConfig.extractHashtableEnv()); } catch (NamingException e) { throw new DataSourceException("Error creating JNDI initial context: " + e.getMessage(), e); } this.checkAndCreateJNDISubContexts(context, jndiConfig.getName()); try { context.rebind(jndiConfig.getName(), dsObject); } catch (NamingException e) { throw new DataSourceException( "Error in binding to JNDI with name '" + jndiConfig.getName() + "' - " + e.getMessage(), e); } } finally { PrivilegedCarbonContext.endTenantFlow(); } }
private void persistDataSource(DataSourceMetaInfo dsmInfo) throws DataSourceException { try { Element element = DataSourceUtils.convertDataSourceMetaInfoToElement(dsmInfo, this.getDSMMarshaller()); DataSourceUtils.secureSaveElement(element); Resource resource = this.getRegistry().newResource(); resource.setContentStream(DataSourceUtils.elementToInputStream(element)); this.getRegistry() .put( DataSourceConstants.DATASOURCES_REPOSITORY_BASE_PATH + "/" + dsmInfo.getName(), resource); } catch (Exception e) { throw new DataSourceException( "Error in persisting data source: " + dsmInfo.getName() + " - " + e.getMessage(), e); } }
private Object createDataSourceObject(DataSourceMetaInfo dsmInfo, boolean isUseDataSourceFactory) throws DataSourceException { DataSourceReader dsReader = DataSourceManager.getInstance().getDataSourceReader(dsmInfo.getDefinition().getType()); if (dsReader == null) { throw new DataSourceException( "A data source reader cannot be found for the type '" + dsmInfo.getDefinition().getType() + "'"); } /* sets the current data source's (tenantId + ":" + name) as a thread local value * so it can be read by data source readers */ DataSourceUtils.setCurrentDataSourceId(this.getTenantId() + ":" + dsmInfo.getName()); return dsReader.createDataSource( DataSourceUtils.elementToString((Element) dsmInfo.getDefinition().getDsXMLConfiguration()), isUseDataSourceFactory); }
private void unregisterJNDI(DataSourceMetaInfo dsmInfo) { try { PrivilegedCarbonContext.startTenantFlow(); PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(this.getTenantId()); JNDIConfig jndiConfig = dsmInfo.getJndiConfig(); if (jndiConfig == null) { return; } try { InitialContext context = new InitialContext(jndiConfig.extractHashtableEnv()); context.unbind(jndiConfig.getName()); } catch (NamingException e) { log.error( "Error in unregistering JNDI name: " + jndiConfig.getName() + " - " + e.getMessage(), e); } } finally { PrivilegedCarbonContext.endTenantFlow(); } }
private synchronized void registerDataSource(DataSourceMetaInfo dsmInfo) throws DataSourceException { /* if a data source is already registered with the given name, unregister it first */ CarbonDataSource currentCDS = this.getDataSource(dsmInfo.getName()); if (currentCDS != null) { /* if the data source is a system data source, throw exception */ if (dsmInfo.isSystem()) { throw new DataSourceException( "System datasource " + dsmInfo.getName() + "can not be updated."); } this.unregisterDataSource(currentCDS.getDSMInfo().getName()); } if (log.isDebugEnabled()) { log.debug("Registering data source: " + dsmInfo.getName()); } Object dsObject = null; boolean isDataSourceFactoryReference = false; DataSourceStatus dsStatus; try { JNDIConfig jndiConfig = dsmInfo.getJndiConfig(); if (jndiConfig != null) { isDataSourceFactoryReference = jndiConfig.isUseDataSourceFactory(); } dsObject = this.createDataSourceObject(dsmInfo, isDataSourceFactoryReference); this.registerJNDI(dsmInfo, dsObject); dsStatus = new DataSourceStatus(DataSourceStatusModes.ACTIVE, null); } catch (Exception e) { String msg = "Error in registering data source: " + dsmInfo.getName() + " - " + e.getMessage(); log.error(msg, e); dsStatus = new DataSourceStatus(DataSourceStatusModes.ERROR, msg); } /* Creating DataSource object , if dsObject is a Reference */ if (isDataSourceFactoryReference) { dsObject = this.createDataSourceObject(dsmInfo, false); } CarbonDataSource cds = new CarbonDataSource(dsmInfo, dsStatus, dsObject); this.dataSources.put(cds.getDSMInfo().getName(), cds); }