private Map<String, String> buildReplacements(final STenant tenant) { return singletonMap("\\$\\{tenantid\\}", Long.toString(tenant.getId())); }
protected void checkThatWeCanLogin(final String userName, final STenant sTenant, TechnicalUser technicalUser) throws LoginException { if (!sTenant.isActivated()) { throw new LoginException("Tenant " + sTenant.getName() + " is not activated !!"); } if (sTenant.isPaused()) { final String technicalUserName = technicalUser.getUserName(); if (!technicalUserName.equals(userName)) { throw new TenantStatusException("Tenant with ID " + sTenant.getId() + " is in pause, unable to login with other user than the technical user."); } } }
@Override public void run() { final TechnicalLoggerService technicalLoggerService = tenantServiceAccessor.getTechnicalLoggerService(); try { final long tenantId = tenantServiceAccessor.getTenantId(); SessionAccessor sessionAccessor = tenantServiceAccessor.getSessionAccessor(); STenant tenant = getTenant(tenantId); technicalLoggerService.log(getClass(), TechnicalLogSeverity.INFO, "Restarting elements of tenant " + tenant.getId() + " that were not finished at the last shutdown"); if (tenant.isPaused() || !tenant.isActivated()) { technicalLoggerService.log(getClass(), TechnicalLogSeverity.WARNING, "Unable to restart elements of tenant " + tenant.getStatus()); return; } executeHandlers(tenantId, sessionAccessor); } catch (Exception e) { technicalLoggerService.log(StarterThread.class, TechnicalLogSeverity.ERROR, "Error while restarting elements", e); } }
void startServicesOfTenants(final PlatformServiceAccessor platformAccessor, final SessionAccessor sessionAccessor, final List<STenant> tenants) throws Exception { for (final STenant tenant : tenants) { final long tenantId = tenant.getId(); if (!tenant.isPaused() && tenant.isActivated()) { final SessionService sessionService = platformAccessor.getTenantServiceAccessor(tenantId).getSessionService(); long sessionId = -1; long platformSessionId = -1; try { platformSessionId = sessionAccessor.getSessionId(); sessionAccessor.deleteSessionId(); sessionId = createSessionAndMakeItActive(platformAccessor, sessionAccessor, tenantId); final SetServiceState startService = new SetServiceState(tenantId, new StartServiceStrategy()); platformAccessor.getTransactionService().executeInTransaction(startService); } finally { sessionService.deleteSession(sessionId); cleanSessionAccessor(sessionAccessor, platformSessionId); } } } }
protected APISession loginInternal(final Long tenantId, final Map<String, Serializable> credentials) throws Exception { final String userName = credentials.get(AuthenticationConstants.BASIC_USERNAME) != null ? String.valueOf(credentials .get(AuthenticationConstants.BASIC_USERNAME)) : null; final PlatformServiceAccessor platformServiceAccessor = ServiceAccessorFactory.getInstance().createPlatformServiceAccessor(); final STenant sTenant = getTenant(tenantId, platformServiceAccessor); final TenantServiceAccessor serviceAccessor = getTenantServiceAccessor(sTenant.getId()); checkThatWeCanLogin(userName, sTenant, serviceAccessor.getTechnicalUser()); final LoginService loginService = serviceAccessor.getLoginService(); final TransactionService transactionService = platformServiceAccessor.getTransactionService(); SessionAccessor sessionAccessor = serviceAccessor.getSessionAccessor(); final Map<String, Serializable> credentialsWithResolvedTenantId = new HashMap<>(credentials); credentialsWithResolvedTenantId.put(AuthenticationConstants.BASIC_TENANT_ID, sTenant.getId()); sessionAccessor.setTenantId(sTenant.getId()); try { final SSession sSession = transactionService.executeInTransaction(() -> loginService.login(credentialsWithResolvedTenantId)); return ModelConvertor.toAPISession(sSession, sTenant.getName()); } catch (Exception e) { //avoid brut force... (should be done differently, but it is the behavior since 6.0.0) Thread.sleep(3000); throw e; } }
@Override public void deleteTenantObjects(final long tenantId) throws STenantDeletionException, STenantNotFoundException, SDeletingActivatedTenantException { if (isTraced) { logger.log(this.getClass(), TechnicalLogSeverity.TRACE, LogUtil.getLogBeforeMethod(this.getClass(), LOG_DELETE_TENANT_OBJECTS)); } final STenant tenant = getTenant(tenantId); if (tenant.getStatus().equals(STenant.ACTIVATED)) { throw new SDeletingActivatedTenantException(); } final Map<String, String> replacements = buildReplacements(tenant); try { executeSQLResources(Collections.singletonList("deleteTenantObjects.sql"), replacements); } catch (final IOException | SQLException e) { throw new STenantDeletionException(e); } }
private void checkIfTenantAlreadyExists(final STenant tenant) throws STenantAlreadyExistException { try { final String tenantName = tenant.getName(); final STenant existingTenant = getTenantByName(tenantName); if (existingTenant != null) { throw new STenantAlreadyExistException("Unable to create the tenant " + tenantName + " : it already exists."); } } catch (final STenantNotFoundException e) { if (isTraced) { logger.log(this.getClass(), TechnicalLogSeverity.TRACE, LogUtil.getLogOnExceptionMethod(this.getClass(), LOG_CREATE_TENANT, e)); } // OK } }
@Override public boolean activateTenant(final long tenantId) throws STenantNotFoundException, STenantActivationException { if (isTraced) { logger.log(this.getClass(), TechnicalLogSeverity.TRACE, LogUtil.getLogBeforeMethod(this.getClass(), LOG_ACTIVATE_TENANT)); } final STenant tenant = getTenant(tenantId); if (tenant.isActivated()) { return false; } final UpdateDescriptor desc = new UpdateDescriptor(tenant); desc.addField(BuilderFactory.get(STenantBuilderFactory.class).getStatusKey(), STenant.ACTIVATED); try { platformPersistenceService.update(desc); if (isTraced) { logger.log(this.getClass(), TechnicalLogSeverity.TRACE, LogUtil.getLogAfterMethod(this.getClass(), LOG_ACTIVATE_TENANT)); } } catch (final SPersistenceException e) { if (isTraced) { logger.log(this.getClass(), TechnicalLogSeverity.TRACE, LogUtil.getLogOnExceptionMethod(this.getClass(), LOG_ACTIVATE_TENANT, e)); } if (logger.isLoggable(getClass(), TechnicalLogSeverity.DEBUG)) { logger.log(this.getClass(), TechnicalLogSeverity.DEBUG, e); } throw new STenantActivationException("Problem while activating tenant: " + tenant, e); } return true; }
@Override @AvailableWhenTenantIsPaused public boolean isPaused() { final long tenantId = getTenantId(); try { return getPlatformAccessorNoException().getPlatformService().getTenant(tenantId).isPaused(); } catch (final SBonitaException e) { throw new RetrieveException("Unable to retrieve the tenant with id " + tenantId, e); } }
void startServicesOfTenants(final PlatformServiceAccessor platformAccessor, final SessionAccessor sessionAccessor, final List<STenant> tenants) throws Exception { for (final STenant tenant : tenants) { final long tenantId = tenant.getId(); if (!tenant.isPaused() && tenant.isActivated()) { final SessionService sessionService = platformAccessor.getTenantServiceAccessor(tenantId).getSessionService(); long sessionId = -1; long platformSessionId = -1; try { platformSessionId = sessionAccessor.getSessionId(); sessionAccessor.deleteSessionId(); sessionId = createSessionAndMakeItActive(platformAccessor, sessionAccessor, tenantId); final SetServiceState startService = new SetServiceState(tenantId, new StartServiceStrategy()); platformAccessor.getTransactionService().executeInTransaction(startService); } finally { sessionService.deleteSession(sessionId); cleanSessionAccessor(sessionAccessor, platformSessionId); } } } }
protected APISession loginInternal(final Long tenantId, final Map<String, Serializable> credentials) throws Exception { final String userName = credentials.get(AuthenticationConstants.BASIC_USERNAME) != null ? String.valueOf(credentials .get(AuthenticationConstants.BASIC_USERNAME)) : null; final PlatformServiceAccessor platformServiceAccessor = ServiceAccessorFactory.getInstance().createPlatformServiceAccessor(); final STenant sTenant = getTenant(tenantId, platformServiceAccessor); final TenantServiceAccessor serviceAccessor = getTenantServiceAccessor(sTenant.getId()); checkThatWeCanLogin(userName, sTenant, serviceAccessor.getTechnicalUser()); final LoginService loginService = serviceAccessor.getLoginService(); final TransactionService transactionService = platformServiceAccessor.getTransactionService(); SessionAccessor sessionAccessor = serviceAccessor.getSessionAccessor(); final Map<String, Serializable> credentialsWithResolvedTenantId = new HashMap<>(credentials); credentialsWithResolvedTenantId.put(AuthenticationConstants.BASIC_TENANT_ID, sTenant.getId()); sessionAccessor.setTenantId(sTenant.getId()); try { final SSession sSession = transactionService.executeInTransaction(() -> loginService.login(credentialsWithResolvedTenantId)); return ModelConvertor.toAPISession(sSession, sTenant.getName()); } catch (Exception e) { //avoid brut force... (should be done differently, but it is the behavior since 6.0.0) Thread.sleep(3000); throw e; } }
@Override public void deleteTenantObjects(final long tenantId) throws STenantDeletionException, STenantNotFoundException, SDeletingActivatedTenantException { if (isTraced) { logger.log(this.getClass(), TechnicalLogSeverity.TRACE, LogUtil.getLogBeforeMethod(this.getClass(), LOG_DELETE_TENANT_OBJECTS)); } final STenant tenant = getTenant(tenantId); if (tenant.getStatus().equals(STenant.ACTIVATED)) { throw new SDeletingActivatedTenantException(); } final Map<String, String> replacements = buildReplacements(tenant); try { executeSQLResources(Collections.singletonList("deleteTenantObjects.sql"), replacements); } catch (final IOException | SQLException e) { throw new STenantDeletionException(e); } }
private void checkIfTenantAlreadyExists(final STenant tenant) throws STenantAlreadyExistException { try { final String tenantName = tenant.getName(); final STenant existingTenant = getTenantByName(tenantName); if (existingTenant != null) { throw new STenantAlreadyExistException("Unable to create the tenant " + tenantName + " : it already exists."); } } catch (final STenantNotFoundException e) { if (isTraced) { logger.log(this.getClass(), TechnicalLogSeverity.TRACE, LogUtil.getLogOnExceptionMethod(this.getClass(), LOG_CREATE_TENANT, e)); } // OK } }
@Override public boolean activateTenant(final long tenantId) throws STenantNotFoundException, STenantActivationException { if (isTraced) { logger.log(this.getClass(), TechnicalLogSeverity.TRACE, LogUtil.getLogBeforeMethod(this.getClass(), LOG_ACTIVATE_TENANT)); } final STenant tenant = getTenant(tenantId); if (tenant.isActivated()) { return false; } final UpdateDescriptor desc = new UpdateDescriptor(tenant); desc.addField(BuilderFactory.get(STenantBuilderFactory.class).getStatusKey(), STenant.ACTIVATED); try { platformPersistenceService.update(desc); if (isTraced) { logger.log(this.getClass(), TechnicalLogSeverity.TRACE, LogUtil.getLogAfterMethod(this.getClass(), LOG_ACTIVATE_TENANT)); } } catch (final SPersistenceException e) { if (isTraced) { logger.log(this.getClass(), TechnicalLogSeverity.TRACE, LogUtil.getLogOnExceptionMethod(this.getClass(), LOG_ACTIVATE_TENANT, e)); } if (logger.isLoggable(getClass(), TechnicalLogSeverity.DEBUG)) { logger.log(this.getClass(), TechnicalLogSeverity.DEBUG, e); } throw new STenantActivationException("Problem while activating tenant: " + tenant, e); } return true; }
@Override @AvailableWhenTenantIsPaused public boolean isPaused() { final long tenantId = getTenantId(); try { return getPlatformAccessorNoException().getPlatformService().getTenant(tenantId).isPaused(); } catch (final SBonitaException e) { throw new RetrieveException("Unable to retrieve the tenant with id " + tenantId, e); } }
private Map<String, String> buildReplacements(final STenant tenant) { return singletonMap("\\$\\{tenantid\\}", Long.toString(tenant.getId())); }
protected void checkThatWeCanLogin(final String userName, final STenant sTenant, TechnicalUser technicalUser) throws LoginException { if (!sTenant.isActivated()) { throw new LoginException("Tenant " + sTenant.getName() + " is not activated !!"); } if (sTenant.isPaused()) { final String technicalUserName = technicalUser.getUserName(); if (!technicalUserName.equals(userName)) { throw new TenantStatusException("Tenant with ID " + sTenant.getId() + " is in pause, unable to login with other user than the technical user."); } } }
@Override public void run() { final TechnicalLoggerService technicalLoggerService = tenantServiceAccessor.getTechnicalLoggerService(); try { final long tenantId = tenantServiceAccessor.getTenantId(); SessionAccessor sessionAccessor = tenantServiceAccessor.getSessionAccessor(); STenant tenant = getTenant(tenantId); technicalLoggerService.log(getClass(), TechnicalLogSeverity.INFO, "Restarting elements of tenant " + tenant.getId() + " that were not finished at the last shutdown"); if (tenant.isPaused() || !tenant.isActivated()) { technicalLoggerService.log(getClass(), TechnicalLogSeverity.WARNING, "Unable to restart elements of tenant " + tenant.getStatus()); return; } executeHandlers(tenantId, sessionAccessor); } catch (Exception e) { technicalLoggerService.log(StarterThread.class, TechnicalLogSeverity.ERROR, "Error while restarting elements", e); } }