-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
msearch-453: add manually components missing from merge with master
- Loading branch information
1 parent
4790a69
commit 5e49571
Showing
5 changed files
with
140 additions
and
5 deletions.
There are no files selected for viewing
87 changes: 87 additions & 0 deletions
87
src/main/java/org/folio/search/model/context/FolioExecutionContextBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package org.folio.search.model.context; | ||
|
||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import lombok.AccessLevel; | ||
import lombok.AllArgsConstructor; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.With; | ||
import org.folio.spring.FolioExecutionContext; | ||
import org.folio.spring.FolioModuleMetadata; | ||
import org.folio.spring.model.SystemUser; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component("searchFolioExecutionContextBuilder") | ||
@RequiredArgsConstructor | ||
public class FolioExecutionContextBuilder { | ||
private final FolioModuleMetadata moduleMetadata; | ||
|
||
public FolioExecutionContextBuilder.Builder builder() { | ||
return new Builder(moduleMetadata); | ||
} | ||
|
||
public FolioExecutionContext forSystemUser(SystemUser systemUser) { | ||
return builder() | ||
.withTenantId(systemUser.tenantId()) | ||
.withOkapiUrl(systemUser.okapiUrl()) | ||
.withToken(systemUser.token() == null ? null : systemUser.token().accessToken()) | ||
.build(); | ||
} | ||
|
||
public FolioExecutionContext dbOnlyContext(String tenantId) { | ||
return builder().withTenantId(tenantId).build(); | ||
} | ||
|
||
@With | ||
@AllArgsConstructor(access = AccessLevel.PRIVATE) | ||
public static class Builder { | ||
private final FolioModuleMetadata moduleMetadata; | ||
private String tenantId; | ||
private String okapiUrl; | ||
private String token; | ||
private String username; | ||
private final Map<String, Collection<String>> allHeaders; | ||
private final Map<String, Collection<String>> okapiHeaders; | ||
|
||
public Builder(FolioModuleMetadata moduleMetadata) { | ||
this.moduleMetadata = moduleMetadata; | ||
this.allHeaders = new HashMap<>(); | ||
this.okapiHeaders = new HashMap<>(); | ||
} | ||
|
||
public FolioExecutionContext build() { | ||
return new FolioExecutionContext() { | ||
@Override | ||
public String getTenantId() { | ||
return tenantId; | ||
} | ||
|
||
@Override | ||
public String getOkapiUrl() { | ||
return okapiUrl; | ||
} | ||
|
||
@Override | ||
public String getToken() { | ||
return token; | ||
} | ||
|
||
@Override | ||
public Map<String, Collection<String>> getAllHeaders() { | ||
return allHeaders; | ||
} | ||
|
||
@Override | ||
public Map<String, Collection<String>> getOkapiHeaders() { | ||
return okapiHeaders; | ||
} | ||
|
||
@Override | ||
public FolioModuleMetadata getFolioModuleMetadata() { | ||
return moduleMetadata; | ||
} | ||
}; | ||
} | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
src/main/java/org/folio/search/service/TenantScopedExecutionService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package org.folio.search.service; | ||
|
||
import java.util.concurrent.Callable; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.SneakyThrows; | ||
import org.folio.search.model.context.FolioExecutionContextBuilder; | ||
import org.folio.spring.FolioExecutionContext; | ||
import org.folio.spring.scope.FolioExecutionContextSetter; | ||
import org.folio.spring.service.SystemUserService; | ||
import org.springframework.scheduling.annotation.Async; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class TenantScopedExecutionService { | ||
private final FolioExecutionContextBuilder contextBuilder; | ||
private final SystemUserService systemUserService; | ||
|
||
/** | ||
* Executes given job tenant scoped. | ||
* | ||
* @param tenantId - The tenant name. | ||
* @param job - Job to be executed in tenant scope. | ||
* @param <T> - Optional return value for the job. | ||
* @return Result of job. | ||
* @throws RuntimeException - Wrapped exception from the job. | ||
*/ | ||
@SneakyThrows | ||
public <T> T executeTenantScoped(String tenantId, Callable<T> job) { | ||
try (var fex = new FolioExecutionContextSetter(folioExecutionContext(tenantId))) { | ||
return job.call(); | ||
} | ||
} | ||
|
||
/** | ||
* Executes given job in scope of tenant asynchronously. | ||
* | ||
* @param tenantId - The tenant name. | ||
* @param job - Job to be executed in tenant scope. | ||
*/ | ||
@Async | ||
public void executeAsyncTenantScoped(String tenantId, Runnable job) { | ||
try (var fex = new FolioExecutionContextSetter(folioExecutionContext(tenantId))) { | ||
job.run(); | ||
} | ||
} | ||
|
||
private FolioExecutionContext folioExecutionContext(String tenant) { | ||
return contextBuilder.forSystemUser(systemUserService.getAuthedSystemUser(tenant)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters