5
5
import org .slf4j .LoggerFactory ;
6
6
import org .springframework .beans .factory .annotation .Value ;
7
7
import org .springframework .stereotype .Repository ;
8
+ import ru .qatools .beanloader .BeanChangeListener ;
9
+ import ru .qatools .beanloader .BeanWatcher ;
8
10
import ru .qatools .gridrouter .config .Browser ;
9
11
import ru .qatools .gridrouter .config .Browsers ;
10
12
import ru .qatools .gridrouter .config .Version ;
11
13
import ru .qatools .gridrouter .json .JsonCapabilities ;
12
14
13
15
import javax .annotation .PostConstruct ;
14
- import javax .annotation .PreDestroy ;
15
16
import javax .xml .bind .JAXB ;
16
17
import javax .xml .bind .JAXBException ;
17
18
import java .io .File ;
18
19
import java .io .IOException ;
19
- import java .nio .file .DirectoryStream ;
20
20
import java .nio .file .Path ;
21
21
import java .util .HashMap ;
22
22
import java .util .Map ;
23
23
24
24
import static java .nio .file .Files .newDirectoryStream ;
25
- import static ru .qatools .gridrouter .utils .DirectoryWatcher .newWatcher ;
26
25
27
26
/**
28
27
* @author Alexander Andyashin [email protected]
29
28
* @author Dmitry Baev [email protected]
30
29
* @author Innokenty Shuvalov [email protected]
31
30
*/
32
31
@ Repository
33
- public class ConfigRepository {
32
+ public class ConfigRepository implements BeanChangeListener < Browsers > {
34
33
35
34
private static final Logger LOGGER = LoggerFactory .getLogger (ConfigRepository .class );
36
35
37
- public static final String XML_GLOB = "*.xml" ;
36
+ private static final String XML_GLOB = "*.xml" ;
38
37
39
38
@ Value ("${grid.config.quota.directory}" )
40
39
private File quotaDirectory ;
@@ -46,62 +45,42 @@ public class ConfigRepository {
46
45
47
46
private Map <String , String > routes = new HashMap <>();
48
47
49
- private Thread quotaWatcherThread ;
50
-
51
48
@ PostConstruct
52
49
public void init () throws JAXBException , IOException {
53
- initBrowsers (getQuotaPath ());
54
50
if (isQuotaHotReload ()) {
55
51
startQuotaWatcher ();
56
- }
57
- }
58
-
59
- @ PreDestroy
60
- public void destroy () {
61
- if (isQuotaHotReload ()) {
62
- stopQuotaWatcher ();
52
+ } else {
53
+ loadQuotaOnce (getQuotaPath ());
63
54
}
64
55
}
65
56
66
57
private void startQuotaWatcher () {
67
58
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
-
59
+ try {
60
+ BeanWatcher .watchFor (Browsers .class , getQuotaPath ().toString (), XML_GLOB , this );
61
+ } catch (IOException e ) {
62
+ LOGGER .error ("Quota configuration loading failed: \n \n {}" , e );
63
+ }
74
64
}
75
65
76
- private void stopQuotaWatcher () {
77
- LOGGER .debug ("Stopping quota watcher" );
78
- if (getQuotaWatcherThread () != null && getQuotaWatcherThread ().isAlive ()) {
79
- getQuotaWatcherThread ().interrupt ();
66
+ private void loadQuotaOnce (Path quotaPath ) throws IOException {
67
+ for (Path filename : newDirectoryStream (quotaPath , XML_GLOB )) {
68
+ beanChanged (filename , JAXB .unmarshal (filename .toFile (), Browsers .class ));
80
69
}
81
70
}
82
71
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
- }
100
- }
101
- setUserBrowsers (temporaryUserBrowsers );
102
- setRoutes (temporaryRoutes );
103
- } catch (IOException e ) {
104
- LOGGER .error ("Loaded configuration failed: \n \n {}" , e );
72
+ @ Override
73
+ public void beanChanged (Path filename , Browsers browsers ) {
74
+ if (browsers == null ) {
75
+ LOGGER .info ("Configuration file [{}] was deleted. "
76
+ + "It is not purged from the running gridrouter process though." , filename );
77
+ } else {
78
+ LOGGER .info ("Loading quota configuration file [{}]" , filename );
79
+ String user = getFileName (filename );
80
+ userBrowsers .put (user , browsers );
81
+ routes .putAll (browsers .getRoutesMap ());
82
+ LOGGER .info ("Loaded quota configuration for [{}] from [{}]: \n \n {}" ,
83
+ user , filename , browsers .toXml ());
105
84
}
106
85
}
107
86
@@ -117,10 +96,6 @@ public Map<String, String> getRoutes() {
117
96
return routes ;
118
97
}
119
98
120
- protected void setRoutes (Map <String , String > routes ) {
121
- this .routes = routes ;
122
- }
123
-
124
99
public Map <String , Browsers > getUserBrowsers () {
125
100
return this .userBrowsers ;
126
101
}
@@ -129,10 +104,6 @@ protected Browsers getUserBrowsers(String user) {
129
104
return getUserBrowsers ().get (user );
130
105
}
131
106
132
- protected void setUserBrowsers (Map <String , Browsers > userBrowsers ) {
133
- this .userBrowsers = userBrowsers ;
134
- }
135
-
136
107
public Version findVersion (String user , JsonCapabilities caps ) {
137
108
return userBrowsers .get (user ).find (caps .getBrowserName (), caps .getVersion ());
138
109
}
@@ -150,13 +121,4 @@ public Map<String, Integer> getBrowsersCountMap(String user) {
150
121
}
151
122
return countMap ;
152
123
}
153
-
154
- public Thread getQuotaWatcherThread () {
155
- return quotaWatcherThread ;
156
- }
157
-
158
- public void setQuotaWatcherThread (Thread quotaWatcherThread ) {
159
- this .quotaWatcherThread = quotaWatcherThread ;
160
- }
161
-
162
124
}
0 commit comments