Skip to content

Commit ae7be9b

Browse files
author
Artem Eroshenko
committed
Merge pull request #2 from innokenty/master
Use beanloader to watch for quota changes
2 parents b3e9474 + 0558428 commit ae7be9b

File tree

3 files changed

+34
-167
lines changed

3 files changed

+34
-167
lines changed

proxy/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@
2727
<version>${project.version}</version>
2828
</dependency>
2929

30+
<dependency>
31+
<groupId>ru.yandex.qatools.beanloader</groupId>
32+
<artifactId>beanloader</artifactId>
33+
<version>2.1</version>
34+
</dependency>
35+
3036
<!-- Commons -->
3137
<dependency>
3238
<groupId>commons-io</groupId>

proxy/src/main/java/ru/qatools/gridrouter/ConfigRepository.java

Lines changed: 28 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,32 @@
55
import org.slf4j.LoggerFactory;
66
import org.springframework.beans.factory.annotation.Value;
77
import org.springframework.stereotype.Repository;
8+
import ru.qatools.beanloader.BeanChangeListener;
9+
import ru.qatools.beanloader.BeanLoader;
10+
import ru.qatools.beanloader.BeanWatcher;
811
import ru.qatools.gridrouter.config.Browser;
912
import ru.qatools.gridrouter.config.Browsers;
1013
import ru.qatools.gridrouter.config.Version;
1114
import ru.qatools.gridrouter.json.JsonCapabilities;
1215

1316
import javax.annotation.PostConstruct;
14-
import javax.annotation.PreDestroy;
15-
import javax.xml.bind.JAXB;
16-
import javax.xml.bind.JAXBException;
1717
import java.io.File;
1818
import java.io.IOException;
19-
import java.nio.file.DirectoryStream;
2019
import java.nio.file.Path;
2120
import java.util.HashMap;
2221
import java.util.Map;
2322

24-
import static java.nio.file.Files.newDirectoryStream;
25-
import static ru.qatools.gridrouter.utils.DirectoryWatcher.newWatcher;
26-
2723
/**
2824
* @author Alexander Andyashin [email protected]
2925
* @author Dmitry Baev [email protected]
3026
* @author Innokenty Shuvalov [email protected]
3127
*/
3228
@Repository
33-
public class ConfigRepository {
29+
public class ConfigRepository implements BeanChangeListener<Browsers> {
3430

3531
private static final Logger LOGGER = LoggerFactory.getLogger(ConfigRepository.class);
3632

37-
public static final String XML_GLOB = "*.xml";
33+
private static final String XML_GLOB = "*.xml";
3834

3935
@Value("${grid.config.quota.directory}")
4036
private File quotaDirectory;
@@ -46,117 +42,51 @@ public class ConfigRepository {
4642

4743
private Map<String, String> routes = new HashMap<>();
4844

49-
private Thread quotaWatcherThread;
50-
5145
@PostConstruct
52-
public void init() throws JAXBException, IOException {
53-
initBrowsers(getQuotaPath());
54-
if (isQuotaHotReload()) {
55-
startQuotaWatcher();
56-
}
57-
}
58-
59-
@PreDestroy
60-
public void destroy() {
61-
if (isQuotaHotReload()) {
62-
stopQuotaWatcher();
63-
}
64-
}
65-
66-
private void startQuotaWatcher() {
67-
LOGGER.debug("Starting quota watcher");
68-
setQuotaWatcherThread(newWatcher(getQuotaPath(), "glob:" + XML_GLOB, (kind, browserPath) -> {
69-
LOGGER.info("Reload configuration [{}] on event [{}]", browserPath, kind.name());
70-
initBrowsers(getQuotaPath());
71-
}));
72-
getQuotaWatcherThread().start();
73-
74-
}
75-
76-
private void stopQuotaWatcher() {
77-
LOGGER.debug("Stopping quota watcher");
78-
if (getQuotaWatcherThread() != null && getQuotaWatcherThread().isAlive()) {
79-
getQuotaWatcherThread().interrupt();
80-
}
81-
}
82-
83-
public void initBrowsers(Path quotaPath) {
84-
Map<String, Browsers> temporaryUserBrowsers = new HashMap<>(getUserBrowsers());
85-
Map<String, String> temporaryRoutes = new HashMap<>(getRoutes());
86-
try (DirectoryStream<Path> stream = newDirectoryStream(quotaPath, XML_GLOB)) {
87-
for (Path browsersPath : stream) {
88-
LOGGER.info("Load configuration from [{}]", browsersPath);
89-
String user = getFileName(browsersPath);
90-
try {
91-
Browsers browsers = JAXB.unmarshal(browsersPath.toFile(), Browsers.class);
92-
temporaryUserBrowsers.put(user, browsers);
93-
temporaryRoutes.putAll(browsers.getRoutesMap());
94-
95-
LOGGER.info("Loaded configuration for [{}] from [{}]: \n\n{}",
96-
user, browsersPath, browsers.toXml());
97-
} catch (Exception e) {
98-
LOGGER.error("Loaded configuration failed for [{}]: \n\n{}", browsersPath, e);
99-
}
46+
public void init() {
47+
try {
48+
if (quotaHotReload) {
49+
LOGGER.debug("Starting quota watcher");
50+
BeanWatcher.watchFor(Browsers.class, quotaDirectory.toPath(), XML_GLOB, this);
51+
} else {
52+
LOGGER.debug("Loading quota configuration");
53+
BeanLoader.loadAll(Browsers.class, quotaDirectory.toPath(), XML_GLOB, this);
10054
}
101-
setUserBrowsers(temporaryUserBrowsers);
102-
setRoutes(temporaryRoutes);
10355
} catch (IOException e) {
104-
LOGGER.error("Loaded configuration failed: \n\n{}", e);
56+
LOGGER.error("Quota configuration loading failed", e);
10557
}
10658
}
10759

108-
protected boolean isQuotaHotReload() {
109-
return quotaHotReload;
110-
}
111-
112-
protected Path getQuotaPath() {
113-
return quotaDirectory.toPath();
60+
@Override
61+
public void beanChanged(Path filename, Browsers browsers) {
62+
if (browsers == null) {
63+
LOGGER.info("Configuration file [{}] was deleted. "
64+
+ "It is not purged from the running gridrouter process though.", filename);
65+
} else {
66+
LOGGER.info("Loading quota configuration file [{}]", filename);
67+
String user = FilenameUtils.getBaseName(filename.toString());
68+
userBrowsers.put(user, browsers);
69+
routes.putAll(browsers.getRoutesMap());
70+
LOGGER.info("Loaded quota configuration for [{}] from [{}]: \n\n{}",
71+
user, filename, browsers.toXml());
72+
}
11473
}
11574

11675
public Map<String, String> getRoutes() {
11776
return routes;
11877
}
11978

120-
protected void setRoutes(Map<String, String> routes) {
121-
this.routes = routes;
122-
}
123-
124-
public Map<String, Browsers> getUserBrowsers() {
125-
return this.userBrowsers;
126-
}
127-
128-
protected Browsers getUserBrowsers(String user) {
129-
return getUserBrowsers().get(user);
130-
}
131-
132-
protected void setUserBrowsers(Map<String, Browsers> userBrowsers) {
133-
this.userBrowsers = userBrowsers;
134-
}
135-
13679
public Version findVersion(String user, JsonCapabilities caps) {
13780
return userBrowsers.get(user).find(caps.getBrowserName(), caps.getVersion());
13881
}
13982

140-
private static String getFileName(Path path) {
141-
return FilenameUtils.getBaseName(path.toString());
142-
}
143-
14483
public Map<String, Integer> getBrowsersCountMap(String user) {
14584
HashMap<String, Integer> countMap = new HashMap<>();
146-
for (Browser browser : getUserBrowsers(user).getBrowsers()) {
85+
for (Browser browser : this.userBrowsers.get(user).getBrowsers()) {
14786
for (Version version : browser.getVersions()) {
14887
countMap.put(browser.getName() + ":" + version.getNumber(), version.getCount());
14988
}
15089
}
15190
return countMap;
15291
}
153-
154-
public Thread getQuotaWatcherThread() {
155-
return quotaWatcherThread;
156-
}
157-
158-
public void setQuotaWatcherThread(Thread quotaWatcherThread) {
159-
this.quotaWatcherThread = quotaWatcherThread;
160-
}
161-
16292
}

proxy/src/main/java/ru/qatools/gridrouter/utils/DirectoryWatcher.java

Lines changed: 0 additions & 69 deletions
This file was deleted.

0 commit comments

Comments
 (0)