config executor some changes
This commit is contained in:
parent
bfa32f453a
commit
702920da9f
6 changed files with 108 additions and 98 deletions
|
|
@ -1,14 +1,15 @@
|
||||||
package org.micord.controller;
|
package org.micord.controller;
|
||||||
|
|
||||||
import org.micord.exceptions.FileNotModifiedException;
|
|
||||||
import org.micord.service.ApiService;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.http.ResponseEntity;
|
|
||||||
import org.springframework.web.bind.annotation.*;
|
|
||||||
|
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.CompletableFuture;
|
|
||||||
|
import org.micord.service.ApiService;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* REST Controller for API operations.
|
* REST Controller for API operations.
|
||||||
|
|
@ -17,31 +18,34 @@ import java.util.concurrent.CompletableFuture;
|
||||||
@RequestMapping("/api")
|
@RequestMapping("/api")
|
||||||
public class ApiController {
|
public class ApiController {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private ApiService apiService;
|
private ApiService apiService;
|
||||||
|
|
||||||
@PostMapping("/block")
|
@PostMapping("/block")
|
||||||
public CompletableFuture<ResponseEntity<?>> block(@RequestBody List<String> ids) throws FileNotFoundException, FileNotModifiedException {
|
public ResponseEntity<?> block(@RequestBody List<String> ids) throws FileNotFoundException {
|
||||||
return apiService.process("block", ids)
|
apiService.process("block", ids);
|
||||||
.thenApply(ResponseEntity::ok);
|
return ResponseEntity.ok("");
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/unblock")
|
@PostMapping("/unblock")
|
||||||
public CompletableFuture<ResponseEntity<?>> unblock(@RequestBody List<String> ids) throws FileNotFoundException, FileNotModifiedException {
|
public ResponseEntity<?> unblock(@RequestBody List<String> ids) throws FileNotFoundException {
|
||||||
return apiService.process("unblock", ids)
|
|
||||||
.thenApply(ResponseEntity::ok);
|
|
||||||
}
|
|
||||||
|
|
||||||
@PostMapping("/removeFromSystem")
|
apiService.process("unblock", ids);
|
||||||
public CompletableFuture<ResponseEntity<?>> removeFromSystem(@RequestBody List<String> ids) throws FileNotFoundException, FileNotModifiedException {
|
return ResponseEntity.ok("");
|
||||||
return apiService.process("removeFromSystem", ids)
|
}
|
||||||
.thenApply(ResponseEntity::ok);
|
|
||||||
}
|
|
||||||
|
|
||||||
@PostMapping("/removeFromCallList")
|
@PostMapping("/removeFromSystem")
|
||||||
public CompletableFuture<ResponseEntity<?>> removeFromCallList(@RequestBody List<String> ids) throws FileNotFoundException, FileNotModifiedException {
|
public ResponseEntity<?> removeFromSystem(@RequestBody List<String> ids)
|
||||||
return apiService.process("removeFromCallList", ids)
|
throws FileNotFoundException {
|
||||||
.thenApply(ResponseEntity::ok);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
apiService.process("removeFromSystem", ids);
|
||||||
|
return ResponseEntity.ok("");
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/removeFromCallList")
|
||||||
|
public ResponseEntity<?> removeFromCallList(@RequestBody List<String> ids)
|
||||||
|
throws FileNotFoundException {
|
||||||
|
apiService.process("removeFromCallList", ids);
|
||||||
|
return ResponseEntity.ok("");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,10 +0,0 @@
|
||||||
package org.micord.exceptions;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author Maksim Tereshin
|
|
||||||
*/
|
|
||||||
public class FileNotModifiedException extends Exception {
|
|
||||||
public FileNotModifiedException(String message) {
|
|
||||||
super(message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -0,0 +1,21 @@
|
||||||
|
package org.micord.models;
|
||||||
|
|
||||||
|
import java.nio.file.attribute.FileTime;
|
||||||
|
|
||||||
|
public class CachedConfig {
|
||||||
|
private final Requests config;
|
||||||
|
private final FileTime modifiedTime;
|
||||||
|
|
||||||
|
public CachedConfig(Requests config, FileTime modifiedTime) {
|
||||||
|
this.config = config;
|
||||||
|
this.modifiedTime = modifiedTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Requests getConfig() {
|
||||||
|
return config;
|
||||||
|
}
|
||||||
|
|
||||||
|
public FileTime getModifiedTime() {
|
||||||
|
return modifiedTime;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -16,26 +16,16 @@ public class ApiService {
|
||||||
@Autowired
|
@Autowired
|
||||||
private ConfigLoader configLoader;
|
private ConfigLoader configLoader;
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private S3Service s3Service;
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private SqlAqlService sqlAndAqlService;
|
private SqlAqlService sqlAndAqlService;
|
||||||
|
|
||||||
public CompletableFuture<String> process(String methodName, List<String> ids) throws FileNotFoundException, FileNotModifiedException {
|
public void process(String methodName, List<String> ids) throws FileNotFoundException {
|
||||||
Optional<Requests> optionalConfig = configLoader.loadConfigIfModified(methodName);
|
Optional<Requests> optionalConfig = configLoader.loadConfigIfModified(methodName);
|
||||||
|
|
||||||
if (optionalConfig.isEmpty()) {
|
if (optionalConfig.isEmpty()) {
|
||||||
throw new FileNotFoundException("Configuration for method " + methodName + " could not be loaded.");
|
throw new FileNotFoundException("Configuration for method " + methodName + " could not be loaded.");
|
||||||
}
|
}
|
||||||
|
|
||||||
Requests config = optionalConfig.get();
|
Requests config = optionalConfig.get();
|
||||||
|
|
||||||
sqlAndAqlService.processSqlAndAqlRequests(config, ids);
|
sqlAndAqlService.processSqlAndAqlRequests(config, ids);
|
||||||
|
|
||||||
return CompletableFuture.runAsync(() -> {
|
|
||||||
s3Service.processS3Requests(config.getS3Requests(), ids);
|
|
||||||
}).thenApply(v -> "Processing complete");
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,7 @@ import org.micord.models.Requests;
|
||||||
import org.micord.models.SqlRequest;
|
import org.micord.models.SqlRequest;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
|
@ -35,6 +36,9 @@ public class SqlAqlService {
|
||||||
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(SqlAqlService.class);
|
private static final Logger logger = LoggerFactory.getLogger(SqlAqlService.class);
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private S3Service s3Service;
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public void processSqlAndAqlRequests(Requests config, List<String> ids) {
|
public void processSqlAndAqlRequests(Requests config, List<String> ids) {
|
||||||
if (config.getSqlRequests() != null) {
|
if (config.getSqlRequests() != null) {
|
||||||
|
|
@ -48,6 +52,7 @@ public class SqlAqlService {
|
||||||
processAqlRequests(request, ids);
|
processAqlRequests(request, ids);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
s3Service.processS3Requests(config.getS3Requests(), ids);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void processSqlRequests(SqlRequest request, List<String> ids) {
|
private void processSqlRequests(SqlRequest request, List<String> ids) {
|
||||||
|
|
|
||||||
|
|
@ -1,23 +1,23 @@
|
||||||
package org.micord.utils;
|
package org.micord.utils;
|
||||||
|
|
||||||
import org.micord.exceptions.FileNotModifiedException;
|
|
||||||
import org.micord.models.Requests;
|
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
|
|
||||||
import javax.xml.bind.JAXBContext;
|
|
||||||
import javax.xml.bind.JAXBException;
|
|
||||||
import javax.xml.bind.Unmarshaller;
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.attribute.FileTime;
|
import java.nio.file.attribute.FileTime;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
import javax.xml.bind.JAXBContext;
|
||||||
|
import javax.xml.bind.JAXBException;
|
||||||
|
import javax.xml.bind.Unmarshaller;
|
||||||
|
|
||||||
|
import org.micord.models.CachedConfig;
|
||||||
|
import org.micord.models.Requests;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Maksim Tereshin
|
* @author Maksim Tereshin
|
||||||
|
|
@ -25,46 +25,46 @@ import java.util.logging.Logger;
|
||||||
@Component
|
@Component
|
||||||
public class ConfigLoader {
|
public class ConfigLoader {
|
||||||
|
|
||||||
private static final Logger LOGGER = Logger.getLogger(ConfigLoader.class.getName());
|
private static final Logger LOGGER = Logger.getLogger(ConfigLoader.class.getName());
|
||||||
private final Map<String, FileTime> lastModifiedTimes = new HashMap<>();
|
private static final Map<String, CachedConfig> cachedConfigs = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
@Value("${configDirectory}")
|
@Value("${configDirectory}")
|
||||||
private String configDirectory;
|
private String configDirectory;
|
||||||
|
|
||||||
public Optional<Requests> loadConfigIfModified(String methodName) throws FileNotModifiedException {
|
public Optional<Requests> loadConfigIfModified(String methodName) {
|
||||||
String fileName = methodName + ".xml";
|
String fileName = methodName + ".xml";
|
||||||
if (configDirectory == null) {
|
|
||||||
LOGGER.log(Level.SEVERE, "No configuration directory found for method: " + methodName);
|
|
||||||
return Optional.empty();
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
if (configDirectory == null) {
|
||||||
File configFile = new File(configDirectory + File.separator + fileName);
|
LOGGER.log(Level.SEVERE, "No configuration directory found for method: " + methodName);
|
||||||
Path configFilePath = configFile.toPath();
|
return Optional.empty();
|
||||||
|
}
|
||||||
|
|
||||||
FileTime currentModifiedTime = Files.getLastModifiedTime(configFilePath);
|
try {
|
||||||
FileTime lastModifiedTime = lastModifiedTimes.getOrDefault(methodName, null);
|
File configFile = new File(configDirectory + File.separator + fileName);
|
||||||
|
Path configFilePath = configFile.toPath();
|
||||||
|
FileTime currentModifiedTime = Files.getLastModifiedTime(configFilePath);
|
||||||
|
CachedConfig cachedConfig = cachedConfigs.getOrDefault(methodName, null);
|
||||||
|
|
||||||
if (lastModifiedTime == null || currentModifiedTime.compareTo(lastModifiedTime) > 0) {
|
if (cachedConfig == null || !currentModifiedTime.equals(cachedConfig.getModifiedTime())) {
|
||||||
lastModifiedTimes.put(methodName, currentModifiedTime);
|
// Load the updated configuration
|
||||||
|
JAXBContext jaxbContext = JAXBContext.newInstance(Requests.class);
|
||||||
|
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
|
||||||
|
Requests loadedConfig = (Requests) unmarshaller.unmarshal(configFile);
|
||||||
|
cachedConfigs.put(methodName, new CachedConfig(loadedConfig, currentModifiedTime));
|
||||||
|
return Optional.of(loadedConfig);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return Optional.of(cachedConfigs.get(methodName).getConfig());
|
||||||
|
}
|
||||||
|
|
||||||
// Load the updated configuration
|
}
|
||||||
JAXBContext jaxbContext = JAXBContext.newInstance(Requests.class);
|
catch (IOException e) {
|
||||||
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
|
LOGGER.log(Level.SEVERE, "Failed to load configuration file: " + fileName, e);
|
||||||
Requests loadedConfig = (Requests) unmarshaller.unmarshal(configFile);
|
return Optional.empty(); // Return empty if there is an IO error
|
||||||
|
}
|
||||||
return Optional.of(loadedConfig);
|
catch (JAXBException e) {
|
||||||
} else {
|
LOGGER.log(Level.SEVERE, "Failed to unmarshal configuration file: " + fileName, e);
|
||||||
throw new FileNotModifiedException("Configuration file for method " + methodName + " has not been modified.");
|
return Optional.empty(); // Return empty if unmarshalling fails
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} catch (IOException e) {
|
}
|
||||||
LOGGER.log(Level.SEVERE, "Failed to load configuration file: " + fileName, e);
|
|
||||||
return Optional.empty(); // Return empty if there is an IO error
|
|
||||||
} catch (JAXBException e) {
|
|
||||||
LOGGER.log(Level.SEVERE, "Failed to unmarshal configuration file: " + fileName, e);
|
|
||||||
return Optional.empty(); // Return empty if unmarshalling fails
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue