/**
  * Add the default Resident Identity Provider entry when a new tenant is registered.
  *
  * @param tenantInfo Information about the newly created tenant
  */
 public void onTenantCreate(TenantInfoBean tenantInfo) throws StratosException {
   try {
     String tenantDomain = tenantInfo.getTenantDomain();
     IdentityProvider identityProvider = new IdentityProvider();
     identityProvider.setIdentityProviderName(
         IdentityApplicationConstants.RESIDENT_IDP_RESERVED_NAME);
     identityProvider.setHomeRealmId("localhost");
     identityProvider.setPrimary(true);
     IdentityProviderManager.getInstance().addResidentIdP(identityProvider, tenantDomain);
   } catch (IdentityApplicationManagementException e) {
     String message =
         "Error when adding Resident Identity Provider entry for tenant "
             + tenantInfo.getTenantDomain();
     log.error(message, e);
     throw new StratosException(message);
   }
 }
  /**
   * super admin adds a tenant
   *
   * @param tenantInfoBean tenant info bean
   * @return UUID
   * @throws Exception if error in adding new tenant.
   */
  public String addTenant(TenantInfoBean tenantInfoBean) throws Exception {
    try {
      PrivilegedCarbonContext.startTenantFlow();
      PrivilegedCarbonContext.getThreadLocalCarbonContext()
          .setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
      PrivilegedCarbonContext.getThreadLocalCarbonContext()
          .setTenantId(MultitenantConstants.SUPER_TENANT_ID);
      Tenant tenant = TenantMgtUtil.initializeTenant(tenantInfoBean);
      tenant.setId(tenantInfoBean.getTenantId());
      TenantPersistor persistor = new TenantPersistor();
      int tenantId;
      // not validating the domain ownership, since created by super tenant
      try {
        tenantId =
            persistor.persistTenant(
                tenant,
                false,
                tenantInfoBean.getSuccessKey(),
                tenantInfoBean.getOriginatedService(),
                false);
      } catch (Exception e) {
        String msg = "Error in persisting tenant ";
        log.error(msg, e);
        throw new AppFactoryException(msg, e);
      }

      tenantInfoBean.setTenantId(tenantId);
      TenantMgtUtil.addClaimsToUserStoreManager(tenant);
      // Notify tenant addition
      try {
        TenantMgtUtil.triggerAddTenant(tenantInfoBean);
      } catch (StratosException e) {
        String msg = "Error in notifying tenant addition.";
        log.error(msg, e);
        throw new AppFactoryException(msg, e);
      }

      TenantMgtUtil.activateTenantInitially(tenantInfoBean, tenantId);
      return TenantMgtUtil.prepareStringToShowThemeMgtPage(tenant.getId());
    } finally {
      PrivilegedCarbonContext.endTenantFlow();
    }
  }
  /** @param message - map message which contains data to tenant creation via a rest call. */
  @Override
  public void onMessage(Message message) {

    TenantInfoBean tenantInfoBean = null;
    MapMessage mapMessage;
    if (message instanceof MapMessage) {
      mapMessage = (MapMessage) message;
      String tenantInfoJson;
      try {
        tenantInfoJson = mapMessage.getString(AppFactoryConstants.TENANT_INFO);
        ObjectMapper mapper = new ObjectMapper();
        tenantInfoBean = mapper.readValue(tenantInfoJson, TenantInfoBean.class);

        if (log.isDebugEnabled()) {
          log.debug("Received a message for tenant domain " + tenantInfoBean.getTenantDomain());
        }
        mapMessage.acknowledge();
      } catch (JMSException e) {
        log.error("Error while getting message content.", e);
        throw new RuntimeException(e);
      } catch (JsonParseException e) {
        log.error("Error while converting the json to object.", e);
        throw new RuntimeException(e);
      } catch (JsonMappingException e) {
        log.error("Error while converting the json to object.", e);
        throw new RuntimeException(e);
      } catch (IOException e) {
        log.error("Error while converting the json to object.", e);
        throw new RuntimeException(e);
      }
    }

    try {
      int tenantId =
          ServiceHolder.getRealmService()
              .getTenantManager()
              .getTenantId(tenantInfoBean.getTenantDomain());
      if (tenantId == MultitenantConstants.INVALID_TENANT_ID) {
        addTenant(tenantInfoBean);
      } else {
        if (log.isDebugEnabled()) {
          log.debug(
              "Tenant Already exist, skipping the tenant addition. Tenant domain : "
                  + tenantInfoBean.getTenantDomain()
                  + "and tenant Id : "
                  + tenantInfoBean.getTenantId());
        }
      }

    } catch (JMSException e) {
      String msg = "Can not read received map massage";
      log.error(msg, e);
      throw new RuntimeException(e);
    } catch (AppFactoryException e) {
      String msg = "Can not create tenant";
      log.error(msg, e);
      throw new RuntimeException(e);
    } catch (Exception e) {
      String msg = "Can not create tenant";
      log.error(msg, e);
      throw new RuntimeException(e);
    }
  }