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